Merge lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs into lp:ubuntu-rest-scopes

Proposed by Facundo Batista
Status: Merged
Approved by: Facundo Batista
Approved revision: 518
Merged at revision: 519
Proposed branch: lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs
Merge into: lp:ubuntu-rest-scopes
Diff against target: 91 lines (+23/-7)
3 files modified
src/scopes/openlibrary.py (+9/-7)
src/scopes/tests/fixtures/openlibrary-search-nodocs.json (+6/-0)
src/scopes/tests/test_openlibrary.py (+8/-0)
To merge this branch: bzr merge lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs
Reviewer Review Type Date Requested Status
Bret Barker (community) Approve
Review via email: mp+293416@code.launchpad.net

Commit message

Support no-docs case in the OpenLibrary search.

Description of the change

Support no-docs case in the OpenLibrary search.

To post a comment you must log in.
Revision history for this message
Bret Barker (noise) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/scopes/openlibrary.py'
2--- src/scopes/openlibrary.py 2015-03-03 20:38:27 +0000
3+++ src/scopes/openlibrary.py 2016-04-29 16:02:40 +0000
4@@ -109,11 +109,9 @@
5 def _get_book_cover(d):
6 """Get book cover."""
7 if d.get('cover_i'):
8- cover = 'http://covers.openlibrary.org/b/id/' \
9- + str(d['cover_i'])+'-M.jpg'
10+ cover = 'http://covers.openlibrary.org/b/id/' + str(d['cover_i']) + '-M.jpg'
11 elif d.get('cover_id'):
12- cover = 'http://covers.openlibrary.org/b/id/' \
13- + str(d['cover_id'])+'-M.jpg'
14+ cover = 'http://covers.openlibrary.org/b/id/' + str(d['cover_id']) + '-M.jpg'
15 else:
16 cover = None
17 return cover
18@@ -190,6 +188,8 @@
19 subject = choice(SURFACING_SUBJECTS)
20 resp = self._hit_backend('subject', subject=subject, limit=limit)
21 docs = resp['docs']
22+ if not docs:
23+ return
24 cat_books = BOOKS_CATEGORY.copy()
25 cat_books['title'] = translator(subject, locale)
26 yield dict(category=cat_books)
27@@ -206,13 +206,15 @@
28 featured_author = None
29 resp = self._hit_backend('search', query=query, limit=limit)
30 docs = resp['docs']
31+ if not docs:
32+ return
33 yield dict(category=localized_category(BOOKS_SEARCH_CATEGORY,
34 locale))
35 parsed_books = []
36 for d in docs:
37 cover = _get_book_cover(d)
38 author = _get_book_author(d)
39- if author+d['title'] in parsed_books or not cover:
40+ if author + d['title'] in parsed_books or not cover:
41 continue
42 first_sentence = d.get('first_sentence', [None])[0]
43 first_published = d.get('first_publish_year')
44@@ -245,7 +247,7 @@
45 'editions': editions,
46 'has_fulltext': d['has_fulltext'],
47 })
48- parsed_books.append(author+d['title'])
49+ parsed_books.append(author + d['title'])
50
51 def preview(self, **kwargs):
52 """Do the preview."""
53@@ -267,7 +269,7 @@
54 desc = _get_description(data)
55 if not desc:
56 if result['first_sentence']:
57- desc = '\"'+result['first_sentence']+'\"'
58+ desc = '"' + result['first_sentence'] + '"'
59 else:
60 desc = None
61
62
63=== added file 'src/scopes/tests/fixtures/openlibrary-search-nodocs.json'
64--- src/scopes/tests/fixtures/openlibrary-search-nodocs.json 1970-01-01 00:00:00 +0000
65+++ src/scopes/tests/fixtures/openlibrary-search-nodocs.json 2016-04-29 16:02:40 +0000
66@@ -0,0 +1,6 @@
67+{
68+ "start": 0,
69+ "num_found": 22630,
70+ "numFound": 22630,
71+ "docs": []
72+}
73
74=== modified file 'src/scopes/tests/test_openlibrary.py'
75--- src/scopes/tests/test_openlibrary.py 2015-03-03 20:38:27 +0000
76+++ src/scopes/tests/test_openlibrary.py 2016-04-29 16:02:40 +0000
77@@ -121,6 +121,14 @@
78 self.maxDiff = None
79 self.assertEqual(response[2], should_res)
80
81+ def test_search_no_authors(self):
82+ app = openlibrary.App(CONFIG)
83+ with patch.object(app, '_hit_backend') as mock:
84+ mock.return_value = json.loads(get_fixture(
85+ 'openlibrary-search-nodocs.json'))
86+ response = list(app.search(query='alice', platform='desktop', limit=5, locale='C'))
87+ self.assertEqual(response, [])
88+
89
90 class PreviewTestCase(TestCase):
91 """Tests for the preview function."""

Subscribers

People subscribed via source and target branches