Skip to content
Browse files

added tests.

fixed field filtering.
  • Loading branch information...
1 parent 000afe1 commit 69644474227350b83bc6193f604c86f0a315b1a1 @aehlke committed Mar 1, 2011
View
12 catnap/restresources.py
@@ -3,6 +3,9 @@
class RestResource(object):
def get_data(self):
+ '''
+ Adds a `url` field if this class has a `get_url` method.
+ '''
data = {}
if hasattr(self, 'get_url'):
data['url'] = self.get_url()
@@ -13,6 +16,7 @@ class RestModelResource(RestResource):
'''
Represents a single object, in a queryset.
'''
+ # If left as None, it will not filter the fields.
fields = None
def __init__(self, obj):
@@ -24,23 +28,23 @@ def _model_to_dict(self, obj):
ret = {}
# If we only have a model, we only want to encode the fields.
for f in obj._meta.fields:
- if f.attname in self.fields:
+ if not self.fields or f.attname in self.fields:
#ret[f.attname] = _any(getattr(obj, f.attname))
ret[f.attname] = getattr(obj, f.attname)
# And additionally encode arbitrary properties
# that had been added.
fields = dir(obj.__class__) + ret.keys()
add_ons = [k for k in dir(obj) if k not in fields]
for k in add_ons:
- if f.attname in self.fields:
+ if not self.fields or k in self.fields:
#ret[k] = _any(getattr(obj, k))
ret[k] = getattr(obj, k)
+ #import pdb;pdb.set_trace()
return ret
def get_data(self):
data = super(RestModelResource, self).get_data()
-
data.update(self._model_to_dict(self.obj))
-
return data
+
View
1 catnap/restviews.py
@@ -154,6 +154,7 @@ def get_context_data(self, object_list=None, **kwargs):
% self.__class__.__name__)
context_object_name = self.get_context_object_name(self.get_queryset())
+ #import pdb;pdb.set_trace()
context[context_object_name] = list(
self.resource(_).get_data()
for _ in context[context_object_name])
View
0 catnap/tests/fixtures/testdata.json → catnap/tests/api/fixtures/testdata.json
File renamed without changes.
View
3 catnap/tests/api/restresources.py
@@ -8,7 +8,7 @@ class UserResource(RestModelResource):
'is_superuser', 'is_active', 'date_joined',)
def get_data(self):
- data = super(UserResource, self).get_data()
+ return super(UserResource, self).get_data()
@@ -21,6 +21,7 @@ def get_url(self):
def get_data(self):
data = super(DeckResource, self).get_data()
data['owner'] = UserResource(self.obj.owner).get_data()
+ return data
View
33 catnap/tests/api/tests.py
@@ -7,12 +7,19 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
-
+from django.utils import simplejson as json
+from models import Deck
+from restresources import DeckResource, UserResource
+from catnap.restresources import RestModelResource
USERNAME = 'alex'
PASSWORD = 'f'
-class RestResourcesIntegrationsTest(TestCase):
+def res_to_json(http_res):
+ return json.loads(http_res.content)
+
+
+class RestResourceIntegrationsTest(TestCase):
fixtures = ['testdata.json']
def setUp(self):
@@ -22,9 +29,31 @@ def test_entry_point(self):
res = self.client.get(reverse('api-entry_point'))
self.assertEqual(res.status_code, 200)
+ data = res_to_json(res)
+ self.assertTrue('deck_list_url' in data.keys())
+ self.assertEqual(data['deck_list_url'], reverse('api-deck_list'))
+
def test_deck_list(self):
res = self.client.get(reverse('api-deck_list'))
self.assertEqual(res.status_code, 200)
+ data = res_to_json(res)
+ self.assertTrue('deck_list' in data.keys())
+ deck_list = data['deck_list']
+ self.assertTrue(len(deck_list) >= 1)
+ self.assertTrue('name' in deck_list[0].keys())
+
+ owner = deck_list[0]['owner']
+ self.assertTrue('password' not in owner.keys())
+
+
+class RestResourcesTest(TestCase):
+ fixtures = ['testdata.json']
+
+ def test_deck_resource(self):
+ deck = Deck.objects.get(id=1)
+ resource = DeckResource(deck)
+
+
View
4 catnap/tests/api/views.py
@@ -39,7 +39,7 @@ def get(self, request):
List the available top-level resource URLs.
'''
context = {
- 'decks_url': reverse('api-deck_list'),
+ 'deck_list_url': reverse('api-deck_list'),
#'users': reverse('rest-users'),
}
return self.render_to_response(context)
@@ -64,8 +64,6 @@ class Deck(DetailView, MyRestView):
def get_object(self):
return get_object_or_404(pk=self.kwargs.get('pk'))
- #deck.url = reverse('api-deck', args=[deck.id])
- #return deck

0 comments on commit 6964447

Please sign in to comment.
Something went wrong with that request. Please try again.