コード例 #1
0
class TestModel(pyperry.Base):
    id = Field()
    foo = Field()

    adapter_conf = {'service': 'test_models', 'host': 'localhost:8888'}
    reader = RestfulHttpAdapter(adapter_conf)
    writer = RestfulHttpAdapter(adapter_conf)
コード例 #2
0
    def test_dont_modify_default_params(self):
        """should not modify the default_params when building params"""
        self.config['default_params'] = {'foo':'bar'}
        expected = copy(self.config['default_params'])

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(adapter.config['default_params'], expected)
コード例 #3
0
    def test_dont_modify_default_params(self):
        """should not modify the default_params when building params"""
        self.config['default_params'] = {'foo': 'bar'}
        expected = copy(self.config['default_params'])

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(adapter.config['default_params'], expected)
コード例 #4
0
 def test_primary_key(self):
     """should use configured primary_key in url"""
     TestModel.attributes("id", "foo")
     model = TestModel({"id": 7, "foo": 12345})
     self.config["primary_key"] = "foo"
     adapter = RestfulHttpAdapter(self.config, mode="read")
     url = adapter.url_for("GET", model)
     self.assertEqual(url, "/widgets/12345.xml")
コード例 #5
0
    def test_dont_modify_default_params(self):
        """should not modify the default_params when building params"""
        self.config["default_params"] = {"foo": "bar"}
        expected = copy(self.config["default_params"])

        adapter = RestfulHttpAdapter(self.config, mode="read")
        params = adapter.params_for(self.model)
        self.assertEqual(adapter.config.default_params, expected)
コード例 #6
0
    def test_with_default_params(self):
        """should include the default_options with the attribuets"""
        self.config['default_params'] = {'foo': 'bar'}
        expected = copy(self.model.fields)
        expected.update({'foo':'bar'})

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #7
0
    def test_custom_serializer(self):
        custom_serializer = lambda value: 'foo' if value == 3 else 'bar'
        self.config['serializer'] = custom_serializer
        adapter = RestfulHttpAdapter(self.config)

        value = adapter.params_value('fudge')
        self.assertEqual(value, 'bar')
        value = adapter.params_value(3)
        self.assertEqual(value, 'foo')
コード例 #8
0
    def test_with_default_params(self):
        """should include the default_options with the attribuets"""
        self.config['default_params'] = {'foo': 'bar'}
        expected = copy(self.model.fields)
        expected.update({'foo': 'bar'})

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #9
0
    def test_custom_serializer(self):
        custom_serializer = lambda value: 'foo' if value == 3 else 'bar'
        self.config['serializer'] = custom_serializer
        adapter = RestfulHttpAdapter(self.config)

        value = adapter.params_value('fudge')
        self.assertEqual(value, 'bar')
        value = adapter.params_value(3)
        self.assertEqual(value, 'foo')
コード例 #10
0
 def test_primary_key(self):
     """should use configured primary_key in url"""
     TestModel.id = Field()
     TestModel.foo = Field()
     model = TestModel({'id': 7, 'foo': 12345})
     self.config['primary_key'] = 'foo'
     adapter = RestfulHttpAdapter(self.config)
     url = adapter.url_for('GET', model)
     self.assertEqual(url, '/widgets/12345.xml')
コード例 #11
0
 def test_primary_key(self):
     """should use configured primary_key in url"""
     TestModel.id = Field()
     TestModel.foo = Field()
     model = TestModel({'id':7, 'foo':12345})
     self.config['primary_key'] = 'foo'
     adapter = RestfulHttpAdapter(self.config)
     url = adapter.url_for('GET', model)
     self.assertEqual(url, '/widgets/12345.xml')
コード例 #12
0
    def test_custom_serializer(self):
        custom_serializer = lambda value: "foo" if value == 3 else "bar"
        self.config["serializer"] = custom_serializer
        adapter = RestfulHttpAdapter(self.config, mode="read")

        value = adapter.params_value("fudge")
        self.assertEqual(value, "bar")
        value = adapter.params_value(3)
        self.assertEqual(value, "foo")
コード例 #13
0
    def test_dont_modify_default_params(self):
        """should not modify the default_params when building a query string"""
        r = pyperry.Base.scoped().where({'id': 3}).limit(1)
        self.config['default_params'] = {'foo': 'bar'}
        expected = copy(self.config['default_params'])
        adapter = RestfulHttpAdapter(self.config)
        adapter.read(relation=r)

        self.assertEqual(adapter.config['default_params'], expected)
コード例 #14
0
    def test_with_default_params(self):
        """should include the default_options with the attribuets"""
        self.config["default_params"] = {"foo": "bar"}
        expected = copy(self.model.attributes)
        expected.update({"foo": "bar"})

        adapter = RestfulHttpAdapter(self.config, mode="read")
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #15
0
    def test_with_default_params_and_params_wrapper(self):
        """should include the attributes inside the wrapper and the default
        params outside the wrapper"""
        self.config['default_params'] = {'foo': 'bar', 'widget': 5}
        self.config['params_wrapper'] = 'widget'
        expected = copy(self.config['default_params'])
        expected.update(copy({'widget':self.model.fields}))

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #16
0
    def test_with_default_params_and_params_wrapper(self):
        """should include the attributes inside the wrapper and the default
        params outside the wrapper"""
        self.config['default_params'] = {'foo': 'bar', 'widget': 5}
        self.config['params_wrapper'] = 'widget'
        expected = copy(self.config['default_params'])
        expected.update(copy({'widget': self.model.fields}))

        adapter = RestfulHttpAdapter(self.config)
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #17
0
    def test_with_default_params_and_params_wrapper(self):
        """should include the attributes inside the wrapper and the default
        params outside the wrapper"""
        self.config["default_params"] = {"foo": "bar", "widget": 5}
        self.config["params_wrapper"] = "widget"
        expected = copy(self.config["default_params"])
        expected.update(copy({"widget": self.model.attributes}))

        adapter = RestfulHttpAdapter(self.config, mode="read")
        params = adapter.params_for(self.model)
        self.assertEqual(params, expected)
コード例 #18
0
 def setUp(self):
     self.config = {
         'host': 'localhost:8888',
         'service': 'widgets',
         'format': 'xml'
     }
     self.adapter = RestfulHttpAdapter(self.config)
     self.model = TestModel({'id': 7})
コード例 #19
0
    def test_default_params_in_query(self):
        """should include the default_params in the query string"""
        r = pyperry.Base.scoped().where({'id': 3}).limit(1)
        self.config['default_params'] = {'foo': 'bar'}
        adapter = RestfulHttpAdapter(self.config)
        adapter.read(relation=r)

        expected = 'where[][id]=3&limit=1&foo=bar'
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)
コード例 #20
0
class ReadTestCase(HttpAdapterTestCase):
    def setUp(self):
        self.config = {'host': 'localhost:8888', 'service': 'foo'}
        self.adapter = RestfulHttpAdapter(self.config)
        self.records = [{'id': 1}, {'id': 2}, {'id': 3}]
        http_server.set_response(body=json.dumps(self.records))

    def test_request(self):
        """should make an HTTP GET request"""
        self.adapter.read(relation=pyperry.Base.scoped())
        last_request = http_server.last_request()
        self.assertEqual(last_request['method'], 'GET')
        self.assertEqual(last_request['headers']['accept'], 'application/json')
        self.assertEqual(last_request['path'], '/foo.json')

    def test_relation_in_query(self):
        """should encode the relation dict in the query string"""
        B = pyperry.Base
        relation = B.select('id', 'foo', 'bar').where({'id': 6}).limit(1)
        expected = "where[][id]=6&select[]=id&select[]=foo&select[]=bar&limit=1"
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        self.adapter.read(relation=relation)
        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)

    def test_modifiers_in_query(self):
        """should include query modifiers in the query string"""
        r = pyperry.Base.scoped().modifiers({'query': {'foo': 'bar'}})
        self.adapter.read(relation=r)
        last_request = http_server.last_request()
        self.assertEqual(last_request['path'], '/foo.json?foo=bar')

    def test_modifiers_and_relation(self):
        """
        should include the relation and query modifiers in the query string
        """
        r = pyperry.Base.scoped().where({
            'id': 3
        }).limit(1).modifiers({'query': {
            'foo': {
                'bar': 'baz'
            }
        }})
        self.adapter.read(relation=r)

        expected = 'where[][id]=3&limit=1&foo[bar]=baz'
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)

    def test_records(self):
        """should return a list of records retrieved from the response"""
        result = self.adapter.read(relation=pyperry.Base.scoped())
        self.assertEqual(result, self.records)

    def test_raise_if_not_list(self):
        """should raise if parsed response is not a list of records"""
        http_server.set_response(body=json.dumps({}))
        self.assertRaises(errors.MalformedResponse,
                          self.adapter.read,
                          relation=pyperry.Base.scoped())
