Commits

Ferran Pegueroles  committed 748ab03

Complete testing

  • Participants
  • Parent commits e11e3c7

Comments (0)

Files changed (11)

 # use glob syntax.
 syntax: glob
 *.pyc
-./*/settings/local.py
-./*/settings/secret.py
-./*/static/
-./*/media/
-./dev.db
+dev.db
+htmlcov
+.coverage

File last_used/__init__.py

+
+from models import LastUsed, ContentType
+
+LIMIT = 10
+
+
+def use(object, user, key=None, when=None):
+    args = {
+       'user': user,
+       'content_object': object
+    }
+
+    if when:
+        args['last_used'] = when
+
+    if key:
+        args['key'] = key
+
+    obj = LastUsed.objects.create(**args)
+
+    filters = args.copy()
+
+    del filters['content_object']
+    filters['content_type'] = obj.content_type
+
+    for lu in LastUsed.objects.filter(**filters)[LIMIT:]:
+        lu.delete()
+
+    return obj
+
+
+def get(model=None, user=None, key=None, limit=0):
+
+    filters = {}
+    if model:
+        filters['content_type'] = ContentType.objects.get_for_model(model)
+
+    if user:
+        filters['user'] = user
+
+    if key:
+        filters['key'] = key
+
+    objects = LastUsed.objects.filter(**filters)
+
+    if limit:
+        objects = objects[:limit]
+
+    return objects

File last_used/admin.py

+
+from django.contrib import admin
+
+from models import LastUsed
+
+
 class LastUsedAdmin(admin.ModelAdmin):
 
-    list_search = ('user__username', 'content_type__name')
+    search_fields = ('user__username', 'content_type__name')
     list_filter = ('last_used', 'key', 'content_type')
 
-    list_fileds = ('user', 'contnet_type', 'contnet_object', 'last_used')
+    list_display = ('user', 'content_type', 'content_object', 'last_used')
 
+
+admin.site.register(LastUsed, LastUsedAdmin)

File last_used/models.py

+
 from django.db import models
+from django.contrib.auth.models import User
+from django.utils import timezone
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes import generic
 
-# Create your models here.
+
+class LastUsed(models.Model):
+    user = models.ForeignKey(User)
+    last_used = models.DateTimeField(default=timezone.now)
+
+    content_type = models.ForeignKey(ContentType)
+    object_id = models.PositiveIntegerField()
+    content_object = generic.GenericForeignKey('content_type', 'object_id')
+
+    key = models.CharField(max_length=64, blank=True, null=True)
+
+    class Meta:
+        ordering = ("-last_used",)
+
+    def __unicode__(self):
+        if self.key:
+            return u"%s used %s (%s) on %s" % \
+                    (self.user, self.content_object, self.key, self.last_used)
+        return u"%s used %s on %s" % \
+                    (self.user, self.content_object, self.last_used)

File last_used/templatetags/__init__.py

Empty file added.

