2022-11-11 16:35:09 +05:30
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Avro parser tests
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
from metadata.parsers.avro_parser import parse_avro_schema
|
|
|
|
|
|
|
|
|
|
|
|
class AvroParserTests(TestCase):
|
|
|
|
"""
|
|
|
|
Check methods from avro_parser.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
sample_avro_schema = """{
|
|
|
|
"namespace": "example.avro",
|
|
|
|
"type": "record",
|
|
|
|
"name": "Order",
|
|
|
|
"fields": [
|
|
|
|
{
|
|
|
|
"name": "order_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "api_client_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "billing_address_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "customer_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "location_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "shipping_address_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "user_id",
|
|
|
|
"type": "int"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "total_price",
|
|
|
|
"type": "double"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "discount_code",
|
|
|
|
"type": "string"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "processed_at",
|
|
|
|
"type": "int"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}"""
|
|
|
|
|
|
|
|
parsed_schema = parse_avro_schema(sample_avro_schema)
|
|
|
|
|
|
|
|
def test_schema_name(self):
|
2023-03-07 06:40:04 -08:00
|
|
|
self.assertEqual(self.parsed_schema[0].name.__root__, "Order")
|
2022-11-11 16:35:09 +05:30
|
|
|
|
|
|
|
def test_schema_type(self):
|
2023-03-07 06:40:04 -08:00
|
|
|
self.assertEqual(self.parsed_schema[0].dataType.name, "RECORD")
|
2022-11-11 16:35:09 +05:30
|
|
|
|
|
|
|
def test_field_names(self):
|
2023-03-07 06:40:04 -08:00
|
|
|
field_names = {
|
|
|
|
str(field.name.__root__) for field in self.parsed_schema[0].children
|
|
|
|
}
|
2022-11-11 16:35:09 +05:30
|
|
|
self.assertEqual(
|
|
|
|
field_names,
|
|
|
|
{
|
|
|
|
"api_client_id",
|
|
|
|
"user_id",
|
|
|
|
"order_id",
|
|
|
|
"discount_code",
|
|
|
|
"location_id",
|
|
|
|
"processed_at",
|
|
|
|
"total_price",
|
|
|
|
"shipping_address_id",
|
|
|
|
"billing_address_id",
|
|
|
|
"customer_id",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_field_types(self):
|
2023-03-07 06:40:04 -08:00
|
|
|
field_types = {
|
|
|
|
str(field.dataType.name) for field in self.parsed_schema[0].children
|
|
|
|
}
|
2022-12-15 16:54:55 +05:30
|
|
|
self.assertEqual(field_types, {"INT", "STRING", "DOUBLE"})
|