Skip to content

Commit 007943f

Browse files
test: fix tests and linting and remove all default_connections
1 parent 56e217f commit 007943f

6 files changed

Lines changed: 36 additions & 30 deletions

File tree

src/dve/core_engine/backends/implementations/duckdb/readers/xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import duckdb
77
import polars as pl
8-
from duckdb import DuckDBPyConnection, DuckDBPyRelation, default_connection
8+
from duckdb import DuckDBPyConnection, DuckDBPyRelation
99
from pydantic import BaseModel
1010

1111
from dve.core_engine.backends.base.reader import read_function

tests/test_core_engine/test_backends/test_implementations/test_duckdb/test_data_contract.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from pathlib import Path
33
from typing import Any, Dict, List, Tuple
44

5+
import duckdb
56
import pytest
6-
from duckdb import DuckDBPyRelation, default_connection
7+
from duckdb import DuckDBPyRelation
78
from duckdb.typing import DuckDBPyType
89

910
from dve.core_engine.backends.implementations.duckdb.contract import DuckDBDataContract
@@ -34,7 +35,7 @@
3435

3536
def test_duckdb_data_contract_csv(temp_csv_file):
3637
uri, _, _, mdl = temp_csv_file
37-
connection = default_connection
38+
connection = duckdb.connect(":memory:")
3839

