コード例 #1
0
    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 == []
コード例 #2
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/')