Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Replace table prefix as well #44

Merged
merged 3 commits into from
Feb 14, 2024
Merged

Fix: Replace table prefix as well #44

merged 3 commits into from
Feb 14, 2024

Conversation

Somtom
Copy link
Collaborator

@Somtom Somtom commented Feb 8, 2024

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: #43

What changed

  • Additionally to purely replace the tables, we loop over the columns as well to replace the references there as well

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)

@Somtom Somtom merged commit de859a5 into main Feb 14, 2024
3 checks passed
@Somtom Somtom deleted the allow-colum-table-ref branch February 14, 2024 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When a table is not expressed as alias the referencing isn't correct later
2 participants