3940
contract_meta = json.dumps(
4041
{
@@ -106,7 +107,7 @@ def test_duckdb_data_contract_csv(temp_csv_file):
106107

107108
def test_duckdb_data_contract_xml(temp_xml_file):
108109
uri, header_model, header_data, class_model, class_data = temp_xml_file
109-
connection = default_connection
110+
connection = duckdb.connect()
110111
contract_meta = json.dumps(
111112
{
112113
"contract": {
@@ -153,10 +154,10 @@ def test_duckdb_data_contract_xml(temp_xml_file):
153154
contract_dict = json.loads(contract_meta).get("contract")
154155
entities: Dict[str, DuckDBPyRelation] = {
155156
"test_header": DuckDBXMLStreamReader(
156-
ddb_connection=connection, root_tag="root", record_tag="Header"
157+
connection=connection, root_tag="root", record_tag="Header"
157158
).read_to_relation(str(uri), "header", header_model),
158159
"test_class_info": DuckDBXMLStreamReader(
159-
ddb_connection=connection, root_tag="root", record_tag="ClassData"
160+
connection=connection, root_tag="root", record_tag="ClassData"
160161
).read_to_relation(str(uri), "class_info", class_model),
161162
}
162163
entity_locations: dict[str, URI] = {}
@@ -218,7 +219,7 @@ def test_ddb_data_contract_read_and_write_basic_parquet(
218219
):
219220
# can we read in a stringified parquet and run the data contract on it?
220221
# basic file - simple data structures
221-
connection = default_connection
222+
connection = duckdb.connect(":memory:")
222223
parquet_uri, contract_meta, _ = simple_all_string_parquet
223224
data_contract = DuckDBDataContract(connection)
224225
# check can read
@@ -279,7 +280,7 @@ def test_ddb_data_contract_read_nested_parquet(nested_all_string_parquet):
279280
# can we read in a stringified parquet and run the data contract on it?
280281
# more complex file - nested, arrays of structs
281282
parquet_uri, contract_meta, _ = nested_all_string_parquet
282-
connection = default_connection
283+
connection = duckdb.connect()
283284
data_contract = DuckDBDataContract(connection)
284285
# check can read
285286
entity = data_contract.read_parquet(path=parquet_uri)
@@ -337,7 +338,7 @@ def test_ddb_data_contract_read_nested_parquet(nested_all_string_parquet):
337338
def test_duckdb_data_contract_custom_error_details(nested_all_string_parquet_w_errors,
338339
nested_parquet_custom_dc_err_details):
339340
parquet_uri, contract_meta, _ = nested_all_string_parquet_w_errors
340-
connection = default_connection
341+
connection = duckdb.connect(":memory:")
341342
data_contract = DuckDBDataContract(connection)
342343

343344
entity = data_contract.read_parquet(path=parquet_uri)

tests/test_core_engine/test_backends/test_implementations/test_duckdb/test_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from typing import Iterator, List, Optional, Set, Tuple, Type
66

7+
import duckdb
78
import numpy as np
89
import polars as pl
910
import pytest
@@ -12,7 +13,6 @@
1213
ConstantExpression,
1314
DuckDBPyRelation,
1415
StarExpression,
15-
default_connection,
1616
)
1717

1818
from dve.core_engine.backends.base.core import EntityManager
@@ -49,7 +49,7 @@
4949
simple_typecast_parquet,
5050
)
5151

52-
DUCKDB_STEP_BACKEND = DuckDBStepImplementations(default_connection)
52+
DUCKDB_STEP_BACKEND = DuckDBStepImplementations(duckdb.connect())
5353
"""The backend for the duckdb steps."""
5454

5555

tests/test_core_engine/test_backends/test_readers/test_ddb_csv.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from pathlib import Path
33
from tempfile import TemporaryDirectory
44

5+
import duckdb
56
import polars as pl
67
import pytest
7-
from duckdb import DuckDBPyRelation, default_connection
8+
from duckdb import DuckDBPyRelation
89
from pydantic import BaseModel
910

1011
from dve.core_engine.backends.exceptions import EmptyFileError, MessageBearingError
@@ -71,7 +72,7 @@ def temp_empty_csv_file(temp_dir: Path):
7172

7273
def test_ddb_csv_reader_all_str(temp_csv_file):
7374
uri, header, data, mdl = temp_csv_file
74-
reader = DuckDBCSVReader(header=True, delim=",", connection=default_connection)
75+
reader = DuckDBCSVReader(header=True, delim=",", connection=duckdb.connect())
7576
rel: DuckDBPyRelation = reader.read_to_entity_type(
7677
DuckDBPyRelation, str(uri), "test", stringify_model(mdl)
7778
)
@@ -84,7 +85,7 @@ def test_ddb_csv_reader_all_str(temp_csv_file):
8485

8586
def test_ddb_csv_reader_cast(temp_csv_file):
8687
uri, header, data, mdl = temp_csv_file
87-
reader = DuckDBCSVReader(header=True, delim=",", connection=default_connection)
88+
reader = DuckDBCSVReader(header=True, delim=",", connection=duckdb.connect())
8889
rel: DuckDBPyRelation = reader.read_to_entity_type(DuckDBPyRelation, str(uri), "test", mdl)
8990
expected_dtypes = {**{
9091
fld.name: str(get_duckdb_type_from_annotation(fld.annotation))
@@ -98,7 +99,7 @@ def test_ddb_csv_reader_cast(temp_csv_file):
9899

99100
def test_ddb_csv_write_parquet(temp_csv_file):
100101
uri, header, data, mdl = temp_csv_file
101-
reader = DuckDBCSVReader(header=True, delim=",", connection=default_connection)
102+
reader = DuckDBCSVReader(header=True, delim=",", connection=duckdb.connect())
102103
rel: DuckDBPyRelation = reader.read_to_entity_type(
103104
DuckDBPyRelation, str(uri), "test", stringify_model(mdl)
104105
)
@@ -110,7 +111,7 @@ def test_ddb_csv_write_parquet(temp_csv_file):
110111

111112
def test_ddb_csv_read_empty_file(temp_empty_csv_file):
112113
uri, mdl = temp_empty_csv_file
113-
reader = DuckDBCSVReader(header=True, delim=",", connection=default_connection)
114+
reader = DuckDBCSVReader(header=True, delim=",", connection=duckdb.connect())
114115

115116
with pytest.raises(EmptyFileError):
116117
reader.read_to_relation(str(uri), "test", mdl)
@@ -119,7 +120,7 @@ def test_ddb_csv_read_empty_file(temp_empty_csv_file):
119120
def test_polars_to_ddb_csv_reader(temp_csv_file):
120121
uri, header, data, mdl = temp_csv_file
121122
reader = PolarsToDuckDBCSVReader(
122-
header=True, delim=",", quotechar='"', connection=default_connection
123+
header=True, delim=",", quotechar='"', connection=duckdb.connect()
123124
)
124125
entity = reader.read_to_relation(str(uri), "test", mdl)
125126

@@ -141,7 +142,7 @@ def test_ddb_csv_repeating_header_reader_non_duplicate(temp_dir):
141142
file_uri = temp_dir.joinpath("test_header.csv")
142143

143144
reader = DuckDBCSVRepeatingHeaderReader(
144-
header=True, delim=",", quotechar='"', connection=default_connection
145+
header=True, delim=",", quotechar='"', connection=duckdb.connect()
145146
)
146147
entity = reader.read_to_relation(str(file_uri), "test", SimpleHeaderModel)
147148

@@ -162,7 +163,7 @@ def test_ddb_csv_repeating_header_reader_with_more_than_one_set_of_distinct_valu
162163

163164
file_uri = temp_dir.joinpath("test_header.csv")
164165
reader = DuckDBCSVRepeatingHeaderReader(
165-
header=True, delim=",", quotechar='"', connection=default_connection
166+
header=True, delim=",", quotechar='"', connection=duckdb.connect()
166167
)
167168

168169
with pytest.raises(MessageBearingError):
@@ -182,7 +183,7 @@ def test_DuckDBCSVReader_with_null_empty_strings(temp_dir):
182183
header=True,
183184
delim=",",
184185
quotechar='"',
185-
connection=default_connection,
186+
connection=duckdb.connect(),
186187
null_empty_strings=True,
187188
)
188189

@@ -207,7 +208,7 @@ def test_DuckDBCSVRepeatingHeaderReader_with_null_empty_strings(temp_dir):
207208
header=True,
208209
delim=",",
209210
quotechar='"',
210-
connection=default_connection,
211+
connection=duckdb.connect(),
211212
null_empty_strings=True,
212213
)
213214

@@ -230,7 +231,7 @@ def test_PolarsToDuckDBCSVReader_with_null_empty_strings(temp_dir):
230231
header=True,
231232
delim=",",
232233
quotechar='"',
233-
connection=default_connection,
234+
connection=duckdb.connect(),
234235
null_empty_strings=True,
235236
)
236237

tests/test_core_engine/test_backends/test_readers/test_ddb_json.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from tempfile import TemporaryDirectory
55
from typing import List
66

7+
import duckdb
78
import pytest
8-
from duckdb import DuckDBPyRelation, default_connection
9+
from duckdb import DuckDBPyRelation
910
from pydantic import BaseModel
1011

1112
from dve.core_engine.backends.implementations.duckdb.duckdb_helpers import (
@@ -87,17 +88,19 @@ def test_ddb_json_write_parquet(temp_json_file):
8788
)
8889
target_loc: Path = uri.parent.joinpath("test_parquet.parquet").as_posix()
8990
reader.write_parquet(rel, target_loc)
90-
parquet_rel = default_connection.read_parquet(target_loc)
91-
assert parquet_rel.df().to_dict(orient="records") == rel.df().to_dict(orient="records")
91+
with duckdb.connect() as cnn:
92+
parquet_rel = cnn.read_parquet(target_loc)
93+
assert parquet_rel.df().to_dict(orient="records") == rel.df().to_dict(orient="records")
9294

9395
def test_ddb_json_write_parquet_py_iterator(temp_json_file):
9496
uri, _, mdl = temp_json_file
9597
reader = DuckDBJSONReader()
98+
conn = duckdb.connect()
9699
data = list(reader.read_to_py_iterator(uri.as_posix(), "test", stringify_model(mdl)))
97100
target_loc: Path = uri.parent.joinpath("test_parquet.parquet").as_posix()
98-
reader.write_parquet(default_connection.query("select dta.* from (select unnest($data) as dta)",
101+
reader.write_parquet(conn.query("select dta.* from (select unnest($data) as dta)",
99102
params={"data": data}),
100103
target_loc)
101-
parquet_data = sorted(default_connection.read_parquet(target_loc).pl().iter_rows(named=True),
104+
parquet_data = sorted(conn.read_parquet(target_loc).pl().iter_rows(named=True),
102105
key= lambda x: x.get("bigint_field"))
103106
assert parquet_data == list(data)

tests/test_core_engine/test_backends/test_readers/test_ddb_xml.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from tempfile import TemporaryDirectory
44
from typing import Dict, List
55

6+
import duckdb
67
import pytest
7-
from duckdb import DuckDBPyRelation, default_connection
8+
from duckdb import DuckDBPyRelation
89
from lxml import etree as ET
910
from pydantic import BaseModel
1011

@@ -65,7 +66,7 @@ class ClassDataModel(BaseModel):
6566

6667
def test_ddb_xml_reader_all_str(temp_xml_file):
6768
uri, header_model, header_data, class_data_model, class_data = temp_xml_file
68-
ddb_conn = default_connection
69+
ddb_conn = duckdb.connect()
6970
header_reader = DuckDBXMLStreamReader(
7071
connection=ddb_conn, root_tag="root", record_tag="Header"
7172
)
@@ -88,7 +89,7 @@ def test_ddb_xml_reader_all_str(temp_xml_file):
8889

8990
def test_ddb_xml_reader_write_parquet(temp_xml_file):
9091
uri, header_model, header_data, class_data_model, class_data = temp_xml_file
91-
ddb_conn = default_connection
92+
ddb_conn = duckdb.connect()
9293
header_reader = DuckDBXMLStreamReader(
9394
connection=ddb_conn, root_tag="root", record_tag="Header"
9495
)

0 commit comments

Comments
 (0)