Commit dba4d502 by Alex Gaynor

Started adding tests.

parent f12660d4
......@@ -2,7 +2,7 @@ from django.contrib.contenttypes.models import ContentType
from django.db import models
from taggit.models import Tag, TaggedItem
from tagging.utils import require_instance_manager
from taggit.utils import require_instance_manager
class TaggableManager(object):
......@@ -23,25 +23,23 @@ class _TaggableManager(models.Manager):
return Tag.objects.filter(items__object_id=self.object_id,
items__content_type=ct)
else:
return Tag.objects.filter(items__content_type=ct)
return Tag.objects.filter(items__content_type=ct).distinct()
@require_instance_manager
def add_tag(self, tag):
def add(self, tags):
if isinstance(tags, basestring):
tags = [tags]
for tag in tags:
tag, _ = Tag.objects.get_or_create(name=tag)
TaggedItem.objects.create(object_id=self.object_id,
content_type=ContentType.objects.get_for_model(self.model), tag=tag)
@require_instance_manager
def add_tags(self, tags):
for tag in tags:
self.add_tag(tag)
@require_instance_manager
def set_tags(self, tags):
self.clear_tags()
self.add_tags(tags)
def set(self, tags):
self.clear()
self.add(tags)
@require_instance_manager
def clear_tags(self):
def clear(self):
TaggedItem.objects.filter(object_id=self.object_id,
content_type=ContentType.objects.get_for_model(self.model)).delete()
from taggit.tests.tests import (AddTagTestCase)
from django.db import models
from taggit.managers import TaggableManager
class Food(models.Model):
name = models.CharField(max_length=50)
tags = TaggableManager()
DATABASE_ENGINE = 'sqlite3'
INSTALLED_APPS = [
'django.contrib.contenttypes',
'taggit',
'taggit.tests',
]
from django.test import TestCase
from taggit.models import Tag
from taggit.tests.models import Food
class BaseTaggingTest(TestCase):
def assert_tags_equal(self, qs, tags):
tags = Tag.objects.filter(name__in=tags)
self.assertEqual(list(qs), list(tags))
class AddTagTestCase(BaseTaggingTest):
def test_add_tag(self):
apple = Food.objects.create(name="apple")
self.assertEqual(list(apple.tags.all()), [])
self.assertEqual(list(Food.tags.all()), [])
apple.tags.add('green')
self.assert_tags_equal(apple.tags.all(), ['green'])
self.assert_tags_equal(Food.tags.all(), ['green'])
pair = Food.objects.create(name="pair")
pair.tags.add('green')
self.assert_tags_equal(pair.tags.all(), ['green'])
self.assert_tags_equal(Food.tags.all(), ['green'])
......@@ -4,7 +4,7 @@ def split_tags(tags):
return [o.strip() for o in tags.split(',') if o.strip()]
def require_instance_manager(func):
@wraps
@wraps(func)
def inner(self, *args, **kwargs):
if self.object_id is None:
raise TypeError("Can't call %s with a non-instance manager" % func.__name__)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment