Add uniqueness test case

This commit is contained in:
Val L33 2011-10-26 14:32:57 -04:00
parent 90c63c180f
commit 8ffd557773
4 changed files with 84 additions and 27 deletions

60
README
View file

@ -6,7 +6,7 @@ A custom slug app for Django, that guarantees Double (U)s for (Uniqueness & Unic
Most of the code is originally by snippet http://www.djangosnippets.org/snippets/369/
Improved slightly to handle unique slugs and unicode chars and packaged by Val L33.
Patches welcome: http://bitbucket.org/un33k/django-uuslug
Patches welcome: https://github.com/un33k/django-uuslug
Usage
=====
@ -15,20 +15,20 @@ If you want a worry free slug-er, then this is for you.
It handles unicode and it produces unique slugs.
Here what you need to do:
1. Install (via source code) or (pip install django-uuslug) ... etc.
2. Stick ``"uuslug"`` in ``INSTALLED_APPS``, Then import it like: from uuslug import uuslug as slugify
1. Install (via source code on github) or (pip install django-uuslug) ... etc.
2. Then import it like: from uuslug import uuslug as slugify
That's it.
String Test Example
Unicode Test Example
=====
from uuslug import uuslug as slugify
s = "This is a test ---"
r = slugify(s)
self.assertEquals(r, "this-is-a-test")
s = 'C\'est déjà l\'été.'
r = slugify(s)
self.assertEquals(r, "c-est-deja-l-ete")
@ -36,30 +36,48 @@ String Test Example
s = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(s)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
s = '影師嗎'
r = slugify(s)
self.assertEquals(r, "ying-shi-ma")
self.assertEquals(r, "ying-shi-ma")
Object Example
Uniqueness Test Example
=====
Override your object's save method with something like this
Override your object's save method with something like this (models.py)
from django.db import models
from uuslug import uuslug as slugify
def save(self, *args, **kwargs):
self.slug = slugify(self.name, instance=self)
super(State, self).save(*args, **kwargs)
class CoolSlug(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
if you had two "names" as John & John, the slug for the first John would be "john" &
for the second John it would be "john-1"
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name, instance=self)
super(CoolSlug, self).save(*args, **kwargs)
Test:
name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
self.assertEquals(c.slug, name)
c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1")
Notice:
the slug for the first object would be "john", while the second object's slug
would be "john-1".
ToDo
=====
add templatetags
add testcase for templatetags
add fixture to test uuslug on objects
clean up README

View file

@ -1,3 +1,19 @@
# left black, required to load the app up
import os
# create a database table only in unit test mode
if os.environ['DJANGO_SETTINGS_MODULE'] == 'uuslug.testsettings':
from django.db import models
from uuslug import uuslug as slugify
class CoolSlug(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=200)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name, instance=self)
super(CoolSlug, self).save(*args, **kwargs)

View file

@ -2,10 +2,11 @@
"""Unit tests for uslug"""
from django.test import TestCase
from django.template import Context, Template
from uuslug.models import CoolSlug
from uuslug import uuslug as slugify
class SlugTestCase(TestCase):
"""Tests for Slug"""
class SlugUnicodeTestCase(TestCase):
"""Tests for Slug - Unicode"""
def test_manager(self):
s = "This is a test ---"
@ -22,4 +23,21 @@ class SlugTestCase(TestCase):
s = '影師嗎'
r = slugify(s)
self.assertEquals(r, "ying-shi-ma")
self.assertEquals(r, "ying-shi-ma")
class SlugUniqueTestCase(TestCase):
"""Tests for Slug - Unique"""
def test_manager(self):
name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
self.assertEquals(c.slug, name)
c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1")

View file

@ -2,11 +2,16 @@ import os
DEBUG = TEMPLATE_DEBUG = True
PROJECT_NAME = "uuslug"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/tmp/uuslug.db'
'NAME': PROJECT_NAME.strip().split(".")[0]+"_db"
}
}
INSTALLED_APPS = ['uuslug']
INSTALLED_APPS = [
'uuslug',
]