Skip to content

Commit 05d6542

Browse files
committed
Add tests, including test Django project
1 parent 59b7912 commit 05d6542

17 files changed

+345
-20
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.pyc
22
.DS_Store
3+
/.tox
4+
/src/django_sliver.egg-info/

Diff for: setup.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from setuptools import setup
1+
from setuptools import setup, find_packages
22

33
setup(
44
name="django-sliver",
@@ -10,14 +10,15 @@
1010
author_email="[email protected]",
1111
url="https://github.com/jarcoal/django-sliver",
1212
license="BSD",
13-
packages=["sliver"],
13+
package_dir={'': 'src'},
14+
packages=find_packages('src'),
1415
zip_safe=False,
15-
install_requires=['django >= 1.3'],
16+
install_requires=['django >= 1.11', 'six'],
1617
include_package_data=True,
1718
classifiers=[
1819
"Programming Language :: Python",
1920
"Topic :: Software Development :: Libraries :: Python Modules",
2021
"Framework :: Django",
2122
"Environment :: Web Environment",
2223
],
23-
)
24+
)

Diff for: sliver/tests.py

-16
This file was deleted.

Diff for: src/_slivertestproject/__init__.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright (C) 2012-2013 by Łukasz Langa
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
24+
from __future__ import absolute_import
25+
from __future__ import division
26+
from __future__ import print_function
27+
from __future__ import unicode_literals
28+
29+
from unittest.loader import defaultTestLoader
30+
31+
from django.conf import settings
32+
from django.test import TestCase
33+
34+
try:
35+
# Added in Django 1.6
36+
from django.test.runner import DiscoverRunner
37+
class DiscoveryDjangoTestSuiteRunner(DiscoverRunner):
38+
"""Unfortunately the new DiscoverRunner doesn't handle custom top level
39+
correctly."""
40+
def __init__(
41+
self, pattern=None, top_level=None, verbosity=1, interactive=True,
42+
failfast=False, **kwargs
43+
):
44+
self.test_label_default = settings.TEST_DISCOVERY_ROOT
45+
if not top_level:
46+
top_level = self.test_label_default
47+
super(DiscoveryDjangoTestSuiteRunner, self).__init__(
48+
pattern=pattern, top_level=top_level, verbosity=verbosity,
49+
interactive=interactive, failfast=failfast, **kwargs
50+
)
51+
52+
def build_suite(self, test_labels=None, extra_tests=None, **kwargs):
53+
if not test_labels:
54+
test_labels = [self.test_label_default]
55+
return super(DiscoveryDjangoTestSuiteRunner, self).build_suite(
56+
test_labels=test_labels, extra_tests=extra_tests, **kwargs
57+
)
58+
59+
except ImportError:
60+
# Django < 1.6 compatibility
61+
from django.utils.importlib import import_module
62+
from django.test.simple import DjangoTestSuiteRunner
63+
class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
64+
"""A test suite runner that uses unittest2 test discovery.
65+
Courtesy of @carljm."""
66+
67+
def build_suite(self, test_labels, extra_tests=None, **kwargs):
68+
suite = None
69+
discovery_root = settings.TEST_DISCOVERY_ROOT
70+
71+
if test_labels:
72+
suite = defaultTestLoader.loadTestsFromNames(test_labels)
73+
# if single named module has no tests, do discovery within it
74+
if not suite.countTestCases() and len(test_labels) == 1:
75+
suite = None
76+
discovery_root = import_module(test_labels[0]).__path__[0]
77+
78+
if suite is None:
79+
suite = defaultTestLoader.discover(
80+
discovery_root,
81+
top_level_dir=settings.BASE_PATH,
82+
)
83+
84+
if extra_tests:
85+
for test in extra_tests:
86+
suite.addTest(test)
87+
88+
from django.test.simple import reorder_suite # Django 1.5 and lower
89+
return reorder_suite(suite, (TestCase,))
File renamed without changes.

Diff for: src/_slivertestproject/app/models.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import
5+
from __future__ import division
6+
from __future__ import print_function
7+
from __future__ import unicode_literals
8+
9+
from django.db import models
10+
11+
12+
class Person(models.Model):
13+
name = models.CharField(max_length=256)

