/tests/roundware/rw/test_models.py

https://github.com/13rac1/roundware-server
Python | 108 lines | 62 code | 25 blank | 21 comment | 0 complexity | 4f0f7f2270eefec575e3ca6eb837cd99 MD5 | raw file
  1. # Roundware Server is released under the GNU Affero General Public License v3.
  2. # See COPYRIGHT.txt, AUTHORS.txt, and LICENSE.txt in the project root directory.
  3. from __future__ import unicode_literals
  4. from model_mommy import mommy
  5. from roundware.rw import models
  6. from roundware.settings import DEFAULT_SESSION_ID
  7. from .common import use_locmemcache, RWTestCase
  8. class TestUIGroup(RWTestCase):
  9. """ exercise UIGroup model class """
  10. def setUp(self):
  11. super(type(self), TestUIGroup).setUp(self)
  12. # make uigroup, makes our tagcategory, uimode, project,
  13. # selectionmethod
  14. self.uigroup = mommy.make('rw.UIGroup')
  15. self.other_uimode = models.UIGroup.SPEAK
  16. self.project = self.uigroup.project
  17. @use_locmemcache(models, 'cache')
  18. def test_tag_category_cache_invalidation_post_save(self):
  19. """ test that cached value for tag categories for the UIGroup's mode
  20. is invalidated after UIGroup saved.
  21. """
  22. ui_mode = self.uigroup.ui_mode
  23. get_orig_tag_cats = self.project.get_tag_cats_by_ui_mode(ui_mode)
  24. # is original tag_cat of uigroup returned by Project's method
  25. self.assertIn(self.uigroup.tag_category, get_orig_tag_cats)
  26. # change the ui mode of our uigroup
  27. self.uigroup.ui_mode = self.other_uimode
  28. # should still get old tag categories... cached
  29. get_tag_cats = self.project.get_tag_cats_by_ui_mode(ui_mode)
  30. self.assertIn(self.uigroup.tag_category, get_tag_cats)
  31. # save the uigroup, now should not get old tag categories...
  32. # cached copy invalidated
  33. self.uigroup.save()
  34. get_tag_cats = self.project.get_tag_cats_by_ui_mode(ui_mode)
  35. self.assertNotIn(self.uigroup.tag_category, get_tag_cats)
  36. class TestProject(RWTestCase):
  37. def setUp(self):
  38. super(type(self), TestProject).setUp(self)
  39. self.project = mommy.make('rw.Project')
  40. self.ui_mode = models.UIGroup.LISTEN
  41. def test_no_tag_cats_returned_new_project(self):
  42. """ test no tag_categories returned for a new project with no uigroup
  43. """
  44. cats = self.project.get_tag_cats_by_ui_mode(self.ui_mode)
  45. self.assertTrue(len(cats) == 0)
  46. def test_correct_tag_cats_returned(self):
  47. """ test that we get correct tagcategories for our project once
  48. we add a UIGroup.
  49. """
  50. uigroup = mommy.make('rw.UIGroup', project=self.project,
  51. ui_mode=self.ui_mode)
  52. cats = self.project.get_tag_cats_by_ui_mode(self.ui_mode)
  53. self.assertIn(uigroup.tag_category, cats)
  54. def test_no_tag_cats_returned_wrong_uimode(self):
  55. """ test no tag_categories returned if we specify a uimode that
  56. is not connected to project via a UIGroup
  57. """
  58. other_ui_group = mommy.make('rw.UIGroup', project=self.project,
  59. ui_mode=models.UIGroup.SPEAK)
  60. cats = self.project.get_tag_cats_by_ui_mode(self.ui_mode)
  61. self.assertNotIn(other_ui_group.tag_category, cats)
  62. class TestAsset(RWTestCase):
  63. def setUp(self):
  64. super(type(self), TestAsset).setUp(self)
  65. self.session1 = mommy.make('rw.Session')
  66. self.session2 = mommy.make('rw.Session')
  67. self.project = mommy.make('rw.Project')
  68. self.asset1 = mommy.make('rw.Asset')
  69. self.asset2 = mommy.make('rw.Asset')
  70. self.vote1 = mommy.make('rw.Vote', session=self.session1,
  71. asset=self.asset1, type="like")
  72. self.vote2 = mommy.make('rw.Vote', session=self.session2,
  73. asset=self.asset1, type="like")
  74. self.vote3 = mommy.make('rw.Vote', session=self.session1,
  75. asset=self.asset2, type="like")
  76. self.vote4 = mommy.make('rw.Vote', session=self.session1,
  77. asset=self.asset1, type="flag")
  78. def test_get_likes(self):
  79. self.assertEquals(2, self.asset1.get_likes())
  80. self.assertEquals(1, self.asset2.get_likes())
  81. def test_get_flags(self):
  82. self.assertEquals(1, self.asset1.get_flags())