"""Unit tests for store_pricing_scheme model.""" from pricing.models import store_pricing_scheme import pytest from tests.utils import db_operations @pytest.fixture def db_fixture(): """Drop and re-create all the SQLite tables and seed them.""" db_operations.create_tables() db_operations.seed_data() def test_create_store_pricing_scheme(db_fixture): """Test that a store pricing scheme can be created.""" data = { 'store_pricing_scheme_id': 100, 'name': 'Amazon Film', 'store_id': 2, 'pricing_family_id': 1 } result = store_pricing_scheme.create_store_pricing_scheme(data) created_store_pricing_scheme = result.message assert created_store_pricing_scheme['name'] == 'Amazon Film' assert created_store_pricing_scheme['pricing_family_id'] == 1 def test_create_store_pricing_scheme_empty_body(db_fixture): """Test that a store pricing scheme requires fields to be created.""" data = {} result = store_pricing_scheme.create_store_pricing_scheme(data) assert result.status == 400 def test_get_store_pricing_scheme_by_id(db_fixture): """Test that a store pricing scheme can be retrieved by id.""" result = store_pricing_scheme.get_store_pricing_scheme_by_id(1) found_store_pricing_scheme = result.message assert found_store_pricing_scheme['name'] == 'iTunes Film' assert len(found_store_pricing_scheme['store_pricing_tiers']) == 3 def test_update_store_pricing_scheme(db_fixture): """Test that a store pricing scheme can be updated.""" store_pricing_scheme_id = 1 data = { 'name': 'Google Film' } result = store_pricing_scheme.update_store_pricing_scheme_by_id( store_pricing_scheme_id, data) updated = result.message assert updated['name'] == 'Google Film' assert updated['store_pricing_scheme_id'] == 1 assert updated['pricing_family_id'] == 1 assert updated['store_id'] == 1 def test_update_store_pricing_scheme_by_id_empty_body(db_fixture): """Test that a store pricing scheme requires fields to be created.""" data = {} result = store_pricing_scheme.update_store_pricing_scheme_by_id(1, data) assert result.status == 400 def test_update_store_pricing_scheme_by_id_not_found(db_fixture): """Test that update fails if store pricing scheme does not exist.""" data = { 'name': 'Updated Scheme', 'pricing_family_id': 1, 'store_id': 1 } result = store_pricing_scheme.update_store_pricing_scheme_by_id(100, data) assert result.status == 404 def test_get_store_pricing_scheme(db_fixture): """Test that a store pricing scheme can be retrieved.""" store_pricing_scheme_id = 1 result = store_pricing_scheme.get_store_pricing_scheme_by_id( store_pricing_scheme_id) found_store_pricing_scheme = result.message assert found_store_pricing_scheme['name'] == 'iTunes Film' assert len(found_store_pricing_scheme['store_pricing_tiers']) == 3 def test_get_store_pricing_scheme_by_id_not_found(db_fixture): """Test for not found response when no store pricing scheme.""" result = store_pricing_scheme.get_store_pricing_scheme_by_id(100) assert result.status == 404 def test_get_by_pricing_family_and_store_id(db_fixture): """Test that a scheme can be retrieved by family and store id.""" result = store_pricing_scheme.get_by_pricing_family_and_store_id(1, 1) assert result found_store_pricing_scheme = result.message assert found_store_pricing_scheme['name'] == 'iTunes Film' assert len(found_store_pricing_scheme['store_pricing_tiers']) == 3 def test_get_by_pricing_family_and_store_id_not_found_family(db_fixture): """Test that a scheme can be retrieved by family and store id.""" result = store_pricing_scheme.get_by_pricing_family_and_store_id(100, 1) assert result.status == 404 def test_get_by_pricing_family_and_store_id_not_found_store(db_fixture): """Test that a scheme can be retrieved by family and store id.""" result = store_pricing_scheme.get_by_pricing_family_and_store_id(1, 100) assert result.status == 404 def test_get_by_store_id(db_fixture): """Test that a list of store pricing schemes can be returned.""" store_id = 1 result = store_pricing_scheme.get_by_store_id(store_id) assert result items = result.message['items'] assert len(items) == 1 assert items[0]['name'] == 'iTunes Film' def test_get_by_pricing_family_id(db_fixture): """Test that a list of store pricing schemes can be returned.""" pricing_family_id = 1 result = store_pricing_scheme.get_by_pricing_family_id(pricing_family_id) assert result items = result.message['items'] assert len(items) == 1 assert items[0]['name'] == 'iTunes Film'