|
| 1 | +import unittest |
| 2 | +from singer.schema_generation import generate_schema |
| 3 | + |
| 4 | +class TestSchemaGeneration(unittest.TestCase): |
| 5 | + def test_simple_schema(self): |
| 6 | + records = [{'a': 1, 'b': 'two', 'c': True, 'dt': '2000-01-01T00:11:22Z'}] |
| 7 | + expected_schema = { |
| 8 | + 'type': ['null', 'object'], |
| 9 | + 'properties': { |
| 10 | + 'a': {'type': ['null', 'integer']}, |
| 11 | + 'b': {'type': ['null', 'string']}, |
| 12 | + 'c': {'type': ['null', 'boolean']}, |
| 13 | + 'dt': {'type': ['null', 'string'], 'format': 'date-time'} |
| 14 | + } |
| 15 | + } |
| 16 | + self.assertEqual(expected_schema, generate_schema(records)) |
| 17 | + |
| 18 | + def test_mix_n_match_records_schema(self): |
| 19 | + records = [ |
| 20 | + {'a': 1, 'b': 'b'}, |
| 21 | + {'a': 'two', 'c': 7, 'd': [1, 'two']}, |
| 22 | + {'a': True, 'c': 7.7, 'd': {'one': 1, 'two': 'two'}} |
| 23 | + ] |
| 24 | + expected_schema = { |
| 25 | + 'type': ['null', 'object'], |
| 26 | + 'properties': { |
| 27 | + 'a': {'type': {'null', 'integer', 'string', 'boolean'}}, |
| 28 | + 'b': {'type': ['null', 'string']}, |
| 29 | + 'c': {'type': {'null', 'integer', 'string'}, 'format': 'singer.decimal'}, |
| 30 | + 'd': { |
| 31 | + 'type': {'null', 'array', 'object'}, |
| 32 | + 'items': {'type': {'null', 'integer', 'string'}}, |
| 33 | + 'properties': {'one': {'type': ['null', 'integer']}, |
| 34 | + 'two': {'type': ['null', 'string']}} |
| 35 | + |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + actual_schema = generate_schema(records) |
| 40 | + actual_schema['properties']['a']['type'] = set(actual_schema['properties']['a']['type']) |
| 41 | + actual_schema['properties']['c']['type'] = set(actual_schema['properties']['c']['type']) |
| 42 | + actual_schema['properties']['d']['type'] = set(actual_schema['properties']['d']['type']) |
| 43 | + actual_schema['properties']['d']['items']['type'] = set(actual_schema['properties']['d']['items']['type']) |
| 44 | + self.assertEqual(expected_schema, actual_schema) |
| 45 | + |
| 46 | + def test_nested_structue_schema(self): |
| 47 | + records = [{'a': {'b': {'c': [{'d': 7}]}, 'e': [[1, 2, 3]]}}] |
| 48 | + expected_schema = { |
| 49 | + 'type': ['null', 'object'], |
| 50 | + 'properties': { |
| 51 | + 'a': { |
| 52 | + 'type': ['null', 'object'], |
| 53 | + 'properties': { |
| 54 | + 'b': { |
| 55 | + 'type': ['null', 'object'], |
| 56 | + 'properties': { |
| 57 | + 'c': { |
| 58 | + 'type': ['null', 'array'], |
| 59 | + 'items': { |
| 60 | + 'type': ['null', 'object'], |
| 61 | + 'properties': {'d': {'type': ['null', 'integer']}} |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + 'e': { |
| 67 | + 'type': ['null', 'array'], |
| 68 | + 'items': { |
| 69 | + 'type': ['null', 'array'], |
| 70 | + 'items': {'type': ['null', 'integer']}} |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + self.assertEqual(expected_schema, generate_schema(records)) |
0 commit comments