def test_get_unique_item_set(self): self.item.tags.add(self.tag1) item2 = TestItem(name="test-item-2") item2.save() item2.tags.add(self.tag1) items = self.tag1.unique_item_set() self.assertTrue(self.item in items) self.assertTrue(item2 in items)
def _setup_items_with_tags(self): self.item.tags.add(self.tag1) ignored_model = IgnoreTestItem(name="ignore_me") ignored_model.save() ignored_model.tags.add(self.tag1) item2 = TestItem(name="test-item-2") item2.save() item2.tags.add(self.tag1)
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)))
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]
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)))