class TestModel(pyperry.Base): id = Field() foo = Field() adapter_conf = {'service': 'test_models', 'host': 'localhost:8888'} reader = RestfulHttpAdapter(adapter_conf) writer = RestfulHttpAdapter(adapter_conf)
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)
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)
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")
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)
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)
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')
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)
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')
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')
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")
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_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)
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)
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)
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)
def setUp(self): self.config = { 'host': 'localhost:8888', 'service': 'widgets', 'format': 'xml' } self.adapter = RestfulHttpAdapter(self.config) self.model = TestModel({'id': 7})
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)
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())
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})
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')
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})
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)
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")
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())
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())
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_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', {})
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 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))