-
-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
265 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ tagging to your project easy and fun. | |
forms | ||
admin | ||
serializers | ||
testing | ||
api | ||
faq | ||
custom_tagging | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Testing | ||
======= | ||
|
||
Natural Key Support | ||
------------------- | ||
We have added `natural key support <https://docs.djangoproject.com/en/5.0/topics/serialization/#natural-keys>`_ to the Tag model in the Django taggit library. This allows you to identify objects by human-readable identifiers rather than by their database ID:: | ||
|
||
python manage.py dumpdata taggit.Tag --natural-foreign --natural-primary > tags.json | ||
|
||
python manage.py loaddata tags.json | ||
|
||
By default tags use the name field as the natural key. | ||
|
||
You can customize this in your own custom tag model by setting the ``natural_key_fields`` property on your model the required fields. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from taggit.models import Tag | ||
|
||
from .models import Food | ||
|
||
|
||
|
@@ -10,6 +12,11 @@ def setUp(self): | |
super().setUp() | ||
self.apple = Food.objects.create(name="apple") | ||
self.apple.tags.add("Red", "red") | ||
self.pear = Food.objects.create(name="pear") | ||
self.pear.tags.add("red", "RED") | ||
self.peach = Food.objects.create(name="peach") | ||
self.peach.tags.add("red", "Yellow") | ||
|
||
user = User.objects.create_superuser( | ||
username="admin", email="[email protected]", password="password" | ||
) | ||
|
@@ -40,3 +47,33 @@ def test_get_change(self): | |
reverse("admin:tests_food_change", args=(self.apple.pk,)) | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_tag_merging(self): | ||
response = self.client.get(reverse("admin:taggit_tag_changelist")) | ||
|
||
# merging red and RED into Red | ||
pks_to_select = [Tag.objects.get(name="red").pk, Tag.objects.get(name="RED").pk] | ||
response = self.client.post( | ||
reverse("admin:taggit_tag_changelist"), | ||
data={"action": "render_tag_form", "_selected_action": pks_to_select}, | ||
) | ||
# we're redirecting | ||
self.assertEqual(response.status_code, 302) | ||
# make sure what we expected got into the session keys | ||
assert "selected_tag_ids" in self.client.session.keys() | ||
self.assertEqual( | ||
self.client.session["selected_tag_ids"], ",".join(map(str, pks_to_select)) | ||
) | ||
|
||
# let's do the actual merge operation | ||
response = self.client.post( | ||
reverse("admin:taggit_tag_merge_tags"), {"new_tag_name": "Red"} | ||
) | ||
self.assertEqual(response.status_code, 302) | ||
|
||
# time to check the result of the merges | ||
self.assertSetEqual({tag.name for tag in self.apple.tags.all()}, {"Red"}) | ||
self.assertSetEqual({tag.name for tag in self.pear.tags.all()}, {"Red"}) | ||
self.assertSetEqual( | ||
{tag.name for tag in self.peach.tags.all()}, {"Yellow", "Red"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from taggit.models import Tag | ||
from tests.models import Food, HousePet | ||
|
||
|
||
class RemoveOrphanedTagsTests(TestCase): | ||
def setUp(self): | ||
# Create some tags, some orphaned and some not | ||
self.orphan_tag1 = Tag.objects.create(name="Orphan1") | ||
self.orphan_tag2 = Tag.objects.create(name="Orphan2") | ||
self.used_tag = Tag.objects.create(name="Used") | ||
|
||
# Create instances of Food and HousePet and tag them | ||
self.food_item = Food.objects.create(name="Apple") | ||
self.pet_item = HousePet.objects.create(name="Fido") | ||
|
||
self.food_item.tags.add(self.used_tag) | ||
self.pet_item.tags.add(self.used_tag) | ||
|
||
def test_remove_orphaned_tags(self): | ||
# Ensure the setup is correct | ||
self.assertEqual(Tag.objects.count(), 3) | ||
self.assertEqual(Tag.objects.filter(taggit_taggeditem_items=None).count(), 2) | ||
|
||
# Call the management command to remove orphaned tags | ||
call_command("remove_orphaned_tags") | ||
|
||
# Check the count of tags after running the command | ||
self.assertEqual(Tag.objects.count(), 1) | ||
self.assertEqual(Tag.objects.filter(taggit_taggeditem_items=None).count(), 0) | ||
|
||
# Ensure that the used tag still exists | ||
self.assertTrue(Tag.objects.filter(name="Used").exists()) | ||
self.assertFalse(Tag.objects.filter(name="Orphan1").exists()) | ||
self.assertFalse(Tag.objects.filter(name="Orphan2").exists()) | ||
|
||
def test_no_orphaned_tags(self): | ||
# Ensure the setup is correct | ||
self.assertEqual(Tag.objects.count(), 3) | ||
self.assertEqual(Tag.objects.filter(taggit_taggeditem_items=None).count(), 2) | ||
|
||
# Add used_tag to food_item to make no tags orphaned | ||
self.food_item.tags.add(self.orphan_tag1) | ||
self.food_item.tags.add(self.orphan_tag2) | ||
|
||
# Call the management command to remove orphaned tags | ||
call_command("remove_orphaned_tags") | ||
|
||
# Check the count of tags after running the command | ||
self.assertEqual(Tag.objects.count(), 3) | ||
self.assertEqual(Tag.objects.filter(taggit_taggeditem_items=None).count(), 0) | ||
|
||
# Ensure all tags still exist | ||
self.assertTrue(Tag.objects.filter(name="Used").exists()) | ||
self.assertTrue(Tag.objects.filter(name="Orphan1").exists()) | ||
self.assertTrue(Tag.objects.filter(name="Orphan2").exists()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters