9572668bd90aabb13239d1fb8de902155f2a8114
[oweals/karmaworld.git] / karmaworld / apps / notes / tests.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4 """
5 """
6 import karmaworld.secret.indexden as secret
7
8 import datetime
9 from django.test import TestCase
10 from karmaworld.apps.notes.search import SearchIndex
11
12 from karmaworld.apps.notes.models import Note
13 from karmaworld.apps.courses.models import Course
14 from karmaworld.apps.courses.models import School
15
16 # Tell SearchIndex to put its entries
17 # in a separate index
18 secret.testing = True
19
20
21 class TestNotes(TestCase):
22
23     def setUp(self):
24         # create base values to test db representations
25         self.now = datetime.datetime.utcnow()
26
27         # create a school to satisfy course requirements
28         self.school = School()
29         self.school.name = 'Marshall College'
30         self.school.save()
31
32         # create a course to test relationships
33         self.course = Course()
34         self.course.school = self.school
35         self.course.name = u'Archaeology 101'
36         self.course.save()
37         # override Course.save() appending an ID to the slug
38         self.course.slug = u'archaeology-101'
39         self.course.save()
40
41         # create a note to test against
42         self.note = Note()
43         self.note.course = self.course
44         self.note.name = u"Lecture notes concerning the use of therefore ∴"
45         self.note.file_type = 'doc'
46         self.note.uploaded_at = self.now
47         self.note.text = "This is the plaintext version of a note. It's pretty cool. Alpaca."
48         self.note.save()
49
50     def test_course_fkey(self):
51         self.assertEqual(self.course, self.note.course)
52
53     def test_slug_natural(self):
54         """ Test that the slug field is slugifying unicode Note.names """
55         expected = u"lecture-notes-concerning-the-use-of-therefore"
56         self.assertEqual(self.note.slug, expected)
57
58     def test_remake_slug(self):
59         """ Test the generation of a Note.slug field based on Note.
60         Name collision is expected, so see if slug handles this."""
61         expected = u"lecture-notes-concerning-the-use-of-therefore-{0}-{1}-{2}".format(
62                     self.note.uploaded_at.month,
63                     self.note.uploaded_at.day, self.note.uploaded_at.microsecond)
64
65         self.note.slug = None
66         self.note.save()
67         self.assertEqual(self.note.slug, expected)
68
69     expected_url_prefix = u'/marshall-college/archaeology-101/'
70     expected_slug = u'lecture-notes-concerning-the-use-of-therefore'
71     expected = expected_url_prefix + expected_slug
72
73     def test_note_get_absolute_url_slug(self):
74         """ Given a note with a slug, test that an expected url is generated """
75         # check that Note.get_absolute_url() is generating the right url
76         self.assertEqual(self.note.get_absolute_url(), self.expected)
77
78     def test_note_get_absolute_url_id(self):
79         self.note.slug = None
80         url = self.expected_url_prefix + str(self.note.id)
81         self.assertEqual(self.note.get_absolute_url(), url)