コード例 #1
0
class TestTapiClient(unittest.TestCase):
    def setUp(self):
        self.wrapper = TesterClient()

    @responses.activate
    def test_added_custom_resources(self):
        wrapper = TesterClient(resource_mapping=[
            Resource("myresource", "http://url.ru/myresource"),
            Resource("myresource2", "https://url.ru/myresource2"),
        ])
        responses.add(responses.GET,
                      url=wrapper.myresource().data,
                      body='[]',
                      status=200,
                      content_type='application/json')

        response = self.wrapper.myresource().get()
        assert response.data == []

    def test_fill_url_template(self):
        expected_url = 'https://api.test.com/user/123/'

        resource = self.wrapper.user(id='123')

        self.assertEqual(resource.data, expected_url)

    def test_fill_another_root_url_template(self):
        expected_url = 'https://api.another.com/another-root/'

        resource = self.wrapper.another_root()

        self.assertEqual(resource.data, expected_url)

    def test_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])

        for i, item in enumerate(client):
            self.assertEqual(item, i)

    def test_client_data_dict(self):
        client = self.wrapper._wrap_in_tapi({"data": 0})

        assert client["data"] == 0
        assert client.data == {"data": 0}

    def test_client_data_list(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])

        assert client[0] == 0
        assert client[1] == 1

    @responses.activate
    def test_in_operator(self):
        responses.add(responses.GET,
                      self.wrapper.test().data,
                      body='{"data": 1, "other": 2}',
                      status=200,
                      content_type='application/json')

        response = self.wrapper.test().get()

        self.assertIn('data', response)
        self.assertIn('other', response)
        self.assertNotIn('wat', response)

    @responses.activate
    def test_accessing_index_out_of_bounds_should_raise_index_error(self):
        responses.add(responses.GET,
                      self.wrapper.test().data,
                      body='["a", "b", "c"]',
                      status=200,
                      content_type='application/json')

        response = self.wrapper.test().get()

        with self.assertRaises(IndexError):
            response[3]

    @responses.activate
    def test_accessing_empty_list_should_raise_index_error(self):
        responses.add(responses.GET,
                      self.wrapper.test().data,
                      body='[]',
                      status=200,
                      content_type='application/json')

        response = self.wrapper.test().get()

        with self.assertRaises(IndexError):
            response[3]

    def test_fill_url_from_default_params(self):
        wrapper = TesterClient(default_url_params={'id': 123})
        self.assertEqual(wrapper.user().data, 'https://api.test.com/user/123/')
コード例 #2
0
class TestTapiExecutor(unittest.TestCase):
    def setUp(self):
        self.wrapper = TesterClient()

    def test_resource_executor_data_should_be_composed_url(self):
        expected_url = 'https://api.test.com/test/'
        resource = self.wrapper.test()

        self.assertEqual(resource.data, expected_url)

    def test_docs(self):
        self.assertEqual(
            '\n'.join(self.wrapper.resource.__doc__.split('\n')[1:]),
            'Resource: ' + self.wrapper.resource._resource['resource'] + '\n'
            'Docs: ' + self.wrapper.resource._resource['docs'] + '\n'
            'Foo: ' + self.wrapper.resource._resource['foo'] + '\n'
            'Spam: ' + self.wrapper.resource._resource['spam'])

    def test_cannot__getittem__(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])
        with self.assertRaises(Exception):
            client()[0]

    def test_cannot_iterate(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])
        with self.assertRaises(Exception):
            for item in client():
                pass

    def test_dir_call_returns_executor_methods(self):
        client = self.wrapper._wrap_in_tapi([0, 1, 2])

        e_dir = dir(client())

        self.assertIn('data', e_dir)
        self.assertIn('response', e_dir)
        self.assertIn('get', e_dir)
        self.assertIn('post', e_dir)
        self.assertIn('pages', e_dir)
        self.assertIn('open_docs', e_dir)
        self.assertIn('open_in_browser', e_dir)

    @responses.activate
    def test_response_executor_object_has_a_response(self):
        next_url = 'http://api.teste.com/next_batch'

        responses.add(
            responses.GET,
            self.wrapper.test().data,
            body='{"data": [{"key": "value"}], "paging": {"next": "%s"}}' %
            next_url,
            status=200,
            content_type='application/json')

        responses.add(
            responses.GET,
            next_url,
            body='{"data": [{"key": "value"}], "paging": {"next": ""}}',
            status=200,
            content_type='application/json')

        response = self.wrapper.test().get()
        executor = response()

        executor.response

        executor._response = None

    def test_raises_error_if_executor_does_not_have_a_response_object(self):
        client = self.wrapper

        with self.assertRaises(Exception):
            client().response

    @responses.activate
    def test_response_executor_has_a_status_code(self):
        responses.add(responses.GET,
                      self.wrapper.test().data,
                      body='{"data": {"key": "value"}}',
                      status=200,
                      content_type='application/json')

        response = self.wrapper.test().get()

        self.assertEqual(response().status_code, 200)