Skip to content
Browse files

created a testrunner and added some initial tests

  • Loading branch information...
1 parent 761ac22 commit bbecfe44888a2de69febdeaf9e819ecf32fe6eda @jefftriplett jefftriplett committed Jan 30, 2010
Showing with 261 additions and 0 deletions.
  1. +32 −0 runtests.py
  2. 0 tests/__init__.py
  3. +14 −0 tests/base.py
  4. +93 −0 tests/models.py
  5. +116 −0 tests/test_models.py
  6. +6 −0 tests/test_settings.py
View
32 runtests.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+
+import os
+import unittest
+
+if not 'DJANGO_SETTINGS_MODULE' in os.environ:
+ os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
+
+from django.conf import settings
+from django.db import connection
+from django.test.utils import setup_test_environment
+from django.test.utils import teardown_test_environment
+from django.utils.importlib import import_module
+
+TEST_MODULES = ["tests.base"]
+
+
+def run_tests():
+ setup_test_environment()
+ db_name = settings.DATABASE_NAME
+ suite = unittest.TestSuite()
+ for module_name in TEST_MODULES:
+ module = import_module(module_name)
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(module))
+ connection.creation.create_test_db()
+ unittest.TextTestRunner().run(suite)
+ connection.creation.destroy_test_db(db_name)
+ teardown_test_environment()
+
+
+if __name__ == "__main__":
+ run_tests()
View
0 tests/__init__.py
No changes.
View
14 tests/base.py
@@ -0,0 +1,14 @@
+import datetime
+import unittest
+
+from django.core import signals
+from django.db import models
+from django.db.models import signals
+from django.test import TestCase
+from django.utils.translation import ugettext_lazy as _
+
+from django_proxy.models import Proxy
+from django_proxy.signals import proxy_save, proxy_delete
+
+from tests.models import *
+from tests.test_models import *
View
93 tests/models.py
@@ -0,0 +1,93 @@
+import datetime
+import unittest
+
+from django.core import signals
+from django.db import models
+from django.db.models import signals
+from django.utils.translation import ugettext_lazy as _
+
+from django_proxy.models import Proxy
+from django_proxy.signals import proxy_save, proxy_delete
+
+
+class BasePost(models.Model):
+ title = models.CharField(_('title'), max_length=200)
+ slug = models.SlugField(_('slug'), unique_for_date='publish')
+ body = models.TextField(_('body'))
+ publish = models.DateTimeField(_('publish'))
+ tag_data = models.CharField(_('tags'), max_length=200)
+
+ class Meta:
+ abstract = True
+ ordering = ('-publish',)
+ get_latest_by = 'publish'
+
+ def __unicode__(self):
+ return u'%s' % self.title
+
+
+class PostWithStatus(BasePost):
+ """ PostWithStatus model """
+ DRAFT_STATUS = 1
+ PUBLIC_STATUS = 2
+ STATUS_CHOICES = (
+ (DRAFT_STATUS, _('Draft')),
+ (PUBLIC_STATUS, _('Public')),
+ )
+ status = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=PUBLIC_STATUS)
+
+ class ProxyMeta:
+ title = 'title'
+ description = 'body'
+ active = {'status': 2}
+ pub_date = 'publish'
+ tags = 'tag_data'
+
+
+class PostBoolean(BasePost):
+ """ PostBoolean model """
+ status = models.BooleanField(default=True)
+
+ class ProxyMeta:
+ title = 'title'
+ description = 'body'
+ pub_date = 'publish'
+ active = 'status'
+ tags = 'tag_data'
+
+
+class PostWithMethod(BasePost):
+ """ PostWithMethod model """
+ status = models.BooleanField(default=True)
+
+ class ProxyMeta:
+ title = 'get_title'
+ description = 'get_description'
+ #pub_date = 'get_publish'
+ active = 'get_active'
+ tags = 'get_tags'
+
+ def get_active(self):
+ return self.status
+
+ def get_description(self):
+ return '--%s--' % self.body
+
+ def get_publish(self):
+ return '--%s--' % self.publish
+
+ def get_title(self):
+ return '--%s--' % self.title
+
+ def get_tags(self):
+ return '--%s--' % self.tag_data
+
+
+signals.post_save.connect(proxy_save, PostWithStatus, True)
+signals.post_delete.connect(proxy_delete, PostWithStatus)
+
+signals.post_save.connect(proxy_save, PostBoolean, True)
+signals.post_delete.connect(proxy_delete, PostBoolean)
+
+signals.post_save.connect(proxy_save, PostWithMethod, True)
+signals.post_delete.connect(proxy_delete, PostWithMethod)
View
116 tests/test_models.py
@@ -0,0 +1,116 @@
+import datetime
+
+from django.test import TestCase
+
+from django_proxy.models import Proxy
+
+from tests.models import *
+
+
+class BasicTests(TestCase):
+
+ def test_models_with_status(self):
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+ now = datetime.datetime.now()
+
+ post = PostWithStatus()
+ post.title = 'test title 1'
+ post.slug = 'test-title-1'
+ post.body = 'the body of my test'
+ post.status = post.PUBLIC_STATUS
+ post.tag_data = 'one two three'
+ post.publish = now
+ post.save()
+
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ proxy = Proxy.objects.get(title='test title 1')
+ self.assertEqual(proxy.title, 'test title 1')
+ self.assertEqual(proxy.description, 'the body of my test')
+ self.assertEqual(proxy.tags, 'one two three')
+ self.assertEqual(proxy.pub_date, now)
+
+ post.status = post.DRAFT_STATUS
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+
+ post.status = post.PUBLIC_STATUS
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ PostWithStatus.objects.all().delete()
+
+
+ def test_models_with_boolean(self):
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+ now = datetime.datetime.now()
+
+ post = PostBoolean()
+ post.title = 'test title 2'
+ post.slug = 'test-title-2'
+ post.body = 'the body of my test'
+ post.status = True
+ post.tag_data = 'one two three'
+ post.publish = now
+ post.save()
+
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ proxy = Proxy.objects.get(title='test title 2')
+ self.assertEqual(proxy.title, 'test title 2')
+ self.assertEqual(proxy.description, 'the body of my test')
+ self.assertEqual(proxy.tags, 'one two three')
+ self.assertEqual(proxy.pub_date, now)
+
+ post.status = False
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+
+ post.status = True
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ PostBoolean.objects.all().delete()
+
+
+ def test_models_with_methods(self):
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+ now = datetime.datetime.now()
+
+ post = PostWithMethod()
+ post.title = 'test title 3'
+ post.slug = 'test-title-3'
+ post.body = 'the body of my test'
+ post.status = True
+ post.tag_data = 'one two three'
+ post.publish = now
+ post.save()
+
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ proxy = Proxy.objects.get(title='--test title 3--')
+ self.assertEqual(proxy.title, '--test title 3--')
+ self.assertEqual(proxy.description, '--the body of my test--')
+ self.assertEqual(proxy.tags, '--one two three--')
+
+ post.status = False
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 0)
+
+ post.status = True
+ post.save()
+ proxy_count = Proxy.objects.count()
+ self.assertEqual(proxy_count, 1)
+
+ PostWithMethod.objects.all().delete()
View
6 tests/test_settings.py
@@ -0,0 +1,6 @@
+DATABASE_ENGINE = 'sqlite3'
+INSTALLED_APPS = [
+ 'django.contrib.contenttypes',
+ 'django_proxy',
+ 'tests',
+]

0 comments on commit bbecfe4

Please sign in to comment.
Something went wrong with that request. Please try again.