"""Unit tests for ProductLiveTime logic.""" from unittest.mock import MagicMock import pytest from oto import response from oto import status as http_status from tests.unit.testutils import db from timed_release.constants import error, success from timed_release.logic import product_live_time from timed_release.models import product_live_time as product_live_time_model from timed_release.validation import validators @db.test_schema def test_get_product_live_time_detail_success(monkeypatch): """Test to get product live time for given product id.""" db.insert_product_live_time_data() db.insert_product_store_data() expected_response = { 'product_id': 12, 'time_of_day_product': '20:30:00', 'time_zone': 'GMT', 'store_ids': [1, 2] } monkeypatch.setattr( product_live_time_model, 'get_product_live_time_details_and_stores', MagicMock(return_value=response.Response(message=expected_response))) result = product_live_time.get_product_live_time_details('12') assert result.status == http_status.OK assert result.message == expected_response def test_create_product_live_time_detail_success(monkeypatch): """Test for create product live time details with success.""" request_body = { 'product_id': 2080166, 'time_of_day_product': '03:02:01', 'time_zone': 'GMT', 'store_ids': [1] } expected_response = {'product': request_body} monkeypatch.setattr( product_live_time_model, 'create_product_live_time_details_with_stores', MagicMock(return_value=response.Response(message=expected_response))) result = product_live_time.create_product_live_time_detail(request_body) assert result.status == http_status.OK assert result.message == expected_response def test_delete_product_live_time_details_success(monkeypatch): """Test for delete product live time with success.""" monkeypatch.setattr( product_live_time_model, 'delete_product_live_time_details', MagicMock(return_value=response.Response( message=success.DELETE_SUCCESS_MESSAGE_TIMED_RELEASE))) result = product_live_time.delete_product_live_time_details('12') assert result.status == http_status.OK assert result.message == success.DELETE_SUCCESS_MESSAGE_TIMED_RELEASE @pytest.mark.parametrize( 'description, product_id, expected_status', [ ('Product id not found', '11', 404), ('Product id must be greater than zero', '0', 400), ('Product id must be greater than zero', '-1', 400), ('Product id must be integer', 'abc', 400)]) @db.test_schema def test_delete_product_live_time_details_failure( description, product_id, expected_status): """Test for delete product live time with failure.""" result = product_live_time.delete_product_live_time_details(product_id) assert result.status == expected_status @pytest.mark.parametrize( 'description, product_id, input_data', [ ('Product id must be integer', 'abc', {'time_of_day_product': '80:10:00', 'time_zone': 'test', 'store_ids': [286]}), ('Product id greater than zero', '-1', {'time_of_day_product': '80:10:00', 'time_zone': 'test', 'store_ids': [286]}), ('Product id greater than zero', '0', {'time_of_day_product': '80:10:00', 'time_zone': 'test', 'store_ids': [286]})]) def test_update_product_live_time_invalid_product_id( description, product_id, input_data, monkeypatch): """Test for update product live time details with invalid product id.""" monkeypatch.setattr( validators, 'is_digit_and_non_zero', MagicMock(return_value=False)) result = product_live_time.update_product_live_time( product_id, input_data) assert result.status == http_status.BAD_REQUEST def test_update_product_live_time_success(monkeypatch): """Test for update product live time details.""" product_id = '11' expected_response = success.UPDATE_SUCCESS_MESSAGE.format(product_id) input_data = { 'product_id': 11, 'time_of_day_product': '03:02:01', 'time_zone': 'GMT', 'store_ids': [1] } monkeypatch.setattr( product_live_time_model, 'update_product_live_time_details_with_stores', MagicMock(return_value=response.Response(message=expected_response))) result = product_live_time.update_product_live_time(product_id, input_data) assert result.status == http_status.OK assert result.message == expected_response def test_update_product_live_time_for_product_not_found(monkeypatch): """Test for update product live time details when product id not found.""" product_id = '11' input_data = { 'product_id': 11, 'time_of_day_product': '03:02:01', 'time_zone': 'GMT', 'store_ids': [1] } monkeypatch.setattr( product_live_time_model, 'update_product_live_time_details_with_stores', MagicMock(return_value=response.create_not_found_response( message=error.ERROR_MESSAGE_PRODUCT_NOT_FOUND.format(product_id)))) result = product_live_time.update_product_live_time(product_id, input_data) assert result.status == http_status.NOT_FOUND def test_update_product_live_time_product_id_does_not_match(monkeypatch): """Test update product live time details for product id does not match.""" product_id = '12' input_data = { 'product_id': 11, 'time_of_day_product': '03:02:01', 'time_zone': 'GMT', 'store_ids': [1]} monkeypatch.setattr( validators, 'is_digit_and_non_zero', MagicMock(return_value=True)) result = product_live_time.update_product_live_time(product_id, input_data) assert result.status == http_status.BAD_REQUEST