From 16c66d6cabfa322aed6b26e027302d40e81a9de5 Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" Date: Fri, 8 Aug 2014 14:07:22 -0500 Subject: [PATCH] python3 support (fixes #3) - simplify test structure - update travis test matrix --- .travis.yml | 3 ++- rest_pandas/renderers.py | 9 +++++++- setup.py | 7 ++++-- tests/__init__.py | 8 +++++++ tests/runtests.py | 28 ----------------------- tests/settings.py | 11 +++++++++ tests/{testapp/tests.py => test_views.py} | 14 +++++++----- 7 files changed, 42 insertions(+), 38 deletions(-) delete mode 100644 tests/runtests.py create mode 100644 tests/settings.py rename tests/{testapp/tests.py => test_views.py} (78%) diff --git a/.travis.yml b/.travis.yml index 9e7c351..46d5b59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ language: python python: - "2.7" + - "3.4" env: - - DJANGO="django==1.6" REST="djangorestframework==2.3.12" PANDAS="pandas==0.13.0" + - DJANGO="django==1.6" REST="djangorestframework==2.3.14" PANDAS="pandas==0.14.1" install: - pip install $DJANGO - pip install $REST diff --git a/rest_pandas/renderers.py b/rest_pandas/renderers.py index b6199b5..03d068c 100644 --- a/rest_pandas/renderers.py +++ b/rest_pandas/renderers.py @@ -1,7 +1,14 @@ from rest_framework.renderers import BaseRenderer from rest_framework import status from tempfile import mkstemp -from StringIO import StringIO + +try: + # Python 2 (uses str) + from StringIO import StringIO +except ImportError: + # Python 3 (Python 2 equivalent uses unicode) + from io import StringIO + import os diff --git a/setup.py b/setup.py index ca60ca6..c51c125 100644 --- a/setup.py +++ b/setup.py @@ -51,10 +51,13 @@ def parse_markdown_readme(): 'Environment :: Web Environment', 'License :: OSI Approved :: MIT License', 'Natural Language :: English', + 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.4', 'Topic :: Scientific/Engineering :: Information Analysis', 'Topic :: Scientific/Engineering :: Visualization', ], - test_suite='tests.runtests.main', - tests_require=['wq.io'], + test_suite='tests', + tests_require=['wq.io>=0.6.0'], ) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..a016022 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,8 @@ +import os +os.environ['DJANGO_SETTINGS_MODULE'] = "tests.settings" + +from django.test.utils import setup_test_environment +setup_test_environment() + +from django.core.management import call_command +call_command('syncdb') diff --git a/tests/runtests.py b/tests/runtests.py deleted file mode 100644 index f8af0b4..0000000 --- a/tests/runtests.py +++ /dev/null @@ -1,28 +0,0 @@ -import sys -from django.conf import settings - - -def main(): - settings.configure( - DATABASES={ - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': 'pandas_test.db', - } - }, - ROOT_URLCONF="tests.urls", - INSTALLED_APPS=( - 'tests.testapp', - ), - ) - - from django.test.utils import get_runner - TestRunner = get_runner(settings) - test_runner = TestRunner() - result = test_runner.run_tests(( - 'tests.testapp', - )) - sys.exit(result) - -if __name__ == "__main__": - main() diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..589f371 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,11 @@ +SECRET_KEY = '1234' +INSTALLED_APPS = ( + 'tests.testapp', +) +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } +} +ROOT_URLCONF = "tests.urls" diff --git a/tests/testapp/tests.py b/tests/test_views.py similarity index 78% rename from tests/testapp/tests.py rename to tests/test_views.py index 2a04970..8e6c244 100644 --- a/tests/testapp/tests.py +++ b/tests/test_views.py @@ -1,5 +1,5 @@ from rest_framework.test import APITestCase -from .models import TimeSeries +from tests.testapp.models import TimeSeries from wq.io import load_string import json @@ -18,26 +18,28 @@ def setUp(self): def test_view(self): response = self.client.get("/timeseries.csv") - data = load_string(unicode(response.content)) + data = self.load_string(response) self.assertEqual(len(data), 5) self.assertEqual(data[0].value, '0.5') def test_view_json(self): response = self.client.get("/timeseries.json") self.assertEqual(response.accepted_media_type, "application/json") - data = load_string(unicode(response.content)) - data = json.loads(response.content) + data = json.loads(response.content.decode('utf-8')) self.assertEqual(len(data.keys()), 5) self.assertEqual(data["1"]["value"], 0.5) def test_viewset(self): response = self.client.get("/router/timeseries/.csv") - data = load_string(unicode(response.content)) + data = self.load_string(response) self.assertEqual(len(data), 5) self.assertEqual(data[0].value, '0.5') def test_no_model(self): response = self.client.get("/nomodel.csv") - data = load_string(unicode(response.content)) + data = self.load_string(response) self.assertEqual(len(data), 4) self.assertEqual(data[0].x, '5') + + def load_string(self, response): + return load_string(response.content.decode('utf-8'))