Browse files

Tests for workers

  • Loading branch information...
1 parent 0c357ac commit 88db72b4baf66f3c799ff3ea962938fa91ae790c @kmike committed May 2, 2011
View
1 .hgignore
@@ -29,3 +29,4 @@ tornado_slacker.egg-info
#my files
db.sqlite
+.coverage
View
1 MANIFEST.in
@@ -1,2 +1,3 @@
include *.txt
include *.rst
+recursive-include test_project *.html *.py
View
2 README.rst
@@ -46,7 +46,7 @@ Slackers and workers
In order to execute some code in non-blocking manner:
-1. Create a Slacker (configured with desired worker) for some python object::
+1. Create a Slacker (configured with the desired worker) for some python object::
from slacker import Slacker
from slacker.workers import ThreadWorker
View
25 runtests.py
@@ -2,26 +2,17 @@
# -*- coding: utf-8 -*-
import os
import sys
-from django.conf import settings
-from django.core.management import call_command
+from django.core.management import execute_manager
# always use slacker from the checkout, not the installed version
sys.path.insert(0, os.path.dirname(__file__))
-settings.configure(
- INSTALLED_APPS=(
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
+# add project to pythonpath
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'test_project'))
- 'slacker',
- 'slacker.django_backend',
- ),
- DATABASES = {
- 'default': dict(
- ENGINE = 'django.db.backends.sqlite3',
- )
- }
-)
+sys.argv.insert(1, 'test')
+if len(sys.argv) == 2:
+ sys.argv.extend(['slacker', 'django_backend'])
-if __name__ == "__main__":
- call_command('test', 'slacker', 'django_backend')
+from test_project import settings
+execute_manager(settings)
View
46 slacker/django_backend/tests.py
@@ -1,9 +1,16 @@
-import pickle
+from __future__ import absolute_import
-from django.test import TestCase as DjangoTestCase
+import pickle
+from tornado.testing import AsyncHTTPTestCase
+from tornado.wsgi import WSGIContainer
+from tornado.ioloop import IOLoop
+from django.core.handlers.wsgi import WSGIHandler
+from django.test import TransactionTestCase as DjangoTestCase
from django.contrib.auth.models import User
+from slacker import Slacker, adisp
from slacker.postpone import Postponed
+from slacker.workers import ThreadWorker, DjangoWorker
class DjangoQueryPostponeTest(DjangoTestCase):
@@ -22,3 +29,38 @@ def test_pickling_unpickling(self):
user_query = self.AsyncUser.objects.get(username='example')
self.assertEqual(pickle.loads(user_query._pickled)._proceed(), self.user)
+class BaseWorkerTest(AsyncHTTPTestCase, DjangoTestCase):
+
+ SlackerClass = Slacker(User)
+
+ def setUp(self):
+ self.user = User.objects.create_user('example', 'example@example.com')
+ self.res = None
+ super(BaseWorkerTest, self).setUp()
+
+ def get_new_ioloop(self):
+ return IOLoop.instance()
+
+ def get_app(self):
+ return WSGIContainer(WSGIHandler())
+
+ @adisp.process
+ def get_user(self):
+ self.res = yield self.SlackerClass.objects.get(username=self.user.username)
+ self.stop()
+
+ def test_get_user(self):
+ self.get_user()
+ self.wait()
+ self.assertEqual(self.res, self.user)
+
+
+class DjangoWorkerTest(BaseWorkerTest):
+ def setUp(self):
+ super(DjangoWorkerTest, self).setUp()
+ self.SlackerClass = Slacker(User, DjangoWorker(self.get_url('')))
+
+
+class ThreadedWorkerTest(BaseWorkerTest):
+ SlackerClass = Slacker(User, ThreadWorker())
+
View
1 slacker/models.py
@@ -0,0 +1 @@
+#hello!
View
4 slacker/postpone.py
@@ -1,13 +1,13 @@
+from __future__ import absolute_import
import pprint
import sys
import types
-
try:
import cPickle as pickle
except ImportError:
import pickle
-from slacker.workers.local import DummyWorker
+from .workers.local import DummyWorker
class SlackerException(Exception):
pass
View
12 slacker/workers/__init__.py
@@ -1,7 +1,9 @@
-from slacker.workers.local import DummyWorker, ThreadWorker
-from slacker.workers.http import HttpWorker, CurlHttpWorker
+from __future__ import absolute_import
+from .local import DummyWorker, ThreadWorker
+from .http import HttpWorker, CurlHttpWorker
try:
- from slacker.workers.django import DjangoWorker
-except ImportError: # django is not installed
- pass
+ from .django import DjangoWorker
+except ImportError, e: # django is not installed
+ import warnings
+ warnings.warn("DjangoWorker is not available: %s" % e)
View
2 slacker/workers/django.py
@@ -2,7 +2,7 @@
from django.core.urlresolvers import reverse
from slacker.django_backend.conf import SLACKER_SERVER
-from slacker.workers.http import HttpWorker
+from .http import HttpWorker
class DjangoWorker(HttpWorker):
View
2 slacker/workers/http.py
@@ -23,7 +23,7 @@ class HttpWorker(object):
HTTPClient = AsyncHTTPClient
- def __init__(self, server='127.0.0.1:8000', path='/'):
+ def __init__(self, server='http://127.0.0.1:8000', path='/'):
self.url = server + path
def proceed(self, postponed, callback=None):
View
1 test_project/__init__.py
@@ -0,0 +1 @@
+
View
50 test_project/settings.py
@@ -0,0 +1,50 @@
+# Django settings for test project.
+import os, sys
+PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
+join = lambda p: os.path.abspath(os.path.join(PROJECT_ROOT, p))
+
+sys.path.insert(0, join('..'))
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.sqlite3',
+ 'NAME': join('db.sqlite'),
+
+ # :memory: databases cause obscure bugs in multithreaded environment
+ # and django uses :memory: as TEST_NAME by default so it is necessary
+ # to make test database real file.
+ 'TEST_NAME': join('db-test.sqlite'),
+ }
+}
+
+SECRET_KEY = '5mcs97ar-(nnxhfkx0%^+0^sr!e(ax=x$2-!8dqy25ff-l1*a='
+TEMPLATE_LOADERS = (
+ 'django.template.loaders.filesystem.Loader',
+ 'django.template.loaders.app_directories.Loader',
+# 'django.template.loaders.eggs.Loader',
+)
+
+TEMPLATE_DIRS = (
+ join('templates'),
+)
+
+MIDDLEWARE_CLASSES = (
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+)
+
+ROOT_URLCONF = 'urls'
+
+INSTALLED_APPS=(
+ 'django.contrib.auth',
+ 'django.contrib.contenttypes',
+
+ 'slacker',
+ 'slacker.django_backend',
+)
View
1 test_project/templates/404.html
@@ -0,0 +1 @@
+page not found
View
1 test_project/templates/500.html
@@ -0,0 +1 @@
+error 500
View
6 test_project/urls.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+ (r'^async/', include('slacker.django_backend.urls')),
+)

0 comments on commit 88db72b

Please sign in to comment.