Merge lp:~verterok/unity-scope-ebay/missing-attributes into lp:unity-scope-ebay

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 13
Merged at revision: 13
Proposed branch: lp:~verterok/unity-scope-ebay/missing-attributes
Merge into: lp:unity-scope-ebay
Diff against target: 112 lines (+55/-7)
2 files modified
src/unity_ebay_daemon.py (+19/-4)
tests/test_ebay.py (+36/-3)
To merge this branch: bzr merge lp:~verterok/unity-scope-ebay/missing-attributes
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Facundo Batista (community) Approve
Review via email: mp+188715@code.launchpad.net

Commit message

Quote search string and handle missing searchResult and condition in the response.

Description of the change

Two fixes:
- quote search string
- handle missing searchResult and condition in the results

To post a comment you must log in.
Revision history for this message
Facundo Batista (facundo) :
review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/unity_ebay_daemon.py'
2--- src/unity_ebay_daemon.py 2013-09-24 18:29:34 +0000
3+++ src/unity_ebay_daemon.py 2013-10-01 21:04:21 +0000
4@@ -78,16 +78,26 @@
5 Search the server for eBay items
6 '''
7 results = []
8- search_url = SEARCH_URI % (API_KEY, search.replace(" ", "%20"))
9+ result = None
10+ search_url = SEARCH_URI % (API_KEY, urllib.parse.quote(search))
11 try:
12 logger.debug("Request: %s", search_url)
13 response = urllib.request.urlopen(search_url).read()
14 response = response.decode('utf8')
15 json_response = json.loads(response)
16- result = json_response["findItemsByKeywordsResponse"][0]['searchResult'][0]['item']
17+ search_result = json_response["findItemsByKeywordsResponse"][0].get('searchResult')
18+ if search_result:
19+ count = int(search_result[0]['@count'])
20+ # if we got a searchResult and count > 0
21+ if count > 0:
22+ result = search_result[0]['item']
23+ else:
24+ # if there is not searchResult, we want to know what's in
25+ # the response body
26+ logger.warning("Missing searchResult key: %r", json_response)
27 except:
28 logger.exception("Error while fetching data.")
29- result = {}
30+ result = []
31 if result:
32 for item in result:
33 icon_hint = Unity.AnnotatedIcon.new(Gio.ThemedIcon.new(item['galleryURL'][0]))
34@@ -104,7 +114,10 @@
35
36 title = item['title'][0]
37 uri = item['viewItemURL'][0]
38- condition = item['condition'][0]['conditionDisplayName'][0]
39+ if 'condition' in item:
40+ condition = item['condition'][0]['conditionDisplayName'][0]
41+ else:
42+ condition = None
43 shipping_to = ', '.join(item['shippingInfo'][0]['shipToLocations'])
44 payment_method = ', '.join(item['paymentMethod'])
45 currency_code = item['sellingStatus'][0]['currentPrice'][0]['@currencyId']
46@@ -178,6 +191,8 @@
47 currency_code = self.result.metadata['currency_code']
48 price = self.result.metadata['price']
49 preview.props.subtitle = "%s %s" % (currency_code.unpack(), price.unpack())
50+ if self.result.metadata['condition']:
51+ preview.add_info(Unity.InfoHint.new("condition", _("Condition"), None, self.result.metadata['condition'].unpack()))
52 preview.add_info(Unity.InfoHint.new("condition", _("Condition"), None, self.result.metadata['condition'].unpack()))
53 preview.add_info(Unity.InfoHint.new("payment_methods", _("Payment method:"), None, self.result.metadata['payment_methods'].unpack()))
54 preview.add_info(Unity.InfoHint.new("shipping_to", _("Shipping locations:"), None, self.result.metadata['shipping_to'].unpack()))
55
56=== modified file 'tests/test_ebay.py'
57--- tests/test_ebay.py 2013-09-20 22:02:28 +0000
58+++ tests/test_ebay.py 2013-10-01 21:04:21 +0000
59@@ -1,8 +1,14 @@
60 #! /usr/bin/python3
61 # -*- coding: utf-8 -*-
62+
63+import imp
64+import json
65+import urllib
66+
67+from io import BytesIO, StringIO
68+from unittest import TestCase, mock
69 from gi.repository import Unity
70-from unittest import TestCase
71-import imp
72+
73
74 class ResultSet(Unity.ResultSet):
75 def __init__(self):
76@@ -57,8 +63,35 @@
77 self.scope_module.SEARCH_URI = 'file:tests/data/mock_ebay_fail#'
78 for s in ['iphone']:
79 result_set = self.perform_query(s)
80- print(result_set)
81 self.assertEqual(len(result_set.results), 0)
82
83+ def test_search_missing_condition(self):
84+ query = "foo"
85+ called = []
86+ urlopen = urllib.request.urlopen
87+ def fake_urlopen(req):
88+ data = urlopen("file:tests/data/mock_ebay_pass").read()
89+ # remove condition values from the result
90+ result = json.loads(data.decode("utf-8"))
91+ sresult = result['findItemsByKeywordsResponse'][0]['searchResult']
92+ for i in sresult[0]['item']:
93+ i.pop('condition')
94+ return BytesIO(json.dumps(result).encode('utf-8'))
95+ with mock.patch('urllib.request.urlopen', new=fake_urlopen):
96+ result_set = self.perform_query(query)
97+ self.assertEqual(len(result_set.results), 3)
98+
99+ def test_search_unicode(self):
100+ query = "Święty Patryk"
101+ called = []
102+ urlopen = urllib.request.urlopen
103+ self.scope_module.SEARCH_URI = '%s#%s'
104+ def fake_urlopen(req):
105+ called.append(req)
106+ return urlopen("file:tests/data/mock_ebay_pass")
107+ with mock.patch('urllib.request.urlopen', new=fake_urlopen):
108+ self.perform_query(query)
109+ self.assertEqual(urllib.parse.quote(query), called[0].split("#")[1])
110+
111 if __name__ == '__main__':
112 unittest.main()

Subscribers

People subscribed via source and target branches