"""Unit tests for demographics Marhsmallow schemas.""" import pytest from sound_recordings.schemas.downloads import ( DownloadsAllSchema, DownloadsByCountrySchema, DownloadsByProductSchema, DownloadsByStoreSchema, DownloadsSchema, ) ITEMS = [ [ {"downloads": 273, "date": "2021-01-06T00:00:00Z"}, {"downloads": 274, "date": "2021-01-07T00:00:00Z"}, ], [ {"downloads": 173, "date": "2021-01-06T00:00:00Z"}, {"downloads": 174, "date": "2021-01-07T00:00:00Z"}, ], ] @pytest.fixture def sources(): """Fixture for sources.""" return [ {"id": 1, "name": "Apple Music", "error": {"code": "rubbish"}}, {"id": 187, "name": "Amazon Music"}, ] @pytest.fixture def stores(): """Fixture for stores.""" return [ {"id": 1, "name": "Apple Music", "items": ITEMS[0]}, {"id": 187, "name": "Amazon Music", "items": ITEMS[1]}, ] @pytest.fixture def countries(): """Fixture for countries.""" return [{"code": "GB", "items": ITEMS[0]}, {"code": "US", "items": ITEMS[1]}] @pytest.fixture def aggregate(): """Fixture for aggregate.""" return { "items": [ {"downloads": 278, "date": "2021-01-06T00:00:00Z"}, {"downloads": 277, "date": "2021-01-07T00:00:00Z"}, {"downloads": 343, "date": "2021-01-08T00:00:00Z"}, {"downloads": 313, "date": "2021-01-09T00:00:00Z"}, ] } @pytest.fixture def payload_downloads(sources, stores, aggregate): """Return the complete downloads payload.""" return { "isrc": "TEST", "aggregate": aggregate, "sources": sources, "stores": stores, } @pytest.fixture def payload_downloads_all(): """Return the complete downloads payload.""" return {"isrc": "TEST", "items": ITEMS[0]} @pytest.fixture def payload_downloads_by_store(stores): """Return the complete downloads by store payload.""" return {"isrc": "TEST", "stores": stores} @pytest.fixture def payload_downloads_by_country(countries): """Return the complete downloads by country payload.""" return {"isrc": "TEST", "countries": countries} def test_downloads_schema(payload_downloads, aggregate, sources, stores): """Top level downloads marshaller test.""" result = DownloadsSchema().dump(payload_downloads) assert result == { "isrc": "TEST", "aggregate": aggregate, "sources": sources, "stores": stores, } def test_downloads_all_schema(payload_downloads_all): """Top level downloads marshaller test.""" result = DownloadsAllSchema().dump(payload_downloads_all) assert result == {"isrc": "TEST", "items": ITEMS[0]} def test_downloads_by_store_schema(payload_downloads_by_store, stores): """Top level downloads by store marshaller test.""" result = DownloadsByStoreSchema().dump(payload_downloads_by_store) assert result == {"isrc": "TEST", "stores": stores} def test_downloads_by_country_schema(payload_downloads_by_country, countries): """Top level downloads by country marshaller test.""" result = DownloadsByCountrySchema().dump(payload_downloads_by_country) assert result == {"isrc": "TEST", "countries": countries} @pytest.fixture def products(): """Fixture for countries.""" return [ {"product_id": "123", "items": ITEMS[0]}, {"product_id": "321", "items": ITEMS[1]}, ] @pytest.fixture def payload_downloads_by_product(products): """Return the complete downloads by products payload.""" return {"isrc": "TEST", "products": products} def test_downloads_by_product_schema(payload_downloads_by_product, products): """Top level downloads by product schema test.""" result = DownloadsByProductSchema().dump(payload_downloads_by_product) assert result == {"isrc": "TEST", "products": products}