Commits

Martin Skala committed dda5273

Cover recently modified code in RemoteQuerySet with tests

  • Participants
  • Parent commits 1528ca2

Comments (0)

Files changed (6)

django_roa/db/query.py

         except Exception, e:
             raise ROAException(e)
 
-        # TODO: create a test for the following code
         cnt = 0
         try:
             cnt = int(response.body_string())
             return self._get_from_id_or_pk(pk=kwargs['pk'])
         # check the case of PK attribute with custom name
         elif kwargs.keys() == [custom_pk]:
-            # TODO: create a test for this case
             return self._get_from_id_or_pk(pk=kwargs[custom_pk])
         # check if there's an exact match filter
         elif len(exact_match) == 1:
-            # TODO: create a test for this case
             # use the value of exact match filter to retrieve object by PK
             return self._get_from_id_or_pk(pk=kwargs[exact_match[0]])
         else:

examples/django_roa_client/models.py

     @staticmethod
     def get_resource_url_list():
         return u'http://127.0.0.1:8081/django_roa_server/remotepagewithproxy/'
+
+class RemotePageWithCustomPrimaryKey(Model):
+    auto_field = models.AutoField(primary_key=True)
+    title = models.CharField(max_length=50)
+
+    def __unicode__(self):
+        return u'%s (%s)' % (self.title, self.pk)
+
+    @staticmethod
+    def get_resource_url_list():
+        return u'http://127.0.0.1:8081/django_roa_server/remotepagewithcustomprimarykey/'
+
+class RemotePageWithCustomPrimaryKeyCountOverridden(RemotePageWithCustomPrimaryKey):
+
+    class Meta:
+        proxy = True
+
+    def get_resource_url_count(self):
+        return u'http://127.0.0.1:8081/django_roa_server/remotepagewithcustomprimarykey/count2/'

examples/django_roa_client/tests.py

     RemotePageWithBooleanFields, RemotePageWithRelations, \
     RemotePageWithCustomSlug, RemotePageWithOverriddenUrls, \
     RemotePageWithNamedRelations, RemotePageWithProxy, \
-    RemotePageWithRelationsThrough
+    RemotePageWithRelationsThrough, RemotePageWithCustomPrimaryKey, \
+    RemotePageWithCustomPrimaryKeyCountOverridden
 from django_roa_client.forms import TestForm, RemotePageForm
+from django_roa.db.exceptions import ROAException
 
 ROA_FILTERS = getattr(settings, 'ROA_FILTERS', {})
 
 
     def setUp(self):
         RemotePage.objects.all().delete()
+        RemotePageWithCustomPrimaryKey.objects.all().delete()
 
     def tearDown(self):
         RemotePage.objects.all().delete()
+        RemotePageWithCustomPrimaryKey.objects.all().delete()
 
 class ROAUserTestCase(ROATestCase):
 
         self.remote_page2 = RemotePage.objects.create(title='Another remote page')
         self.remote_page3 = RemotePage.objects.create(title='Yet another remote page')
         self.remote_page4 = RemotePage.objects.create(title='Still another remote page')
+        self.remote_page_with_custom_primary_key1 = RemotePageWithCustomPrimaryKey.objects.create(title=u'Remote test page with custom primary')
+        self.remote_page_with_custom_primary_key2 = RemotePageWithCustomPrimaryKey.objects.create(title=u'Another remote test page with custom primary')
 
     def test_getorcreate(self):
         self.assertEqual(repr(RemotePage.objects.get_or_create(title='A remote page')), '(<RemotePage: A remote page (1)>, False)')
     def test_combined(self):
         self.assertEqual(repr(RemotePage.objects.exclude(title__contains='yet').order_by('title', '-id')[:2]), '[<RemotePage: A remote page (1)>, <RemotePage: Another remote page (2)>]')
 
