class ItemAdminTest(TestCase):
    """
    Tests for mojo.navigation.admin.ItemAdmin
    """

    def setUp(self):
        # create a menu parent
        self.menu_parent = TestItem(name=u"Parent")
        self.menu_parent.save()

        # create a menu child
        self.menu_child = TestItem(parent=self.menu_parent, name=u"Child")
        self.menu_child.save()

        # create a site instance
        self.site = AdminSite()

    def test_level_limit_not_set_all_levels_returned(self):
        """
        Testing if level_limit is not set, all levels should be returned in the parent select.
        """
        item_admin = ItemAdmin(TestItem, self.site)
        form = item_admin.get_form(request)
        queryset = form.base_fields["parent"]._queryset
        self.assertEqual(len(queryset), 2)
        self.assertIn(self.menu_parent, queryset)
        self.assertIn(self.menu_child, queryset)

    def test_level_limit_set_only_filtered_levels_returned(self):
        """
        Testing if level_limit is set the parent field queryset should filter and remove levels underneath.
        """
        item_admin = ItemAdmin(TestItem, self.site)
        item_admin.level_limit = 1
        form = item_admin.get_form(request)
        queryset = form.base_fields["parent"]._queryset
        self.assertEqual(len(form.base_fields["parent"]._queryset), 1)
        self.assertNotIn(self.menu_child, queryset)

    def test_level_limit_not_set_do_move_any_level_allowed(self):
        """
        Testing if when the level_limit attribute is not set the do_move method should allow anything.
        """
        item_admin = ItemAdmin(TestItem, self.site)
        instance = item_admin.get_object(request, self.menu_child.id)
        parent_instance = item_admin.get_object(request, self.menu_parent.id)
        item_admin.do_move(instance, "inside", parent_instance)
        self.assertEqual(instance.parent, parent_instance)

    def test_level_limit_set_do_move_filters_level_allowed(self):
        """
        Testing if when the level_limit attribute is set the move_to method should not allow exceeding it.
        """
        item_admin = ItemAdmin(TestItem, self.site)
        item_admin.level_limit = 0
        instance = item_admin.get_object(request, self.menu_child.id)
        parent_instance = item_admin.get_object(request, self.menu_parent.id)
        with self.assertRaises(Exception):
            item_admin.do_move(instance, "inside", parent_instance)
 def test_unique_slug_generated(self):
     """
     Testing if a unique slug is generated when saving.
     """
     self.item.save()
     self.assertNotEqual('', self.item.slug)
     new_item = TestItem(name=self.item.name)
     new_item.save()
     self.assertNotEqual(self.item.slug, new_item.slug)
     self.assertIn('-', new_item.slug)
    def setUp(self):
        # create a menu parent
        self.menu_parent = TestItem(name=u"Parent")
        self.menu_parent.save()

        # create a menu child
        self.menu_child = TestItem(parent=self.menu_parent, name=u"Child")
        self.menu_child.save()

        # create a site instance
        self.site = AdminSite()
class ItemTest(TestCase):
    """
    Tests for mojo.navigation.models.MojoMenu
    """

    def setUp(self):
        self.item = TestItem(name=u"Test")

    def test_unicode_str_retun_object_name(self):
        """
        Testing if __str__ or __unicode__ return the Item object name.
        """
        self.assertEqual(self.item.__str__(), self.item.name)

    def test_url_is_valid_validation(self):
        """
        Testing if url_is_valid method triggers accurate result.
        """
        # when url is unset, False should be returned.
        self.item.url = ''
        self.assertFalse(self.item.url_is_valid())
        # when an invalid url is passed, False should be returned
        self.item.url = 'test.com'
        self.assertFalse(self.item.url_is_valid())
        self.item.url = '/test.com'
        self.assertFalse(self.item.url_is_valid())
        self.item.url = 'http://'
        self.assertFalse(self.item.url_is_valid())
        # when a valid url is passed, True should be returned
        self.item.url = 'http://test.com/test'
        self.assertTrue(self.item.url_is_valid())

    def test_url_is_pattern_validation(self):
        """
        Testing if url_is_pattern method triggers accurate result.
        """
        # when url is unset, False should be returned.
        self.item.url = ''
        self.assertFalse(self.item.url_is_pattern())
        # when an invalid url is passed, False should be returned
        self.item.url = 'http://test.com'
        self.assertFalse(self.item.url_is_pattern())
        # when a valid url is passed, True should be returned
        self.item.url = 'admin:auth_user_changelist'
        self.assertTrue(self.item.url_is_pattern())

    def test_setting_href_from_url(self):
        """
        Testing if the href field is properly set from the url field.
        """
        # when url is unset, href should not be set as well.
        self.item.url = ''
        self.item.save()
        self.assertEqual(self.item.href, '')
        # when an absolute url is set for url field href should take it.
        self.item.url = 'http://test.com'
        self.item.save()
        self.assertEqual(self.item.href, self.item.url)
        # when a django url pattern is set for url field href should be set to its path.
        self.item.url = 'admin:auth_user_changelist'
        self.item.save()
        self.assertEqual(self.item.href, '/admin/auth/user/')
        # when url field set to a relative, href take the same value.
        self.item.url = '/test'
        self.item.save()
        self.assertEqual(self.item.href, self.item.url)
        # when the url field is set and valid as well as content_object, href should prioritize url field.
        self.item.content_object = self.item
        self.item.save()
        self.assertEqual(self.item.href, self.item.url)
        # when the url field is not set or valid but content_object is set, href should be equal to the conten_object
        # get_absolute_url() value.
        self.item.url = ''
        self.item.save()
        self.assertEqual(self.item.href, self.item.get_absolute_url())

    def test_unique_slug_generated(self):
        """
        Testing if a unique slug is generated when saving.
        """
        self.item.save()
        self.assertNotEqual('', self.item.slug)
        new_item = TestItem(name=self.item.name)
        new_item.save()
        self.assertNotEqual(self.item.slug, new_item.slug)
        self.assertIn('-', new_item.slug)
 def setUp(self):
     self.item = TestItem(name=u"Test")