コード例 #1
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)
コード例 #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')
コード例 #3
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')
コード例 #4
0
class TestTapiocaExecutor(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_access_data_attributres_through_executor(self):
        client = self.wrapper._wrap_in_tapioca({'test': 'value'})

        items = client().items()

        self.assertTrue(isinstance(items, TapiocaClient))

        data = dict(items().data())

        self.assertEqual(data, {'test': 'value'})

    def test_is_possible_to_reverse_a_list_through_executor(self):
        client = self.wrapper._wrap_in_tapioca([0,1,2])
        client().reverse()
        self.assertEqual(client().data(), [2,1,0])

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

    def test_cannot_iterate(self):
        client = self.wrapper._wrap_in_tapioca([0,1,2])
        with self.assertRaises(Exception):
            for item in client():
                pass
コード例 #5
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/')
コード例 #6
0
class TestTapiocaExecutor(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_access_data_attributres_through_executor(self):
        client = self.wrapper._wrap_in_tapioca({'test': 'value'})

        items = client().items()

        self.assertTrue(isinstance(items, TapiocaClient))

        data = dict(items().data)

        self.assertEqual(data, {'test': 'value'})

    def test_is_possible_to_reverse_a_list_through_executor(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        client().reverse()
        self.assertEqual(client().data, [2, 1, 0])

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

    def test_cannot_iterate(self):
        client = self.wrapper._wrap_in_tapioca([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_tapioca([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)
コード例 #7
0
class TestTapiocaExecutor(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_access_data_attributres_through_executor(self):
        client = self.wrapper._wrap_in_tapioca({'test': 'value'})

        items = client().items()

        self.assertTrue(isinstance(items, TapiocaClient))

        data = dict(items().data)

        self.assertEqual(data, {'test': 'value'})

    def test_is_possible_to_reverse_a_list_through_executor(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        client().reverse()
        self.assertEqual(client().data, [2, 1, 0])

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

    def test_cannot_iterate(self):
        client = self.wrapper._wrap_in_tapioca([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_tapioca([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)
コード例 #8
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]
コード例 #9
0
class TestTapiocaExecutor(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_access_data_attributres_through_executor(self):
        client = self.wrapper._wrap_in_tapioca({"test": "value"})

        items = client().items()

        self.assertTrue(isinstance(items, TapiocaClient))

        data = dict(items().data)

        self.assertEqual(data, {"test": "value"})

    def test_is_possible_to_reverse_a_list_through_executor(self):
        client = self.wrapper._wrap_in_tapioca([0, 1, 2])
        client().reverse()
        self.assertEqual(client().data, [2, 1, 0])

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

    def test_cannot_iterate(self):
        client = self.wrapper._wrap_in_tapioca([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_tapioca([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)