How to use check_arguments method in Testify

Best Python code snippet using Testify_python

test_checker.py

Source:test_checker.py Github

copy

Full Screen

...3import os4import unittest5from pykit import argchecker6class TestChecker(unittest.TestCase):7 def test_check_arguments(self):8 cases = (9 (True, {'checker': {'type': 'bool'}}),10 (False, {'checker': {'type': 'bool'}}),11 (1, {'checker': {'type': 'integer'}}),12 (100, {'checker': {'type': 'integer'}}),13 (1, {'checker': {'type': 'float'}}),14 (50, {'checker': {'type': 'float'}}),15 (100.0, {'checker': {'type': 'float'}}),16 ('1', {'checker': {'type': 'string_number'}}),17 ('50', {'checker': {'type': 'string_number'}}),18 ('100.0', {'checker': {'type': 'string_number'}}),19 ('20', {'checker': {'type': 'string'}}),20 ('100.0', {'checker': {'type': 'string'}}),21 ('xxx', {'checker': {'type': 'string'}}),22 ([], {'checker': {'type': 'array'}}),23 ([1, 2], {'checker': {'type': 'array'}}),24 (['a', 'b'], {'checker': {'type': 'array'}}),25 ({'xx': {}}, {'xx': {'checker': {'type': 'dict'}}}),26 ({'xx': {'a': ''}}, {'xx': {'checker': {'type': 'dict'}}}),27 )28 for val, schema in cases:29 argchecker.check_arguments(val, schema)30 def test_required_arg(self):31 schema = {32 'x': {33 'checker': {'type': 'any'},34 'required': True35 }36 }37 with self.assertRaises(argchecker.LackArgumentError) as e:38 argchecker.check_arguments({}, schema)39 self.assertEqual(e.exception[0], 'x', "'x' is required")40 def test_undefined_arg(self):41 args = {'undefined': 0}42 schema = {'x': {'checker': {'type': 'any'}}}43 with self.assertRaises(argchecker.UnexpectedArgumentError) as e:44 argchecker.check_arguments(args, schema)45 self.assertEqual(e.exception[0], 'undefined')46 def test_float_arg(self):47 schema = {48 'num': {49 'required': True,50 'checker': {51 'type': 'float',52 'min': -100,53 'max': 100,54 'not': [0]55 }56 }57 }58 ex_cases = (59 ('1', argchecker.InvalidTypeError),60 (False, argchecker.InvalidTypeError),61 (-101, argchecker.OutOfRangeError),62 (101, argchecker.OutOfRangeError),63 (0, argchecker.InvalidValueError),64 )65 for val, err in ex_cases:66 self.assertRaises(err,67 argchecker.check_arguments,68 {'num': val},69 schema)70 cases = (71 -100,72 -50,73 -10.88,74 50,75 99.99,76 100,77 )78 for val in cases:79 argchecker.check_arguments({'num': val}, schema)80 def test_integer_arg(self):81 schema = {82 'int': {83 'checker': {84 'type': 'integer'85 }86 }87 }88 with self.assertRaises(argchecker.InvalidTypeError):89 argchecker.check_arguments({'int': 1.1}, schema)90 argchecker.check_arguments({'int': 100}, schema)91 long_num = 10 ** 2192 argchecker.check_arguments({'int': long_num}, schema)93 def test_string_number(self):94 schema = {95 'num': {96 'checker': {97 'type': 'string_number',98 'min': 0,99 'max': 10,100 'not': ['5'],101 }102 }103 }104 ex_cases = (105 (1, argchecker.InvalidTypeError),106 ('a', argchecker.InvalidTypeError),107 ('11', argchecker.OutOfRangeError),108 ('-1', argchecker.OutOfRangeError),109 ('5', argchecker.InvalidValueError),110 )111 for val, err in ex_cases:112 self.assertRaises(err,113 argchecker.check_arguments,114 {'num': val},115 schema)116 cases = (117 '10',118 '0',119 '9.99',120 '4.30',121 )122 for val in cases:123 argchecker.check_arguments({'num': val}, schema)124 def test_string_and_string_number(self):125 schema = {126 'num': {127 'checker': [128 {129 'type': 'string_number'130 },131 {132 'type': 'string',133 'enum': ['auto']134 }135 ]136 }137 }138 cases = (139 '123',140 'auto',141 )142 for val in cases:143 argchecker.check_arguments({'num': val}, schema)144 def test_fixed_len(self):145 str_schema = {146 'checker': {147 'type': 'string',148 'fixed_length': 3,149 },150 }151 array_schema = {152 'checker': {153 'type': 'array',154 'fixed_length': 3,155 },156 }157 dict_schema = {158 'xx': {159 'checker': {160 'type': 'dict',161 'fixed_length': 3,162 },163 }164 }165 cases = (166 ('abc', str_schema),167 (['a', 'b', 'abc'], array_schema),168 ({'xx': {'a': 'a', 'b': 'b', 'c': 'c'}}, dict_schema),169 )170 for v, s in cases:171 argchecker.check_arguments(v, s)172 ex_cases = (173 ('a', str_schema),174 ('ab', str_schema),175 ('abcd', str_schema),176 (['a'], array_schema),177 (['a', 'b'], array_schema),178 (['a', 'b', 'abc', 'abcd'], array_schema),179 ({'xx': {'a': ''}}, dict_schema),180 ({'xx': {'a': '', 'b': ''}}, dict_schema),181 ({'xx': {'a': '', 'b': '', 'c': '', 'd': ''}}, dict_schema),182 )183 for v, s in ex_cases:184 self.assertRaises(argchecker.InvalidLengthError,185 argchecker.check_arguments,186 v,187 s)188 def test_min_max_len(self):189 str_schema = {190 'checker': {191 'type': 'string',192 'min_length': 2,193 'max_length': 4,194 }195 }196 array_schema = {197 'checker': {198 'type': 'array',199 'min_length': 2,200 'max_length': 4,201 }202 }203 dict_schema = {204 'xx': {205 'checker': {206 'type': 'dict',207 'min_length': 2,208 'max_length': 4,209 }210 }211 }212 cases = (213 ('12', str_schema),214 ('123', str_schema),215 ('1234', str_schema),216 (['a', 'b'], array_schema),217 (['a', 'b', 'c'], array_schema),218 (['a', 'b', 'c', 'd'], array_schema),219 ({'xx': {'a': '', 'b': ''}}, dict_schema),220 ({'xx': {'a': '', 'b': '', 'c': ''}}, dict_schema),221 ({'xx': {'a': '', 'b': '', 'c': '', 'd': ''}}, dict_schema),222 )223 for v, s in cases:224 argchecker.check_arguments(v, s)225 ex_cases = (226 ('1', str_schema),227 ('12345', str_schema),228 (['a'], array_schema),229 (['a', 'b', 'c', 'd', 'e'], array_schema),230 ({'xx': {'a': ''}}, dict_schema),231 ({'xx': {'a': '', 'b': '', 'c': '', 'd': '', 'e': ''}}, dict_schema),232 )233 for v, s in ex_cases:234 self.assertRaises(argchecker.InvalidLengthError,235 argchecker.check_arguments,236 v,237 s)238 def test_str_enum(self):239 enum = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',240 'Friday', 'Saturday', 'Sunday']241 schema = {242 'week_day': {243 'checker': {244 'type': 'string',245 'enum': enum,246 }247 }248 }249 ex_cases = (250 'xx',251 'foo',252 'bar',253 'today',254 'monday',255 'tUesday',256 )257 for c in ex_cases:258 self.assertRaises(argchecker.NotInEnumError,259 argchecker.check_arguments,260 {'week_day': c},261 schema)262 for c in enum:263 argchecker.check_arguments({'week_day': c}, schema)264 def test_str_regexp(self):265 schema = {266 'alphabet_only': {267 'checker': {268 'type': 'string',269 'regexp': "^[a-z]+$"270 }271 }272 }273 with self.assertRaises(argchecker.PatternNotMatchError):274 argchecker.check_arguments({'alphabet_only': 'abcd123'}, schema)275 argchecker.check_arguments({'alphabet_only': 'abcd'}, schema)276 def test_array_arg(self):277 schema = {278 'numbers': {279 'checker': {280 'type': 'array',281 'element_checker': {282 'type': 'integer'283 }284 }285 }286 }287 with self.assertRaises(argchecker.InvalidTypeError):288 argchecker.check_arguments({'numbers': 123}, schema)289 with self.assertRaises(argchecker.InvalidTypeError):290 argchecker.check_arguments({'numbers': [1, 2, 'a']}, schema)291 argchecker.check_arguments({'numbers': []}, schema)292 argchecker.check_arguments({'numbers': [1, 2]}, schema)293 def test_dict_arg(self):294 schema = {295 'log_counter': {296 'checker': {297 'type': 'dict',298 'key_checker': {299 'type': 'string',300 'enum': ['info', 'error', 'warn']301 },302 'value_checker': {303 'type': 'integer',304 'min': 0305 }306 }307 }308 }309 with self.assertRaises(argchecker.InvalidTypeError):310 argchecker.check_arguments({'log_counter': {1: 2}}, schema)311 with self.assertRaises(argchecker.NotInEnumError):312 argchecker.check_arguments({'log_counter': {'err': 1}}, schema)313 argchecker.check_arguments({'log_counter': {'error': 2, 'info': 1}},314 schema)315 def test_any_arg(self):316 schema = {317 'anything': {318 'checker': {'type': 'any'}319 }320 }321 for x in ([], {}, 0, ''):322 argchecker.check_arguments({'anything': x}, schema)323 def test_any_array(self):324 schema = {325 'anything': {326 'checker': {'type': 'array'}327 }328 }329 argchecker.check_arguments({'anything': [[], {}, 0, '']}, schema)330 def test_any_dict(self):331 schema = {332 'anything': {333 'checker': {'type': 'dict'}334 }335 }336 argchecker.check_arguments({'anything': {'1': 'a', 2: 'b', 3: [4]}}, schema)337 def test_nested_shema(self):338 sub_schema = {339 'nested_arg': {340 'checker': {'type': 'string'}341 }342 }343 schema = {344 'arg': {345 'checker': {346 'type': 'dict',347 'sub_schema': sub_schema348 }349 }350 }351 with self.assertRaises(argchecker.UnexpectedArgumentError) as e:352 argchecker.check_arguments({'arg': {'invalid_arg': '123'}}, schema)353 self.assertEqual(e.exception[0], 'invalid_arg')354 with self.assertRaises(argchecker.InvalidTypeError) as e:355 argchecker.check_arguments({'arg': {'nested_arg': 1}}, schema)356 self.assertEqual(e.exception[0], 'nested_arg')357 argchecker.check_arguments({'arg': {'nested_arg': '123'}}, schema)358 def test_checker_with_multi_types(self):359 schema = {360 'str_or_int': {361 'checker': [362 {'type': 'string'},363 {'type': 'integer'},364 ]365 }366 }367 cases = (368 'xx',369 123,370 )371 for val in cases:372 argchecker.check_arguments({'str_or_int': val}, schema)373 ex_cases = (374 [],375 (),376 {},377 0.1,378 )379 for val in ex_cases:380 self.assertRaises(argchecker.InvalidTypeError,381 argchecker.check_arguments,382 {'str_or_int': val},383 schema)384 def test_read_schema(self):385 test_dir = os.path.dirname(os.path.abspath(__file__))386 test_schema_path = test_dir + '/schema_example.yaml'...