+    def test_get(self):
+        # test get by pk, id directly
+        self.assertEqual(repr(RemotePage.objects.get(id=1)), '<RemotePage: A remote page (1)>')
+        self.assertEqual(repr(RemotePage.objects.get(pk=2)), '<RemotePage: Another remote page (2)>')
+        # test get by pk, id with exact filter
+        self.assertEqual(repr(RemotePage.objects.get(id__exact=3)), '<RemotePage: Yet another remote page (3)>')
+        self.assertEqual(repr(RemotePage.objects.get(pk__exact=4)), '<RemotePage: Still another remote page (4)>')
+
+        # test get by custom primary key attribute name
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(auto_field=1)), '<RemotePageWithCustomPrimaryKey: Remote test page with custom primary (1)>')
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(auto_field=2)), '<RemotePageWithCustomPrimaryKey: Another remote test page with custom primary (2)>')
+        with self.assertRaisesRegexp(ROAException, 'Not Found'):
+            RemotePageWithCustomPrimaryKey.objects.get(auto_field=999)
+
+        # test get by custom primary key with exact filter
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(auto_field__exact=1)), '<RemotePageWithCustomPrimaryKey: Remote test page with custom primary (1)>')
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(auto_field__exact=2)), '<RemotePageWithCustomPrimaryKey: Another remote test page with custom primary (2)>')
+        with self.assertRaisesRegexp(ROAException, 'Not Found'):
+            RemotePageWithCustomPrimaryKey.objects.get(auto_field__exact=999)
+
+        # test get by multiple attributes
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(title='Another remote test page with custom primary', auto_field=2)), '<RemotePageWithCustomPrimaryKey: Another remote test page with custom primary (2)>')
+        self.assertEqual(repr(RemotePageWithCustomPrimaryKey.objects.get(title__exact='Another remote test page with custom primary', auto_field__exact=2)), '<RemotePageWithCustomPrimaryKey: Another remote test page with custom primary (2)>')
+        with self.assertRaisesRegexp(ROAException, 'Not Found'):
+            RemotePageWithCustomPrimaryKey.objects.get(title__exact='Another remote test page with custom primary', auto_field__exact=999)
+
+    def test_count(self):
+        self.assertEqual(RemotePageWithCustomPrimaryKey.objects.count(), 2)
+        RemotePageWithCustomPrimaryKey.objects.all().delete()
+        self.assertEqual(RemotePageWithCustomPrimaryKey.objects.count(), 0)
+
+        RemotePageWithCustomPrimaryKey.objects.create(title=u'Remote test page with custom primary')
+        self.assertEqual(RemotePageWithCustomPrimaryKeyCountOverridden.objects.count(), 0)
+
 class ROAAdminTests(ROAUserTestCase):
 
     def test_admin_views(self):

examples/django_roa_server/handlers.py

 from django_roa_server.models import RemotePage, RemotePageWithManyFields, \
     RemotePageWithBooleanFields, RemotePageWithCustomSlug, \
     RemotePageWithOverriddenUrls, RemotePageWithRelations, \
-    RemotePageWithNamedRelations, RemotePageWithRelationsThrough
+    RemotePageWithNamedRelations, RemotePageWithRelationsThrough, \
+    RemotePageWithCustomPrimaryKey
 
 logger = logging.getLogger("django_roa_server")
 
 class RemotePageWithCustomSlugCountHandler(ROACountHandler):
     model = RemotePageWithCustomSlug
 
+class RemotePageWithCustomPrimaryKeyHandler(ROAHandler):
+    model = RemotePageWithCustomPrimaryKey
+
+class RemotePageWithCustomPrimaryKeyCountHandler(ROACountHandler):
+    model = RemotePageWithCustomPrimaryKey
+
+class RemotePageWithCustomPrimaryKeyCount2Handler(ROACountHandler):
+    model = RemotePageWithCustomPrimaryKey
+
+    def read(self, request, *args, **kwargs):
+        return 'invalid counter'
+
 
 class RemotePageWithOverriddenUrlsHandler(ROAWithSlugHandler):
     model = RemotePageWithOverriddenUrls

examples/django_roa_server/models.py

 
     def __unicode__(self):
         return u'%s (%s)' % (self.title, self.pk)
