Best Python code snippet using Testify_python
test_schema.py
Source:test_schema.py
1from unittest import TestCase2from portality import schema3from copy import deepcopy4# set this to true if you want the tests to fail, so you can see the actual errors5fail = False6_complete = {7 "bools" : ["mybool1", "mybool2"],8 "fields" : ["field1", "field2"],9 "lists" : ["list1", "list2"],10 "objects" : ["obj1", "obj2"],11 12 "list_entries" : {13 "list1" : {14 "bools" : ["listbool"],15 "fields" : ["listfield"],16 "lists" : ["listlist"],17 "objects" : ["listobj"],18 19 "object_entries" : {20 "listobj" : {21 "fields" : ["objfield1", "objfield2"]22 }23 }24 }25 },26 27 "object_entries" : {28 "obj1" : {29 "bools" : ["objbool"],30 "fields" : ["objfield"],31 "lists" : ["objlist"],32 "objects" : ["objobj"],33 34 "object_entries" : {35 "objobj" : {36 "fields" : ["objfield3", "objfield4"]37 }38 }39 },40 "obj2" : {41 "fields" : ["objfield5", "objfield6"]42 }43 }44}45class TestSchema(TestCase):46 def setUp(self):47 pass48 49 def tearDown(self):50 pass51 52 def test_01_complete_correct(self):53 correct = {54 "mybool1" : True,55 "mybool2" : False,56 "field1" : "stuff",57 "field2" : "other stuff",58 "list1" : [{59 "listbool" : True,60 "listfield" : "more stuff",61 "listlist" : ["plain string", "another string"],62 "listobj" : {63 "objfield1" : "object property 1",64 "objfield2" : "object property 2"65 }66 }],67 "list2" : ["string", u"unicode"],68 "obj1" : {69 "objbool" : False,70 "objfield" : "a field",71 "objlist" : [],72 "objobj" : {73 "objfield3" : "3",74 "objfield4" : "4"75 }76 },77 "obj2" : {78 "objfield5" : 5,79 "objfield6" : 680 }81 }82 schema.validate(correct, _complete)83 84 def test_02_complete_bool_error(self):85 wrong = {86 "mybool1" : "wibble",87 "mybool2" : False,88 "field1" : "stuff",89 "field2" : "other stuff",90 "list1" : [{91 "listbool" : True,92 "listfield" : "more stuff",93 "listlist" : ["plain string", "another string"],94 "listobj" : {95 "objfield1" : "object property 1",96 "objfield2" : "object property 2"97 }98 }],99 "list2" : ["string", u"unicode"],100 "obj1" : {101 "objbool" : False,102 "objfield" : "a field",103 "objlist" : [],104 "objobj" : {105 "objfield3" : "3",106 "objfield4" : "4"107 }108 },109 "obj2" : {110 "objfield5" : 5,111 "objfield6" : 6112 }113 }114 with self.assertRaises(schema.ObjectSchemaValidationError):115 schema.validate(wrong, _complete)116 if fail:117 schema.validate(wrong, _complete)118 119 def test_03_complete_field_error(self):120 wrong = {121 "mybool1" : True,122 "mybool2" : False,123 "field1" : [],124 "field2" : "other stuff",125 "list1" : [{126 "listbool" : True,127 "listfield" : "more stuff",128 "listlist" : ["plain string", "another string"],129 "listobj" : {130 "objfield1" : "object property 1",131 "objfield2" : "object property 2"132 }133 }],134 "list2" : ["string", u"unicode"],135 "obj1" : {136 "objbool" : False,137 "objfield" : "a field",138 "objlist" : [],139 "objobj" : {140 "objfield3" : "3",141 "objfield4" : "4"142 }143 },144 "obj2" : {145 "objfield5" : 5,146 "objfield6" : 6147 }148 }149 with self.assertRaises(schema.ObjectSchemaValidationError):150 schema.validate(wrong, _complete)151 if fail:152 schema.validate(wrong, _complete)153 154 def test_04_complete_list_error(self):155 wrong = {156 "mybool1" : True,157 "mybool2" : False,158 "field1" : "stuff",159 "field2" : "other stuff",160 "list1" : "not a list",161 "list2" : ["string", u"unicode"],162 "obj1" : {163 "objbool" : False,164 "objfield" : "a field",165 "objlist" : [],166 "objobj" : {167 "objfield3" : "3",168 "objfield4" : "4"169 }170 },171 "obj2" : {172 "objfield5" : 5,173 "objfield6" : 6174 }175 }176 with self.assertRaises(schema.ObjectSchemaValidationError):177 schema.validate(wrong, _complete)178 if fail:179 schema.validate(wrong, _complete)180 181 def test_05_complete_obj_error(self):182 wrong = {183 "mybool1" : True,184 "mybool2" : False,185 "field1" : "stuff",186 "field2" : "other stuff",187 "list1" : [{188 "listbool" : True,189 "listfield" : "more stuff",190 "listlist" : ["plain string", "another string"],191 "listobj" : {192 "objfield1" : "object property 1",193 "objfield2" : "object property 2"194 }195 }],196 "list2" : ["string", u"unicode"],197 "obj1" : [],198 "obj2" : {199 "objfield5" : 5,200 "objfield6" : 6201 }202 }203 with self.assertRaises(schema.ObjectSchemaValidationError):204 schema.validate(wrong, _complete)205 if fail:206 schema.validate(wrong, _complete)207 208 def test_06_complete_inside_list_error(self):209 wrong = {210 "mybool1" : True,211 "mybool2" : False,212 "field1" : "stuff",213 "field2" : "other stuff",214 "list1" : [{215 "listbool" : True,216 "listfield" : "more stuff",217 "listlist" : "not a list",218 "listobj" : {219 "objfield1" : "object property 1",220 "objfield2" : "object property 2"221 }222 }],223 "list2" : ["string", u"unicode"],224 "obj1" : {225 "objbool" : False,226 "objfield" : "a field",227 "objlist" : [],228 "objobj" : {229 "objfield3" : "3",230 "objfield4" : "4"231 }232 },233 "obj2" : {234 "objfield5" : 5,235 "objfield6" : 6236 }237 }238 with self.assertRaises(schema.ObjectSchemaValidationError):239 schema.validate(wrong, _complete)240 if fail:241 schema.validate(wrong, _complete)242 243 def test_07_complete_inside_obj_error(self):244 wrong = {245 "mybool1" : True,246 "mybool2" : False,247 "field1" : "stuff",248 "field2" : "other stuff",249 "list1" : [{250 "listbool" : True,251 "listfield" : "more stuff",252 "listlist" : ["plain string", "another string"],253 "listobj" : {254 "objfield1" : "object property 1",255 "objfield2" : "object property 2"256 }257 }],258 "list2" : ["string", u"unicode"],259 "obj1" : {260 "objbool" : "not a bool",261 "objfield" : "a field",262 "objlist" : [],263 "objobj" : {264 "objfield3" : "3",265 "objfield4" : "4"266 }267 },268 "obj2" : {269 "objfield5" : 5,270 "objfield6" : 6271 }272 }273 with self.assertRaises(schema.ObjectSchemaValidationError):274 schema.validate(wrong, _complete)275 if fail:276 schema.validate(wrong, _complete)277 278 def test_08_complete_extra_field(self):279 wrong = {280 "not_allowed" : "I shouldn't be here",281 "mybool1" : True,282 "mybool2" : False,283 "field1" : "stuff",284 "field2" : "other stuff",285 "list1" : [{286 "listbool" : True,287 "listfield" : "more stuff",288 "listlist" : ["plain string", "another string"],289 "listobj" : {290 "objfield1" : "object property 1",291 "objfield2" : "object property 2"292 }293 }],294 "list2" : ["string", u"unicode"],295 "obj1" : {296 "objbool" : False,297 "objfield" : "a field",298 "objlist" : [],299 "objobj" : {300 "objfield3" : "3",301 "objfield4" : "4"302 }303 },304 "obj2" : {305 "objfield5" : 5,306 "objfield6" : 6307 }308 }309 with self.assertRaises(schema.ObjectSchemaValidationError):310 schema.validate(wrong, _complete)311 if fail:312 schema.validate(wrong, _complete)313 314 def test_09_complete_default_list_error(self):315 wrong = {316 "mybool1" : True,317 "mybool2" : False,318 "field1" : "stuff",319 "field2" : "other stuff",320 "list1" : [{321 "listbool" : True,322 "listfield" : "more stuff",323 "listlist" : [{}, []],324 "listobj" : {325 "objfield1" : "object property 1",326 "objfield2" : "object property 2"327 }328 }],329 "list2" : ["string", u"unicode"],330 "obj1" : {331 "objbool" : False,332 "objfield" : "a field",333 "objlist" : [],334 "objobj" : {335 "objfield3" : "3",336 "objfield4" : "4"337 }338 },339 "obj2" : {340 "objfield5" : 5,341 "objfield6" : 6342 }343 }344 with self.assertRaises(schema.ObjectSchemaValidationError):345 schema.validate(wrong, _complete)346 if fail:347 schema.validate(wrong, _complete)348 349 def test_10_incomplete_error(self):350 correct = {351 "mybool1" : True,352 "mybool2" : False,353 "field1" : "stuff",354 "field2" : "other stuff",355 "list1" : [{356 "listbool" : True,357 "listfield" : "more stuff",358 "listlist" : ["plain string", "another string"],359 "listobj" : {360 "objfield1" : "object property 1",361 "objfield2" : "object property 2"362 }363 }],364 "list2" : ["string", u"unicode"],365 "obj1" : {366 "objbool" : False,367 "objfield" : "a field",368 "objlist" : [],369 "objobj" : {370 "objfield3" : "3",371 "objfield4" : "4"372 }373 },374 "obj2" : {375 "objfield5" : 5,376 "objfield6" : 6377 }378 }379 380 incomplete = deepcopy(_complete)381 del incomplete["object_entries"]["obj1"]382 383 with self.assertRaises(schema.ObjectSchemaValidationError):384 schema.validate(correct, incomplete)385 if fail:386 schema.validate(correct, incomplete)387 388 389 ...
test_lists.py
Source:test_lists.py
...9 self.listField = value10async def get_async(value):11 return value12def describe_execute_accepts_any_iterable_as_list_value():13 def _complete(list_field):14 return execute_sync(15 build_schema("type Query { listField: [String] }"),16 parse("{ listField }"),17 Data(list_field),18 )19 def accepts_a_set_as_a_list_value():20 # Note that sets are not ordered in Python.21 list_field = {"apple", "banana", "coconut"}22 result = _complete(list_field)23 assert result.errors is None24 assert isinstance(result.data, dict)25 assert list(result.data) == ["listField"]26 assert isinstance(result.data["listField"], list)27 assert set(result.data["listField"]) == list_field28 def accepts_a_generator_as_a_list_value():29 def list_field():30 yield "one"31 yield 232 yield True33 assert _complete(list_field()) == (34 {"listField": ["one", "2", "true"]},35 None,36 )37 def accepts_a_custom_iterable_as_a_list_value():38 class ListField:39 def __iter__(self):40 self.last = "hello"41 return self42 def __next__(self):43 last = self.last44 if last == "stop":45 raise StopIteration46 self.last = "world" if last == "hello" else "stop"47 return last48 assert _complete(ListField()) == (49 {"listField": ["hello", "world"]},50 None,51 )52 def accepts_function_arguments_as_a_list_value():53 def get_args(*args):54 return args # actually just a tuple, nothing special in Python55 assert _complete(get_args("one", "two")) == (56 {"listField": ["one", "two"]},57 None,58 )59 def does_not_accept_a_dict_as_a_list_value():60 assert _complete({1: "one", 2: "two"}) == (61 {"listField": None},62 [63 {64 "message": "Expected Iterable,"65 " but did not find one for field 'Query.listField'.",66 "locations": [(1, 3)],67 "path": ["listField"],68 }69 ],70 )71 def does_not_accept_iterable_string_literal_as_a_list_value():72 assert _complete("Singular") == (73 {"listField": None},74 [75 {76 "message": "Expected Iterable,"77 " but did not find one for field 'Query.listField'.",78 "locations": [(1, 3)],79 "path": ["listField"],80 }81 ],82 )83def describe_execute_handles_list_nullability():84 async def _complete(list_field: Any, as_type: str) -> ExecutionResult:85 schema = build_schema(f"type Query {{ listField: {as_type} }}")86 document = parse("{ listField }")87 def execute_query(list_value: Any) -> Any:88 return execute(schema, document, Data(list_value))89 result = execute_query(list_field)90 assert isinstance(result, ExecutionResult)91 assert await execute_query(get_async(list_field)) == result92 if isinstance(list_field, list):93 assert await execute_query(list(map(get_async, list_field))) == result94 assert await execute_query(get_async(list_field)) == result95 return result96 @mark.asyncio97 async def contains_values():98 list_field = [1, 2]99 assert await _complete(list_field, "[Int]") == ({"listField": [1, 2]}, None)100 assert await _complete(list_field, "[Int]!") == ({"listField": [1, 2]}, None)101 assert await _complete(list_field, "[Int!]") == ({"listField": [1, 2]}, None)102 assert await _complete(list_field, "[Int!]!") == ({"listField": [1, 2]}, None)103 @mark.asyncio104 async def contains_null():105 list_field = [1, None, 2]106 errors = [107 {108 "message": "Cannot return null for non-nullable field Query.listField.",109 "locations": [(1, 3)],110 "path": ["listField", 1],111 }112 ]113 assert await _complete(list_field, "[Int]") == (114 {"listField": [1, None, 2]},115 None,116 )117 assert await _complete(list_field, "[Int]!") == (118 {"listField": [1, None, 2]},119 None,120 )121 assert await _complete(list_field, "[Int!]") == ({"listField": None}, errors)122 assert await _complete(list_field, "[Int!]!") == (None, errors)123 @mark.asyncio124 async def returns_null():125 list_field = None126 errors = [127 {128 "message": "Cannot return null for non-nullable field Query.listField.",129 "locations": [(1, 3)],130 "path": ["listField"],131 }132 ]133 assert await _complete(list_field, "[Int]") == ({"listField": None}, None)134 assert await _complete(list_field, "[Int]!") == (None, errors)135 assert await _complete(list_field, "[Int!]") == ({"listField": None}, None)136 assert await _complete(list_field, "[Int!]!") == (None, errors)137 @mark.asyncio138 async def contains_error():139 list_field = [1, RuntimeError("bad"), 2]140 errors = [141 {142 "message": "bad",143 "locations": [(1, 3)],144 "path": ["listField", 1],145 }146 ]147 assert await _complete(list_field, "[Int]") == (148 {"listField": [1, None, 2]},149 errors,150 )151 assert await _complete(list_field, "[Int]!") == (152 {"listField": [1, None, 2]},153 errors,154 )155 assert await _complete(list_field, "[Int!]") == (156 {"listField": None},157 errors,158 )159 assert await _complete(list_field, "[Int!]!") == (160 None,161 errors,162 )163 @mark.asyncio164 async def results_in_errors():165 list_field = RuntimeError("bad")166 errors = [167 {168 "message": "bad",169 "locations": [(1, 3)],170 "path": ["listField"],171 }172 ]173 assert await _complete(list_field, "[Int]") == (174 {"listField": None},175 errors,176 )177 assert await _complete(list_field, "[Int]!") == (178 None,179 errors,180 )181 assert await _complete(list_field, "[Int!]") == (182 {"listField": None},183 errors,184 )185 assert await _complete(list_field, "[Int!]!") == (186 None,187 errors,188 )189def describe_experimental_execute_accepts_async_iterables_as_list_value():190 async def _complete(list_field):191 result = execute(192 build_schema("type Query { listField: [String] }"),193 parse("{ listField }"),194 Data(list_field),195 )196 assert is_awaitable(result)197 result = cast(Awaitable, result)198 return await result199 @mark.asyncio200 async def accepts_an_async_generator_as_a_list_value():201 async def list_field():202 yield "one"203 yield 2204 yield True205 assert await _complete(list_field()) == (206 {"listField": ["one", "2", "true"]},207 None,208 )209 @mark.asyncio210 async def accepts_a_custom_async_iterable_as_a_list_value():211 class ListField:212 def __aiter__(self):213 self.last = "hello"214 return self215 async def __anext__(self):216 last = self.last217 if last == "stop":218 raise StopAsyncIteration219 self.last = "world" if last == "hello" else "stop"220 return last221 assert await _complete(ListField()) == (222 {"listField": ["hello", "world"]},223 None,...
Test_Completer.py
Source:Test_Completer.py
...27 """28 Test completions for builtin commands29 """30 tester = self.tester()31 self.assertEqual(tester.CLI._complete("."), [".setting", ".read"])3233 self.assertEqual(tester.CLI._complete("-"), ["--help"])34 self.assertEqual(tester.CLI._complete("oper -"), ["--help"])35 self.assertEqual(tester.CLI._complete("oper _ -"), ["--help"])36 self.assertEqual(tester.CLI._complete("oper _ aa -"), ["--help"])37 self.assertEqual(tester.CLI._complete("oper _ aa -"), ["--help"])38 self.assertEqual(tester.CLI._complete(".set -"), ["--help"])39 self.assertEqual(tester.CLI._complete(".set set11 -"), ["--help"])40 self.assertEqual(tester.CLI._complete(".set set11 11 -"), ["--help"])41 self.assertEqual(tester.CLI._complete("non existing -"), ["--help"])4243 cli = CLI()44 @cli.Program()45 class NoSettings: pass46 self.assertEqual(NoSettings().CLI._complete("."), [".read"])474849 def test_complete_method(self):50 """51 Test completions for methods52 """53 tester = self.tester()54 self.assertEqual(tester.CLI._complete("op"), ["oper", "oper_no_args"])55 self.assertEqual(tester.CLI._complete("oper_"), ["oper_no_args"])56 57 def test_complete_setting(self):58 """59 Test completions for settings60 """61 tester = self.tester()62 self.assertEqual(tester.CLI._complete(".s"), [".setting"])63 self.assertEqual(tester.CLI._complete(".set"), [])64 self.assertEqual(tester.CLI._complete(".sett"), [".setting"])65 self.assertEqual(tester.CLI._complete(".set "), ["set11", "set22"])66 self.assertEqual(tester.CLI._complete(".set set"), ["set11", "set22"])67 self.assertEqual(tester.CLI._complete(".set set1"), ["set11"])68 self.assertEqual(tester.CLI._complete(".set set2"), ["set22"])69 self.assertEqual(tester.CLI._complete(".setting "), ["set11", "set22"])70 self.assertEqual(tester.CLI._complete(".setting set"), ["set11", "set22"])71 self.assertEqual(tester.CLI._complete(".setting set1"), ["set11"])72 self.assertEqual(tester.CLI._complete(".setting set2"), ["set22"])73 7475 def test_complete_iterable(self):76 """77 Test completions for iterable annotation78 """79 tester = self.tester()80 self.assertEqual(tester.CLI._complete("oper "), [])81 self.assertEqual(tester.CLI._complete("oper _ "), ["aa", "ab", "bb"])82 self.assertEqual(tester.CLI._complete("oper _ a"), ["aa", "ab"])83 self.assertEqual(tester.CLI._complete("oper _ aa"), ["aa"])84 self.assertEqual(tester.CLI._complete("oper _ aa "), [])8586 def test_complete_dict(self):87 """88 Test completions for dict annotation89 """90 tester = self.tester()91 self.assertEqual(tester.CLI._complete(".setting set11 "), ["11", "12", "22"])92 self.assertEqual(tester.CLI._complete(".setting set11 1"), ["11", "12"])93 self.assertEqual(tester.CLI._complete(".setting set11 2"), ["22"])94 self.assertEqual(tester.CLI._complete(".setting set11 1 "), [])95 self.assertEqual(tester.CLI._complete(".setting set11 11 "), [])
...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!