Beispiel #1
0
class TestSlugification(TestCase):
    def setUp(self):
        self.group = TagGroup(name="normal group", system=False)
        self.group.save()

        self.tag_a = Tag(group=self.group, name="tag a")
        self.tag_b = Tag(group=self.group, name="TAG b")
        self.tag_a.save()
        self.tag_b.save()

    def test_slug_values_a(self):
        self.assertEquals(self.tag_a.slug, "tag-a")

    def test_slug_values_b(self):
        self.assertEquals(self.tag_b.slug, "tag-b")
Beispiel #2
0
class TestSystemTags(TestCase):
    """
    System tags are designed not to appear in most UI - they are auto-added
    by models so that they can be searched on and have other functionality.
    A custom manager on the Tag prevents these appearing in e.g. admin for
    users when tagging items. But other managers allow access to the whole
    world of tags. Here we test these managers, as well as the rendering of
    system tags.
    """
    def setUp(self):
        self.sys_group = TagGroup(name="system_group", system=True)
        self.sys_group.save()
        self.nonsys_group = TagGroup(name="normal_group", system=False)
        self.nonsys_group.save()

        self.tag_a = Tag(group=self.sys_group, name="tag_a")
        self.tag_b = Tag(group=self.sys_group, name="tag_b")
        self.tag_c = Tag(group=self.nonsys_group, name="tag_c")
        self.tag_d = Tag(group=self.nonsys_group, name="tag_d")
        [getattr(self, "tag_%s" % x).save() for x in "abcd"]

        self.systags = set([self.tag_a, self.tag_b])
        self.nonsystags = set([self.tag_c, self.tag_d])

    def test_manager_public(self):
        nonsystags = set(Tag.public_objects.all())
        self.assertEquals(nonsystags, self.nonsystags)

    def test_manager_systags(self):
        systags = set(Tag.sys_objects.all())
        self.assertEquals(systags, self.systags)

    def test_manager_default(self):
        alltags = set(self.systags)
        alltags.update(self.nonsystags)
        returnedtags = set(Tag.objects.all())
        self.assertEquals(alltags, returnedtags)

    def test_sysgroup_representations(self):
        self.assertEquals(str(self.tag_a), "*system_group:tag_a")

    def test_non_sysgroup_representation(self):
        self.assertEquals(str(self.tag_c), "normal_group:tag_c")

    def test_tag_for_string_system_group(self):
        t = Tag.tag_for_string("*system_group:tag_a")
        self.assertEquals(t, self.tag_a)
Beispiel #3
0
class TestTags(TestCase):

    def setUp(self):
        self.group = TagGroup(name="test-group")
        self.group.save()
        self.groupb = TagGroup(name="test-group-b")
        self.groupb.save()
        self.groupc = TagGroup(name="system-group", system=True)
        self.groupc.save()
        self.tag1 = Tag(group=self.group, name="test-tag1")
        self.tag1.save()
        self.tag2 = Tag(group=self.group, name="test-tag2")
        self.tag2.save()
        self.item = TestItem(name="test-item")
        self.item.save()
        self.tags = [self.tag1, self.tag2]

    def test_create_tag(self):
        self.assertIsNotNone(self.tag1.pk)

    def test_create_group(self):
        self.assertIsNotNone(self.group.pk)

    def test_add_tag_to_item(self):
        [self.item.tags.add(tag) for tag in self.tags]
        self.assertTrue([tag for tag in self.item.tags.all()
                        if tag in self.tags])

    def test_tags_in_group(self):
        [self.group.tag_set.add(tag) for tag in self.tags]
        self.assertTrue([tag for tag in self.group.tags_for_group()
                         if tag in self.tags])

    def test_unique_tag_in_group(self):
        tag1_clone = Tag(group=self.group, name=self.tag1.name)
        self.assertRaises(IntegrityError, tag1_clone.save)

        # ... also test to see if we can make a tag with the same name as
        # tag1 but a different group. This must now be allowed.
        tag1_noclone = Tag(group=self.groupb, name=self.tag1.name)
        try:
            tag1_noclone.save()
        except IntegrityError, ie:
            self.fail("Unique constraint on group,name fails: {0}"
                      .format(str(ie)))
