"""Tests for instant grats schemas.""" from datetime import datetime import pytest from backend.schemas.instant_grat import InstantGratBaseSchema from backend.schemas.instant_grat import InstantGratsUpdateSchema @pytest.fixture def test_instant_grat(): """Test instant grat fixture.""" return { 'tuid': 1981851, 'store_id': 181, 'date': '2018-05-20'} def test_instant_grat_base_schema(test_instant_grat): """Test instant grat base schema.""" result = InstantGratBaseSchema().load(test_instant_grat.copy()) test_instant_grat['date'] = datetime.strptime( test_instant_grat['date'], '%Y-%m-%d') assert result.data == test_instant_grat assert result.errors == {} def test_instant_grat_base_schema_required(test_instant_grat): """Test instant grat base schema.""" del test_instant_grat['tuid'] result = InstantGratBaseSchema().load(test_instant_grat.copy()) assert result.errors == { 'tuid': ['Missing data for required field.']} def test_instant_grat_base_schema_bad_tuid_type(test_instant_grat): """Test instant grat base schema.""" test_instant_grat['tuid'] = 'string, not integer' result = InstantGratBaseSchema().load(test_instant_grat.copy()) assert result.errors == {'tuid': ['Not a valid integer.']} def test_instant_grat_base_schema_bad_date(test_instant_grat): """Test instant grat base schema.""" test_instant_grat['date'] = '2018-05-58' result = InstantGratBaseSchema().load(test_instant_grat.copy()) assert result.errors == {'date': ['Not a valid datetime.']} def test_instant_grat_update_schema(test_instant_grat): """Test update instant grat schema.""" test_data = { 'items': [ test_instant_grat.copy(), test_instant_grat.copy() ] } test_data['items'][1]['tuid'] = 1985123 result = InstantGratsUpdateSchema().load(test_data.copy()) for item_num in range(len(test_data['items'])): test_data['items'][item_num]['date'] = datetime.strptime( test_data['items'][item_num]['date'], '%Y-%m-%d') assert result.data == test_data assert result.errors == {}