Skip to content

Commit b1e0f5d

Browse files
authored
Merge pull request #53 from yassun7010/fix_ci
fix: ci.
2 parents bbf84d8 + 27387dd commit b1e0f5d

File tree

12 files changed

+124
-101
lines changed

12 files changed

+124
-101
lines changed

.github/workflows/test-suite.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
- turu-snowflake
2929
- turu-bigquery
3030
- .
31+
install-flags:
32+
- ""
33+
- "--all-extras"
3134
defaults:
3235
run:
3336
working-directory: ${{ matrix.package }}
@@ -44,7 +47,7 @@ jobs:
4447
poetry --version
4548
- name: Poetry Install Dependencies
4649
run: |
47-
poetry install --no-interaction --all-extras
50+
poetry install --no-interaction ${{ matrix.install-flags }}
4851
- name: Lint
4952
run: |
5053
poetry run task format --diff

turu-core/src/turu/core/cursor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Type,
1111
TypeVar,
1212
Union,
13+
cast,
1314
)
1415

1516
from turu.core.exception import TuruRowTypeMismatchError
@@ -140,13 +141,17 @@ def map_row(row_type: Optional[Type[GenericRowType]], row: Any) -> GenericRowTyp
140141
}
141142
) # type: ignore
142143

143-
if issubclass(row_type, tuple):
144+
elif issubclass(row_type, tuple):
144145
return row_type._make(row) # type: ignore
145146

146-
if USE_PYDANTIC:
147-
if issubclass(row_type, PydanticModel):
148-
return row_type(
149-
**{key: data for key, data in zip(row_type.model_fields.keys(), row)}
150-
)
147+
elif USE_PYDANTIC and issubclass(row_type, PydanticModel):
148+
return row_type(
149+
**{
150+
key: data
151+
for key, data in zip(
152+
cast(PydanticModel, row_type).model_fields.keys(), row
153+
)
154+
}
155+
) # type: ignore
151156

152157
raise TuruRowTypeMismatchError(row_type, row.__class__)
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
from typing_extensions import TypeAlias
2+
3+
14
class _NotSupportFeature:
25
pass
36

47

58
try:
6-
import pydantic # noqa: F401
9+
import pydantic # type: ignore[import]
710

811
USE_PYDANTIC = True
9-
PydanticModel = pydantic.BaseModel
12+
PydanticModel: TypeAlias = pydantic.BaseModel # type: ignore
1013

1114
except ImportError:
1215
USE_PYDANTIC = False
1316

14-
PydanticModel = _NotSupportFeature
17+
PydanticModel: TypeAlias = _NotSupportFeature # type: ignore

turu-core/tests/turu/core/test_mock.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55
import turu.core.mock
6-
from pydantic import BaseModel
6+
from turu.core.features import USE_PYDANTIC
77
from turu.core.mock.exception import (
88
TuruMockStoreDataNotFoundError,
99
TuruMockUnexpectedFetchError,
@@ -21,8 +21,16 @@ class RowDataclass:
2121
id: int
2222

2323

24-
class RowPydantic(BaseModel):
25-
id: int
24+
if USE_PYDANTIC:
25+
from pydantic import BaseModel # type: ignore[import]
26+
27+
class RowPydantic(BaseModel):
28+
id: int
29+
30+
ROW_TYPE_LIST = [RowNamedTuple, RowDataclass, RowPydantic]
31+
32+
else:
33+
ROW_TYPE_LIST = [RowNamedTuple, RowDataclass]
2634

2735

2836
class TestTuruMock:
@@ -57,10 +65,10 @@ def test_execute(self, mock_connection: turu.core.mock.MockConnection):
5765
def test_mock_execute_map_fetchone(
5866
self, mock_connection: turu.core.mock.MockConnection, rowsize: int
5967
):
60-
expected = [RowPydantic(id=i) for i in range(rowsize)]
61-
mock_connection.inject_response(RowPydantic, expected)
68+
expected = [RowDataclass(id=i) for i in range(rowsize)]
69+
mock_connection.inject_response(RowDataclass, expected)
6270

63-
cursor = mock_connection.cursor().execute_map(RowPydantic, "SELECT 1")
71+
cursor = mock_connection.cursor().execute_map(RowDataclass, "SELECT 1")
6472
for i in range(rowsize):
6573
assert cursor.fetchone() == expected[i]
6674

@@ -72,10 +80,10 @@ def test_mock_execute_map_fetchmany(
7280
mock_connection: turu.core.mock.MockConnection,
7381
rowsize: int,
7482
):
75-
expected = [RowPydantic(id=i) for i in range(rowsize)]
76-
mock_connection.inject_response(RowPydantic, expected)
83+
expected = [RowDataclass(id=i) for i in range(rowsize)]
84+
mock_connection.inject_response(RowDataclass, expected)
7785

78-
cursor = mock_connection.cursor().execute_map(RowPydantic, "SELECT 1")
86+
cursor = mock_connection.cursor().execute_map(RowDataclass, "SELECT 1")
7987

8088
assert list(cursor.fetchmany(rowsize)) == expected
8189
assert cursor.fetchone() is None
@@ -84,17 +92,15 @@ def test_mock_execute_map_fetchmany(
8492
def test_mock_execute_map_fetchall(
8593
self, mock_connection: turu.core.mock.MockConnection, rowsize: int
8694
):
87-
expected = [RowPydantic(id=i) for i in range(rowsize)]
88-
mock_connection.inject_response(RowPydantic, expected)
95+
expected = [RowDataclass(id=i) for i in range(rowsize)]
96+
mock_connection.inject_response(RowDataclass, expected)
8997

90-
cursor = mock_connection.cursor().execute_map(RowPydantic, "SELECT 1")
98+
cursor = mock_connection.cursor().execute_map(RowDataclass, "SELECT 1")
9199

92100
assert list(cursor.fetchall()) == expected
93101
assert cursor.fetchall() == []
94102

95-
@pytest.mark.parametrize(
96-
"GenericRowType", [RowNamedTuple, RowDataclass, RowPydantic]
97-
)
103+
@pytest.mark.parametrize("GenericRowType", ROW_TYPE_LIST)
98104
def test_execute_map_by_rowtype(
99105
self, GenericRowType: Any, mock_connection: turu.core.mock.MockConnection
100106
):
@@ -109,30 +115,30 @@ def test_execute_map_multi_call(
109115
self, mock_connection: turu.core.mock.MockConnection
110116
):
111117
expected_array = [
112-
[RowPydantic(id=1), RowPydantic(id=2), RowPydantic(id=3)],
113-
[RowPydantic(id=4), RowPydantic(id=5), RowPydantic(id=6)],
114-
[RowPydantic(id=7), RowPydantic(id=8), RowPydantic(id=9)],
118+
[RowDataclass(id=1), RowDataclass(id=2), RowDataclass(id=3)],
119+
[RowDataclass(id=4), RowDataclass(id=5), RowDataclass(id=6)],
120+
[RowDataclass(id=7), RowDataclass(id=8), RowDataclass(id=9)],
115121
]
116122

117123
for expected in expected_array:
118-
mock_connection.inject_response(RowPydantic, expected)
124+
mock_connection.inject_response(RowDataclass, expected)
119125

120126
cursor = mock_connection.cursor()
121127
for expected in expected_array:
122-
assert cursor.execute_map(RowPydantic, "SELECT 1").fetchall() == expected
128+
assert cursor.execute_map(RowDataclass, "SELECT 1").fetchall() == expected
123129

124130
assert cursor.fetchone() is None
125131

126132
@pytest.mark.parametrize("execition_time", range(5))
127133
def test_execute_map_each_inject_and_execute(
128134
self, execition_time: int, mock_connection: turu.core.mock.MockConnection
129135
):
130-
expected = [RowPydantic(id=i) for i in range(3)]
136+
expected = [RowDataclass(id=i) for i in range(3)]
131137
for _ in range(execition_time):
132-
mock_connection.inject_response(RowPydantic, expected)
138+
mock_connection.inject_response(RowDataclass, expected)
133139
cursor = mock_connection.cursor()
134140

135-
assert cursor.execute_map(RowPydantic, "SELECT 1").fetchall() == expected
141+
assert cursor.execute_map(RowDataclass, "SELECT 1").fetchall() == expected
136142
assert cursor.fetchone() is None
137143

138144
def test_executemany(self, mock_connection: turu.core.mock.MockConnection):
@@ -141,42 +147,45 @@ def test_executemany(self, mock_connection: turu.core.mock.MockConnection):
141147
assert cursor.fetchall() == [(1,), (1,)]
142148

143149
def test_executemany_map(self, mock_connection: turu.core.mock.MockConnection):
144-
expected = [RowPydantic(id=i) for i in range(3)]
145-
mock_connection.inject_response(RowPydantic, expected)
150+
expected = [RowDataclass(id=i) for i in range(3)]
151+
mock_connection.inject_response(RowDataclass, expected)
146152

147153
with mock_connection.executemany_map(
148-
RowPydantic, "SELECT 1", [(), ()]
154+
RowDataclass, "SELECT 1", [(), ()]
149155
) as cursor:
150156
assert cursor.fetchall() == expected
151157
assert cursor.fetchone() is None
152158

153159
def test_multi_injection(self, mock_connection: turu.core.mock.MockConnection):
154-
expected = [RowPydantic(id=i) for i in range(3)]
160+
expected = [RowDataclass(id=i) for i in range(3)]
155161
(
156162
mock_connection.chain()
157-
.inject_response(RowPydantic, expected)
158-
.inject_response(RowPydantic, expected)
159-
.inject_response(RowPydantic, expected)
160-
.inject_response(RowPydantic, expected)
163+
.inject_response(RowDataclass, expected)
164+
.inject_response(RowDataclass, expected)
165+
.inject_response(RowDataclass, expected)
166+
.inject_response(RowDataclass, expected)
161167
)
162168

163169
cursor = mock_connection.cursor()
164170
for _ in range(4):
165-
assert cursor.execute_map(RowPydantic, "SELECT 1").fetchall() == expected
171+
assert cursor.execute_map(RowDataclass, "SELECT 1").fetchall() == expected
166172
assert cursor.fetchone() is None
167173

168174
def test_cursor_iterator(self, mock_connection: turu.core.mock.MockConnection):
169-
expected = [RowPydantic(id=i) for i in range(3)]
170-
mock_connection.inject_response(RowPydantic, expected)
175+
expected = [RowDataclass(id=i) for i in range(3)]
176+
mock_connection.inject_response(RowDataclass, expected)
171177

172178
for i, row in enumerate(
173-
mock_connection.cursor().execute_map(RowPydantic, "SELECT 1")
179+
mock_connection.cursor().execute_map(RowDataclass, "SELECT 1")
174180
):
175181
assert row == expected[i]
176182

183+
@pytest.mark.skipif(not USE_PYDANTIC, reason="pydantic is not found")
177184
def test_inject_response_from_csv(
178185
self, mock_connection: turu.core.mock.MockConnection
179186
):
187+
from pydantic import BaseModel # type: ignore[import]
188+
180189
class Row(BaseModel):
181190
id: int
182191
name: str
@@ -194,16 +203,16 @@ class Row(BaseModel):
194203
]
195204

196205
def test_with_statement(self, mock_connection: turu.core.mock.MockConnection):
197-
expected = [RowPydantic(id=i) for i in range(3)]
206+
expected = [RowDataclass(id=i) for i in range(3)]
198207

199-
mock_connection.inject_response(RowPydantic, expected)
208+
mock_connection.inject_response(RowDataclass, expected)
200209

201-
with mock_connection.execute_map(RowPydantic, "SELECT 1") as cursor:
210+
with mock_connection.execute_map(RowDataclass, "SELECT 1") as cursor:
202211
assert cursor.fetchall() == expected
203212
assert cursor.fetchone() is None
204213

205214
def test_inject_execption(self, mock_connection: turu.core.mock.MockConnection):
206-
mock_connection.inject_response(RowPydantic, ValueError("test"))
215+
mock_connection.inject_response(RowDataclass, ValueError("test"))
207216

208217
with pytest.raises(ValueError):
209-
mock_connection.execute_map(RowPydantic, "SELECT 1")
218+
mock_connection.execute_map(RowDataclass, "SELECT 1")

turu-core/tests/turu/core/test_record.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import tempfile
2+
from dataclasses import dataclass
23
from pathlib import Path
34
from textwrap import dedent
45
from typing import Any, Literal
56

67
import pytest
78
import turu.core.mock
8-
from pydantic import BaseModel
99
from turu.core.exception import TuruRowTypeNotSupportedError
1010
from turu.core.record import RecordCursor, record_to_csv
1111
from typing_extensions import Never, Self
1212

1313

14-
class RowPydantic(BaseModel):
14+
@dataclass
15+
class Row:
1516
id: int
1617
name: str
1718

@@ -63,13 +64,13 @@ def test_record_to_csv_execute_tuple_without_header(
6364
def test_record_to_csv_execute_map(
6465
self, mock_connection: turu.core.mock.MockConnection
6566
):
66-
expected = [RowPydantic(id=i, name=f"name{i}") for i in range(5)]
67-
mock_connection.inject_response(RowPydantic, expected)
67+
expected = [Row(id=i, name=f"name{i}") for i in range(5)]
68+
mock_connection.inject_response(Row, expected)
6869

6970
with tempfile.NamedTemporaryFile() as file:
7071
with record_to_csv(
7172
file.name,
72-
mock_connection.execute_map(RowPydantic, "select 1, 'name"),
73+
mock_connection.execute_map(Row, "select 1, 'name"),
7374
) as cursor:
7475
assert cursor.fetchall() == expected
7576

@@ -90,13 +91,13 @@ def test_record_to_csv_execute_map(
9091
def test_record_to_csv_execute_map_without_header_options(
9192
self, mock_connection: turu.core.mock.MockConnection
9293
):
93-
expected = [RowPydantic(id=i, name=f"name{i}") for i in range(5)]
94-
mock_connection.inject_response(RowPydantic, expected)
94+
expected = [Row(id=i, name=f"name{i}") for i in range(5)]
95+
mock_connection.inject_response(Row, expected)
9596

9697
with tempfile.NamedTemporaryFile() as file:
9798
with record_to_csv(
9899
file.name,
99-
mock_connection.execute_map(RowPydantic, "select 1, 'name"),
100+
mock_connection.execute_map(Row, "select 1, 'name"),
100101
header=False,
101102
) as cursor:
102103
assert cursor.fetchall() == expected
@@ -117,13 +118,13 @@ def test_record_to_csv_execute_map_without_header_options(
117118
def test_record_to_csv_execute_map_with_limit_options(
118119
self, mock_connection: turu.core.mock.MockConnection
119120
):
120-
expected = [RowPydantic(id=i, name=f"name{i}") for i in range(5)]
121-
mock_connection.inject_response(RowPydantic, expected)
121+
expected = [Row(id=i, name=f"name{i}") for i in range(5)]
122+
mock_connection.inject_response(Row, expected)
122123

123124
with tempfile.NamedTemporaryFile() as file:
124125
with record_to_csv(
125126
file.name,
126-
mock_connection.execute_map(RowPydantic, "select 1, 'name"),
127+
mock_connection.execute_map(Row, "select 1, 'name"),
127128
limit=3,
128129
) as cursor:
129130
assert cursor.fetchall() == expected
@@ -146,13 +147,13 @@ def test_record_to_csv_execute_map_with_enable_options(
146147
mock_connection: turu.core.mock.MockConnection,
147148
enable: Literal["true", True],
148149
):
149-
expected = [RowPydantic(id=i, name=f"name{i}") for i in range(5)]
150-
mock_connection.inject_response(RowPydantic, expected)
150+
expected = [Row(id=i, name=f"name{i}") for i in range(5)]
151+
mock_connection.inject_response(Row, expected)
151152

152153
with tempfile.NamedTemporaryFile() as file:
153154
with record_to_csv(
154155
file.name,
155-
mock_connection.execute_map(RowPydantic, "select 1, 'name"),
156+
mock_connection.execute_map(Row, "select 1, 'name"),
156157
enable=enable,
157158
) as cursor:
158159
assert isinstance(cursor, RecordCursor)
@@ -178,13 +179,13 @@ def test_record_to_csv_execute_map_with_disable_options(
178179
mock_connection: turu.core.mock.MockConnection,
179180
enable: Literal["false", False, None],
180181
):
181-
expected = [RowPydantic(id=i, name=f"name{i}") for i in range(5)]
182-
mock_connection.inject_response(RowPydantic, expected)
182+
expected = [Row(id=i, name=f"name{i}") for i in range(5)]
183+
mock_connection.inject_response(Row, expected)
183184

184185
with tempfile.NamedTemporaryFile() as file:
185186
with record_to_csv(
186187
file.name,
187-
mock_connection.execute_map(RowPydantic, "select 1, 'name"),
188+
mock_connection.execute_map(Row, "select 1, 'name"),
188189
enable=enable,
189190
) as cursor:
190191
assert not isinstance(cursor, RecordCursor)

0 commit comments

Comments
 (0)