Skip to content

Commit

Permalink
Merge cced330 into 7105fac
Browse files Browse the repository at this point in the history
  • Loading branch information
kennknowles committed May 24, 2013
2 parents 7105fac + cced330 commit c8b01b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions commcare_export/commcare_hq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# This import pattern supports Python 2 and 3
try:
from urllib.request import urlopen
from urllib.parse import urlparse, urlencode
from urllib.parse import urlparse, urlencode, parse_qs
except ImportError:
from urlparse import urlparse
from urlparse import urlparse, parse_qs
from urllib import urlopen, urlencode

from commcare_export.repeatable_iterator import RepeatableIterator
Expand Down Expand Up @@ -98,7 +98,7 @@ def iterate_resource(resource=resource, params=params):
yield obj

if batch['meta']['next']:
params = urlparse.parse_qs(urlparse.urlparse(batch['meta']['next']).query)
params = parse_qs(urlparse(batch['meta']['next']).query)
else:
more_to_fetch = False

Expand Down
41 changes: 41 additions & 0 deletions tests/test_commcare_hq_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
import simplejson
from itertools import *

import requests

from commcare_export.commcare_hq_client import CommCareHqClient

class FakeSession(object):
def get(self, resource_url, params=None):
if params:
result = {
'meta': { 'next': None, 'offset': params['offset'][0], 'limit': 1, 'total_count': 2 },
'objects': [ {'foo': 2} ]
}
else:
result = {
'meta': { 'next': '?offset=1', 'offset': 0, 'limit': 1, 'total_count': 2 },
'objects': [ {'foo': 1} ]
}

# Mutatey construction method required by requests.Response
response = requests.Response()
response._content = simplejson.dumps(result)
response.status_code = 200
return response

class TestCommCareHqClient(unittest.TestCase):

@classmethod
def setup_class(cls):
pass

def test_iterate(self):
client = CommCareHqClient('/fake/commcare-hq/url', project='fake-project', session = FakeSession())

# Iteration should do two "gets" because the first will have something in the "next" metadata field
results = list(client.iterate('/fake/uri'))
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['foo'], 1)
self.assertEqual(results[1]['foo'], 2)

0 comments on commit c8b01b3

Please sign in to comment.