Пример #1
0
def test_deserialize_record():
    n_recs = 10
    serializer = Serializer(SCHEMA)
    deserializer = pyavroc.AvroDeserializer(SCHEMA)
    obj_deserializer = pyavroc.AvroDeserializer(SCHEMA, types=True)
    for i in range(n_recs):
        name, office = "name-%d" % i, "office-%d" % i
        record = {'name': name, 'office': office}
        rec_bytes = serializer.serialize(record)
        deser_rec = deserializer.deserialize(rec_bytes)
        assert set(deser_rec) == set(['name', 'office', 'favorite_number'])
        assert deser_rec['name'] == name
        assert deser_rec['office'] == office
        assert deser_rec['favorite_number'] is None
        deser_rec = obj_deserializer.deserialize(rec_bytes)
        assert deser_rec.name == name
        assert deser_rec.office == office
        assert deser_rec.favorite_number is None
Пример #2
0
def test_enum():
    schema = '''{
    "type": "enum",
    "name": "suits",
    "symbols": ["CLUBS", "DIAMONDS", "HEARTS", "SPADES"]
    }'''
    symbols = json.loads(schema)['symbols']
    serializer = Serializer(schema)
    deserializer = pyavroc.AvroDeserializer(schema)
    for s in symbols:
        assert deserializer.deserialize(serializer.serialize(s)) == s
Пример #3
0
def test_resolution():
    schema_write = '''{
        "type": "record",
        "name": "User",
        "fields": [
            {"name": "a", "type": "int"},
            {"name": "b", "type": "string"},
            {"name": "c",  "type": ["int", "null"]},
            {"name": "d",  "type": ["float", "null"]},
            {"name": "e",  "type": {
                    "type": "long",
                    "logicalType": "timestamp-millis"
                }
            }
        ]
    }'''

    schema_read = '''{
        "type": "record",
        "name": "User",
        "fields": [
            {"name": "a", "type": "double"},
            {"name": "b", "type": "string"},
            {"name": "c",  "type": ["float", "null"]},
            {"name": "d",  "type": "float"},
            {"name": "e",  "type": {
                    "type": "long",
                    "logicalType": "timestamp-millis"
                }
            },
            {"name": "f", "type": "string", "default": "default f value"}
        ]
    }'''
    serializer = Serializer(schema_write)
    deserializer = pyavroc.AvroDeserializer(schema_read)
    record = {
        'a': 1,
        'b': 'the string value',
        'c': 2,
        'd': 3.0,
        'e': 12345,
    }
    rec_bytes = serializer.serialize(record)
    deser_rec = deserializer.deserialize(rec_bytes, writer_schema=schema_write)
    assert deser_rec['a'] == 1.0
    assert deser_rec['b'] == 'the string value'
    assert deser_rec['c'] == 2.0
    assert deser_rec['d'] == 3.0
    assert deser_rec['e'] == 12345
    assert deser_rec['f'] == 'default f value'
Пример #4
0
def test_exc():
    with pytest.raises(TypeError):
        pyavroc.AvroDeserializer(1)
    with pytest.raises(IOError):
        pyavroc.AvroDeserializer('NOT_A_VALID_JSON')