Commits

Omer Katz committed 67b4c4c

Completed ArchiveAutoTreeItemFactory with tests.
Fixed a bug that caused the tree item to created or updated without
the urlaspattern field. Refactored tests accordingly.
The month_tree_item_factory and year_tree_item_factory mocks now return
a tree item with a real title (month or year according to date).

  • Participants
  • Parent commits 9404b24

Comments (0)

Files changed (2)

File autotree/factories.py

         self.tree_factory = TreeFactory(tree_alias)
         self.args = {
                         'inmenu': is_in_menu,
+                        'urlaspattern': is_url_a_pattern,
                         'inbreadcrumbs': is_in_breadcrumbs,
                         'insitetree': is_in_sitetree,
                         'access_loggedin': logged_in_only_access,
                         'access_perm_type': access_perm_type
         }
 
-    def create_or_update(self, title, url, parent, sort_order=0, alias='', hint='', description=''):
+    def _prepare_args(self, title, url, parent, sort_order=0, alias='', hint='', description=''):
         self.args['title'] = title
         self.args['url'] = url
         self.args['tree'] = self.tree_factory.get_or_create()
         self.args['hint'] = hint
         self.args['description'] = description
 
+    def create_or_update(self, title, url, parent, sort_order=0, alias='', hint='', description=''):
+        self._prepare_args(title, url, parent, sort_order, alias, hint, description)
+
         item, exists = TreeItem.objects.get_or_create(**self.args)
 
         if exists:
             item.save(**self.args)
 
         return item
+
+class ArchiveAutoTreeItemFactory(object):
+        def __init__(self,
+                     view_name,
+                     tree_alias,
+                     is_in_menu=True,
+                     is_in_breadcrumbs=True,
+                     is_in_sitetree=True,
+                     is_url_a_pattern=False,
+                     logged_in_only_access=False,
+                     restricted_only_access=False,
+                     access_permissions = [],
+                     access_perm_type = 1):
+            self.tree_factory = TreeFactory(tree_alias)
+            self.month_tree_item_factory = MonthTreeItemFactory(view_name,
+                                                                tree_alias,
+                                                                is_in_menu = is_in_menu,
+                                                                is_in_breadcrumbs = is_in_breadcrumbs,
+                                                                is_in_sitetree = is_in_sitetree,
+                                                                logged_in_only_access = logged_in_only_access,
+                                                                restricted_only_access = restricted_only_access,
+                                                                access_permissions = access_permissions,
+                                                                access_perm_type = access_perm_type)
+            self.auto_tree_item_factory = AutoTreeItemFactory(tree_alias,
+                                                              is_in_menu = is_in_menu,
+                                                              is_in_breadcrumbs = is_in_breadcrumbs,
+                                                              is_in_sitetree = is_in_sitetree,
+                                                              logged_in_only_access = logged_in_only_access,
+                                                              restricted_only_access = restricted_only_access,
+                                                              access_permissions = access_permissions,
+                                                              access_perm_type = access_perm_type)
+
+            self.args = {
+                        'inmenu': is_in_menu,
+                        'urlaspattern': is_url_a_pattern,
+                        'inbreadcrumbs': is_in_breadcrumbs,
+                        'insitetree': is_in_sitetree,
+                        'access_loggedin': logged_in_only_access,
+                        'access_restricted': restricted_only_access,
+                        'access_permissions': access_permissions,
+                        'access_perm_type': access_perm_type
+            }
+
+        def _prepare_args(self, title, url, date, sort_order=0, alias='', hint='', description=''):
+            self.args['title'] = title
+            self.args['url'] = url
+            self.args['tree'] = self.tree_factory.get_or_create()
+            self.args['parent'] = self.month_tree_item_factory.get_or_create(date)
+            self.args['sort_order'] = sort_order
+            self.args['alias'] = alias
+            self.args['hint'] = hint
+            self.args['description'] = description
+
+        def create_or_update(self, title, url, date, sort_order=0, alias='', hint='', description=''):
+            self._prepare_args(title, url, date, sort_order, alias, hint, description)
+
+            self.auto_tree_item_factory.create_or_update(**self.args)
   

File autotree/tests.py

 from django.test import TestCase
 from mock import Mock
 from sitetree.models import  TreeItem, Tree
-from autotree.factories import DateFormattedTreeItemFactory, TreeFactory, YearTreeItemFactory, MonthTreeItemFactory, AutoTreeItemFactory
+from autotree.factories import DateFormattedTreeItemFactory, TreeFactory, YearTreeItemFactory, MonthTreeItemFactory, AutoTreeItemFactory, ArchiveAutoTreeItemFactory
 
 class FactoryMocks(object):
     def mock_tree_manager(self):
     def unmock_resolve_function(self):
         urlresolvers.reverse = self.reverse_func
 
