From 9ffb68d9270e6681156b4350d89542d2567bc846 Mon Sep 17 00:00:00 2001 From: steve Date: Sat, 16 Jan 2016 17:39:29 -0500 Subject: [PATCH] Misc small fixes * Run coverage across entire build matrix * Use the root url for 'get_csv' in the demo app, because it's the only view. * Add flake8 linting to travis jobs * Make a few small syntax changes to make it easier to support python3 in the future * Fix lint * Simplify development dependencies http://python-packaging-user-guide.readthedocs.org/en/latest/requirements/ it makes sense to specify a loose requirement for django in `setup.py` and a duplicate it in `dev_requirements.txt` --- .travis.yml | 17 +++++++++-------- dev_requirements.txt | 2 ++ djqscsv/__init__.py | 4 ++-- djqscsv/_csql.py | 5 ++++- test_app/djqscsv_tests/context.py | 2 +- test_app/djqscsv_tests/models.py | 7 ++++--- test_app/djqscsv_tests/tests/__init__.py | 4 ++-- .../djqscsv_tests/tests/test_csv_creation.py | 3 +-- test_app/djqscsv_tests/tests/test_utilities.py | 16 +++++++++------- test_app/djqscsv_tests/urls.py | 9 ++++----- test_app/djqscsv_tests/util.py | 1 + test_app/djqscsv_tests/views.py | 4 +--- 12 files changed, 40 insertions(+), 34 deletions(-) diff --git a/.travis.yml b/.travis.yml index 02a4d5b..828a1ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,21 +3,22 @@ python: - "2.6" - "2.7" env: - - DJANGO=1.5 RUNNER="coverage run --source=djqscsv" SUCCESS="coveralls" - - DJANGO=1.6 RUNNER="python" SUCCESS="echo DONE" - - DJANGO=1.7 RUNNER="python" SUCCESS="echo DONE" - - DJANGO=1.8 RUNNER="python" SUCCESS="echo DONE" + - DJANGO=1.5 + - DJANGO=1.6 + - DJANGO=1.7 + - DJANGO=1.8 matrix: exclude: - python: "2.6" - env: DJANGO=1.7 RUNNER="python" SUCCESS="echo DONE" + env: DJANGO=1.7 - python: "2.6" - env: DJANGO=1.8 RUNNER="python" SUCCESS="echo DONE" + env: DJANGO=1.8 install: - pip install -q Django==$DJANGO - pip install -r dev_requirements.txt - python setup.py install script: - - $RUNNER test_app/manage.py test djqscsv_tests + - coverage run --source=djqscsv test_app/manage.py test djqscsv_tests + - flake8 --exclude=migrations djqscsv/ test_app/ after_success: - - $SUCCESS + - coveralls diff --git a/dev_requirements.txt b/dev_requirements.txt index 036d9c3..5ba9722 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,2 +1,4 @@ coveralls==0.3 coverage==3.6 +flake8==2.5.1 +django>=1.5 diff --git a/djqscsv/__init__.py b/djqscsv/__init__.py index 63f0ff0..f0b591c 100644 --- a/djqscsv/__init__.py +++ b/djqscsv/__init__.py @@ -1,2 +1,2 @@ -from djqscsv import (render_to_csv_response, write_csv, # NOQA - generate_filename, CSVException) # NOQA +from .djqscsv import (render_to_csv_response, write_csv, # NOQA + generate_filename, CSVException) # NOQA diff --git a/djqscsv/_csql.py b/djqscsv/_csql.py index c9fbe5b..cb0ce73 100644 --- a/djqscsv/_csql.py +++ b/djqscsv/_csql.py @@ -6,9 +6,11 @@ This module may later be officially supported. """ + def _identity(x): return x + def _transform(dataset, arg): if isinstance(arg, str): field = arg @@ -43,6 +45,7 @@ def EXCLUDE(dataset, *args): def CONSTANT(value, display_name): return (None, display_name, lambda x: value) - + + def AS(field, display_name): return (field, display_name, _identity) diff --git a/test_app/djqscsv_tests/context.py b/test_app/djqscsv_tests/context.py index ba5f2ae..31dc179 100644 --- a/test_app/djqscsv_tests/context.py +++ b/test_app/djqscsv_tests/context.py @@ -6,4 +6,4 @@ import djqscsv.djqscsv as djqscsv # NOQA -from djqscsv._csql import SELECT, EXCLUDE, AS, CONSTANT +from djqscsv._csql import SELECT, EXCLUDE, AS, CONSTANT # NOQA diff --git a/test_app/djqscsv_tests/models.py b/test_app/djqscsv_tests/models.py index 4651de2..af9c9e9 100644 --- a/test_app/djqscsv_tests/models.py +++ b/test_app/djqscsv_tests/models.py @@ -1,16 +1,17 @@ from django.db import models from django.utils.translation import ugettext as _ - +from django.utils.encoding import python_2_unicode_compatible from datetime import datetime -SOME_TIME = datetime(2001, 01, 01, 01, 01) +SOME_TIME = datetime(2001, 1, 1, 1, 1) class Activity(models.Model): name = models.CharField(max_length=50, verbose_name="Name of Activity") +@python_2_unicode_compatible class Person(models.Model): name = models.CharField(max_length=50, verbose_name=_("Person's name")) address = models.CharField(max_length=255) @@ -18,5 +19,5 @@ class Person(models.Model): hobby = models.ForeignKey(Activity) born = models.DateTimeField(default=SOME_TIME) - def __unicode__(self): + def __str__(self): return self.name diff --git a/test_app/djqscsv_tests/tests/__init__.py b/test_app/djqscsv_tests/tests/__init__.py index 7b9f4d7..68981de 100644 --- a/test_app/djqscsv_tests/tests/__init__.py +++ b/test_app/djqscsv_tests/tests/__init__.py @@ -1,2 +1,2 @@ -from test_csv_creation import * -from test_utilities import * +from djqscsv_tests.tests.test_csv_creation import * # NOQA +from djqscsv_tests.tests.test_utilities import * # NOQA diff --git a/test_app/djqscsv_tests/tests/test_csv_creation.py b/test_app/djqscsv_tests/tests/test_csv_creation.py index 29b9f49..843c98c 100644 --- a/test_app/djqscsv_tests/tests/test_csv_creation.py +++ b/test_app/djqscsv_tests/tests/test_csv_creation.py @@ -1,6 +1,5 @@ from django.db.models import Count from django.test import TestCase -from django.core.exceptions import ValidationError from django import VERSION as DJANGO_VERSION @@ -16,7 +15,6 @@ from django.utils import six if six.PY3: - from functools import filter from io import StringIO else: from StringIO import StringIO @@ -285,6 +283,7 @@ def test_extra_select_header_map(self): self.qs, csv_with_extra, field_header_map={'Most Powerful': 'Sturdiest'}) + class RenderToCSVResponseTests(CSVTestCase): def test_render_to_csv_response_with_filename_and_datestamp(self): diff --git a/test_app/djqscsv_tests/tests/test_utilities.py b/test_app/djqscsv_tests/tests/test_utilities.py index 96123e0..cebd8a2 100644 --- a/test_app/djqscsv_tests/tests/test_utilities.py +++ b/test_app/djqscsv_tests/tests/test_utilities.py @@ -1,9 +1,13 @@ # -*- coding: utf-8 -*- import datetime +from operator import attrgetter + from django.test import TestCase from django.core.exceptions import ValidationError +from django.utils.encoding import python_2_unicode_compatible + from djqscsv_tests.context import djqscsv from djqscsv_tests.util import create_people_and_get_queryset @@ -81,9 +85,8 @@ def test_sanitize_date_with_formatter(self): def test_sanitize_date_with_bad_formatter(self): record = {'name': 'Tenar', 'created': datetime.datetime(1973, 5, 13)} - formatter = lambda d: d.day with self.assertRaises(AttributeError): - djqscsv._sanitize_unicode_record(formatter, record) + djqscsv._sanitize_unicode_record(attrgetter('day'), record) class AppendDatestampTests(TestCase): @@ -120,15 +123,14 @@ def test_generate_filename(self): class SafeUtf8EncodeTest(TestCase): def test_safe_utf8_encode(self): + @python_2_unicode_compatible class Foo(object): - def __unicode__(self): + def __str__(self): return u'¯\_(ツ)_/¯' - def __str_(self): - return self.__unicode__().encode('utf-8') for val in (u'¯\_(ツ)_/¯', 'plain', r'raw', - b'123', 11312312312313L, False, - datetime.datetime(2001, 01, 01), + b'123', 11312312312313, False, + datetime.datetime(2001, 1, 1), 4, None, [], set(), Foo): first_pass = djqscsv._safe_utf8_stringify(val) diff --git a/test_app/djqscsv_tests/urls.py b/test_app/djqscsv_tests/urls.py index 84d3193..09d5f9a 100644 --- a/test_app/djqscsv_tests/urls.py +++ b/test_app/djqscsv_tests/urls.py @@ -1,7 +1,6 @@ -from django.conf.urls import patterns, include, url -import views +from django.conf.urls import url +from djqscsv_tests import views -urlpatterns = patterns( - '', - url(r'^get_csv/', views.get_csv, name='get_csv'), +urlpatterns = ( + url(r'^$', views.get_csv, name='get_csv'), ) diff --git a/test_app/djqscsv_tests/util.py b/test_app/djqscsv_tests/util.py index dd4dd92..0b0a419 100644 --- a/test_app/djqscsv_tests/util.py +++ b/test_app/djqscsv_tests/util.py @@ -1,5 +1,6 @@ from .models import Person, Activity + def create_people_and_get_queryset(): doing_magic, _ = Activity.objects.get_or_create(name="Doing Magic") resting, _ = Activity.objects.get_or_create(name="Resting") diff --git a/test_app/djqscsv_tests/views.py b/test_app/djqscsv_tests/views.py index 16d2be1..38dd904 100644 --- a/test_app/djqscsv_tests/views.py +++ b/test_app/djqscsv_tests/views.py @@ -1,6 +1,4 @@ - -import djqscsv -from models import Person +from djqscsv_tests.context import djqscsv from .util import create_people_and_get_queryset