From 7b4ef06dd852659a0fbe5aa4f30d1fcddd901022 Mon Sep 17 00:00:00 2001 From: Rodrigo Lopes de Almeida Date: Wed, 21 Oct 2020 14:18:56 +0200 Subject: [PATCH] tests: added test for nestedattr --- tests/test_nestedattr.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/test_nestedattr.py diff --git a/tests/test_nestedattr.py b/tests/test_nestedattr.py new file mode 100644 index 0000000..8d8d674 --- /dev/null +++ b/tests/test_nestedattr.py @@ -0,0 +1,44 @@ +from datetime import date +from pprint import pprint + +from marshmallow import Schema, fields + +from marshmallow_utils.fields import nestedattr + + +class ArtistSchema(Schema): + """Artist Schema.""" + + name = fields.Str() + + +class AlbumSchema(Schema): + """Album Schema.""" + + artist = nestedattr.NestedAttribute(ArtistSchema()) + + +class MyAlbum(dict): + + @property + def artist(self): + return {'name': 'property'} + + +album = MyAlbum( + artist={'name': 'david'}, # this shouldn't be used by the schema + title="Hunky Dory", + release_date=date(1971, 12, 17) +) + + +def test_test_schema(): + album = MyAlbum( + title="Hunky Dory", + release_date=date(1971, 12, 17) + ) + + schema = AlbumSchema() + result = schema.dump(album) + + assert result['artist'] == {'name': 'property'}