Best Python code snippet using pandera_python
test_coerce_value.py
Source:test_coerce_value.py
...21 errors = result.errors22 messages = [error.message for error in errors] if errors else []23 assert result.value is INVALID24 return messages25def describe_coerce_value():26 def describe_for_graphql_string():27 def returns_error_for_array_input_as_string():28 result = coerce_value([1, 2, 3], GraphQLString)29 assert expect_error(result) == [30 f"Expected type String."31 " String cannot represent a non string value: [1, 2, 3]"32 ]33 def describe_for_graphql_id():34 def returns_error_for_array_input_as_string():35 result = coerce_value([1, 2, 3], GraphQLID)36 assert expect_error(result) == [37 f"Expected type ID. ID cannot represent value: [1, 2, 3]"38 ]39 def describe_for_graphql_int():40 def returns_value_for_integer():41 result = coerce_value(1, GraphQLInt)42 assert expect_value(result) == 143 def returns_no_error_for_numeric_looking_string():44 result = coerce_value("1", GraphQLInt)45 assert expect_error(result) == [46 f"Expected type Int. Int cannot represent non-integer value: '1'"47 ]48 def returns_value_for_negative_int_input():49 result = coerce_value(-1, GraphQLInt)50 assert expect_value(result) == -151 def returns_value_for_exponent_input():52 result = coerce_value(1e3, GraphQLInt)53 assert expect_value(result) == 100054 def returns_null_for_null_value():55 result = coerce_value(None, GraphQLInt)56 assert expect_value(result) is None57 def returns_a_single_error_for_empty_string_as_value():58 result = coerce_value("", GraphQLInt)59 assert expect_error(result) == [60 "Expected type Int. Int cannot represent non-integer value: ''"61 ]62 def returns_a_single_error_for_2_32_input_as_int():63 result = coerce_value(1 << 32, GraphQLInt)64 assert expect_error(result) == [65 "Expected type Int. Int cannot represent"66 " non 32-bit signed integer value: 4294967296"67 ]68 def returns_a_single_error_for_float_input_as_int():69 result = coerce_value(1.5, GraphQLInt)70 assert expect_error(result) == [71 "Expected type Int. Int cannot represent non-integer value: 1.5"72 ]73 def returns_a_single_error_for_nan_input_as_int():74 result = coerce_value(nan, GraphQLInt)75 assert expect_error(result) == [76 "Expected type Int. Int cannot represent non-integer value: nan"77 ]78 def returns_a_single_error_for_infinity_input_as_int():79 result = coerce_value(inf, GraphQLInt)80 assert expect_error(result) == [81 "Expected type Int. Int cannot represent non-integer value: inf"82 ]83 def returns_a_single_error_for_char_input():84 result = coerce_value("a", GraphQLInt)85 assert expect_error(result) == [86 "Expected type Int. Int cannot represent non-integer value: 'a'"87 ]88 def returns_a_single_error_for_string_input():89 result = coerce_value("meow", GraphQLInt)90 assert expect_error(result) == [91 "Expected type Int. Int cannot represent non-integer value: 'meow'"92 ]93 def describe_for_graphql_float():94 def returns_value_for_integer():95 result = coerce_value(1, GraphQLFloat)96 assert expect_value(result) == 197 def returns_value_for_decimal():98 result = coerce_value(1.1, GraphQLFloat)99 assert expect_value(result) == 1.1100 def returns_no_error_for_exponent_input():101 result = coerce_value(1e3, GraphQLFloat)102 assert expect_value(result) == 1000103 def returns_error_for_numeric_looking_string():104 result = coerce_value("1", GraphQLFloat)105 assert expect_error(result) == [106 "Expected type Float. Float cannot represent non numeric value: '1'"107 ]108 def returns_null_for_null_value():109 result = coerce_value(None, GraphQLFloat)110 assert expect_value(result) is None111 def returns_a_single_error_for_empty_string_input():112 result = coerce_value("", GraphQLFloat)113 assert expect_error(result) == [114 "Expected type Float. Float cannot represent non numeric value: ''"115 ]116 def returns_a_single_error_for_nan_input():117 result = coerce_value(nan, GraphQLFloat)118 assert expect_error(result) == [119 "Expected type Float. Float cannot represent non numeric value: nan"120 ]121 def returns_a_single_error_for_infinity_input():122 result = coerce_value(inf, GraphQLFloat)123 assert expect_error(result) == [124 "Expected type Float. Float cannot represent non numeric value: inf"125 ]126 def returns_a_single_error_for_char_input():127 result = coerce_value("a", GraphQLFloat)128 assert expect_error(result) == [129 "Expected type Float. Float cannot represent non numeric value: 'a'"130 ]131 def returns_a_single_error_for_string_input():132 result = coerce_value("meow", GraphQLFloat)133 assert expect_error(result) == [134 "Expected type Float. Float cannot represent non numeric value: 'meow'"135 ]136 def describe_for_graphql_enum():137 TestEnum = GraphQLEnumType(138 "TestEnum", {"FOO": "InternalFoo", "BAR": 123_456_789}139 )140 def returns_no_error_for_a_known_enum_name():141 foo_result = coerce_value("FOO", TestEnum)142 assert expect_value(foo_result) == "InternalFoo"143 bar_result = coerce_value("BAR", TestEnum)144 assert expect_value(bar_result) == 123_456_789145 def results_error_for_misspelled_enum_value():146 result = coerce_value("foo", TestEnum)147 assert expect_error(result) == ["Expected type TestEnum. Did you mean FOO?"]148 def results_error_for_incorrect_value_type():149 result1 = coerce_value(123, TestEnum)150 assert expect_error(result1) == ["Expected type TestEnum."]151 result2 = coerce_value({"field": "value"}, TestEnum)152 assert expect_error(result2) == ["Expected type TestEnum."]153 def describe_for_graphql_input_object():154 TestInputObject = GraphQLInputObjectType(155 "TestInputObject",156 {157 "foo": GraphQLInputField(GraphQLNonNull(GraphQLInt)),158 "bar": GraphQLInputField(GraphQLInt),159 },160 )161 def returns_no_error_for_a_valid_input():162 result = coerce_value({"foo": 123}, TestInputObject)163 assert expect_value(result) == {"foo": 123}164 def returns_an_error_for_a_non_dict_value():165 result = coerce_value(123, TestInputObject)166 assert expect_error(result) == [167 "Expected type TestInputObject to be a dict."168 ]169 def returns_an_error_for_an_invalid_field():170 result = coerce_value({"foo": "abc"}, TestInputObject)171 assert expect_error(result) == [172 "Expected type Int at value.foo."173 " Int cannot represent non-integer value: 'abc'"174 ]175 def returns_multiple_errors_for_multiple_invalid_fields():176 result = coerce_value({"foo": "abc", "bar": "def"}, TestInputObject)177 assert expect_error(result) == [178 "Expected type Int at value.foo."179 " Int cannot represent non-integer value: 'abc'",180 "Expected type Int at value.bar."181 " Int cannot represent non-integer value: 'def'",182 ]183 def returns_error_for_a_missing_required_field():184 result = coerce_value({"bar": 123}, TestInputObject)185 assert expect_error(result) == [186 "Field value.foo of required type Int! was not provided."187 ]188 def returns_error_for_an_unknown_field():189 result = coerce_value({"foo": 123, "unknownField": 123}, TestInputObject)190 assert expect_error(result) == [191 "Field 'unknownField' is not defined by type TestInputObject."192 ]193 def returns_error_for_a_misspelled_field():194 result = coerce_value({"foo": 123, "bart": 123}, TestInputObject)195 assert expect_error(result) == [196 "Field 'bart' is not defined by type TestInputObject."197 " Did you mean bar?"198 ]199 def transforms_names_using_out_name():200 # This is an extension of GraphQL.js.201 ComplexInputObject = GraphQLInputObjectType(202 "Complex",203 {204 "realPart": GraphQLInputField(GraphQLFloat, out_name="real_part"),205 "imagPart": GraphQLInputField(206 GraphQLFloat, default_value=0, out_name="imag_part"207 ),208 },209 )210 result = coerce_value({"realPart": 1}, ComplexInputObject)211 assert expect_value(result) == {"real_part": 1, "imag_part": 0}212 def transforms_values_with_out_type():213 # This is an extension of GraphQL.js.214 ComplexInputObject = GraphQLInputObjectType(215 "Complex",216 {217 "real": GraphQLInputField(GraphQLFloat),218 "imag": GraphQLInputField(GraphQLFloat),219 },220 out_type=lambda value: complex(value["real"], value["imag"]),221 )222 result = coerce_value({"real": 1, "imag": 2}, ComplexInputObject)223 assert expect_value(result) == 1 + 2j224 def describe_for_graphql_list():225 TestList = GraphQLList(GraphQLInt)226 def returns_no_error_for_a_valid_input():227 result = coerce_value([1, 2, 3], TestList)228 assert expect_value(result) == [1, 2, 3]229 def returns_an_error_for_an_invalid_input():230 result = coerce_value([1, "b", True], TestList)231 assert expect_error(result) == [232 "Expected type Int at value[1]."233 " Int cannot represent non-integer value: 'b'",234 "Expected type Int at value[2]."235 " Int cannot represent non-integer value: True",236 ]237 def returns_a_list_for_a_non_list_value():238 result = coerce_value(42, TestList)239 assert expect_value(result) == [42]240 def returns_null_for_a_null_value():241 result = coerce_value(None, TestList)242 assert expect_value(result) is None243 def describe_for_nested_graphql_list():244 TestNestedList = GraphQLList(GraphQLList(GraphQLInt))245 def returns_no_error_for_a_valid_input():246 result = coerce_value([[1], [2], [3]], TestNestedList)247 assert expect_value(result) == [[1], [2], [3]]248 def returns_a_list_for_a_non_list_value():249 result = coerce_value(42, TestNestedList)250 assert expect_value(result) == [[42]]251 def returns_null_for_a_null_value():252 result = coerce_value(None, TestNestedList)253 assert expect_value(result) is None254 def returns_nested_list_for_nested_non_list_values():255 result = coerce_value([1, 2, 3], TestNestedList)256 assert expect_value(result) == [[1], [2], [3]]257 def returns_nested_null_for_nested_null_values():258 result = coerce_value([42, [None], None], TestNestedList)...
test_config.py
Source:test_config.py
...90 with pytest.raises(RuntimeError):91 config.load(environment="staging")92def test_coerce():93 with override_default_config({"foo": (int, None)}):94 assert config.coerce_value("foo", "1") == 195 assert config.coerce_value("foo", "0") == 096 assert config.coerce_value("foo", "-1") == -197 with pytest.raises(ValueError):98 assert config.coerce_value("foo", "bar")99def test_coerce_bool():100 with override_default_config({"foo": (bool, None)}):101 assert config.coerce_value("foo", "1")102 assert config.coerce_value("foo", "Yes")103 assert config.coerce_value("foo", "True")104 assert not config.coerce_value("foo", "0")105 assert not config.coerce_value("foo", "No")106 assert not config.coerce_value("foo", "False")107 with pytest.raises(ValueError):...
_ast_factory_spec.py
Source:_ast_factory_spec.py
...17 unsupported = UnsupportedClass()18 expect(calling(_ast_factory.coerce_value, unsupported)).to(raise_error)19 with it('supports bool literals'):20 expect(calling(_ast_factory.coerce_value, True)).not_to(raise_error)21 expect(_ast_factory.coerce_value(True)).to(be_a(ast.NameConstant))22 with it('supports number literals'):23 expect(calling(_ast_factory.coerce_value, 1)).not_to(raise_error)24 expect(_ast_factory.coerce_value(1)).to(be_a(ast.Num))25 expect(calling(_ast_factory.coerce_value, 1.5)).not_to(raise_error)26 expect(_ast_factory.coerce_value(1.5)).to(be_a(ast.Num))27 with it('supports str literals'):28 expect(calling(_ast_factory.coerce_value, 'a')).not_to(raise_error)29 expect(_ast_factory.coerce_value('a')).to(be_a(ast.Str))30 with it('supports expressions'):31 node = ast.Expr(value=ast.Load())32 expect(calling(_ast_factory.coerce_value, node)).not_to(raise_error)33 expect(_ast_factory.coerce_value(node)).to(be_a(ast.Load))34 with it('supports all other AST nodes'):35 node = ast.Load()36 expect(calling(_ast_factory.coerce_value, node)).not_to(raise_error)37 expect(_ast_factory.coerce_value(node)).to(be_a(ast.Load))38 with it('supports lists'):39 value = [1, 2, 3, 4]40 expect(calling(_ast_factory.coerce_value, value)).not_to(raise_error)41 expect(_ast_factory.coerce_value(value)).to(be_a(ast.List))42 with it('supports tuples'):43 value = (1, 2, 3, 4)44 expect(calling(_ast_factory.coerce_value, value)).not_to(raise_error)45 expect(_ast_factory.coerce_value(value)).to(be_a(ast.List))46 with it('supports generators'):47 value = range(5)48 expect(calling(_ast_factory.coerce_value, value)).not_to(raise_error)49 expect(_ast_factory.coerce_value(value)).to(be_a(ast.List))50 with description('binary operations'):51 with it('supports +'):52 expect(_ast_factory.bin_op(self.andy, '+', 1)).to(be_a(ast.Expr))53 with it('supports -'):54 expect(_ast_factory.bin_op(self.andy, '-', 1)).to(be_a(ast.Expr))55 with it('supports |'):56 expect(_ast_factory.bin_op(self.andy, '|', True)).to(be_a(ast.Expr))57 with it('supports ^'):58 expect(_ast_factory.bin_op(self.andy, '|', False)).to(be_a(ast.Expr))59 with it('supports *'):60 expect(_ast_factory.bin_op(self.andy, '*', 12)).to(be_a(ast.Expr))61 with it('supports &'):62 expect(_ast_factory.bin_op(self.bob, '&', self.dates)).to(be_a(ast.Expr))63 with description('unary operations'):...
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!!