|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, Mapping |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +import pytest |
| 8 | +from source_us_census.components import USCensusSchema |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class MockConfig: |
| 13 | + query_params: str = None |
| 14 | + |
| 15 | + def get(self, key): |
| 16 | + if key == "query_params": |
| 17 | + return self.query_params |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def census_schema(): |
| 22 | + def _create_schema(query_params=None): |
| 23 | + config = MockConfig(query_params=query_params) |
| 24 | + return USCensusSchema(config=config) |
| 25 | + return _create_schema |
| 26 | + |
| 27 | + |
| 28 | +def test_get_json_schema_basic_case(census_schema): |
| 29 | + schema_instance = census_schema(query_params="get=NAME,POP&for=state:*") |
| 30 | + schema = schema_instance.get_json_schema() |
| 31 | + |
| 32 | + expected_properties = { |
| 33 | + "NAME": {"type": "string"}, |
| 34 | + "POP": {"type": "string"}, |
| 35 | + "state": {"type": "string"} |
| 36 | + } |
| 37 | + |
| 38 | + assert schema["properties"] == expected_properties |
| 39 | + assert schema["$schema"] == "http://json-schema.org/draft-07/schema#" |
| 40 | + assert schema["type"] == "object" |
| 41 | + assert schema["additionalProperties"] is True |
| 42 | + |
| 43 | + |
| 44 | +def test_get_json_schema_with_get_param(census_schema): |
| 45 | + schema_instance = census_schema(query_params="get=NAME,AGE,EMPLOYMENT") |
| 46 | + schema = schema_instance.get_json_schema() |
| 47 | + |
| 48 | + expected_properties = { |
| 49 | + "NAME": {"type": "string"}, |
| 50 | + "AGE": {"type": "string"}, |
| 51 | + "EMPLOYMENT": {"type": "string"} |
| 52 | + } |
| 53 | + |
| 54 | + assert schema["properties"] == expected_properties |
| 55 | + |
| 56 | + |
| 57 | +def test_get_json_schema_with_for_param(census_schema): |
| 58 | + schema_instance = census_schema(query_params="for=county:1234") |
| 59 | + schema = schema_instance.get_json_schema() |
| 60 | + |
| 61 | + expected_properties = { |
| 62 | + "county": {"type": "string"} |
| 63 | + } |
| 64 | + |
| 65 | + assert schema["properties"] == expected_properties |
| 66 | + |
| 67 | + |
| 68 | +def test_get_json_schema_with_additional_params(census_schema): |
| 69 | + schema_instance = census_schema(query_params="get=NAME&year=2020&for=us:*") |
| 70 | + schema = schema_instance.get_json_schema() |
| 71 | + |
| 72 | + expected_properties = { |
| 73 | + "NAME": {"type": "string"}, |
| 74 | + "year": {"type": "string"}, |
| 75 | + "us": {"type": "string"} |
| 76 | + } |
| 77 | + |
| 78 | + assert schema["properties"] == expected_properties |
| 79 | + |
| 80 | + |
| 81 | +def test_get_json_schema_no_query_params(census_schema): |
| 82 | + schema_instance = census_schema(query_params=None) |
| 83 | + schema = schema_instance.get_json_schema() |
| 84 | + |
| 85 | + expected_properties = { |
| 86 | + "{ @context: https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld": {"type": "string"} |
| 87 | + } |
| 88 | + |
| 89 | + assert schema["properties"] == expected_properties |
0 commit comments