Skip to content

Commit 412d7dd

Browse files
chris48sBethanyG
andauthored
add tests for tag slugging (#121)
* add tests for tag slugging * validate slugs on model save - prevent generation of tag with non-unique slug - prevent generation of tag with name containing only invalid characters * skip assertions about brahmic/abugida slugs Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
1 parent 1ad65a9 commit 412d7dd

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed

project/resources/migrations/0006_auto_20200303_1257.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
88

99
dependencies = [
1010
('resources', '0005_auto_20200118_1036'),
11-
('tagging', '__latest__')
11+
('tagging', '0001_initial')
1212
]
1313

1414
operations = [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.2.4 on 2020-04-05 10:12
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('tagging', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='customtag',
15+
name='slug',
16+
field=models.SlugField(allow_unicode=True, max_length=100, unique=True, verbose_name='Slug'),
17+
),
18+
]

project/tagging/models.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
class CustomTag(TagBase):
1010
guid = models.UUIDField(default=uuid.uuid1, editable=False)
11-
slug = models.SlugField(verbose_name=_("Slug"), unique=True, max_length=100)
11+
slug = models.SlugField(
12+
verbose_name=_("Slug"),
13+
unique=True,
14+
max_length=100,
15+
allow_unicode=True
16+
)
1217
name = models.CharField(verbose_name=_("Name"), unique=True, max_length=100)
1318

1419
class Meta:
@@ -17,15 +22,13 @@ class Meta:
1722
verbose_name_plural = _("Tags")
1823
app_label = 'tagging'
1924

25+
def save(self, *args, **kwargs):
26+
self.slug = self.slugify(self.name)
27+
self.full_clean()
28+
return super().save(*args, **kwargs)
2029

2130
def slugify(self, tag, i=None):
22-
slug = slugify(tag, allow_unicode=True)
23-
24-
if i is not None:
25-
slug += "_%d" % i
26-
27-
return slug
28-
31+
return slugify(tag, allow_unicode=True)
2932

3033
class TaggedItems(GenericTaggedItemBase, TaggedItemBase):
3134
tag = models.ForeignKey(CustomTag,

project/tagging/tests.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
1+
import unittest
2+
from django.core.exceptions import ValidationError
13
from django.test import TestCase
4+
from .models import CustomTag
25

3-
# Create your tests here.
6+
7+
class CustomTagTests(TestCase):
8+
def test_valid_slugs(self):
9+
test_tags = [
10+
{"name": "programming", "expected_slug": "programming"},
11+
{"name": "PyCon", "expected_slug": "pycon"},
12+
{"name": "local storage", "expected_slug": "local-storage"},
13+
{"name": "PEN testing", "expected_slug": "pen-testing"},
14+
{"name": "תִּיכְנוּת", "expected_slug": "תיכנות"},
15+
{"name": " 프로그램 작성", "expected_slug": "프로그램-작성"},
16+
{"name": "程式设计", "expected_slug": "程式设计"},
17+
{"name": "برمجة", "expected_slug": "برمجة"},
18+
{"name": "आनंद", "expected_slug": "आनद"},
19+
{"name": "лягушачий", "expected_slug": "лягушачий"},
20+
{"name": "教程", "expected_slug": "教程"},
21+
{"name": "Inicio r\u00e1pido", "expected_slug": "inicio-r\u00e1pido"},
22+
{"name": "最后", "expected_slug": "最后"},
23+
{"name": " 欲求不満", "expected_slug": "欲求不満"},
24+
{"name": "စမ်းသပ်ခြင်း", "expected_slug": "စမသပခင"},
25+
{"name": "ฐานข้อมูล", "expected_slug": "ฐานขอมล"},
26+
{"name": "основы", "expected_slug": "основы"},
27+
{"name": "אַלגערידאַם", "expected_slug": "אלגערידאם"},
28+
{"name": "自動化する", "expected_slug": "自動化する"},
29+
{"name": "sjálfvirkan", "expected_slug": "sjálfvirkan"},
30+
{"name": "پژوهش ", "expected_slug": "پژوهش"},
31+
{"name": " గ్రాఫ్", "expected_slug": "గరఫ"},
32+
{"name": "데이터 베이스", "expected_slug": "데이터-베이스"},
33+
{"name": "stòran-dàta", "expected_slug": "stòran-dàta"},
34+
]
35+
36+
for entry in test_tags:
37+
tag = CustomTag(name=entry["name"])
38+
tag.save()
39+
self.assertEqual(tag.slug, entry["expected_slug"])
40+
41+
@unittest.skip('https://github.com/codebuddies/backend/issues/123')
42+
def test_brahmic_abugida_slugs(self):
43+
test_tags = [
44+
{"name": "हिंदी में जानकारी", "expected_slug": "TODO"},
45+
{"name": "प्रयास है", "expected_slug": "TODO"},
46+
{"name": "స్వయంచాలక", "expected_slug": "TODO"},
47+
]
48+
49+
for entry in test_tags:
50+
tag = CustomTag(name=entry["name"])
51+
tag.save()
52+
self.assertEqual(tag.slug, entry["expected_slug"])
53+
54+
def test_invalid_slugs(self):
55+
invalid_tag_names = [
56+
"❤🐸",
57+
"🐸",
58+
" %",
59+
"//",
60+
]
61+
for name in invalid_tag_names:
62+
with self.assertRaises(ValidationError):
63+
tag = CustomTag(name=name)
64+
tag.save()
65+
66+
def test_duplicates(self):
67+
tag1 = CustomTag(name='javascript')
68+
tag1.save()
69+
70+
# fail if we try to generate more tags with the same slug
71+
with self.assertRaises(ValidationError):
72+
tag2 = CustomTag(name='Javascript')
73+
tag2.save()
74+
75+
with self.assertRaises(ValidationError):
76+
tag3 = CustomTag(name='JaVascripT%/')
77+
tag3.save()

0 commit comments

Comments
 (0)