Skip to content

Commit 0c24b2a

Browse files
committed
Add tests for AttributeError NoneType 'get_primary_key_field'
1 parent 955b7e1 commit 0c24b2a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818

1919
### Fixed
2020

21+
- Fixed `AttributeError: 'get_primary_key_field'` when None is passed to a field with an optional nested model
22+
2123

2224
## [0.6.0] - 2024-07-01
2325

test/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Library(syn.Model):
5858
new: Optional[Tuple[Book, Author, Book, int]] = None
5959
list_of_tuples: Optional[List[Tuple[str, Book]]] = None
6060
dict_of_models: Optional[Dict[str, Book]] = None
61+
optional_nested: Optional[Book] = None
6162

6263

6364
class AsyncLibrary(asy.Model):

test/test_pydantic_redis.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ def test_update_optional_nested_tuple_of_models(store: Store):
203203
@pytest.mark.parametrize("store", redis_store_fixture)
204204
def test_update_list_of_tuples_of_nested_models(store: Store):
205205
list_of_tuples = [("some book", books[0]), ("book2", books[2])]
206-
data = [Library(name="Babel Library", address="In a book", list_of_tuples=list_of_tuples)]
206+
data = [
207+
Library(
208+
name="Babel Library", address="In a book", list_of_tuples=list_of_tuples
209+
)
210+
]
207211
Library.insert(data)
208212
# the tuple of nested models is automatically inserted
209213
got = sorted(Book.select(), key=lambda x: x.title)
@@ -219,9 +223,13 @@ def test_update_list_of_tuples_of_nested_models(store: Store):
219223
@pytest.mark.parametrize("store", redis_store_fixture)
220224
def test_update_dict_of_models(store: Store):
221225
dict_of_models = {"some book": books[0], "book2": books[2]}
222-
data = [Library(name="Babel Library", address="In a book", dict_of_models=dict_of_models)]
226+
data = [
227+
Library(
228+
name="Babel Library", address="In a book", dict_of_models=dict_of_models
229+
)
230+
]
223231
Library.insert(data)
224-
# the tuple of nested models is automatically inserted
232+
# the dict of nested models is automatically inserted
225233
got = sorted(Book.select(), key=lambda x: x.title)
226234
expected_books = [book for _, book in dict_of_models.items()]
227235
expected = sorted(expected_books, key=lambda x: x.title)
@@ -232,6 +240,34 @@ def test_update_dict_of_models(store: Store):
232240
assert got == expected
233241

234242

243+
@pytest.mark.parametrize("store", redis_store_fixture)
244+
def test_update_filled_optional_nested_model(store: Store):
245+
data = [
246+
Library(name="Babel Library", address="In a book", optional_nested=books[0])
247+
]
248+
Library.insert(data)
249+
250+
got = sorted(Book.select(), key=lambda x: x.title)
251+
assert [books[0]] == got
252+
253+
got = sorted(Library.select(), key=lambda x: x.name)
254+
expected = sorted(data, key=lambda x: x.name)
255+
assert got == expected
256+
257+
258+
@pytest.mark.parametrize("store", redis_store_fixture)
259+
def test_update_unfilled_optional_nested_model(store: Store):
260+
data = [Library(name="Babel Library", address="In a book")]
261+
Library.insert(data)
262+
263+
got = Book.select()
264+
assert got is None
265+
266+
got = sorted(Library.select(), key=lambda x: x.name)
267+
expected = sorted(data, key=lambda x: x.name)
268+
assert got == expected
269+
270+
235271
@pytest.mark.parametrize("store", redis_store_fixture)
236272
def test_select_default(store: Store):
237273
"""Selecting without arguments returns all the book models"""

0 commit comments

Comments
 (0)