コード例 #21
0
 def test_with_wrapper(self):
     """should wrap the model's attributes with the given string"""
     self.config["params_wrapper"] = "widget"
     adapter = RestfulHttpAdapter(self.config, mode="read")
     params = adapter.params_for(self.model)
     self.assertEqual(params, {"widget": self.model.attributes})
コード例 #22
0
 def test_missing_format(self):
     """should use 'json' as the default format"""
     del self.config['format']
     adapter = RestfulHttpAdapter(self.config)
     url = adapter.url_for('GET', self.model)
     self.assertEqual(url, '/widgets/7.json')
コード例 #23
0
 def test_with_wrapper(self):
     """should wrap the model's attributes with the given string"""
     self.config['params_wrapper'] = 'widget'
     adapter = RestfulHttpAdapter(self.config)
     params = adapter.params_for(self.model)
     self.assertEqual(params, {'widget': self.model.fields})
コード例 #24
0
 def test_missing_format(self):
     """should use 'json' as the default format"""
     del self.config['format']
     adapter = RestfulHttpAdapter(self.config)
     url = adapter.url_for('GET', self.model)
     self.assertEqual(url, '/widgets/7.json')
コード例 #25
0
 def test_missing_service(self):
     """should raise on missing service"""
     del self.config['service']
     adapter = RestfulHttpAdapter(self.config)
     self.assertRaises(errors.ConfigurationError, adapter.url_for, 'GET',
                       self.model)
コード例 #26
0
 def test_missing_format(self):
     """should use 'json' as the default format"""
     del self.config["format"]
     adapter = RestfulHttpAdapter(self.config, mode="read")
     url = adapter.url_for("GET", self.model)
     self.assertEqual(url, "/widgets/7.json")
コード例 #27
0
class ReadTestCase(HttpAdapterTestCase):
    def setUp(self):
        self.config = {"host": "localhost:8888", "service": "foo"}
        self.adapter = RestfulHttpAdapter(self.config, mode="read")
        self.records = [{"id": 1}, {"id": 2}, {"id": 3}]
        http_server.set_response(body=json.dumps(self.records))

    def test_request(self):
        """should make an HTTP GET request"""
        self.adapter.read(relation=pyperry.Base.scoped())
        last_request = http_server.last_request()
        self.assertEqual(last_request["method"], "GET")
        self.assertEqual(last_request["headers"]["accept"], "application/json")
        self.assertEqual(last_request["path"], "/foo.json")

    def test_relation_in_query(self):
        """should encode the relation dict in the query string"""
        B = pyperry.Base
        relation = B.select("id", "foo", "bar").where({"id": 6}).limit(1)
        expected = "where[][id]=6&select[]=id&select[]=foo&select[]=bar&limit=1"
        expected = expected.replace("[", "%5B").replace("]", "%5D")
        expected = expected.split("&")
        expected.sort()

        self.adapter.read(relation=relation)
        last_request = http_server.last_request()
        query = last_request["path"].split("?")[1]
        query = query.split("&")
        query.sort()

        self.assertEqual(query, expected)

    def test_modifiers_in_query(self):
        """should include query modifiers in the query string"""
        r = pyperry.Base.scoped().modifiers({"query": {"foo": "bar"}})
        self.adapter.read(relation=r)
        last_request = http_server.last_request()
        self.assertEqual(last_request["path"], "/foo.json?foo=bar")

    def test_modifiers_and_relation(self):
        """
        should include the relation and query modifiers in the query string
        """
        r = pyperry.Base.scoped().where({"id": 3}).limit(1).modifiers({"query": {"foo": {"bar": "baz"}}})
        self.adapter.read(relation=r)

        expected = "where[][id]=3&limit=1&foo[bar]=baz"
        expected = expected.replace("[", "%5B").replace("]", "%5D")
        expected = expected.split("&")
        expected.sort()

        last_request = http_server.last_request()
        query = last_request["path"].split("?")[1]
        query = query.split("&")
        query.sort()

        self.assertEqual(query, expected)

    def test_records(self):
        """should return a list of records retrieved from the response"""
        result = self.adapter.read(relation=pyperry.Base.scoped())
        self.assertEqual(result, self.records)

    def test_raise_if_not_list(self):
        """should raise if parsed response is not a list of records"""
        http_server.set_response(body=json.dumps({}))
        self.assertRaises(errors.MalformedResponse, self.adapter.read, relation=pyperry.Base.scoped())
