Exemple #1
0
class TestMockDataError(TestCase):

    def setUp(self):
        super(TestMockDataError, self).setUp()
        self.mdlwr = MockDataApiMiddleware({
            'a': {'data': {'result': 'a'}},
            'b': {'data': {'result': 'b'}},
            'c': {'filter': {'method': 'post'}, 'data': {'result': 'b'}},
        })
        self.cnx = ApiConnexion(url='http://localapi/v2/')
        self.cnx.push_middleware(self.mdlwr, 3)

    def test_not_found(self):
        with self.assertRaises(Exception) as e:
            self.cnx.get('dada')
        self.assertEqual(e.exception.args, ("the query 'dada' was not provided as mocked data: "
                                            "urls was %s" % ['a', 'b', 'c'], ))

    def test_not_found_abs(self):
        with self.assertRaises(Exception) as e:
            self.cnx.get('/dada')
        self.assertEqual(e.exception.args, ("the query 'http://localapi/dada' was not provided as mocked data: "
                                            "urls was %s" % ['a', 'b', 'c'],))

    def test_found_bad_filters(self):
        with self.assertRaises(Exception) as e:
            self.cnx.get('c')
        self.assertTrue(e.exception.args[0].startswith("the query 'c' was not provided as mocked data: "
                                                       "1 fixture for this url, but filter did not match. "
                                                       "got "
                                                       ))

    def test_ok(self):
        self.cnx.post('c', data={})
        self.cnx.get('a')
Exemple #2
0
class TestPrintQueryMiddleware(TestCase):
    def setUp(self):
        self.s = six.StringIO()
        self.mdlw = PrintQueryMiddleware(self.s)
        self.mdlw.colors = {
            'reset': "",
            'yellow': "",
            'red': "",
            'green': "",
            'purple': "",
            'white': "",
        }

        self.cnx = ApiConnexion(url='http://localapi/v2/')
        self.cnx.push_middleware(self.mdlw, 3)
        self.cnx.push_middleware(
            MockDataApiMiddleware({
                '/a': [{
                    'data': {
                        'res': 'a'
                    }
                }],
                'b': {
                    'data': {
                        'res': 'b'
                    }
                },
                'c': {
                    'data': {
                        'res': object()
                    }
                }
            }))

    def test_print_null_settings_missings(self):
        res = self.cnx.get('b', params={'name': 'rest'})
        self.assertEqual(res.status_code, 200)
        self.assertEqual(
            self.s.getvalue(), """## BEGIN GET b =>
<truncated by missing settings.REST_API_OUTPUT_FORMAT>
## END GET b <=
""")

    def test_print_null_settings_null(self):
        with self.settings(REST_API_OUTPUT_FORMAT='null'):
            res = self.cnx.get('b', params={'name': 'rest'})
        self.assertEqual(res.status_code, 200)
        self.assertEqual(
            self.s.getvalue(), """## BEGIN GET b =>
<truncated by settings.REST_API_OUTPUT_FORMAT="null">
## END GET b <=
""")

    def test_keep_call_middleware(self):
        for i in range(50):
            self.mdlw.process_request({}, i + 700, self.cnx)
        self.assertEqual(len(self.mdlw.reqid_to_url), 50)
        self.cnx.get('b')
        self.assertEqual(len(self.mdlw.reqid_to_url), 50)

    def test_bug_call_middleware(self):
        for i in range(550):
            self.mdlw.process_request({}, i + 700, self.cnx)
        self.assertEqual(len(self.mdlw.reqid_to_url), 49)
        self.cnx.get('b')
        self.assertEqual(len(self.mdlw.reqid_to_url), 49)

    def test_print_pprint(self):
        self.mdlw.format = 'pprint'
        res = self.cnx.get('b', params={'name': 'rest', 'l': list(range(15))})
        self.assertEqual(res.status_code, 200)
        output = self.s.getvalue()
        split_output = output.split('\n')
        self.assertEqual(split_output[0], "## BEGIN GET b =>")
        if six.PY2:
            expected = "u'params': {u'l': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], u'name': u'rest'}},"
        else:
            expected = "'params': {'l': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 'name': 'rest'}},"

        for l in split_output:
            if expected in l:
                break
        else:
            self.fail("%s not found in %s" % (expected, split_output))

        self.assertEqual(len(split_output), 8)

    def test_print_pprint_long(self):
        self.mdlw.format = 'pprint'
        res = self.cnx.get('b', params={
            'name': 'rest',
            'l': list(range(255))
        })  # generate many lines
        self.assertEqual(res.status_code, 200)
        self.assertEqual(self.s.getvalue().count('\n'), 3)

    def test_print_pprint_long2(self):
        self.mdlw.format = 'pprint'
        res = self.cnx.get('/a',
                           params={
                               'name': 'rest',
                               'l': list(range(255))
                           })  # generate many lines
        self.assertEqual(res.status_code, 200)
        self.assertEqual(self.s.getvalue().count('\n'), 3)

    def test_print_json(self):
        self.maxDiff = None
        self.mdlw.format = 'json'
        res = self.cnx.get('b', params={'name': 'rest', 'l': list(range(15))})
        self.assertEqual(res.status_code, 200)
        output = self.s.getvalue()
        split_output = output.split('\n')
        self.assertEqual(split_output[0], "## BEGIN GET b =>")
        self.assertEqual(split_output[1], "{")
        self.assertEqual(split_output[2], '    "filter": {')
        self.assertEqual(len(split_output), 33)

    def test_print_json_long(self):
        self.mdlw.format = 'json'
        res = self.cnx.get('b', params={
            'name': 'rest',
            'l': list(range(255))
        })  # generate many lines
        self.assertEqual(res.status_code, 200)
        self.assertEqual(self.s.getvalue().count('\n'), 3)

    def test_unserializable(self):
        self.mdlw.format = 'json'
        res = self.cnx.get('c')  # generate many lines
        self.assertEqual(res.status_code, 200)
        getvalue = self.s.getvalue()
        if six.PY2:
            self.assertIn("u'exception': TypeError('<object object at ",
                          getvalue)
            self.assertIn("u'text': \"{u'res': <object object at ", getvalue)
        elif sys.version_info[:2] >= (3, 6):
            self.assertIn(
                "'exception': TypeError(\"Object of type 'object' is not JSON serializable\"",
                getvalue)
            self.assertIn("'text': \"{'res': <object object at ", getvalue)
        else:
            self.assertIn("'exception': TypeError('<object object at ",
                          getvalue)
            self.assertIn("'text': \"{'res': <object object at ", getvalue)