def test_get_not_found(self): """ Tests the get with a default value """ r = RequestContainer() self.assertEqual(r.get('fake', 'hey'), 'hey') self.assertEqual(r.get('fak'), None)
def test_content_type(self): content_type = 'for real;' headers = {'Content-Type': content_type} r = RequestContainer(headers=headers) self.assertEqual(content_type, r.content_type) r = RequestContainer() self.assertIsNone(r.content_type) # set the content type r.content_type = 'blah' self.assertEqual(r.content_type, 'blah')
def dict_helper(self, name): d = dict(some='object') r = RequestContainer(**{name: d}) self.assertDictEqual(d, getattr(r, name)) self.assertNotEqual(id(d), id(getattr(r, name))) # Test setting the dict d2 = dict(another='object') setattr(r, name, d2) self.assertNotEqual(d, getattr(r, name)) self.assertDictEqual(d2, getattr(r, name)) self.assertNotEqual(id(d2), id(getattr(r, name))) # Test empty dict r = RequestContainer() self.assertIsInstance(getattr(r, name), dict)
def test_set_key_error(self): """ Asserts that set raises a key error when the value cannot be found. """ req = RequestContainer(query_args=dict(x=1)) self.assertRaises(KeyError, req.set, 'blah', 'blah')
def setUp(self): HelloWorldViewset = get_refreshed_helloworld_viewset() self.properties = {'content': 'hello'} self.resource = HelloWorldViewset.hello( RequestContainer( query_args=dict(content='hello', related='world'))) self.adapter = BasicJSONAdapter(self.resource) self.data = json.loads(self.adapter.formatted_body)
def setUp(self): HelloWorldViewset = get_refreshed_helloworld_viewset() self.resource = HelloWorldViewset.hello( RequestContainer(query_args={ 'content': 'hello', 'related': 'world' })) self.adapter = SirenAdapter(self.resource, base_url='http://localhost:') self.data = json.loads(self.adapter.formatted_body)
def setUp(self): ResourceMetaClass.registered_names_map.clear() ResourceMetaClass.registered_resource_classes.clear() HelloWorldViewset = get_refreshed_helloworld_viewset() self.properties = {'content': 'hello'} self.resource = HelloWorldViewset.hello(RequestContainer(query_args=dict(content='hello', related='world'))) self.adapter = HalAdapter(self.resource) self.data = json.loads(self.adapter.formatted_body)
def test_set(self): """ Tests the basic set on the request without location specified """ original_dict = dict(x=1) req = RequestContainer(query_args=original_dict) req.set('x', 2) self.assertDictEqual(dict(x=2), req.query_args) req = RequestContainer(url_params=original_dict) req.set('x', 2) self.assertDictEqual(dict(x=2), req.url_params) req = RequestContainer(body_args=original_dict) req.set('x', 2) self.assertDictEqual(dict(x=2), req.body_args)
def test_set_location_specified(self): """ Tests the set on the request container with the location specified. """ original_dict = dict(x=1) req = RequestContainer(query_args=original_dict) req.set('x', 2, QUERY_ARGS) self.assertDictEqual(dict(x=2), req.query_args) req = RequestContainer(url_params=original_dict) req.set('x', 2, URL_PARAMS) self.assertDictEqual(dict(x=2), req.url_params) req = RequestContainer(body_args=original_dict) req.set('x', 2, BODY_ARGS) self.assertDictEqual(dict(x=2), req.body_args)
def test_translate_failure(self): """ Tests whether the translate decorator appropriately calls translate when validate=False """ class TranslateClass(ResourceBase): @apimethod(methods=['GET']) @translate(fields=[IntegerField('id', required=True)], validate=False) def hey(cls, req): return req = RequestContainer(body_args=dict(id='notvalid')) self.assertRaises(TranslationException, TranslateClass.hey, req)
def test_translate_success(self): """ Tests that the translate decorator will succesfully translate a parameter """ class TranslateClass2(ResourceBase): @apimethod(methods=['GET']) @translate(fields=[IntegerField('id', required=True)], validate=False) def hey(cls, req): return req.body_args.get('id') req = RequestContainer(body_args=dict(id='10')) id = TranslateClass2.hey(req) self.assertIsInstance(id, int)
def help_test_validate_with_manager_field_validators(self, klass): """ Helper for the couple test_validate_with_manager_field_validators. Just checks that it gets the right responses. Abstracted out poorly to reduce code duplication and because I hate typing. :param type klass: The class that you are checking. """ request = RequestContainer(body_args=dict(first=[1], second=[2])) response = klass.hello(request) self.assertDictEqual(dict(first=1, second=2), response.properties) self.assertDictEqual(request.body_args, response.properties) # Without list inputs since requests are mutable response = klass.hello(request) self.assertDictEqual(dict(first=1, second=2), response.properties) self.assertDictEqual(request.body_args, response.properties) request2 = RequestContainer(body_args=dict(second=[2])) self.assertRaises(ValidationException, klass.hello, request2) request3 = RequestContainer(body_args=dict(first=[1])) response = klass.hello(request3) self.assertDictEqual(dict(first=1), response.properties)
def test_set_location_specified_new(self): """ Tests the set on the request container when the key is not already available and the location is specified. """ original_dict = dict(x=1) req = RequestContainer(url_params=original_dict) req.set('x', 2, QUERY_ARGS) self.assertDictEqual(dict(x=2), req.query_args) self.assertDictEqual(dict(x=1), req.url_params) req = RequestContainer(query_args=original_dict) req.set('x', 2, URL_PARAMS) self.assertDictEqual(dict(x=2), req.url_params) self.assertDictEqual(dict(x=1), req.query_args) req = RequestContainer(url_params=original_dict) req.set('x', 2, BODY_ARGS) self.assertDictEqual(dict(x=2), req.body_args) self.assertDictEqual(dict(x=1), req.url_params)
def test_get(self): """ Tests that the get method appropriately retrieves paramaters. """ r = RequestContainer(url_params=dict(key=1, key2=2)) self.assertEqual(r.get('key'), 1) self.assertEqual(r.get('key2'), 2) r = RequestContainer(query_args=dict(key=1, key2=2)) self.assertEqual(r.get('key'), 1) self.assertEqual(r.get('key2'), 2) r = RequestContainer(body_args=dict(key=1, key2=2)) self.assertEqual(r.get('key'), 1) self.assertEqual(r.get('key2'), 2) r = RequestContainer(url_params=dict(key=1), query_args=dict(key=2), body_args=dict(key=3)) self.assertEqual(r.get('key'), 1)
def test_format_request(self): """Dumb test for format_request""" request = RequestContainer() response = SirenAdapter.format_request(request) self.assertIs(response, request)