Skip to content

Commit

Permalink
Merge pull request #1 from grahamu/refactored
Browse files Browse the repository at this point in the history
Refactored
  • Loading branch information
brosner committed Mar 17, 2016
2 parents a144be7 + a1daae4 commit 7c77bf6
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ MANIFEST
*.egg
docs/_build/
htmlcov/
.eggs/
.python-version
9 changes: 0 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ sudo: false
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
env:
- DJANGO=1.7
- DJANGO=1.8
- DJANGO=1.9
- DJANGO=master
matrix:
exclude:
- python: "3.3"
env: DJANGO=1.9
- python: "3.3"
env: DJANGO=master
- python: "3.5"
env: DJANGO=1.7
install:
- pip install tox coveralls
script:
Expand Down
4 changes: 1 addition & 3 deletions pinax/api/authentication.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from django.contrib.auth.models import AnonymousUser


def add(backends):
def decorator(func):
func.authentication = backends
Expand All @@ -11,4 +8,5 @@ def decorator(func):
class Anonymous(object):

def authenticate(self, request):
from django.contrib.auth.models import AnonymousUser
return AnonymousUser()
5 changes: 4 additions & 1 deletion pinax/api/jsonapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import unicode_literals

from collections import abc
try:
from collections import abc
except ImportError:
import collections as abc

from .resource import Resource

Expand Down
9 changes: 6 additions & 3 deletions pinax/api/rfc3339.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import re


_datetime_re = re.compile(r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T"
r"(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{2})(\.(?P<fraction>\d+))?)"
r"((?P<tzzulu>Z)|((?P<tzoffset>[\-+])(?P<tzhour>\d{2}):(?P<tzminute>\d{2})))$")
_datetime_re = re.compile(
r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T"
r"(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{2})(\.(?P<fraction>\d+))?)"
r"((?P<tzzulu>Z)|((?P<tzoffset>[\-+])(?P<tzhour>\d{2}):(?P<tzminute>\d{2})))$"
)


def parse(text):
Expand All @@ -26,6 +28,7 @@ def __init__(self, tzoffset, tzhour, tzminute):
self.minutes = +minutes
else:
self.minutes = -minutes

def utcoffset(self, dt):
return datetime.timedelta(minutes=self.minutes)

Expand Down
15 changes: 10 additions & 5 deletions pinax/api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

class TestCase(BaseTestCase):

def assertGraphEqual(self, a, b):
def assertResourceGraphEqual(self, a, b):
"""
Compare two lists of JSON:API resources.
"""
self.assertTrue(len(a) == len(b), "Mismatched list lengths")

a_graph, b_graph = {}, {}
for i in a:
a_graph[(i["type"], i["id"])] = i
for i in b:
b_graph[(i["type"], i["id"])] = i
self.assertTrue(len(a) == len(b), "Mismatched included lengths")
for rid in a_graph:
self.assertIn(rid, b_graph, "{} is missing from other included".format(rid))
self.assertEqual(a_graph[rid], b_graph[rid])
for resource_id in a_graph:
self.assertIn(
resource_id,
b_graph,
"Resource {} is missing from second list".format(resource_id)
)
self.assertEqual(a_graph[resource_id], b_graph[resource_id])
130 changes: 110 additions & 20 deletions pinax/api/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,117 @@
from __future__ import unicode_literals

import json
from ..test import TestCase

from django.core.urlresolvers import reverse
from django.test import TestCase


class Tests(TestCase):
class TestAssertResourceGraph(TestCase):

def setUp(self):
pass

def test_something(self):
r = self.client.get(reverse("user-list"))
self.assertEqual(r.status_code, 200)
self.assertEqual(r["Content-Type"], "application/vnd.api+json")
self.assertDictEqual(
{
"jsonapi": {"version": "1.0"},
"links": {
"self": "http://testserver/user/",
},
"data": [],
self.first_item = {
"type": "lorem",
"id": "33",
"attributes": {
"quantity": 1
},
"links": {
"self": "http://testserver/lorem/33"
},
"relationships": {
"product": {
"links": {
"self": "http://testserver/relationships/product"
},
"data": {
"id": "44",
"type": "product"
}
}
}
}

self.second_item = {
"type": "product",
"id": "44",
"attributes": {
"name": "Excelsior",
"slug": "excelsior",
},
json.loads(r.content.decode("utf-8")),
)
"links": {"self": "http://testserver/products/44"},
"relationships": {
"category": {
"data": {
"type": "category",
"id": "12",
},
"links": {
"self": "http://testserver/products/44/relationships/category",
}
}
}
}

self.third_item = {
"type": "ipsum",
"id": "55",
"attributes": {
"words": 42
},
"links": {
"self": "http://testserver/ipsum/55"
}
}

self.fourth_item = {
"type": "product",
"id": "44",
"attributes": {
"name": "Excelsiore", # slightly different name than in self.second_item
"slug": "excelsiore",
},
"links": {"self": "http://testserver/products/44"},
"relationships": {
"category": {
"data": {
"type": "category",
"id": "12",
},
"links": {
"self": "http://testserver/products/44/relationships/category",
}
}
}
}

def test_unordered_identical(self):

a = [self.first_item, self.second_item]
b = [self.second_item, self.first_item]
self.assertResourceGraphEqual(a, b)

def test_unequal_count(self):
"""
Ensure we catch when lists have different amount of resources.
"""
a = [self.first_item, self.second_item]
b = [self.first_item]
with self.assertRaises(AssertionError):
self.assertResourceGraphEqual(a, b)

def test_different_resources(self):
"""
Ensure we catch when lists contain different resources.
"""
a = [self.first_item, self.second_item]
b = [self.first_item, self.third_item]
with self.assertRaises(AssertionError):
self.assertResourceGraphEqual(a, b)

def test_different_items(self):
"""
Ensure we catch when item count is identical
and item resource ID is identical but
items themselves are different.
"""
a = [self.first_item, self.second_item]
b = [self.first_item, self.fourth_item]
with self.assertRaises(AssertionError):
self.assertResourceGraphEqual(a, b)
19 changes: 0 additions & 19 deletions pinax/api/tests/viewsets.py

This file was deleted.

3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def read(*parts):
},
test_suite="runtests.runtests",
tests_require=[
"django-appconf>=1.0.1",
],
install_requires=[
],
classifiers=[
"Development Status :: 4 - Beta",
Expand Down
9 changes: 3 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ exclude = migrations/*,docs/*

[tox]
envlist =
py27-{1.7,1.8,1.9,master},
py33-{1.7,1.8},
py34-{1.7,1.8,1.9,master},
py35-{1.8,1.9,master}
py27-{1.9,master},
py34-{1.9,master},
py35-{1.9,master}

[testenv]
deps =
coverage == 4.0.2
flake8 == 2.5.0
1.7: Django>=1.7,<1.8
1.8: Django>=1.8,<1.9
1.9: Django>=1.9,<1.10
master: https://github.com/django/django/tarball/master
usedevelop = True
Expand Down

0 comments on commit 7c77bf6

Please sign in to comment.