コード例 #28
0
class ReadTestCase(HttpAdapterTestCase):

    def setUp(self):
        self.config = { 'host': 'localhost:8888', 'service': 'foo' }
        self.adapter = RestfulHttpAdapter(self.config)
        self.records = [{'id': 1}, {'id': 2}, {'id': 3}]
        http_server.set_response(body=json.dumps(self.records))

    def test_request(self):
        """should make an HTTP GET request"""
        self.adapter.read(relation=pyperry.Base.scoped())
        last_request = http_server.last_request()
        self.assertEqual(last_request['method'], 'GET')
        self.assertEqual(last_request['headers']['accept'], 'application/json')
        self.assertEqual(last_request['path'], '/foo.json')

    def test_relation_in_query(self):
        """should encode the relation dict in the query string"""
        B = pyperry.Base
        relation = B.select('id', 'foo', 'bar').where({'id':6}).limit(1)
        expected = "where[][id]=6&select[]=id&select[]=foo&select[]=bar&limit=1"
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        self.adapter.read(relation=relation)
        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)

    def test_modifiers_in_query(self):
        """should include query modifiers in the query string"""
        r = pyperry.Base.scoped().modifiers({
                'query': {'foo': 'bar'}
            })
        self.adapter.read(relation=r)
        last_request = http_server.last_request()
        self.assertEqual(last_request['path'], '/foo.json?foo=bar')

    def test_modifiers_and_relation(self):
        """
        should include the relation and query modifiers in the query string
        """
        r = pyperry.Base.scoped().where({'id': 3}).limit(1).modifiers({
                'query': {'foo': {'bar': 'baz'}}
            })
        self.adapter.read(relation=r)

        expected = 'where[][id]=3&limit=1&foo[bar]=baz'
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)

    def test_default_params_in_query(self):
        """should include the default_params in the query string"""
        r = pyperry.Base.scoped().where({'id': 3}).limit(1)
        self.config['default_params'] = {'foo': 'bar'}
        adapter = RestfulHttpAdapter(self.config)
        adapter.read(relation=r)

        expected = 'where[][id]=3&limit=1&foo=bar'
        expected = expected.replace('[', '%5B').replace(']', '%5D')
        expected = expected.split('&')
        expected.sort()

        last_request = http_server.last_request()
        query = last_request['path'].split('?')[1]
        query = query.split('&')
        query.sort()

        self.assertEqual(query, expected)

    def test_dont_modify_default_params(self):
        """should not modify the default_params when building a query string"""
        r = pyperry.Base.scoped().where({'id': 3}).limit(1)
        self.config['default_params'] = {'foo': 'bar'}
        expected = copy(self.config['default_params'])
        adapter = RestfulHttpAdapter(self.config)
        adapter.read(relation=r)

        self.assertEqual(adapter.config['default_params'], expected)

    def test_records(self):
        """should return a list of records retrieved from the response"""
        result = self.adapter.read(relation=pyperry.Base.scoped())
        self.assertEqual(result, self.records)

    def test_raise_if_not_list(self):
        """should raise if parsed response is not a list of records"""
        http_server.set_response(body=json.dumps({}))
        self.assertRaises(errors.MalformedResponse, self.adapter.read,
                          relation=pyperry.Base.scoped())
コード例 #29
0
 def test_with_wrapper(self):
     """should wrap the model's attributes with the given string"""
     self.config['params_wrapper'] = 'widget'
     adapter = RestfulHttpAdapter(self.config)
     params = adapter.params_for(self.model)
     self.assertEqual(params, {'widget': self.model.fields})
コード例 #30
0
 def setUp(self):
     self.config = {"host": "localhost:8888", "service": "foo"}
     self.adapter = RestfulHttpAdapter(self.config, mode="read")
     self.records = [{"id": 1}, {"id": 2}, {"id": 3}]
     http_server.set_response(body=json.dumps(self.records))
コード例 #31
0
 def test_missing_host(self):
     """should raise if host is not configured"""
     del self.config['host']
     adapter = RestfulHttpAdapter(self.config)
     self.assertRaises(errors.ConfigurationError, adapter.http_request,
                       'GET', 'foo', {})
コード例 #32
0
 def setUp(self):
     self.config = {'host': 'localhost:8888', 'service': 'foo'}
     self.adapter = RestfulHttpAdapter(self.config)
     self.records = [{'id': 1}, {'id': 2}, {'id': 3}]
     http_server.set_response(body=json.dumps(self.records))
コード例 #33
0
 def setUp(self):
     self.config = { 'host': 'localhost:8888', 'service': 'foo' }
     self.adapter = RestfulHttpAdapter(self.config)
     self.records = [{'id': 1}, {'id': 2}, {'id': 3}]
     http_server.set_response(body=json.dumps(self.records))