Skip to content

Commit 781b688

Browse files
Update query to use UUID for id.
1 parent ec3e782 commit 781b688

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

db_wrapper/model/async_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(
8989
super().__init__(table, return_constructor)
9090
self._client = client
9191

92-
async def one_by_id(self, id_value: str, changes: Dict[str, Any]) -> T:
92+
async def one_by_id(self, id_value: UUID, changes: Dict[str, Any]) -> T:
9393
"""Apply changes to row with given id.
9494
9595
Arguments:

db_wrapper/model/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class UpdateABC(CRUDABC[T]):
134134

135135
def _query_one_by_id(
136136
self,
137-
id_value: str,
137+
id_value: UUID,
138138
changes: Dict[str, Any]
139139
) -> sql.Composed:
140140
"""Build Query to apply changes to row with given id."""
@@ -157,7 +157,7 @@ def compose_changes(changes: Dict[str, Any]) -> sql.Composed:
157157
).format(
158158
table=self._table,
159159
changes=compose_changes(changes),
160-
id_value=sql.Literal(id_value),
160+
id_value=sql.Literal(str(id_value)),
161161
)
162162

163163
return query

db_wrapper/model/sync_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(
8888
super().__init__(table, return_constructor)
8989
self._client = client
9090

91-
def one_by_id(self, id_value: str, changes: Dict[str, Any]) -> T:
91+
def one_by_id(self, id_value: UUID, changes: Dict[str, Any]) -> T:
9292
"""Apply changes to row with given id.
9393
9494
Arguments:

test/test_model.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@
3434
)
3535

3636

37-
# Generic doesn't need a more descriptive name
38-
# pylint: disable=invalid-name
39-
T = TypeVar('T', bound=ModelData)
40-
41-
42-
def setupAsync(query_result: List[T]) -> Tuple[AsyncModel[T], AsyncClient]:
37+
def setupAsync(
38+
query_result: List[ModelData]
39+
) -> Tuple[AsyncModel[ModelData], AsyncClient]:
4340
"""Setup helper that returns instances of both a Model & a Client.
4441
4542
Mocks the execute_and_return method on the Client instance to skip
@@ -60,12 +57,14 @@ def setupAsync(query_result: List[T]) -> Tuple[AsyncModel[T], AsyncClient]:
6057
return_value=query_result)
6158

6259
# init a real model with mocked client
63-
model = AsyncModel[Any](client, 'test')
60+
model = AsyncModel[Any](client, 'test', ModelData)
6461

6562
return model, client
6663

6764

68-
def setupSync(query_result: List[T]) -> Tuple[SyncModel[T], SyncClient]:
65+
def setupSync(
66+
query_result: List[ModelData]
67+
) -> Tuple[SyncModel[ModelData], SyncClient]:
6968
"""Setup helper that returns instances of both a Model & a Client.
7069
7170
Mocks the execute_and_return method on the Client instance to skip
@@ -86,7 +85,7 @@ def setupSync(query_result: List[T]) -> Tuple[SyncModel[T], SyncClient]:
8685
return_value=query_result)
8786

8887
# init a real model with mocked client
89-
model = SyncModel[Any](client, 'test')
88+
model = SyncModel[ModelData](client, 'test', ModelData)
9089

9190
return model, client
9291

@@ -100,8 +99,8 @@ async def test_it_correctly_builds_query_with_given_id(self) -> None:
10099
async_model, async_client = setupAsync([item])
101100
sync_model, sync_client = setupSync([item])
102101

103-
await async_model.read.one_by_id(str(item.id))
104-
sync_model.read.one_by_id(str(item.id))
102+
await async_model.read.one_by_id(item.id)
103+
sync_model.read.one_by_id(item.id)
105104

106105
async_query_composed = cast(
107106
helpers.AsyncMock, async_client.execute_and_return).call_args[0][0]
@@ -124,8 +123,8 @@ async def test_it_returns_a_single_result(self) -> None:
124123
item = ModelData(id=uuid4())
125124
async_model, _ = setupAsync([item])
126125
sync_model, _ = setupSync([item])
127-
results = [await async_model.read.one_by_id(str(item.id)),
128-
sync_model.read.one_by_id(str(item.id))]
126+
results = [await async_model.read.one_by_id(item.id),
127+
sync_model.read.one_by_id(item.id)]
129128

130129
for result in results:
131130
with self.subTest():
@@ -139,11 +138,11 @@ async def test_it_raises_exception_if_more_than_one_result(self) -> None:
139138

140139
with self.subTest():
141140
with self.assertRaises(UnexpectedMultipleResults):
142-
await async_model.read.one_by_id(str(item.id))
141+
await async_model.read.one_by_id(item.id)
143142

144143
with self.subTest():
145144
with self.assertRaises(UnexpectedMultipleResults):
146-
sync_model.read.one_by_id(str(item.id))
145+
sync_model.read.one_by_id(item.id)
147146

148147
@ helpers.async_test
149148
async def test_it_raises_exception_if_no_result_to_return(self) -> None:
@@ -265,8 +264,8 @@ async def test_it_returns_the_new_record(self) -> None:
265264
sync_model, _ = setupSync([updated])
266265

267266
results = [
268-
await async_model.update.one_by_id(str(item.id), {'b': 'c'}),
269-
sync_model.update.one_by_id(str(item.id), {'b': 'c'})
267+
await async_model.update.one_by_id(item.id, {'b': 'c'}),
268+
sync_model.update.one_by_id(item.id, {'b': 'c'})
270269
]
271270

272271
for result in results:

0 commit comments

Comments
 (0)