+
+
+class RemotePageWithCustomPrimaryKey(models.Model):
+    auto_field = models.AutoField(primary_key=True)
+    title = models.CharField(max_length=50)
+
+    def __unicode__(self):
+        return u'%s (%s)' % (self.title, self.pk)

examples/django_roa_server/urls.py

     RemotePageWithManyFieldsCountHandler, RemotePageWithBooleanFieldsCountHandler, \
     RemotePageWithCustomSlugCountHandler, RemotePageWithOverriddenUrlsCountHandler, \
     RemotePageWithRelationsHandler, RemotePageWithNamedRelationsHandler, \
-    RemotePageWithNamedRelationsCountHandler, RemotePageWithRelationsThroughHandler
+    RemotePageWithNamedRelationsCountHandler, RemotePageWithRelationsThroughHandler, \
+    RemotePageWithCustomPrimaryKeyHandler, RemotePageWithCustomPrimaryKeyCountHandler, \
+    RemotePageWithCustomPrimaryKeyCount2Handler
 
 # Enable HTTP authentication through django-piston
 ad = { 'authentication': HttpBasicAuthentication(
 remote_pages_with_custom_slug = Resource(handler=RemotePageWithCustomSlugHandler, **ad)
 remote_pages_with_custom_slug_count = Resource(handler=RemotePageWithCustomSlugCountHandler, **ad)
 
+remote_pages_with_custom_primary_key = Resource(handler=RemotePageWithCustomPrimaryKeyHandler, **ad)
+remote_pages_with_custom_primary_key_count = Resource(handler=RemotePageWithCustomPrimaryKeyCountHandler, **ad)
+remote_pages_with_custom_primary_key_count2 = Resource(handler=RemotePageWithCustomPrimaryKeyCount2Handler, **ad)
+
 remote_pages_with_overridden_urls = Resource(handler=RemotePageWithOverriddenUrlsHandler, **ad)
 remote_pages_with_overridden_urls_count = Resource(handler=RemotePageWithOverriddenUrlsCountHandler, **ad)
 
     url(r'^django_roa_server/remotepagewithmanyfields/count/$', remote_pages_with_many_fields_count),
     url(r'^django_roa_server/remotepagewithbooleanfields/count/$', remote_pages_with_boolean_fields_count),
     url(r'^django_roa_server/remotepagewithcustomslug/count/$', remote_pages_with_custom_slug_count),
+    url(r'^django_roa_server/remotepagewithcustomprimarykey/count/$', remote_pages_with_custom_primary_key_count),
+    url(r'^django_roa_server/remotepagewithcustomprimarykey/count2/$', remote_pages_with_custom_primary_key_count2),
     url(r'^django_roa_server/remotepagewithoverriddenurls/count/$', remote_pages_with_overridden_urls_count),
     url(r'^django_roa_server/remotepagewithrelations/count/$', remote_pages_with_relations_count),
     url(r'^django_roa_server/remotepagewithrelationsthrough/count/$', remote_pages_with_relations_through_count),
     url(r'^django_roa_server/remotepagewithmanyfields/?(?P<pk>\d+)?/?$', remote_pages_with_many_fields),
     url(r'^django_roa_server/remotepagewithbooleanfields/?(?P<pk>\d+)?/?$', remote_pages_with_boolean_fields),
     url(r'^django_roa_server/remotepagewithcustomslug/?(?P<object_slug>[-\w]+)?/?$', remote_pages_with_custom_slug),
+    url(r'^django_roa_server/remotepagewithcustomprimarykey/?(?P<pk>[-\w]+)?/?$', remote_pages_with_custom_primary_key),
     url(r'^django_roa_server/remotepagewithoverriddenurls/?(?P<object_slug>[-\w]+)?/?$', remote_pages_with_overridden_urls),
     url(r'^django_roa_server/remotepagewithrelations/?(?P<pk>\d+)?/?$', remote_pages_with_relations),
     url(r'^django_roa_server/remotepagewithrelationsthrough/?(?P<pk>\d+)?/?$', remote_pages_with_relations_through),