Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /squirrels-analytics_squirrels /tests /_model_builder_test.py
| from pathlib import Path | |
| import pytest, asyncio, sqlite3, duckdb, polars as pl | |
| from squirrels._connection_set import ConnectionSet, ConnectionProperties | |
| from squirrels._manifest import ConnectionTypeEnum | |
| from squirrels._models import SourceModel | |
| from squirrels._model_builder import ModelBuilder | |
| from squirrels._model_configs import ColumnConfig | |
| from squirrels._sources import Source, UpdateHints | |
| from squirrels._arguments.init_time_args import ConnectionsArgs | |
| def sqlite_path(): | |
| path = Path("playground/sqlite_test.db") | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| return str(path) | |
| def sqlite_conn(sqlite_path): | |
| conn = sqlite3.connect(sqlite_path) | |
| try: | |
| conn.execute("DROP TABLE IF EXISTS test_table") | |
| conn.execute(""" | |
| CREATE TABLE test_table ( | |
| id INTEGER PRIMARY KEY, | |
| name VARCHAR, | |
| value INTEGER | |
| ) | |
| """) | |
| conn.execute(""" | |
| INSERT INTO test_table (id, name, value) | |
| VALUES (1, 'foo', 100), | |
| (2, 'bar', 200), | |
| (3, 'baz', 300) | |
| """) | |
| conn.commit() | |
| finally: | |
| conn.close() | |
| def connection_set(sqlite_path): | |
| return ConnectionSet({"test_conn": ConnectionProperties(type=ConnectionTypeEnum.CONNECTORX, uri=f"sqlite://{sqlite_path}")}) | |
| def create_model_builder(connection_set): | |
| def _create(static_models): | |
| return ModelBuilder( | |
| _datalake_db_path=':memory:', | |
| _conn_set=connection_set, | |
| _static_models=static_models, | |
| _conn_args=ConnectionsArgs(project_path=".", proj_vars={}, env_vars={}) | |
| ) | |
| return _create | |
| def expected_df0(): | |
| return pl.DataFrame({ | |
| "id": [1, 2, 2, 3], | |
| "name": ["foo", "bar", "bar", "baz"], | |
| "value": [0, 100, 200, 300] | |
| }) | |
| def expected_df1(): | |
| return pl.DataFrame({ | |
| "id": [1, 1, 2, 3], | |
| "name": ["foo", "foo", "bar", "baz"], | |
| "value": [0, 100, 200, 300] | |
| }) | |
| def expected_df2(): | |
| return pl.DataFrame({ | |
| "id": [1, 2, 3], | |
| "name": ["foo", "bar", "baz"], | |
| "value": [0, 200, 300] | |
| }) | |
| def expected_df3(): | |
| return pl.DataFrame({ | |
| "id": [1, 2, 3], | |
| "name": ["foo", "bar", "baz"], | |
| "value": [100, 200, 300] | |
| }) | |
| def test_build_sources_with_increasing_column(request: pytest.FixtureRequest, primary_key: list[str], strictly_increasing: bool, expected_df_name: str, create_model_builder): | |
| connection_set = request.getfixturevalue("connection_set") | |
| source_name = 'test_table' | |
| source = Source( | |
| connection='test_conn', | |
| load_to_vdl=True, | |
| primary_key=primary_key, | |
| update_hints=UpdateHints(increasing_column='value', strictly_increasing=strictly_increasing), | |
| columns=[ | |
| ColumnConfig(name="id", type="BIGINT"), | |
| ColumnConfig(name="name", type="VARCHAR"), | |
| ColumnConfig(name="value", type="BIGINT") | |
| ] | |
| ).finalize_table(source_name) | |
| source_model = SourceModel(source_name, source, conn_set=connection_set) | |
| model_builder = create_model_builder({source_model.name: source_model}) | |
| duckdb_conn = duckdb.connect() | |
| try: | |
| model_builder._attach_connections(duckdb_conn) | |
| cols = source.get_cols_for_create_table_stmt() | |
| duckdb_conn.execute(f"CREATE TABLE test_table ({cols})") | |
| duckdb_conn.execute(""" | |
| INSERT INTO test_table (id, name, value) | |
| VALUES (1, 'foo', 0), (2, 'bar', 100) | |
| """) | |
| asyncio.run(model_builder._build_models(duckdb_conn, select="test_table", full_refresh=False)) | |
| result = duckdb_conn.table("test_table").pl() | |
| finally: | |
| duckdb_conn.close() | |
| expected_df = request.getfixturevalue(expected_df_name) | |
| assert result.equals(expected_df) | |
| def test_build_sources(connection_set, expected_df3: pl.DataFrame, create_model_builder): | |
| source_name = 'test_table' | |
| source = Source( | |
| connection='test_conn', | |
| columns=[ | |
| ColumnConfig(name="id", type="BIGINT"), | |
| ColumnConfig(name="name", type="VARCHAR"), | |
| ColumnConfig(name="value", type="BIGINT") | |
| ], | |
| load_to_vdl=True | |
| ).finalize_table(source_name) | |
| source_model = SourceModel(source_name, source, conn_set=connection_set) | |
| model_builder = create_model_builder({source_model.name: source_model}) | |
| duckdb_conn = duckdb.connect() | |
| try: | |
| model_builder._attach_connections(duckdb_conn) | |
| cols = source.get_cols_for_create_table_stmt() | |
| duckdb_conn.execute(f"CREATE TABLE test_table ({cols})") | |
| duckdb_conn.execute(""" | |
| INSERT INTO test_table (id, name, value) | |
| VALUES (1, 'foo', 0), (2, 'bar', 100) | |
| """) | |
| asyncio.run(model_builder._build_models(duckdb_conn, select=None, full_refresh=False)) | |
| result = duckdb_conn.table("test_table").pl() | |
| finally: | |
| duckdb_conn.close() | |
| assert result.equals(expected_df3) | |
Xet Storage Details
- Size:
- 5.59 kB
- Xet hash:
- 184b668caadfc10c4f37470c940b02fdd2aa2bc6d5acdf91ac255fc4dadb8e7c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.