def test_validate_failure(self): """ if param() fails validation check, raise exception """ request = DummyRequest() request.params = {'field': 'myfield'} validate = lambda x: x.startswith('foo') with self.assertRaises(HTTPBadRequest): param(request, 'field', validate=validate)
def test_timedelta_param(self): """ Pull timedelta off of request object """ request = DummyRequest() diff = 3600 request.params = {'field': diff} field = param(request, 'field', type=datetime.timedelta) self.assertEquals(field, datetime.timedelta(seconds=diff))
def test_datetime_param(self): """ Pull datetime off of request object """ request = DummyRequest() now = int(time.time()) request.params = {'field': now} field = param(request, 'field', type=datetime) self.assertEquals(calendar.timegm(field.utctimetuple()), now)
def test_validate(self): """ param() can run a validation check on parameter values """ request = DummyRequest() request.params = {'field': 'foobar'} validate = lambda x: x.startswith('foo') field = param(request, 'field', validate=validate) self.assertEquals(field, 'foobar')
def test_unicode_param(self): """ Pull unicode params off of request object """ request = DummyRequest() request.params = {'field': 'myfield'} field = param(request, 'field') self.assertEquals(field, 'myfield') self.assertTrue(isinstance(field, six.text_type))
def test_unicode_param_explicit(self): """ Specifying type=unicode checks arg type before returning it """ request = DummyRequest() request.params = {'field': 'myfield'} field = param(request, 'field', type=six.text_type) self.assertEquals(field, 'myfield') self.assertTrue(isinstance(field, six.text_type))
def test_bytes_param(self): """ Pull binary string param off of request object """ request = DummyRequest() request.params = {'field': 'myfield'} field = param(request, 'field', type=bytes) self.assertEquals(field, b'myfield') self.assertTrue(isinstance(field, six.binary_type))
def test_bool_json_body(self): """ Pull bool params out of json body """ request = DummyRequest() request.params = {} request.headers = {'Content-Type': 'application/json'} request.json_body = {'field': True} field = param(request, 'field', type=bool) self.assertTrue(field is True)
def test_set_json_body(self): """ Pull set params out of json body """ request = DummyRequest() request.params = {} request.json_body = {'field': set(['a', 'b'])} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field', type=set) self.assertEquals(field, set(['a', 'b']))
def test_missing_param(self): """ Raise HTTPBadRequest if param is missing """ request = DummyRequest() request.params = {} request.json_body = {} myvar = object() field = param(request, 'field', default=myvar) self.assertTrue(field is myvar)
def test_dict_json_body(self): """ Pull dict params out of json body """ request = DummyRequest() request.params = {} request.json_body = {'field': {'a': 'b'}} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field', type=dict) self.assertEquals(field, {'a': 'b'})
def test_list_json_body(self): """ Pull list params out of json body """ request = DummyRequest() request.params = {} request.json_body = {'field': [1, 2, 3]} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field', type=list) self.assertEquals(field, [1, 2, 3])
def test_date_param_ts(self): """ Pull date off of request object as unix ts """ request = DummyRequest() now_date = datetime.date(2014, 1, 1) now_ts = time.mktime(now_date.timetuple()) request.params = {'field': now_ts} field = param(request, 'field', type=datetime.date) self.assertEquals(field, now_date)
def test_datetime_json_body(self): """ Pull datetime params out of json body """ request = DummyRequest() request.params = {} now = int(time.time()) request.json_body = {'field': now} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field', type=datetime) self.assertEquals(calendar.timegm(field.utctimetuple()), now)
def test_str_json_body(self): """ Pull str params out of json body """ request = DummyRequest() request.params = {} request.json_body = {'field': 'myfield'} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field', type=bytes) self.assertEquals(field, b'myfield') self.assertTrue(isinstance(field, six.binary_type))
def test_object_param_simple(self): """ Hydrate an object from json data directly from constructor """ request = DummyRequest() data = {'alpha': 'a', 'beta': 'b'} request.params = {'field': json.dumps(data)} field = param(request, 'field', type=SimpleParamContainer) self.assertTrue(isinstance(field, SimpleParamContainer)) self.assertEqual(field.alpha, data['alpha']) self.assertEqual(field.beta, data['beta'])
def test_unicode_json_body(self): """ Pull unicode params out of json body """ request = DummyRequest() request.params = {} request.json_body = {'field': 'myfield'} request.headers = {'Content-Type': 'application/json'} field = param(request, 'field') self.assertEquals(field, 'myfield') self.assertTrue(isinstance(field, six.text_type))
def test_object_param_static(self): """ Hydrate an object from json data using static method """ request = DummyRequest() data = {'alpha': 'a', 'beta': 'b'} request.params = {'field': json.dumps(data)} field = param(request, 'field', type=ParamContainerStatic) self.assertTrue(isinstance(field, ParamContainerStatic)) self.assertEqual(field.alpha, data['alpha']) self.assertEqual(field.beta, data['beta'])
def test_object_param_path(self): """ Type parameter can be a dotted path """ request = DummyRequest() data = {'alpha': 'a', 'beta': 'b'} request.params = {'field': json.dumps(data)} field = param(request, 'field', type='tests.test_params.ParamContainerFancy') self.assertTrue(isinstance(field, ParamContainerFancy)) self.assertEqual(field.alpha, data['alpha']) self.assertEqual(field.beta, data['beta']) self.assertEqual(field.request, request)
def test_unicode_param_bad_type(self): """ Raise exception if unicode param as incorrect type """ request = DummyRequest() request.params = {'field': 4} with self.assertRaises(HTTPBadRequest): param(request, 'field', type=six.text_type)
def test_int_param(self): """ Pull integer off of request object """ request = DummyRequest() request.params = {'field': '1'} field = param(request, 'field', type=int) self.assertEquals(field, 1)
def test_list_param(self): """ Pull encoded lists off of request object """ request = DummyRequest() request.params = {'field': json.dumps([1, 2, 3])} field = param(request, 'field', type=list) self.assertEquals(field, [1, 2, 3])
def test_dict_param(self): """ Pull encoded dicts off of request object """ request = DummyRequest() request.params = {'field': json.dumps({'a': 'b'})} field = param(request, 'field', type=dict) self.assertEquals(field, {'a': 'b'})
def test_dict_param_bad_type(self): """ If dict param isn't a dict, raise exception """ request = DummyRequest() request.params = {'field': json.dumps(['a', 'b'])} with self.assertRaises(HTTPBadRequest): param(request, 'field', type=dict)
def test_bad_format(self): """ Raise exception if any error during type conversion """ request = DummyRequest() request.params = {'field': 'abc'} with self.assertRaises(HTTPBadRequest): param(request, 'field', type=int)
def test_set_param(self): """ Pull encoded sets off of request object """ request = DummyRequest() request.params = {'field': json.dumps(['a', 'b'])} field = param(request, 'field', type=set) self.assertEquals(field, set(['a', 'b']))
def test_date_param(self): """ Pull date off of request object as YYYY-mm-dd """ request = DummyRequest() request.params = {'field': '2014-1-1'} field = param(request, 'field', type=datetime.date) self.assertEquals(field, datetime.date(2014, 1, 1))
def test_bool_param(self): """ Pull bool off of request object """ request = DummyRequest() request.params = {'field': 'true'} field = param(request, 'field', type=bool) self.assertTrue(field is True)