"""Test for Product Live Time Model CRUD operation.""" import datetime from unittest.mock import patch import pytest from oto import response from oto import status as http_status from sqlalchemy.exc import SQLAlchemyError from tests.unit.testutils import db from timed_release.constants import success from timed_release.models import product_live_time def test_to_dict(): """Test that the to dict function only returns the expected properties.""" test_timed_release = product_live_time.ProductLiveTime( product_id='12', time_of_day_product='00:00:01', time_zone='GMT') expected_dict = { 'product_id': test_timed_release.product_id, 'time_of_day_product': test_timed_release.time_of_day_product, 'time_zone': test_timed_release.time_zone} assert test_timed_release.to_dict() == expected_dict @db.test_schema def test_delete_product_live_time_details_success(): """Test for delete time release details with success.""" product_id = 12 db.insert_product_live_time_data() delete_response = product_live_time.delete_product_live_time_details( product_id) assert delete_response.status == http_status.OK assert delete_response.message == \ success.DELETE_SUCCESS_MESSAGE_TIMED_RELEASE @db.test_schema def test_delete_product_live_time_details_not_found(): """Test for delete time release details with product id not found.""" product_id = 20801682080168 delete_response = product_live_time.delete_product_live_time_details( product_id) assert delete_response.status == http_status.NOT_FOUND @db.test_schema def test_delete_product_live_time_details_internal_error(mocker): """Test delete_product_live_time_details with SQLAlchemyError.""" product_id = 12 db.insert_product_live_time_data() mocker.patch.object( product_live_time, 'delete_product_live_time_details', return_value=response.create_fatal_response('fatal_error')) delete_response = product_live_time.delete_product_live_time_details( product_id) assert delete_response.status == http_status.INTERNAL_ERROR @db.test_schema def test_update_product_live_time_details_with_stores_success(): """Test to update product live time by product id.""" db.insert_product_live_time_data() product_id = 12 update_data = { 'product_id': 12, 'time_of_day_product': datetime.time(10, 20, 30), 'time_zone': 'GMT', 'store_ids': [286, 123]} expected_response = success.UPDATE_SUCCESS_MESSAGE.format(str(product_id)) response = product_live_time.update_product_live_time_details_with_stores( product_id, update_data) assert response.message == expected_response @db.test_schema def test_update_product_live_time_details_with_stores_not_found(): """Test to update product live time where product id not found.""" update_data = { 'product_id': 11, 'time_of_day_product': datetime.time(10, 20, 30), 'time_zone': 'GMT', 'store_ids': [286, 123]} response = product_live_time.update_product_live_time_details_with_stores( 11, update_data) assert response.status == http_status.NOT_FOUND @db.test_schema @patch( 'timed_release.connectors.sql.db_session', side_effect=SQLAlchemyError()) def test_update_product_live_time_details_with_stores_internal_error( monkeypatch): """Test to update product live time with SQLAlchemyError.""" update_data = { 'product_id': 11, 'time_of_day_product': datetime.time(10, 20, 30), 'time_zone': 'GMT', 'store_ids': [286, 123]} db.insert_product_live_time_data() response = product_live_time.update_product_live_time_details_with_stores( 11, update_data) assert response.status == http_status.INTERNAL_ERROR @db.test_schema @pytest.mark.parametrize( 'description, product_id, time_of_day_product, time_zone, store_ids', [ ('testing with GMT timezone', 2080166, datetime.time(20, 30, 00), 'GMT', [286]), ('testing with local timezone', 2080168, datetime.time(11, 45, 00), 'local', [286])]) def test_create_product_live_time_details_with_stores_success( description, product_id, time_of_day_product, time_zone, store_ids): """Test to create time release with store details for success.""" response_body = { 'product_id': product_id, 'time_of_day_product': time_of_day_product, 'time_zone': time_zone, 'store_ids': store_ids} expected_response = {'product': response_body} insert_response = \ product_live_time.create_product_live_time_details_with_stores( product_id, time_of_day_product, time_zone, store_ids) assert insert_response.status == http_status.OK assert insert_response.message == expected_response @patch( 'timed_release.connectors.sql.db_session', side_effect=SQLAlchemyError()) def test_create_product_live_time_details_with_stores_internal_error( monkeypatch): """Test to create time release with store details for SQLAlchemyError.""" product_id = 2080166 time_of_day_product = '20:30:00' time_zone = 'GMT' store_ids = [286] insert_response = \ product_live_time.create_product_live_time_details_with_stores( product_id, time_of_day_product, time_zone, store_ids) assert insert_response.status == http_status.INTERNAL_ERROR @db.test_schema def test_get_product_live_time_details_and_stores_success(): """Test to get product live time and stores by product id.""" expected_data = { 'product_id': 12, 'time_of_day_product': '20:30:00', 'time_zone': 'GMT', 'store_ids': [1, 2]} db.insert_product_live_time_data() db.insert_product_store_data() get_response = product_live_time.get_product_live_time_details_and_stores( '12') assert get_response.message == expected_data @db.test_schema def test_get_product_live_time_details_and_stores_not_found(): """Test that timed release data not found for requested product id.""" get_response = product_live_time.get_product_live_time_details_and_stores( '12') assert get_response.status == http_status.NOT_FOUND @db.test_schema @patch( 'timed_release.connectors.sql.db_session', side_effect=SQLAlchemyError()) def test_get_product_live_time_details_and_stores_internal_error( monkeypatch): """Test get_product_live_time_details_with_stores with SQLAlchemyError.""" db.insert_product_live_time_data() db.insert_product_store_data() get_response = product_live_time.get_product_live_time_details_and_stores( '12') assert get_response.status == http_status.INTERNAL_ERROR