-    def mock_year_tree_item_factory(self):
-        self.factory.year_tree_item_factory = Mock()
-        self.factory.year_tree_item_factory.get_or_create.return_value = TreeItem(title='parent')
+    def mock_auto_tree_item_factory(self):
+        self.factory.auto_tree_item_factory = Mock(spec_set=AutoTreeItemFactory)
+
+    def mock_year_tree_item_factory(self, date):
+        self.factory.year_tree_item_factory = Mock(spec_set=YearTreeItemFactory)
+        self.factory.year_tree_item_factory.get_or_create.return_value = TreeItem(title=date.strftime('%Y'))
+
+    def mock_month_tree_item_factory(self, date):
+        self.factory.month_tree_item_factory = Mock(spec_set=MonthTreeItemFactory)
+        self.factory.month_tree_item_factory.get_or_create.return_value = TreeItem(title=date.strftime('%B'))
 
 class TreeFactoryTests(TestCase, FactoryMocks):
     def setUp(self):
         self.assertEqual(result, datetime.datetime.now().strftime('%B'))
 
     def test_that_when_requesting_the_tree_item_parent_the_the_year_tree_item_is_requested(self):
-        self.mock_year_tree_item_factory()
-
         test_date = datetime.datetime.now()
         test_date = test_date - datetime.timedelta(microseconds=test_date.microsecond, seconds=test_date.second)
+
+        self.mock_year_tree_item_factory(test_date)
+
         self.factory.get_parent(test_date)
 
         self.factory.year_tree_item_factory.get_or_create.assert_called_once_with(test_date)
 
     def test_that_when_requesting_the_tree_item_the_arguments_are_correct(self):
+        test_date = datetime.datetime.now()
+        test_date = test_date - datetime.timedelta(microseconds=test_date.microsecond, seconds=test_date.second)
+        month = datetime.datetime.now().strftime('%B')
+
         self.mock_tree_factory()
-        self.mock_year_tree_item_factory()
+        self.mock_year_tree_item_factory(test_date)
 
-        self.factory.get_or_create(datetime.datetime.now())
-        month = datetime.datetime.now().strftime('%B')
+        self.factory.get_or_create(test_date)
 
         args = {
                     'inmenu': True,
         TreeItem.objects.get_or_create.assert_called_once_with(inmenu=True,
                                                                inbreadcrumbs= True,
                                                                insitetree=True,
+                                                               urlaspattern=False,
                                                                access_loggedin=False,
                                                                access_restricted=False,
                                                                access_permissions=[],
         tree_item.save.assert_called_once_with(inmenu=True,
                                                inbreadcrumbs= True,
                                                insitetree=True,
+                                               urlaspattern=False,
                                                access_loggedin=False,
                                                access_restricted=False,
                                                access_permissions=[],
                                                alias='',
                                                description='',
                                                hint='')
+
+class ArchiveAutoTreeFactoryTests(TestCase, FactoryMocks):
+    def setUp(self):
+        self.test_date = datetime.datetime.now()
+        self.month = self.test_date.strftime('%B')
+
+        TreeItem.objects = self.mock_tree_item_manager()[0]
+        self.factory = ArchiveAutoTreeItemFactory('test', 'test')
+
+        self.mock_auto_tree_item_factory()
+        self.mock_month_tree_item_factory(self.test_date)
+
+        self.mock_tree_factory()
+
+    def tearDown(self):
+        super(ArchiveAutoTreeFactoryTests, self).setUp()
+
+    def testThat_when_updating_a_tree_item_all_parameters_are_passed_correctly(self):
+        self.factory.create_or_update('test', '/test/', self.test_date)
+
+        self.factory.auto_tree_item_factory.create_or_update.assert_called_once_with(inmenu=True,
+                                                                         inbreadcrumbs= True,
+                                                                         insitetree=True,
+                                                                         access_loggedin=False,
+                                                                         access_restricted=False,
+                                                                         access_permissions=[],
+                                                                         access_perm_type=TreeItem.PERM_TYPE_ANY,
+                                                                         title='test',
+                                                                         tree=Tree(alias='test'),
+                                                                         url='/test/',
+                                                                         parent=TreeItem(title=self.month),
+                                                                         sort_order=0,
+                                                                         alias='',
+                                                                         description='',
+                                                                         hint='')
+
+    def testThat_when_updating_a_tree_item_all_parameters_are_passed_correctly(self):
+        test_date = datetime.datetime.now()
+
+        self.factory.create_or_update('test', '/test/', test_date)
+
+        self.factory.month_tree_item_factory.get_or_create.assert_called_once_with(test_date)