Skip to content

Commit d2462cc

Browse files
committed
Update tests for CrunchBase.more
1 parent d2b3dfc commit d2462cc

File tree

2 files changed

+91
-55
lines changed

2 files changed

+91
-55
lines changed

src/pycrunchbase/pycrunchbase.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Product,
1111
Relationship,
1212
)
13+
from .resource.relationship import NoneRelationshipSingleton
1314

1415

1516
@six.python_2_unicode_compatible
@@ -110,7 +111,7 @@ def more(self, relationship):
110111
Relationship with the new data
111112
"""
112113
if relationship.total_items <= len(relationship):
113-
return None
114+
return NoneRelationshipSingleton
114115

115116
if relationship.first_page_url:
116117
url_to_call = relationship.first_page_url
@@ -119,7 +120,7 @@ def more(self, relationship):
119120
url_to_call = relationship.next_page_url
120121
return self._relationship(relationship.name, url_to_call)
121122
else:
122-
return None
123+
return NoneRelationshipSingleton
123124

124125
def _relationship(self, name, url):
125126
"""Loads a relationship for a Node
@@ -134,7 +135,7 @@ def _relationship(self, name, url):
134135
"""
135136
data = self._make_request(url)
136137
if not data or data.get('error'):
137-
return None
138+
return NoneRelationshipSingleton
138139
return Relationship(name, data)
139140

140141
def _get_first_organization_match(self, list_of_result=None):

tests/test_pycrunchbase.py

Lines changed: 87 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import httpretty
1313

1414
from pycrunchbase import CrunchBase, Relationship
15+
from pycrunchbase.resource.relationship import NoneRelationship
1516

1617
MOCK_RELATIONSHIP_PAGE = '''{
1718
"paging": {
@@ -224,58 +225,6 @@ def test_exception_raised_when_making_calls(self):
224225
cb = CrunchBase('123')
225226
cb.organizations('organization')
226227

227-
@httpretty.activate
228-
def test_more_relationship_for_relationship_summary(self):
229-
"""Load more relationship data from summary"""
230-
data = {'data': json.loads(MOCK_RELATIONSHIP_PAGE)}
231-
httpretty.register_uri(
232-
httpretty.GET,
233-
'https://api.crunchbase.com/v/2/organization/example/past_team'
234-
'?user_key=123',
235-
body=json.dumps(data))
236-
237-
rs = Relationship('past_team', json.loads(PAST_TEAM_RELATIONSHIP))
238-
cb = CrunchBase('123')
239-
240-
rs = cb.more(rs)
241-
self.assertIsNotNone(rs)
242-
self.assertEquals(3, len(rs))
243-
244-
def test_more_relationship_for_relationship_page1(self):
245-
"""At summary page, there is no more next page, so return None"""
246-
past_team = json.loads(PAST_TEAM_RELATIONSHIP)
247-
past_team['paging']['total_items'] = 1
248-
rs = Relationship('past_team', past_team)
249-
cb = CrunchBase('123')
250-
251-
self.assertIsNone(cb.more(rs))
252-
253-
@httpretty.activate
254-
def test_more_relationship_for_relationship_page2(self):
255-
"""At page 1, there is no more next page, so return None"""
256-
return_data = json.loads(MOCK_RELATIONSHIP_PAGE)
257-
return_data['paging']['total_items'] = 6
258-
return_data['paging']['first_page_url'] = None
259-
return_data['paging']['items_per_page'] = 3
260-
return_data['paging']['next_page_url'] = (
261-
'https://api.crunchbase.com/v/2/organization/example/past_team'
262-
'user_key=123&page=2')
263-
rs = Relationship('past_team', return_data)
264-
265-
return_data['paging']['next_page_url'] = None
266-
return_data['paging']['current_page'] = 2
267-
data = {'data': return_data}
268-
httpretty.register_uri(
269-
httpretty.GET,
270-
'https://api.crunchbase.com/v/2/organization/example/past_team'
271-
'user_key=123&page=2',
272-
body=json.dumps(data))
273-
cb = CrunchBase('123')
274-
rs = cb.more(rs)
275-
276-
self.assertIsNotNone(rs)
277-
self.assertEqual(2, rs.current_page)
278-
279228
@httpretty.activate
280229
def test_get_funding_round_data(self):
281230
httpretty.register_uri(
@@ -359,3 +308,89 @@ def test_get_acquisition_data(self):
359308
acquisition = cb.acquisition('uuid1')
360309
self.assertEqual(acquisition.disposition_of_acquired, "Combined")
361310
self.assertEqual(acquisition.acquisition_type, "Acqui-hire")
311+
312+
313+
class LoadMoreTestCase(TestCase):
314+
def setUp(self):
315+
self.cb = CrunchBase('123')
316+
317+
def test_more_from_relationship_summary_but_no_more(self):
318+
"""At summary page, there is no more next page,
319+
because the summary already contains everything,
320+
i.e. total_items < 8 since CrunchBase defaults to returning
321+
8 in the relationship summary, so return NoneRelationship"""
322+
past_team = json.loads(PAST_TEAM_RELATIONSHIP)
323+
past_team['paging']['total_items'] = 1
324+
rs = Relationship('past_team', past_team)
325+
326+
self.assertIsInstance(self.cb.more(rs), NoneRelationship)
327+
328+
@httpretty.activate
329+
def test_more_from_relationship_summary_returns_error(self):
330+
"""Load more relationship data from summary"""
331+
httpretty.register_uri(
332+
httpretty.GET,
333+
'https://api.crunchbase.com/v/2/organization/example/past_team'
334+
'?user_key=123',
335+
body=json.dumps({'data': {'error': 'error'}}))
336+
337+
rs = Relationship('past_team', json.loads(PAST_TEAM_RELATIONSHIP))
338+
339+
rs = self.cb.more(rs)
340+
self.assertIsInstance(rs, NoneRelationship)
341+
342+
@httpretty.activate
343+
def test_more_relationship_for_relationship_summary(self):
344+
"""Load more relationship data from summary"""
345+
data = {'data': json.loads(MOCK_RELATIONSHIP_PAGE)}
346+
httpretty.register_uri(
347+
httpretty.GET,
348+
'https://api.crunchbase.com/v/2/organization/example/past_team'
349+
'?user_key=123',
350+
body=json.dumps(data))
351+
352+
rs = Relationship('past_team', json.loads(PAST_TEAM_RELATIONSHIP))
353+
354+
rs = self.cb.more(rs)
355+
self.assertIsNotNone(rs)
356+
self.assertEquals(3, len(rs))
357+
358+
@httpretty.activate
359+
def test_more_relationship_for_relationship_page2(self):
360+
"""At page 1, there a next_page, get it and return the Relationship"""
361+
data = {
362+
"paging": {
363+
"items_per_page": 8,
364+
"current_page": 1,
365+
"number_of_pages": 2,
366+
"next_page_url": "https://api.crunchbase.com/v/2/"
367+
"organization/example/past_team?user_key=123&page=2",
368+
"prev_page_url": None,
369+
"total_items": 10,
370+
"sort_order": "custom"
371+
},
372+
"items": [{}, {}, {}, {}, {}, {}, {}, {}]
373+
}
374+
rs = Relationship('past_team', data)
375+
376+
httpretty.register_uri(
377+
httpretty.GET,
378+
'https://api.crunchbase.com/v/2/organization/example/past_team'
379+
'?user_key=123&page=2',
380+
body=json.dumps({
381+
"data": {
382+
"paging": {
383+
"items_per_page": 8,
384+
"current_page": 2,
385+
"number_of_pages": 2,
386+
"next_page_url": None,
387+
"prev_page_url": None,
388+
"total_items": 10,
389+
"sort_order": "custom"
390+
},
391+
"items": [{}, {}]
392+
}}))
393+
394+
more_rs = self.cb.more(rs)
395+
396+
self.assertEqual(2, len(more_rs))

0 commit comments

Comments
 (0)