Diff for: src/_slivertestproject/app/views.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from sliver.views import ModelResource, CollectionResource
2+
from sliver.mixins import JSONMixin
3+
4+
from .models import Person
5+
6+
7+
class PersonCollectionResource(JSONMixin, CollectionResource):
8+
model = Person
9+
10+
class PersonResource(JSONMixin, ModelResource):
11+
model = Person

Diff for: src/_slivertestproject/manage.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "_slivertestproject.settings")
7+
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)

Diff for: src/_slivertestproject/settings.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
DEBUG = True
2+
TEMPLATE_DEBUG = DEBUG
3+
4+
ADMINS = ()
5+
6+
MANAGERS = ADMINS
7+
8+
DATABASES = {
9+
'default': {
10+
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
11+
'NAME': ':memory:', # Or path to database file if using sqlite3.
12+
'USER': '', # Not used with sqlite3.
13+
'PASSWORD': '', # Not used with sqlite3.
14+
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
15+
'PORT': '', # Set to empty string for default. Not used with sqlite3.
16+
}
17+
}
18+
19+
# Local time zone for this installation. Choices can be found here:
20+
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
21+
# although not all choices may be available on all operating systems.
22+
# On Unix systems, a value of None will cause Django to use the same
23+
# timezone as the operating system.
24+
# If running in a Windows environment this must be set to the same as your
25+
# system time zone.
26+
TIME_ZONE = 'America/Chicago'
27+
28+
# Language code for this installation. All choices can be found here:
29+
# http://www.i18nguy.com/unicode/language-identifiers.html
30+
LANGUAGE_CODE = 'en-us'
31+
32+
SITE_ID = 1
33+
34+
# If you set this to False, Django will make some optimizations so as not
35+
# to load the internationalization machinery.
36+
USE_I18N = False
37+
38+
# If you set this to False, Django will not format dates, numbers and
39+
# calendars according to the current locale
40+
USE_L10N = True
41+
42+
# Absolute filesystem path to the directory that will hold user-uploaded files.
43+
# Example: "/home/media/media.lawrence.com/media/"
44+
MEDIA_ROOT = ''
45+
46+
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
47+
# trailing slash.
48+
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
49+
MEDIA_URL = ''
50+
51+
# Absolute path to the directory static files should be collected to.
52+
# Don't put anything in this directory yourself; store your static files
53+
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
54+
# Example: "/home/media/media.lawrence.com/static/"
55+
STATIC_ROOT = ''
56+
57+
# URL prefix for static files.
58+
# Example: "http://media.lawrence.com/static/"
59+
STATIC_URL = '/static/'
60+
61+
# Additional locations of static files
62+
STATICFILES_DIRS = (
63+
# Put strings here, like "/home/html/static" or "C:/www/django/static".
64+
# Always use forward slashes, even on Windows.
65+
# Don't forget to use absolute paths, not relative paths.
66+
)
67+
68+
# List of finder classes that know how to find static files in
69+
# various locations.
70+
STATICFILES_FINDERS = (
71+
'django.contrib.staticfiles.finders.FileSystemFinder',
72+
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
73+
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
74+
)
75+
76+
# Make this unique, and don't share it with anybody.
77+
SECRET_KEY = 'dqro09o2k7eha5px#h_t75ji)qmz3va)tdk!91tn7-%f99=qj&'
78+
79+
# List of callables that know how to import templates from various sources.
80+
TEMPLATE_LOADERS = (
81+
'django.template.loaders.filesystem.Loader',
82+
'django.template.loaders.app_directories.Loader',
83+
# 'django.template.loaders.eggs.Loader',
84+
)
85+
86+
MIDDLEWARE_CLASSES = (
87+
'django.middleware.common.CommonMiddleware',
88+
'django.contrib.sessions.middleware.SessionMiddleware',
89+
'django.middleware.csrf.CsrfViewMiddleware',
90+
'django.contrib.auth.middleware.AuthenticationMiddleware',
91+
'django.contrib.messages.middleware.MessageMiddleware',
92+
)
93+
94+
ROOT_URLCONF = '_slivertestproject.urls'
95+
96+
TEMPLATE_DIRS = (
97+
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
98+
# Always use forward slashes, even on Windows.
99+
# Don't forget to use absolute paths, not relative paths.
100+
)
101+
102+
INSTALLED_APPS = (
103+
'django.contrib.auth',
104+
'django.contrib.contenttypes',
105+
'django.contrib.sessions',
106+
'django.contrib.sites',
107+
'django.contrib.messages',
108+
'django.contrib.staticfiles',
109+
'sliver',
110+
'_slivertestproject.app',
111+
)
112+
113+
# A sample logging configuration. The only tangible logging
114+
# performed by this configuration is to send an email to
115+
# the site admins on every HTTP 500 error.
116+
# See http://docs.djangoproject.com/en/dev/topics/logging for
117+
# more details on how to customize your logging configuration.
118+
LOGGING = {
119+
'version': 1,
120+
'disable_existing_loggers': False,
121+
'filters': {
122+
'require_debug_false': {
123+
'()': 'django.utils.log.RequireDebugFalse'
124+
}
125+
},
126+
'handlers': {
127+
'mail_admins': {
128+
'level': 'ERROR',
129+
'filters': ['require_debug_false'],
130+
'class': 'django.utils.log.AdminEmailHandler'
131+
}
132+
},
133+
'loggers': {
134+
'django.request': {
135+
'handlers': ['mail_admins'],
136+
'level': 'ERROR',
137+
'propagate': True,
138+
},
139+
}
140+
}
141+
142+
import os
143+
import sliver
144+
145+
BASE_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
146+
TEST_DISCOVERY_ROOT = os.path.realpath(os.path.dirname(sliver.__file__))
147+
TEST_RUNNER = "_slivertestproject.DiscoveryDjangoTestSuiteRunner"

