test_models.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. '''
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from catalog.models import Canvas, IdeaCategory, IdeaTag, Idea, Comment
  5. from django.contrib.auth.models import User
  6. import pytz
  7. import datetime
  8. # CANVAS IDEA USER COMMENT TAG COLLABORATIONS
  9. # Tests include 10 instances of any model being tested
  10. # ID is queried with i+1, as i begins at zero but IDs begin with 1 (and as it turns out cannot be 0)
  11. global TEST_LIMIT
  12. TEST_LIMIT = 10
  13. class CanvasTestModel(TestCase):
  14. """
  15. """
  16. @classmethod
  17. def setUpTestData(cls):
  18. for i in range(TEST_LIMIT):
  19. Canvas.objects.create(title = 'Hella Derp %s' % i, date = ( timezone.now() - datetime.timedelta(days = i) ) )
  20. def testTitle(self):
  21. for i in range(TEST_LIMIT):
  22. canvas = Canvas.objects.get(id=(i+1))
  23. field_label = canvas.title
  24. self.assertEquals(field_label, 'Hella Derp %s' % i)
  25. def testDate(self):
  26. for i in range(TEST_LIMIT):
  27. canvas = Canvas.objects.get(id=(i+1))
  28. field_label = canvas.date
  29. self.assertTrue( ( timezone.now() - datetime.timedelta(days = i) ).date() == field_label )
  30. class IdeaTestModel(TestCase):
  31. """
  32. """
  33. @classmethod
  34. def setUpTestData(cls):
  35. for i in range(TEST_LIMIT):
  36. User.objects.create_user( email = 'fatclub%s@example.com' % i, username = 'Fatclub %s' % i)
  37. Idea.objects.create( ownerID = User.objects.get(id = i+1),text = 'I am an idea and my name is %s' % (i+1))
  38. def testTextIsRight(self):
  39. for i in range(TEST_LIMIT):
  40. idea = Idea.objects.get(id=(i+1))
  41. self.assertEquals(idea.text, 'I am an idea and my name is %s' % (i+1))
  42. def testID(self):
  43. for i in range(TEST_LIMIT):
  44. idea = Idea.objects.get(id=(i+1))
  45. self.assertEquals(idea.id, (i+1))
  46. def testLinkedUser(self):
  47. for i in range(TEST_LIMIT):
  48. idea = Idea.objects.get(id=(i+1))
  49. user = idea.ownerID
  50. self.assertEquals( user.username, 'Fatclub %s' % i )
  51. self.assertEquals( user.email, 'fatclub%s@example.com' % i )
  52. class CollaborationsTestModel(TestCase):
  53. """
  54. """
  55. @classmethod
  56. def setUpTestData(cls):
  57. for i in range(TEST_LIMIT):
  58. # First create the user and the canvas instances
  59. User.objects.create_user( email = 'fatclub%s@example.com' % i, username = 'Fatclub %s' % i)
  60. Canvas.objects.create(title = 'Hella Derp %s' % i, date = (timezone.now() - datetime.timedelta(days = i)) )
  61. # Then create the collaborations instance and assign the two prior instances to it
  62. Collaborations.objects.create(canvasID = Canvas.objects.get(id = (i+1)), userID = User.objects.get(id = (i+1)) )
  63. def testIDs(self):
  64. for i in range(TEST_LIMIT):
  65. collab = Collaborations.objects.get(id = (i+1))
  66. self.assertEquals(collab.canvasID, Canvas.objects.get(id = (i+1)))
  67. self.assertEquals(collab.userID, User.objects.get(id = (i+1)))
  68. def testLinkedUsers(self):
  69. for i in range(TEST_LIMIT):
  70. collab = Collaborations.objects.get(id = (i+1))
  71. # as it turns out the user instance itself is acquired by assigning from the userID field - the instance is stored here, not the numeric ID
  72. user = collab.userID
  73. self.assertEquals(user.email, 'fatclub%s@example.com' % i)
  74. self.assertEquals(user.username, 'Fatclub %s' % i)
  75. def testLinkedCanvasses(self):
  76. for i in range(TEST_LIMIT):
  77. collab = Collaborations.objects.get(id = (i+1))
  78. canvas = collab.canvasID
  79. self.assertEquals(canvas.title, 'Hella Derp %s' % i)
  80. self.assertTrue( (timezone.now() - datetime.timedelta(days = i)).date() == canvas.date )
  81. class CommentsTestModel(TestCase):
  82. """
  83. """
  84. @classmethod
  85. def setUpTestData(cls):
  86. for i in range(TEST_LIMIT * 2):
  87. # ID Sequence: 1,3,5,7,9,11,13,15,17,19
  88. User.objects.create_user( email = 'fatclub%s@example.com' % i, username = 'Fatclub %s' % i) # ODD
  89. # ID Sequence: 2,4,6,8,10,12,14,16,18,20
  90. User.objects.create_user( email = 'commenter%s@example.com' % i, username = 'Commenter %s' % i) # EVEN
  91. for i in range(TEST_LIMIT):
  92. # ID Sequence: 1,3,5,7,9,11,13,15,17,19
  93. Idea.objects.create( ownerID = User.objects.get(id = i*2 + 1),text = 'I am an idea and my name is %s' % (i+1))
  94. # ID Sequence: 2,4,6,8,10,12,14,16,18,20
  95. Comments.objects.create( ownerID = User.objects.get(id = i * 2 + 2), ideaID = Idea.objects.get(id = (i+1)), text = "ok but why though, evil boss %s" %i )
  96. def testInitiallyUnresolved(self):
  97. for i in range(TEST_LIMIT):
  98. comment = Comments.objects.get(id = i+1)
  99. self.assertFalse(comment.isResolved)
  100. def testLinkedIdea(self):
  101. for i in range(TEST_LIMIT):
  102. comment = Comments.objects.get(id = i+1)
  103. idea = comment.ideaID
  104. self.assertEquals(idea.text, 'I am an idea and my name is %s' % (i+1))
  105. def testCommenter(self):
  106. for i in range(TEST_LIMIT):
  107. comment = Comments.objects.get(id = i+1)
  108. user = comment.ownerID
  109. self.assertEquals( user.username, 'Commenter %s' % i )
  110. self.assertEquals( user.email, 'commenter%s@example.com' % i )
  111. def testOwnerOfLinkedIdea(self):
  112. for i in range(TEST_LIMIT):
  113. idea = Idea.objects.get(id=(i+1))
  114. user = idea.ownerID
  115. self.assertEquals( user.username, 'Fatclub %s' % i )
  116. self.assertEquals( user.email, 'fatclub%s@example.com' % i )
  117. '''
  118. '''
  119. TAGS MODEL CHANGED - TESTS NO LONGER VALID
  120. class TagListDoublingTestModel(TestCase):
  121. """
  122. """
  123. @classmethod
  124. def setUpTestData(cls):
  125. for i in range (TEST_LIMIT * 2):
  126. # 20 ideas, 2:1 idea:tag ratio - only text wanted for purposes of the test
  127. Idea.objects.create(text = 'I am an idea and my name is %s' % (i+1))
  128. for i in range (TEST_LIMIT):
  129. # Tag ID range 1,1,2,2,3,3,4,4,5,5
  130. # Compound ID (1,1)--(1,2) (2,3)--(2,4) (3,5)--(3,6) (4,7)--(4,8) (5,9)--(5,10)
  131. Tags.objects.create(ideaID = Idea.objects.get(id = i + 1))
  132. def testIdeaPairing(self):
  133. for i in range(TEST_LIMIT):
  134. # only test i values 0,2,4,6,8 as for odd values the tests will fail due to them starting too high for the given tagID
  135. if (i % 2 == 0):
  136. tag = Tags.objects.all()
  137. idea0 = tag[0].ideaID
  138. idea1 = tag[1].ideaID
  139. self.assertEquals(idea0.text, 'I am an idea and my name is %s' % (i+1))
  140. self.assertEquals(idea1.text, 'I am an idea and my name is %s' % (i+2))
  141. class TagListTriplingTestModel(TestCase):
  142. """
  143. """
  144. @classmethod
  145. def setUpTestData(cls):
  146. for i in range (TEST_LIMIT * 3):
  147. # 30 ideas, 3:1 idea:tag ratio - only text wanted for purposes of the test
  148. Idea.objects.create(text = 'I am an idea and my name is %s' % (i+1))
  149. for i in range (12):
  150. # Tag ID range 1,1,1,2,2,2,3,3,3,4,4,4
  151. # Compound ID (1,1)--(1,2)--(1,3) (2,4)--(2,5)--(2,6) (3,7)--(3,8)--(3,9) (4,10)--(4,11)--(4,12)
  152. Tags.objects.create(ideaID = Idea.objects.get(id = i + 1))
  153. def testIdeaTripling(self):
  154. for i in range(12):
  155. # only test i values 0,3,6,9 as for other values the tests will fail due to them starting too high for the given tagID
  156. if (i%3 == 0):
  157. tag = Tags.objects.all()
  158. idea0 = tag[0].ideaID
  159. idea1 = tag[1].ideaID
  160. idea2 = tag[2].ideaID
  161. self.assertEquals(idea0.text, 'I am an idea and my name is %s' % (i+1))
  162. self.assertEquals(idea1.text, 'I am an idea and my name is %s' % (i+2))
  163. self.assertEquals(idea2.text, 'I am an idea and my name is %s' % (i+3))
  164. class TagListManyTagsSingleIdea(TestCase):
  165. """
  166. """
  167. @classmethod
  168. def setUpTestData(cls):
  169. for i in range (TEST_LIMIT):
  170. Idea.objects.create(text = 'I am an idea and my name is %s' % (i+1))
  171. Tags.objects.create(Idea.tags = Idea.objects.get(id = 1))
  172. def testIdeaTripling(self):
  173. for i in range(TEST_LIMIT):
  174. tag = Tags.objects.all()
  175. # above query returns a singleton list of tag objects for some reason, which is distinct from a tag object
  176. idea = tag[0].Idea.tags
  177. self.assertEquals(idea.text, 'I am an idea and my name is %s' % (1))
  178. '''