Beispiel #4
0
class TestTags(TestCase):

    def setUp(self):
        self.group = TagGroup(name="test-group")
        self.group.save()
        self.groupb = TagGroup(name="test-group-b")
        self.groupb.save()
        self.groupc = TagGroup(name="system-group", system=True)
        self.groupc.save()
        self.tag1 = Tag(group=self.group, name="test-tag1")
        self.tag1.save()
        self.tag2 = Tag(group=self.group, name="test-tag2")
        self.tag2.save()
        self.item = TestItem(name="test-item")
        self.item.save()
        self.tags = [self.tag1, self.tag2]

    def test_create_tag(self):
        self.assertIsNotNone(self.tag1.pk)

    def test_create_group(self):
        self.assertIsNotNone(self.group.pk)

    def test_group_name(self):
        """
        Test that the group name is correctly saved on tag

        This is inserted at time of re-factoring code to de-normalise
        group name into Tag.group_name
        """
        self.assertEquals(self.tag1.group_name, self.group.name)

    def test_group_name_change(self):
        """
        Test that group name re-set on group change for tag

        Confirms that de-normalisation works when group changes
        """
        self.assertEquals(self.tag1.group_name, self.group.name)
        self.tag1.group = self.groupb
        self.tag1.save()
        self.assertEquals(self.tag1.group_name, self.groupb.name)

    def test_group_slug(self):
        """
        Test that the group slug is correctly saved on tag

        This is inserted at time of re-factoring code to de-normalise
        group slug into Tag.group_slug
        """
        self.assertEquals(self.tag1.group_slug, self.group.slug)

    def test_group_slug_change(self):
        """
        Test that group slug re-set on group change for tag

        Confirms that de-normalisation works when group changes
        """
        self.assertEquals(self.tag1.group_slug, self.group.slug)
        self.tag1.group = self.groupb
        self.tag1.save()
        self.assertEquals(self.tag1.group_slug, self.groupb.slug)

    def test_unicode(self):
        """
        Does the tag represent itself correctly when stringified
        """
        self.assertEquals(str(self.tag1), "test-group:test-tag1")

    def test_unicode_system_tag(self):
        tag = Tag(group=self.groupc, name="foo")
        tag.save()
        self.assertEquals(str(tag), "*system-group:foo")

    def test_repr_system_tag(self):
        tag = Tag(group=self.groupc, name="foo")
        tag.save()
        self.assertEquals(repr(tag), "system-group:foo")

    def test_repr(self):
        """
        Does the tag represent itself correctly
        """
        self.assertEquals(repr(self.tag1), "test-group:test-tag1")

    def test_denormalised_system_flag(self):
        """
        Does tag reflect its group's system flag
        """
        tag = Tag(group=self.groupc, name="foo")
        tag.save()
        self.assertFalse(self.tag1.group_is_system)
        self.assertFalse(self.tag1.system)
        self.assertTrue(tag.group_is_system)
        self.assertTrue(tag.system)

    def test_group_change_updates_denormalised_tags_name(self):
        """
        Change group name and check tags de-normalised fields update
        """
        self.assertEquals(str(self.tag1), "test-group:test-tag1")
        self.group.name = "new-test-group"
        self.group.save()
        tag = Tag.objects.get(name="test-tag1")
        self.assertEquals(str(tag), "new-test-group:test-tag1")

    def test_group_change_updates_denormalised_tags_slug(self):
        """
        Change group slug and check tags de-normalised fields update
        """
        self.assertEquals(self.tag1.group_slug, "test-group")
        self.group.slug = "new-test-group"
        self.group.save()
        tag = Tag.objects.get(name="test-tag1")
        self.assertEquals(tag.group_slug, "new-test-group")

    def test_group_change_updates_denormalised_tags_system(self):
        """
        Change group system and check tags de-normalised fields update
        """
        self.assertEquals(self.tag1.group_is_system, False)
        self.group.system = True
        self.group.save()
        tag = Tag.objects.get(name="test-tag1")
        self.assertEquals(tag.group_is_system, True)

    def test_add_tag_to_item(self):
        [self.item.tags.add(tag) for tag in self.tags]
        self.assertTrue([tag for tag in self.item.tags.all()
                        if tag in self.tags])

    def test_tags_in_group(self):
        [self.group.tag_set.add(tag) for tag in self.tags]
        self.assertTrue([tag for tag in self.group.tags_for_group()
                         if tag in self.tags])

    def test_unique_tag_in_group(self):
        tag1_clone = Tag(group=self.group, name=self.tag1.name)
        self.assertRaises(IntegrityError, tag1_clone.save)

        # ... also test to see if we can make a tag with the same name as
        # tag1 but a different group. This must now be allowed.
        tag1_noclone = Tag(group=self.groupb, name=self.tag1.name)
        try:
            tag1_noclone.save()
        except IntegrityError, ie:
            self.fail("Unique constraint on group,name fails: {0}"
                      .format(str(ie)))