class TestTapiocaClient(unittest.TestCase):

    def setUp(self):
        self.wrapper = TesterClient()

    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_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapioca([0,1,2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items_should_be_tapioca_instances(self):
        client = self.wrapper._wrap_in_tapioca([0,1,2])

        for item in client:
            self.assertTrue(isinstance(item, TapiocaClient))

    def test_iterated_client_items_should_contain_list_items(self):
        client = self.wrapper._wrap_in_tapioca([0,1,2])

        for i, item in enumerate(client):
            self.assertEqual(item().data(), i)
Esempio n. 2
0
class TestTapiocaClient(unittest.TestCase):
    def setUp(self):
        self.wrapper = TesterClient()

    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_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items_should_be_tapioca_instances(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

        for item in client:
            self.assertTrue(isinstance(item, TapiocaClient))

    def test_iterated_client_items_should_contain_list_items(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

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

    @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_trasnform_camelCase_in_snake_case(self):
        next_url = 'http://api.teste.com/next_batch'

        responses.add(
            responses.GET,
            self.wrapper.test().data,
            body=
            '{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}'
            % next_url,
            status=200,
            content_type='application/json')

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

        self.assertEqual(response.data.key_snake().data, 'value')
        self.assertEqual(response.data.camel_case().data, 'data in camel case')
        self.assertEqual(response.data.normal_camel_case().data,
                         'data in camel case')
class TestTapiocaClient(unittest.TestCase):

    def setUp(self):
        self.wrapper = TesterClient()

    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_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items_should_be_tapioca_instances(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

        for item in client:
            self.assertTrue(isinstance(item, TapiocaClient))

    def test_iterated_client_items_should_contain_list_items(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

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

    @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_trasnform_camelCase_in_snake_case(self):
        next_url = 'http://api.teste.com/next_batch'

        responses.add(responses.GET, self.wrapper.test().data,
                      body='{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}' % next_url,
                      status=200,
                      content_type='application/json')


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

        self.assertEqual(response.data.key_snake().data, 'value')
        self.assertEqual(response.data.camel_case().data, 'data in camel case')
        self.assertEqual(response.data.normal_camel_case().data, 'data in camel case')
Esempio n. 4
0
class TestTapiocaClient(unittest.TestCase):
    def setUp(self):
        self.wrapper = TesterClient()

    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_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items_should_be_tapioca_instances(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

        for item in client:
            self.assertTrue(isinstance(item, TapiocaClient))

    def test_iterated_client_items_should_contain_list_items(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

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

    @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_transform_camelCase_in_snake_case(self):
        next_url = 'http://api.teste.com/next_batch'

        responses.add(
            responses.GET,
            self.wrapper.test().data,
            body=
            '{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}'
            % next_url,
            status=200,
            content_type='application/json')

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

        self.assertEqual(response.data.key_snake().data, 'value')
        self.assertEqual(response.data.camel_case().data, 'data in camel case')
        self.assertEqual(response.data.normal_camel_case().data,
                         'data in camel case')

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

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

        self.assertEqual(response[0]().data, 'a')
        self.assertEqual(response[1]().data, 'b')
        self.assertEqual(response[2]().data, 'c')

    @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/')
Esempio n. 5
0
 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/')
Esempio n. 6
0
class TestTapiocaClient(unittest.TestCase):
    def setUp(self):
        self.wrapper = TesterClient()

    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_calling_len_on_tapioca_list(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        self.assertEqual(len(client), 3)

    def test_iterated_client_items_should_be_tapioca_instances(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

        for item in client:
            self.assertTrue(isinstance(item, TapiocaClient))

    def test_iterated_client_items_should_contain_list_items(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])

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

    @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_transform_camelCase_in_snake_case(self):
        next_url = "http://api.teste.com/next_batch"

        responses.add(
            responses.GET,
            self.wrapper.test().data,
            body='{"data" :{"key_snake": "value", "camelCase": "data in camel case", "NormalCamelCase": "data in camel case"}, "paging": {"next": "%s"}}'
            % next_url,
            status=200,
            content_type="application/json",
        )

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

        self.assertEqual(response.data.key_snake().data, "value")
        self.assertEqual(response.data.camel_case().data, "data in camel case")
        self.assertEqual(response.data.normal_camel_case().data, "data in camel case")

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

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

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

        self.assertEqual(response[0]().data, "a")
        self.assertEqual(response[1]().data, "b")
        self.assertEqual(response[2]().data, "c")

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

        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):
        next_url = "http://api.teste.com/next_batch"

        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]
Esempio n. 7
0
 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/')
Esempio n. 8
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/')