Diff for: src/_slivertestproject/urls.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.conf.urls import url
2+
3+
from app.views import PersonCollectionResource, PersonResource
4+
5+
urlpatterns = [
6+
url(r'^people/$', PersonCollectionResource.as_view(), name='api-people-collection'),
7+
url(r'^people/(?P<pk>\d+)/$', PersonResource.as_view(), name='api-people-model'),
8+
]

Diff for: src/sliver/__init__.py

Whitespace-only changes.

Diff for: sliver/mixins.py renamed to src/sliver/mixins.py

File renamed without changes.

Diff for: sliver/models.py renamed to src/sliver/models.py

File renamed without changes.
File renamed without changes.

Diff for: src/sliver/tests.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Tests for sliver."""
5+
6+
from __future__ import absolute_import
7+
from __future__ import division
8+
from __future__ import print_function
9+
from __future__ import unicode_literals
10+
11+
import json
12+
import six
13+
14+
from django.core.urlresolvers import reverse
15+
from django.test import TestCase
16+
17+
18+
class SimpleTest(TestCase):
19+
def test_dummy(self):
20+
"""Just see if the import works as expected."""
21+
import sliver
22+
23+
def test_collection(self):
24+
from _slivertestproject.app.models import Person
25+
person = Person.objects.create(name='Juan')
26+
r = self.client.get(
27+
reverse('api-people-collection')
28+
)
29+
30+
self.assertEqual(
31+
json.loads(r.content),
32+
[{'name': 'Juan', 'id': 1}]
33+
)
34+
35+
def test_model(self):
36+
from _slivertestproject.app.models import Person
37+
person = Person.objects.create(name='Juan')
38+
r = self.client.get(
39+
reverse('api-people-model', kwargs={'pk': person.pk})
40+
)
41+
42+
self.assertEqual(
43+
json.loads(r.content),
44+
{'name': 'Juan', 'id': 1}
45+
)

Diff for: sliver/views.py renamed to src/sliver/views.py

File renamed without changes.

Diff for: tox.ini

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tox]
2+
envlist =
3+
py37-django{200,202},
4+
{py27,py37}-django{110,111},
5+
6+
[testenv]
7+
commands =
8+
{envbindir}/django-admin.py test --pythonpath=src/
9+
setenv =
10+
DJANGO_SETTINGS_MODULE=_slivertestproject.settings
11+
deps =
12+
django110: Django==1.10.8
13+
django111: Django==1.11.26
14+
django200: Django==2.0.13
15+
django202: Django==2.2.7

0 commit comments

Comments
 (0)