Skip to content

Commit c632953

Browse files
committed
Additional test cases for shadow fields
1 parent 138fa94 commit c632953

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

tests/resources.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Meta:
2929
class Book(LibraryBook):
3030
class Meta:
3131
key_field_name = "isbn"
32+
allow_field_shadowing = True
3233

3334
title = odin.StringField()
3435
isbn = odin.StringField()
@@ -54,6 +55,17 @@ def __eq__(self, other):
5455
return False
5556

5657

58+
class AltBook(Book):
59+
"""A special case of book.
60+
61+
This is a special case of book that has a limit on the length of the title.
62+
63+
And to test overriding fields.
64+
"""
65+
66+
title = odin.StringField(max_length=10)
67+
68+
5769
class From(enum.Enum):
5870
Dumpster = "dumpster"
5971
Shop = "shop"

tests/resources_annotated.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Book(LibraryBook):
3636
class Meta:
3737
namespace = "annotated"
3838
key_field_name = "isbn"
39+
allow_field_shadowing = True
3940

4041
title: str
4142
isbn: str = odin.Options(
@@ -70,6 +71,17 @@ def __eq__(self, other):
7071
return False
7172

7273

74+
class AltBook(Book):
75+
"""A special case of book.
76+
77+
This is a special case of book that has a limit on the length of the title.
78+
79+
And to test overriding fields.
80+
"""
81+
82+
title: str = odin.Options(max_length=10)
83+
84+
7385
class From(enum.Enum):
7486
Dumpster = "dumpster"
7587
Shop = "shop"

tests/test_annotated_resources.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
InheritedCamelCaseResource,
1111
Library,
1212
Publisher,
13+
AltBook,
1314
)
1415

1516

@@ -32,9 +33,13 @@ def test_fields_are_identified_in_correct_order(self):
3233

3334
class TestAnnotatedResource:
3435
def test_fields_are_inherited_from_parent_resources(self):
35-
meta = getmeta(IdentifiableBook)
36+
target = getmeta(IdentifiableBook)
3637

37-
assert [f.name for f in meta.fields] == [
38+
actual = target.fields
39+
40+
assert [f.name for f in actual] == [
41+
"id",
42+
"purchased_from",
3843
"title",
3944
"isbn",
4045
"num_pages",
@@ -44,8 +49,6 @@ def test_fields_are_inherited_from_parent_resources(self):
4449
"published",
4550
"authors",
4651
"publisher",
47-
"id",
48-
"purchased_from",
4952
]
5053

5154
def test_cached_properties_work_as_expected(self):
@@ -64,7 +67,15 @@ def test_field_name_format(self):
6467

6568
actual = [field.name for field in options.fields]
6669

67-
assert actual == ["fullName", "yearOfBirth", "emailAddress"]
70+
assert actual == ["emailAddress", "fullName", "yearOfBirth"]
71+
72+
def test_shadow_fields_are_identified(self):
73+
target = getmeta(AltBook)
74+
75+
actual = target.shadow_fields
76+
77+
assert isinstance(actual, tuple)
78+
assert [f.name for f in actual] == ["title"]
6879

6980

7081
class TestAnnotatedKitchenSink:

tests/test_resources.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
)
1212
from odin.utils import getmeta, snake_to_camel
1313

14-
from .resources import Book, BookProxy, InheritedCamelCaseResource, Library, Subscriber
14+
from .resources import (
15+
Book,
16+
BookProxy,
17+
InheritedCamelCaseResource,
18+
Library,
19+
Subscriber,
20+
AltBook,
21+
)
1522

1623

1724
class Author(odin.Resource):
@@ -226,6 +233,14 @@ def test_use_a_reserved_field(self):
226233
class InvalidFieldsResource(Resource):
227234
fields = odin.StringField()
228235

236+
def test_shadow_fields_are_identified(self):
237+
target = getmeta(AltBook)
238+
239+
actual = target.shadow_fields
240+
241+
assert isinstance(actual, tuple)
242+
assert [f.name for f in actual] == ["title"]
243+
229244

230245
class TestConstructionMethods:
231246
def test_build_object_graph_empty_dict_no_clean(self):
@@ -393,3 +408,13 @@ def test_field_name_format(self):
393408
actual = [field.name for field in options.fields]
394409

395410
assert actual == ["fullName", "yearOfBirth", "emailAddress"]
411+
412+
def test_shadowing(self):
413+
actual = AltBook(title="Foo", isbn="123456")
414+
415+
getmeta(actual)
416+
417+
book = Book(title="Foo", isbn="123456")
418+
419+
assert book.isbn == "123456"
420+
assert book.title == "Foo"

0 commit comments

Comments
 (0)