File: test_exceptions.py

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (37 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from boto.compat import json
from tests.compat import mock, unittest

from tests.unit.cloudsearch.test_search import HOSTNAME, \
                                               CloudSearchSearchBaseTest
from boto.cloudsearch.search import SearchConnection, SearchServiceException


def fake_loads_value_error(content, *args, **kwargs):
    """Callable to generate a fake ValueError"""
    raise ValueError("HAHAHA! Totally not simplejson & you gave me bad JSON.")


def fake_loads_json_error(content, *args, **kwargs):
    """Callable to generate a fake JSONDecodeError"""
    raise json.JSONDecodeError('Using simplejson & you gave me bad JSON.',
                               '', 0)


class CloudSearchJSONExceptionTest(CloudSearchSearchBaseTest):
    response = b'{}'

    def test_no_simplejson_value_error(self):
        with mock.patch.object(json, 'loads', fake_loads_value_error):
            search = SearchConnection(endpoint=HOSTNAME)

            with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
                search.search(q='test')

    @unittest.skipUnless(hasattr(json, 'JSONDecodeError'),
                         'requires simplejson')
    def test_simplejson_jsondecodeerror(self):
        with mock.patch.object(json, 'loads', fake_loads_json_error):
            search = SearchConnection(endpoint=HOSTNAME)

            with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
                search.search(q='test')