22
22
from bzrlib import tests
23
23
from bzrlib.plugins.launchpad import lp_api_lite
25
class _JSONParserFeature(tests.Feature):
28
return lp_api_lite.json is not None
30
def feature_name(self):
31
return 'simplejson or json'
33
JSONParserFeature = _JSONParserFeature()
25
35
_example_response = r"""
185
195
self.assertIs(None, latest_pub._parse_json_info(_example_response))
187
197
def test__parse_json_example_response(self):
188
if lp_api_lite.json is None:
189
raise tests.UnavailableFeature('json or simplejson module')
198
self.requireFeature(JSONParserFeature)
190
199
latest_pub = self.make_latest_publication()
191
200
content = latest_pub._parse_json_info(_example_response)
192
201
self.assertIsNot(None, content)
197
206
self.assertEqual('bzr', entry['source_package_name'])
198
207
self.assertEqual("2.1.4-0ubuntu1", entry["source_package_version"])
209
def test__parse_json_not_json(self):
210
self.requireFeature(JSONParserFeature)
211
latest_pub = self.make_latest_publication()
212
self.assertIs(None, latest_pub._parse_json_info('Not_valid_json'))
200
214
def test_get_latest_version_no_response(self):
201
215
latest_pub = self.make_latest_publication()
202
216
latest_pub._get_lp_info = lambda: None
207
221
latest_pub = self.make_latest_publication()
208
222
self.assertEqual(None, latest_pub.get_latest_version())
224
def test_get_latest_version_invalid_json(self):
225
self.requireFeature(JSONParserFeature)
226
latest_pub = self.make_latest_publication()
227
latest_pub._get_lp_info = lambda: "not json"
228
self.assertEqual(None, latest_pub.get_latest_version())
210
230
def test_get_latest_version_no_versions(self):
231
self.requireFeature(JSONParserFeature)
211
232
latest_pub = self.make_latest_publication()
212
233
latest_pub._get_lp_info = lambda: _no_versions_response
213
234
self.assertEqual(None, latest_pub.get_latest_version())
236
def test_get_latest_version_missing_entries(self):
237
# Launchpad's no-entries response does have an empty entries value.
238
# However, lets test that we handle other failures without tracebacks
239
self.requireFeature(JSONParserFeature)
240
latest_pub = self.make_latest_publication()
241
latest_pub._get_lp_info = lambda: '{}'
242
self.assertEqual(None, latest_pub.get_latest_version())
244
def test_get_latest_version_invalid_entries(self):
245
# Make sure we sanely handle a json response we don't understand
246
self.requireFeature(JSONParserFeature)
247
latest_pub = self.make_latest_publication()
248
latest_pub._get_lp_info = lambda: '{"entries": {"a": 1}}'
249
self.assertEqual(None, latest_pub.get_latest_version())
215
251
def test_get_latest_version_example(self):
252
self.requireFeature(JSONParserFeature)
216
253
latest_pub = self.make_latest_publication()
217
254
latest_pub._get_lp_info = lambda: _example_response
218
255
self.assertEqual("2.1.4-0ubuntu1", latest_pub.get_latest_version())
220
257
def DONT_test_get_latest_version_from_launchpad(self):
258
self.requireFeature(JSONParserFeature)
221
259
latest_pub = self.make_latest_publication()
222
260
self.assertIsNot(None, latest_pub.get_latest_version())