~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/test_lp_api_lite.py

  • Committer: John Arbash Meinel
  • Date: 2011-07-13 12:22:27 UTC
  • mto: (6024.3.1 integration)
  • mto: This revision was merged to the branch mainline in revision 6033.
  • Revision ID: john@arbash-meinel.com-20110713122227-0nb381xfj9bi965c
Put several tests behind a Feature object.
Add some edge-case tests that even when we don't get the
response we are expecting, we don't explode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from bzrlib import tests
23
23
from bzrlib.plugins.launchpad import lp_api_lite
24
24
 
 
25
class _JSONParserFeature(tests.Feature):
 
26
 
 
27
    def _probe(self):
 
28
        return lp_api_lite.json is not None
 
29
 
 
30
    def feature_name(self):
 
31
        return 'simplejson or json'
 
32
 
 
33
JSONParserFeature = _JSONParserFeature()
 
34
 
25
35
_example_response = r"""
26
36
{
27
37
    "total_size": 2,
185
195
        self.assertIs(None, latest_pub._parse_json_info(_example_response))
186
196
 
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"])
199
208
 
 
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'))
 
213
 
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())
209
223
 
 
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())
 
229
 
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())
214
235
 
 
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())
 
243
 
 
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())
 
250
 
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())
219
256
 
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())