"""Unit tests for Promo Player model.""" import datetime import pytest from promo_player.constants import promo_player as promo_player_constants from promo_player.models import promo_player 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(db_fixture): """Test that a promo player can be created.""" data = { 'promo_player_id': 666, 'product_id': 4, 'vendor_id': 5, 'code': 'abcde0123456789fghij', 'expiration_mode': promo_player_constants.EXPIRATION_MODE_NONE, 'skin': promo_player_constants.SKIN_LIGHT } result = promo_player.create(data) created = result.message assert created['promo_player_id'] == 666 assert created['product_id'] == 4 assert created['active'] def test_create_without_product(db_fixture): """Test that a promo player can be created without a product_id.""" data = { 'promo_player_id': 666, 'vendor_id': 5, 'code': 'abcde0123456789fghij', 'expiration_mode': promo_player_constants.EXPIRATION_MODE_NONE, 'skin': promo_player_constants.SKIN_LIGHT } result = promo_player.create(data) created = result.message assert created['promo_player_id'] == 666 assert created['active'] def test_create_expiry_time(db_fixture): """Test that a promo player with an expiry_time can be created.""" now = datetime.datetime.now() data = { 'promo_player_id': 666, 'product_id': 4, 'vendor_id': 5, 'code': 'abcde0123456789fghij', 'expiration_mode': promo_player_constants.EXPIRATION_MODE_TIME, 'skin': promo_player_constants.SKIN_LIGHT, 'expiry_time': now } result = promo_player.create(data) created = result.message assert created['promo_player_id'] == 666 assert created['product_id'] == 4 assert created['expiry_time'] == now.timestamp() def test_create_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" data = {'product_id': 1} result = promo_player.create(data) assert result.status == 400 def test_create_bad_parameters_expiration_mode(db_fixture): """Test that an error response is returned if bad parameters.""" data = { 'promo_player_id': 666, 'product_id': 4, 'vendor_id': 5, 'code': 'abcde0123456789fghij', 'expiration_mode': promo_player_constants.EXPIRATION_MODE_NONE, 'skin': 'INVALID' } result = promo_player.create(data) assert result.status == 400 def test_create_bad_parameters_skin(db_fixture): """Test that an error response is returned if bad parameters.""" data = { 'promo_player_id': 666, 'product_id': 4, 'vendor_id': 5, 'code': 'abcde0123456789fghij', 'expiration_mode': 'INVALID', 'skin': promo_player_constants.SKIN_LIGHT } result = promo_player.create(data) assert result.status == 400 def test_get_active_by_product_id(db_fixture): """Test that the active promo player can be retrieved for a product.""" product_id = 2 result = promo_player.get_active_by_product_id(product_id) item = result.message assert item['promo_player_id'] == 1 def test_get_active_by_product_id_multiple_results(db_fixture): """Test that the most recent active promo player is returned.""" product_id = 123 result = promo_player.get_active_by_product_id(product_id) item = result.message assert item['promo_player_id'] == 5 def test_get_active_by_product_id_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" product_id = None result = promo_player.get_active_by_product_id(product_id) assert result.status == 400 def test_get_active_by_product_id_not_found(db_fixture): """Test that an error response is returned if not found.""" product_id = 666 result = promo_player.get_active_by_product_id(product_id) assert result.status == 404 def test_update(db_fixture): """Test that a promo player can be updated.""" data = { 'promo_player_id': 1, 'product_id': 2, 'active': False } result = promo_player.update(data) updated = result.message assert updated['promo_player_id'] == 1 assert not updated['active'] def test_update_without_product_without_product(db_fixture): """Test that a pp without a pid can be updated without a pid.""" data = { 'promo_player_id': 3, 'active': False } result = promo_player.update(data) updated = result.message assert updated['promo_player_id'] == 3 assert not updated['active'] def test_update_with_product_without_product(db_fixture): """Test that a pp with a pid cannot be updated without a pid.""" data = { 'promo_player_id': 1, 'active': False } result = promo_player.update(data) assert result.status == 404 def test_update_not_allowed_field(db_fixture): """Test that a not allowed field is not updated.""" data = { 'promo_player_id': 1, 'product_id': 2, 'vendor_id': 10 } result = promo_player.update(data) updated = result.message assert updated['promo_player_id'] == 1 assert updated['vendor_id'] == 3 def test_update_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" data = { 'active': False } result = promo_player.update(data) assert result.status == 400 def test_update_bad_parameters_expiration_mode(db_fixture): """Test that an error response is returned if bad parameters.""" data = { 'promo_player_id': 1, 'product_id': 2, 'expiration_mode': 'INVALID' } result = promo_player.update(data) assert result.status == 400 def test_update_bad_parameters_skin(db_fixture): """Test that an error response is returned if bad parameters.""" data = { 'promo_player_id': 1, 'product_id': 2, 'skin': 'INVALID' } result = promo_player.update(data) assert result.status == 400 def test_update_not_found(db_fixture): """Test that an error response is returned if not found.""" data = { 'promo_player_id': 666, 'product_id': 2, 'active': False } result = promo_player.update(data) assert result.status == 404 def test_update_wrong_product(db_fixture): """Test that an error response is returned if the product is wrong.""" data = { 'promo_player_id': 1, 'product_id': 666, 'active': False } result = promo_player.update(data) assert result.status == 404 def test_get_by_product_id(db_fixture): """Test that promo players can be retrieved by product_id.""" product_id = 2 result = promo_player.get_by_product_id(product_id) items = result.message['items'] assert len(items) == 1 assert items[0]['promo_player_id'] == 1 def test_get_by_product_id_no_results(db_fixture): """Test that an empty list is returned if no results.""" product_id = 666 result = promo_player.get_by_product_id(product_id) items = result.message['items'] assert len(items) == 0 def test_get_by_product_id_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" product_id = None result = promo_player.get_by_product_id(product_id) assert result.status == 400 def test_get_by_vendor_id(db_fixture): """Test that promo players can be retrieved by vendor_id.""" vendor_id = 3 result = promo_player.get_by_vendor_id(vendor_id) items = result.message['items'] assert len(items) == 1 assert items[0]['promo_player_id'] == 1 def test_get_by_vendor_id_no_results(db_fixture): """Test that an empty list is returned if no results.""" vendor_id = 666 result = promo_player.get_by_vendor_id(vendor_id) items = result.message['items'] assert len(items) == 0 def test_get_by_vendor_id_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" vendor_id = None result = promo_player.get_by_vendor_id(vendor_id) assert result.status == 400 def test_get_by_code(db_fixture): """Test that a promo player can be retrieved by code.""" code = '0123456789abcdefghij' result = promo_player.get_by_code(code) item = result.message assert item['promo_player_id'] == 1 def test_get_by_code_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" code = None result = promo_player.get_by_code(code) assert result.status == 400 def test_get_by_code_not_found(db_fixture): """Test that an error response is returned if not found.""" code = '66666666666666666666' result = promo_player.get_by_code(code) assert result.status == 404 def test_delete_by_product_id(db_fixture): """Test that promo players can be deleted by product_id.""" product_id = 3 result = promo_player.delete_by_product_id(product_id) items = result.message['items'] assert len(items) == 1 assert items[0]['promo_player_id'] == 2 def test_delete_by_product_id_no_results(db_fixture): """Test that an empty list is returned if no results.""" product_id = 666 result = promo_player.delete_by_product_id(product_id) items = result.message['items'] assert len(items) == 0 def test_delete_by_product_id_missing_parameters(db_fixture): """Test that an error response is returned if missing parameters.""" product_id = None result = promo_player.delete_by_product_id(product_id) assert result.status == 400