File last_used/tests.py

 
 Replace this with more appropriate tests for your application.
 """
+import datetime
+from django.test import TestCase
+from django.utils.timezone import utc
 
-from django.test import TestCase
+from last_used import use, get, LIMIT
+from last_used.models import LastUsed, User
 
-from last_used import use, get
-from last_used.models import LastUsed
 
-def TestUse(TestCase):
+class TestUse(TestCase):
 
     def setUp(self):
-        self.user = User.obejcts.create(name='test_user')
-
+        self.user = User.objects.create(username='test_user')
 
     def test_use(self):
 
         obj = use(self.user, self.user)
 
         self.assertEqual(obj.user, self.user)
-        self.assertEqual(obj.content_object, user)
+        self.assertEqual(obj.content_object, self.user)
 
     def test_use_key(self):
 
         self.assertEqual(obj.content_object, self.user)
 
     def test_use_when(self):
-        when = datetime.datetime(2000,1,1,4,3,2,1,0)
-        obj = use(user, user, when=when)
+        when = datetime.datetime(2000, 1, 1, 4, 3, 2, 1).replace(tzinfo=utc)
+        obj = use(self.user, self.user, when=when)
 
-        self.assertEqual(obj.when, when)
+        self.assertEqual(obj.last_used, when)
         self.assertEqual(obj.user, self.user)
         self.assertEqual(obj.content_object, self.user)
 
     def test_use_delete(self):
 
         for i in range(LIMIT + 2):
-           use(self.user, self.user)
+            use(self.user, self.user)
 
-        self.assertEqual(LastUser.objects.count(), LIMIT)
+        self.assertEqual(LastUsed.objects.count(), LIMIT)
 
     def test_use_keys(self):
 
         for i in range(LIMIT + 2):
-           use(self.user, self.user, key=str(i))
+            use(self.user, self.user, key=str(i))
 
-        self.assertEqual(LastUser.objects.count(), LIMIT + 2)
+        self.assertEqual(LastUsed.objects.count(), LIMIT + 2)
 
 
-def TestGet(TestCase):
+class TestGet(TestCase):
 
     def setUp(self):
-        self.user = User.obejcts.create(name='test_user')
+        self.user = User.objects.create(username='test_user')
 
     def test_empty(self):
 
 
     def test_found(self):
 
-        last_used = LastUsed.objects.create(user=self.user, content_object=self.user)
-        objs = list(get(self.user, self.user))
+        last_used = LastUsed.objects.create(user=self.user,
+                                        content_object=self.user)
+        objs = list(get(User, self.user))
 
         self.assertEqual(len(objs), 1)
         self.assertEqual(objs[0], last_used)
 
         self.assertEqual(len(objs), 2)
 
+    def test_no_model(self):
 
+        objs = get(user=self.user)
+
+        self.assertEqual(objs.count(), 0)
+
+    def test_no_user(self):
+
+        objs = get(model=self.user)
+
+        self.assertEqual(objs.count(), 0)
+
+    def test_with_key(self):
+
+        objs = get(key="some_key")
+
+        self.assertEqual(objs.count(), 0)
+
+
+class TestModel(TestCase):
+
+    def test_unicode_no_key(self):
+        user = User(username="test_user")
+        last_used = LastUsed(content_object=user, user=user)
+
+        self.assertIn(unicode(user), unicode(last_used))
+
+    def test_unicode_key(self):
+        user = User(username="test_user")
+        last_used = LastUsed(content_object=user, user=user, key="key_test")
+
+        self.assertIn(unicode(user), unicode(last_used))
+        self.assertIn("key_test", unicode(last_used))

File last_used/views.py

-# Create your views here.

File manage.py

File contents unchanged.
+#!/bin/sh
+
+MODULES=${1:-last_used}
+
+coverage run --branch ./manage.py test $MODULES || exit 2
+coverage report --include="./last_used/*" --omit="./*/migrations/*"
+coverage html --include="./last_used/*" --omit="./*/migrations/*"

File test_project/settings.py

 
 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
-        'NAME': '',                      # Or path to database file if using sqlite3.
+        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
+        'NAME': 'dev.db',                      # Or path to database file if using sqlite3.
         'USER': '',                      # Not used with sqlite3.
         'PASSWORD': '',                  # Not used with sqlite3.
         'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
     'django.contrib.messages',
     'django.contrib.staticfiles',
     # Uncomment the next line to enable the admin:
-    # 'django.contrib.admin',
+    'django.contrib.admin',
     # Uncomment the next line to enable admin documentation:
     # 'django.contrib.admindocs',
+    'last_used'
 )
 
 # A sample logging configuration. The only tangible logging

File test_project/urls.py

 from django.conf.urls import patterns, include, url
 
 # Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+from django.contrib import admin
+admin.autodiscover()
 
 urlpatterns = patterns('',
     # Examples:
     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
     # Uncomment the next line to enable the admin:
-    # url(r'^admin/', include(admin.site.urls)),
+    url(r'^admin/', include(admin.site.urls)),
 )