Full Screen

Full Screen

test_misc.py

Source:test_misc.py Github

copy

Full Screen

...6from conpaas.core.misc import is_list_dict, is_list_dict27from conpaas.core.misc import is_uploaded_file8from conpaas.core.https.server import FileUploadField9class TestMisc(unittest.TestCase):10 def test_check_arguments(self):11 ## No parameters12 exp_params = []13 args = {}14 parsed_args = check_arguments(exp_params, args)15 self.assertEqual(parsed_args, [])16 # One single integer parameter17 exp_params = [('name1', is_int)]18 args = {'name1': 3}19 name1 = check_arguments(exp_params, args)20 self.assertEqual(name1, 3)21 # Missing one parameter22 exp_params = [('name1', is_int)]23 args = {}24 self.assertRaises(Exception, check_arguments, exp_params, args)25 # Wrong type26 exp_params = [('name1', is_int)]27 args = {'name1': 'value'}28 self.assertRaises(Exception, check_arguments, exp_params, args)29 # One single string parameter30 exp_params = [('name1', is_string)]31 args = {'name1': 'value'}32 name1 = check_arguments(exp_params, args)33 self.assertEqual(name1, 'value')34 exp_params = [('name1', is_string)]35 args = {'name1': u'value'}36 name1 = check_arguments(exp_params, args)37 self.assertEqual(name1, 'value')38 # An integer when a string was expected39 exp_params = [('name1', is_string)]40 args = {'name1': 3}41 self.assertRaises(Exception, check_arguments, exp_params, args)42 # A positive integer43 exp_params = [('name1', is_pos_int)]44 args = {'name1': 3}45 name1 = check_arguments(exp_params, args)46 self.assertEqual(name1, 3)47 # A positive integer48 exp_params = [('name1', is_pos_int)]49 args = {'name1': -3}50 self.assertRaises(Exception, check_arguments, exp_params, args)51 # A positive integer52 exp_params = [('name1', is_pos_int)]53 args = {'name1': 0}54 self.assertRaises(Exception, check_arguments, exp_params, args)55 # A positive or null integer56 exp_params = [('name1', is_pos_nul_int)]57 args = {'name1': 3}58 name1 = check_arguments(exp_params, args)59 self.assertEqual(name1, 3)60 # A positive or null integer61 exp_params = [('name1', is_pos_nul_int)]62 args = {'name1': -3}63 self.assertRaises(Exception, check_arguments, exp_params, args)64 # A positive or null integer65 exp_params = [('name1', is_pos_nul_int)]66 args = {'name1': 0}67 name1 = check_arguments(exp_params, args)68 self.assertEqual(name1, 0)69 # Extra unknown parameter70 exp_params = [('name1', is_int)]71 args = {'name1': 3, 'name2': 'value'}72 self.assertRaises(Exception, check_arguments, exp_params, args)73 # Default value74 exp_params = [('name1', is_int, 5)]75 args = {}76 name1 = check_arguments(exp_params, args)77 self.assertEqual(name1, 5)78 # Mix of mandatory args and optional ones79 exp_params = [('name1', is_int, 5), ('name2', is_int)]80 args = {'name2': 7}81 name1, name2 = check_arguments(exp_params, args)82 self.assertEqual(name1, 5)83 self.assertEqual(name2, 7)84 # Check uploaded file type85 exp_params = [('uploaded_file', is_uploaded_file)]86 filename = 'myfile.txt'87 filecontent = 'file'88 args = {'uploaded_file': FileUploadField(filename, filecontent)}89 uploaded_file = check_arguments(exp_params, args)90 self.assertEqual(uploaded_file.filename, filename)91 self.assertEqual(uploaded_file.file, filecontent)92 # Check in_list constraint93 exp_params = [('name1', is_in_list([3, 5, 6]))]94 args = {'name1': 6}95 name1 = check_arguments(exp_params, args)96 self.assertEqual(name1, 6)97 # Check not in_list constraint98 exp_params = [('name1', is_in_list([3, 5, 6]))]99 args = {'name1': 1}100 self.assertRaises(Exception, check_arguments, exp_params, args)101 # Check is_list constraint102 exp_params = [('name1', is_list)]103 mylist = [3, 'a']104 args = {'name1': mylist}105 name1 = check_arguments(exp_params, args)106 self.assertEqual(name1, mylist)107 # Check is_dict constraint108 exp_params = [('name1', is_dict)]109 mydict = {'key': 3}110 args = {'name1': mydict}111 name1 = check_arguments(exp_params, args)112 self.assertEqual(name1, mydict)113 # Check is_dict2 constraint114 exp_params = [('name1', is_dict2(["key"]))]115 mydict = {'key': 3}116 args = {'name1': mydict}117 name1 = check_arguments(exp_params, args)118 self.assertEqual(name1, mydict)119 # Check is_dict2 constraint120 exp_params = [('name1', is_dict2(["key"], ["optkey"]))]121 mydict = {'key': 3}122 args = {'name1': mydict}123 name1 = check_arguments(exp_params, args)124 self.assertEqual(name1, mydict)125 # Check is_dict2 constraint: missing mandatory126 exp_params = [('name1', is_dict2(["key"], ["optkey"]))]127 mydict = {'optkey': 3}128 args = {'name1': mydict}129 self.assertRaisesRegexp(Exception, ".*expecting key.*", check_arguments, exp_params, args)130 # Check is_dict2 constraint: extra unknown key131 exp_params = [('name1', is_dict2(["key"], ["optkey"]))]132 mydict = {'key': 42, 'optkey': 3, 'newkey': 'great value'}133 args = {'name1': mydict}134 self.assertRaisesRegexp(Exception, ".*Unexpected key.*", check_arguments, exp_params, args)135 # Check is_list_dict constraint136 exp_params = [('name1', is_list_dict)]137 mylistdict = [{'key': 3}]138 args = {'name1': mylistdict}139 name1 = check_arguments(exp_params, args)140 self.assertEqual(name1, mylistdict)141 # Check is_list_dict2 constraint142 exp_params = [('name1', is_list_dict2(['key']))]143 mylistdict = [{'key': 3}, {'key': 56}]144 args = {'name1': mylistdict}145 name1 = check_arguments(exp_params, args)146 self.assertEqual(name1, mylistdict)147 # Check is_list_dict2 constraint148 exp_params = [('name1', is_list_dict2(['key']))]149 mylistdict = [{'key': 3}, {'key': 56, 'nokey': 42}]150 args = {'name1': mylistdict}151 self.assertRaisesRegexp(Exception, ".*Unexpected key.*", check_arguments, exp_params, args)152 # Try with a JSON conversion first153 # Check is_list constraint154 exp_params = [('name1', is_list)]155 mylist = unicode('[3, "a"]')156 mylist = json.loads(mylist)157 args = {'name1': mylist}158 name1 = check_arguments(exp_params, args)159 self.assertEqual(name1, mylist)160 # Check is_dict constraint161 exp_params = [('name1', is_dict)]162 mydict = unicode('{"key": 3}')163 mydict = json.loads(mydict)164 args = {'name1': mydict}165 name1 = check_arguments(exp_params, args)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful