33
44import pytest
55import turu .core .mock
6- from pydantic import BaseModel
6+ from turu . core . features import USE_PYDANTIC
77from 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
2836class 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" )
0 commit comments