DeepLcom / sql-mock

A Python library to test your SQL models using mocked input data
MIT License
34 stars 5 forks source link

Error for BigQueryMockTable -> "E KeyError: 'relation_name'" #47

Closed patygold closed 8 months ago

patygold commented 8 months ago

I'm using V1 of pydantic, thus I've installed the package using this branch: https://github.com/DeepLcom/sql-mock/tree/pydantic-v1/docs

Added the config in my conftest.py: SQLMockConfig.set_dbt_manifest_path("../dbt/parkdepot/target/manifest.json") Created two mocks: 1- A source:

@dbt_source_meta(source_name="postgres", table_name="car_owner")
class PostgresCarOwner(BigQueryMockTable):
    id = col.Int(default=1)
    person_id = col.Int(default=1)
    company_id = col.Int(default=1)
    request_id = col.Int(default=1)

    plate = col.String(default="test1")

2- A model:

@dbt_model_meta(model_name="stg_postgres__car_owner")
class StgPostgresCarOwner(BigQueryMockTable):
    owner_id = col.Int(default=1)
    person_id = col.Int(default=1)
    company_id = col.Int(default=1)
    request_id = col.Int(default=1)

    plate = col.String(default="test1")

And then when testing the model, like this:

class TestStgPostgresCarOwner:
    def test_stg_postgres__car_owner(self):
        source_data = [{"id": 1}, {"id": 2}, {"id": 3}]
        expected_output = [{"owner_id": 1}, {"owner_id": 2}, {"owner_id": 3}]

        source = PostgresCarOwner.from_dicts(source_data)
        final = StgPostgresCarOwner.from_mocks(input_data=[source])

        final.assert_equal(expected_output)

I get the error while importing the tests:

parkdepot/.venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:178: in exec_module
    exec(co, module.__dict__)
parkdepot/repository_parkdepot_tests/dbt/staging/postgres/test_stg_postgres__car_owner.py:2: in <module>
    from repository_parkdepot_tests.dbt.staging.postgres.mocked_stg_postgres__car_owner import StgPostgresCarOwner
parkdepot/repository_parkdepot_tests/dbt/staging/postgres/mocked_stg_postgres__car_owner.py:7: in <module>
    class StgPostgresCarOwner(BigQueryMockTable):
parkdepot/.venv/lib/python3.9/site-packages/sql_mock/dbt.py:101: in decorator
    dbt_meta = _get_model_metadata_from_dbt_manifest(manifest_path=path, model_name=model_name)
parkdepot/.venv/lib/python3.9/site-packages/sql_mock/dbt.py:31: in _get_model_metadata_from_dbt_manifest
    "table_ref": node["relation_name"],
E   KeyError: 'relation_name'

Versions:

Somtom commented 8 months ago

@patygold I realized that the pydantic-v1 branch was a bit outdated. Would you mind double-checking whether it works on the latest version?

In the meanwhile we improved the dbt support and you would need to set the path to your project yaml file

SQLMockConfig.set_dbt_project_path('/path/to/your/dbt_project.yml')

Let me know whether this works with the latest update :)

patygold commented 8 months ago

@Somtom Unfortunately, it is still not working. I've re-installed the package with the newest version, changed the SQLMockConfig to set dbt project path as specified, and also changed all the mocked models to inherit from BigQueryTableMock (in the old version it was BigQueryMockTable).

The problem now seems to lie with the SQLMockConfig:

parkdepot/repository_parkdepot_tests/dbt/staging/postgres/mocked_stg_postgres__car_owner.py:7: in <module>
    class StgPostgresCarOwner(BigQueryTableMock):
parkdepot/.venv/lib/python3.9/site-packages/sql_mock/dbt.py:114: in decorator
    dbt_meta = _get_model_metadata(project_path=path, model_name=model_name)
parkdepot/.venv/lib/python3.9/site-packages/sql_mock/dbt.py:45: in _get_model_metadata
    "query_path": os.path.join(project_dir, node["compiled_path"]),
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py:90: in join
    genericpath._check_arg_types('join', a, *p)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/genericpath.py:152: in _check_arg_types
    raise TypeError(f'{funcname}() argument must be str, bytes, or '
E   TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'

At the moment, my config looks like this:

SQLMockConfig.set_dbt_project_path("/Users/patricia.goldberg/Documents/ParkDepot/dagster/parkdepot/dbt/parkdepot/dbt_project.yml")

Adding the full path of the file, just to test it.

And this is indeed the correct path of this file in my local machine:

(.venv) patricia.goldberg@parkdepot:ls /Users/patricia.goldberg/Documents/ParkDepot/dagster/parkdepot/dbt/parkdepot | grep dbt_project.yml
dbt_project.yml
(.venv) patricia.goldberg@parkdepot:
patygold commented 8 months ago

As discussed, the last error was solved by compiling the whole dbt project again. This issue was solved then! Thank you!