Пример #1
0
def test_encoded_union_output():
    schema = {
        "type": "record",
        "name": "Test",
        "namespace": "test",
        "fields": [{
            "name": "union",
            "type": [
                'null',
                'int',
                {
                    "type": "record",
                    "name": "union_record",
                    "fields": [{
                        "name": "union_record_field",
                        "type": "string",
                    }],
                },
            ]
        }]
    }

    # A null value is encoded as just null
    records = [{'union': None}]
    new_file = StringIO()
    json_writer(new_file, schema, records)
    assert new_file.getvalue().strip() == json.dumps({"union": None})

    # A non-null, non-named type is encoded as an object with a key for the
    # type
    records = [{'union': 321}]
    new_file = StringIO()
    json_writer(new_file, schema, records)
    assert new_file.getvalue().strip() == json.dumps({"union": {'int': 321}})

    # A non-null, named type is encoded as an object with a key for the name
    records = [{'union': {'union_record_field': 'union_field'}}]
    new_file = StringIO()
    json_writer(new_file, schema, records)
    expected = json.dumps({
        "union": {
            'test.union_record': {
                'union_record_field': 'union_field'
            }
        }
    })
    assert new_file.getvalue().strip() == expected