Skip to content

Commit f214373

Browse files
committed
IterableResult must be indexable
1 parent d1636a0 commit f214373

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

pydanticrud/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def __len__(self):
2626
def __iter__(self):
2727
return self
2828

29+
def __getitem__(self, indices):
30+
return self.records.__getitem__(indices)
31+
2932
def __next__(self):
3033
try:
3134
member = self.records[self._current_index]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydanticrud"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
description = "Supercharge your Pydantic models with CRUD methods and a pluggable backend"
55
authors = ["Timothy Farrell <[email protected]>"]
66
license = "MIT"

tests/test_model.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def __init__(self, cfg):
1111
def get(cls, id):
1212
pass
1313

14+
@classmethod
15+
def query(cls, id):
16+
pass
17+
1418

1519
class Model(BaseModel):
1620
id: int
@@ -38,5 +42,19 @@ def test_model_backend_get():
3842
assert m.total == 3.0
3943

4044

45+
def test_model_backend_query():
46+
with patch.object(
47+
FalseBackend, "query", return_value=[dict(id=1, name="two", total=3.0)]
48+
) as mock_query:
49+
m = Model.query(2)
50+
51+
mock_query.assert_called_with(2)
52+
assert m.count is None # In this case the backend did not provide a total count.
53+
assert len(m[:]) == 1 # .. but we can cast to a list and get that length.
54+
assert m[0].id == 1
55+
assert m[0].name == "two"
56+
assert m[0].total == 3.0
57+
58+
4159
def test_model_table_name_from_title():
4260
assert Model.get_table_name() == Model.Config.title.lower()

0 commit comments

Comments
 (0)