SRserves85 / avro-to-python

Light tool for compiling avro schema files (.avsc) to python classes
MIT License
25 stars 19 forks source link

Mandatory record fields should not accept None #42

Closed fmiguelez closed 3 months ago

fmiguelez commented 3 months ago

Consider following definition

{
    "type": "record",
    "name": "RecordWithRecord",
    "namespace": "records",
    "fields": [
        {
            "name": "thing1",
            "type": {
                "type": "record",
                "name": "Thing",
                "fields": [
                    {
                        "name": "id",
                        "type": "int"
                    }
                ]
            }
        },
        {
            "name": "thing2",
            "type": "Thing",
            "default": {
                "id": 0
            }
        }
    ]
}

The generated class should check in the setter that a proper value for thing1 field is given (not None). Following modified test (in test_compiled_files.py should pass:

    def test_record_with_record(self):
        """ tests nested records work """

        from records import RecordWithRecord
        from records import Thing

        data1 = {'thing1': {'id': 10}, 'thing2': {'id': 0}}
        data2 = {'thing1': Thing({'id': 10}), 'thing2': Thing({'id': 0})}
        data_with_default = {'thing1': {'id': 10}}
        invalid_data = {'thing1': None}

        record1 = RecordWithRecord(data1)
        record2 = RecordWithRecord(data2)
        record3 = RecordWithRecord(data_with_default)

        self.assertEqual(
            record1.serialize(),
            record2.serialize(),
            'nested records should be equal'
        )

        self.assertEqual(
            record1.serialize(),
            record3.serialize(),
            'records should be equal'
        )

        with self.assertRaises(TypeError):
            RecordWithRecord(invalid_data)