DeepLcom / sql-mock

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

Fix: Replace table prefix as well #44

Closed Somtom closed 7 months ago

Somtom commented 7 months ago

Problem context

SQLMock currently has issues if the table name is used as a prefix to columns in the query and is not able to properly replace it.

Closes: https://github.com/DeepLcom/sql-mock/issues/43

What changed

Testing

I added a new test but also tested the case provided in the issue (with a small correction that it returns the expected value):

from sql_mock.clickhouse.table_mocks import ClickHouseTableMock
from sql_mock.table_mocks import table_meta
import sql_mock.clickhouse.column_mocks as col

QUERY_SIMPLE = """
SELECT col1
FROM bee as b
JOIN a ON a.col1 = b.col1
"""

@table_meta(table_ref="a")
class aMock(ClickHouseTableMock):
    col1 = col.String(default="1")

@table_meta(table_ref="bee")
class bMock(ClickHouseTableMock):
    col1 = col.String(default="1")

@table_meta(query=QUERY_SIMPLE)
class BugTableMock(ClickHouseTableMock):
    col1 = col.String(default="1")

def test_working():
    input_table_mock_1 = aMock([{}])
    input_table_mock_2 = bMock([{}])

    res = BugTableMock.from_mocks(input_data=[
        input_table_mock_1, input_table_mock_2])

    expected = [{'col1': '1'}]
    res.assert_equal(expected)