Omer Katz avatar Omer Katz committed b4939c9

Fixed a test issue where the microseconds difference in datetime.datetime.now()
caused a test to fail.
Refactored tests to reflect the addition of the missing tree item fields.
Refactored the code accordingly.
AutoTreeItemFactory now takes it's configuration out of the model's meta class (uncertain)

Comments (0)

Files changed (2)

autotree/factories.py

         self.alias = alias
 
     def get_or_create(self):
-        Tree.objects.get_or_create(alias=self.alias)
+        return Tree.objects.get_or_create(alias=self.alias)[0]
 
 class DateFormattedTreeItemFactory(object):
     def __init__(self,
                  logged_in_only_access=False,
                  restricted_only_access=False,
                  access_permissions = [],
-                 access_perm_type = 1):
+                 access_perm_type = TreeItem.PERM_TYPE_ANY):
         self.view_name = view_name
         self.tree_factory = TreeFactory(tree_alias)
-        self.is_in_menu = is_in_menu
-        self.is_in_breadcrumbs = is_in_breadcrumbs
-        self.is_in_sitetree = is_in_sitetree
-        self.logged_in_only_access = logged_in_only_access
-        self.restricted_only_access = restricted_only_access
-        self.access_permissions = access_permissions
-        self.access_perm_type = access_perm_type
+
+        self.args = {
+                        'inmenu': is_in_menu,
+                        '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 get_formatted_date(self, date):
         raise NotImplementedError('This function must be implemented in a derived class')
         return urlresolvers.reverse(self.view_name, args=[formatted_date])
 
     def get_or_create(self, date):
-        title = self.get_formatted_date(date)
-        url = self.get_url(title)
-        tree = self.tree_factory.get_or_create()
-        parent = self.get_parent(date)
+        self.args['title'] = self.get_formatted_date(date)
+        self.args['url'] = self.get_url(date)
+        self.args['tree'] = self.tree_factory.get_or_create()
+        self.args['parent'] = self.get_parent(date)
 
-        TreeItem.objects.get_or_create(title=title, url=url, tree=tree, parent=parent)
+        return TreeItem.objects.get_or_create(**self.args)[0]
 
 class YearTreeItemFactory(DateFormattedTreeItemFactory):
     def __init__(self,
                  logged_in_only_access=False,
                  restricted_only_access=False,
                  access_permissions = [],
-                 access_perm_type = 1):
+                 access_perm_type = TreeItem.PERM_TYPE_ANY):
         super(YearTreeItemFactory, self).__init__(view_name,
                                                   tree_alias,
                                                   is_in_menu,
         return self.year_tree_item_factory.get_or_create(date)
 
 class AutoTreeItemFactory(object):
-    def __init__(self,
-                 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,
-                 **kwargs):
-        self.tree_alias = tree_alias
-        self.is_in_menu = is_in_menu
-        self.is_in_breadcrumbs = is_in_breadcrumbs
-        self.is_in_sitetree = is_in_sitetree
-        self.is_url_a_pattern = is_url_a_pattern
-        self.logged_in_only_access = logged_in_only_access
-        self.restricted_only_access = restricted_only_access
-        self.access_permissions = access_permissions
-        self.access_perm_type = access_perm_type
+    def __init__(self, model_meta_class):
+        self.model_meta_class = model_meta_class
+        self.tree_alias = model_meta_class.tree_alias
+        self.is_in_menu = getattr(model_meta_class, "is_in_menu", True)
+        self.is_in_breadcrumbs = getattr(model_meta_class, "is_in_breadcrumbs", True)
+        self.is_in_sitetree = getattr(model_meta_class, "is_in_menu", True)
+        self.is_url_a_pattern = getattr(model_meta_class, "is_in_breadcrumbs", False)
+        self.logged_in_only_access = getattr(model_meta_class, "logged_in_only_access", False)
+        self.restricted_only_access = getattr(model_meta_class, "restricted_only_access", False)
+        self.access_permissions = getattr(model_meta_class, "access_permissions", [])
+        self.access_perm_type = getattr(model_meta_class, "access_perm_type", TreeItem.PERM_TYPE_ANY)
 
     def resolve_title(self, model_instance):
         pass
     def resolve_sort_order(self, model_instance):
         pass
 
-    def create_tree(self):
-        pass
-
     def create_item(self, model_instance, **kwargs):
         pass
 

autotree/tests.py

 
     def mock_tree_manager(self):
         mock = Mock(spec_set=Manager)
+        mock.get_or_create.return_value = (Tree(alias="test"), True)
 
         return mock
 
 
         self.factory.get_or_create(datetime.datetime.now())
 
-        TreeItem.objects.get_or_create.assert_called_once_with(title='test', url='/test/', tree=Tree(alias='test'), parent=TreeItem(title='parent'))
+        args = {
+                    'inmenu': True,
+                    'inbreadcrumbs': True,
+                    'insitetree': True,
+                    'access_loggedin': False,
+                    'access_restricted': False,
+                    'access_permissions': [],
+                    'access_perm_type': TreeItem.PERM_TYPE_ANY,
+                    'title': 'test',
+                    'url': '/test/',
+                    'tree': Tree(alias='test'),
+                    'parent': TreeItem(title='parent')
+        }
+
+        TreeItem.objects.get_or_create.assert_called_once_with(**args)
 
     def mock_tree_item_manager(self):
         mock = Mock(spec_set=Manager)
-
+        mock.get_or_create.return_value = (TreeItem(title="test"), True)
+        
         return mock
 
     def mock_tree_factory(self):
 
     def mock_tree_item_manager(self):
         mock = Mock(spec_set=Manager)
-
+        mock.get_or_create.return_value = (TreeItem(title="test"), True)
+        
         return mock
 
     def test_that_when_requesting_the_tree_item_formatted_date_the_return_value_is_the_year(self):
 
         self.factory.get_or_create(datetime.datetime.now())
         year = datetime.datetime.now().strftime('%Y')
-        TreeItem.objects.get_or_create.assert_called_once_with(title=year, url='/test/', tree=Tree(alias='test'), parent=None)
+
+        args = {
+                    'inmenu': True,
+                    'inbreadcrumbs': True,
+                    'insitetree': True,
+                    'access_loggedin': False,
+                    'access_restricted': False,
+                    'access_permissions': [],
+                    'access_perm_type': TreeItem.PERM_TYPE_ANY,
+                    'title': year,
+                    'url': '/test/',
+                    'tree': Tree(alias='test'),
+                    'parent': None
+        }
+
+        TreeItem.objects.get_or_create.assert_called_once_with(**args)
 
     def mock_tree_factory(self):
         self.factory.tree_factory = Mock(spec_set=TreeFactory)
         TreeItem.objects = Manager()
 
         urlresolvers.reverse = self.reverse_func
+        
+    def test_that_when_requesting_the_tree_item_formatted_date_the_return_value_is_the_month_name(self):
+        result = self.factory.get_formatted_date(datetime.datetime.now())
 
-    def mock_tree_item_manager(self):
-        mock = Mock(spec_set=Manager)
-
-        return mock
-
-    def test_that_when_requesting_the_tree_item_formatted_date_the_return_value_is_the_month_name(self):
-        self.assertEqual(self.factory.get_formatted_date(datetime.datetime.now()), datetime.datetime.now().strftime('%B'))
+        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()
 
-        self.factory.get_parent(datetime.datetime.now())
+        test_date = datetime.datetime.now()
+        test_date = test_date - datetime.timedelta(microseconds=test_date.microsecond, seconds=test_date.second)
+        self.factory.get_parent(test_date)
 
-        self.factory.year_tree_item_factory.get_or_create.assert_called_once_with(datetime.datetime.now())
+        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):
         self.mock_tree_factory()
 
         self.factory.get_or_create(datetime.datetime.now())
-        year = datetime.datetime.now().strftime('%B')
-        TreeItem.objects.get_or_create.assert_called_with(title=year, url='/test/', tree=Tree(alias='test'), parent=None)
+        month = datetime.datetime.now().strftime('%B')
+
+        args = {
+                    'inmenu': True,
+                    'inbreadcrumbs': True,
+                    'insitetree': True,
+                    'access_loggedin': False,
+                    'access_restricted': False,
+                    'access_permissions': [],
+                    'access_perm_type': TreeItem.PERM_TYPE_ANY,
+                    'title': month,
+                    'url': '/test/',
+                    'tree': Tree(alias='test'),
+                    'parent': TreeItem(title='parent')
+        }
+
+        TreeItem.objects.get_or_create.assert_called_with(**args)
+
+    def mock_tree_item_manager(self):
+        mock = Mock(spec_set=Manager)
+        mock.get_or_create.return_value = (TreeItem(title="test"), True)
+
+        return mock
 
     def mock_tree_factory(self):
         self.factory.tree_factory = Mock(spec_set=TreeFactory)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.