def test_schema_nested_exceptions(): with pytest.raises(cryptocurrency.ParseError) as exc: cryptocurrency.read_data({"a": {"b": "hello"}}, NestedSchema) assert isinstance(exc.value.__cause__, cryptocurrency.ParseError) assert isinstance(exc.value.__cause__.__cause__, cryptocurrency.MissingSchemaField)
def test_schema_unknown_fields(): input_data = {'a': {'a': 'hello', 'b': 'world'}, 'c': 1} with pytest.warns( UserWarning, match= r"Unknown fields: \['c'\] while parsing schema 'NestedSchema'", ): cryptocurrency.read_data(input_data, NestedSchema) obj = cryptocurrency.read_data(input_data, NestedSchema) assert cryptocurrency.serialize(obj) == input_data
def test_schema_unknown_fields(): input_data = {"a": {"a": "hello", "b": "world"}, "c": 1} with pytest.warns( UserWarning, match=re.escape("Unknown fields: ['c'] while parsing schema 'NestedSchema'"), ): obj = cryptocurrency.read_data(input_data, NestedSchema) assert cryptocurrency.serialize(obj) == input_data
def test_parse(): assert cryptocurrency.ResponseStatus._fields != cryptocurrency.Quote._fields cryptocurrency.Platform( # nosec id=1, name="name", symbol="symbol", slug="slug", token_address="foobar", ) assert len(cryptocurrency.Platform._fields) == 5 data = { "status": { "timestamp": "ts", "error_code": 200, "error_message": None, "elapsed": 1, "credit_count": 1, "notice": None, } } obj = cryptocurrency.read_data(data, cryptocurrency.APIRequestResponse) assert obj.status.credit_count == 1 assert cryptocurrency.serialize(obj) == data
def test_parse(): assert cryptocurrency.ResponseStatus._fields != cryptocurrency.Quote._fields cryptocurrency.Platform( id=1, name='name', symbol='symbol', slug='slug', token_address='token', ) assert len(cryptocurrency.Platform._fields) == 5 data = { 'status': { 'timestamp': 'ts', 'error_code': 200, 'error_message': None, 'elapsed': 1, 'credit_count': 1, 'notice': None, } } obj = cryptocurrency.read_data(data, cryptocurrency.APIRequestResponse) assert obj.status.credit_count == 1 assert cryptocurrency.serialize(obj) == data
def test_schema_missing_field(): with pytest.raises(cryptocurrency.ParseError) as exc: cryptocurrency.read_data({"b": "hello"}, OtherConcreteSchema) assert isinstance(exc.value.__cause__, cryptocurrency.MissingSchemaField)
def test_invalid_schema_type(): with pytest.raises( TypeError, match="field 'a' expected type <class 'str'>, got type <class 'int'>" ): cryptocurrency.read_data({"a": 1, "b": "world"}, OtherConcreteSchema)
def test_complex_schema(): cryptocurrency.read_data({"a": "hello", "b": "world"}, OtherConcreteSchema)
def test_schema(): cryptocurrency.read_data({"a": [[{"a": ["1"]}]]}, SomeSchema)
def test_complex_schema(): cryptocurrency.read_data({'a': 'hello', 'b': 'world'}, OtherConcreteSchema)
def test_schema(): cryptocurrency.read_data({'a': [[{'a': ['1']}]]}, SomeSchema)