"""Tests for query parameters validators.""" from oto import response as oto_response from oto import status as oto_status import pytest from werkzeug import datastructures from availability.constants import error from availability.constants import stores from availability.validation import query_params def test_validate_product_ids_parameter(): """Assert can determine valid parameters.""" # This is how Flask will store it in request.args params = datastructures.ImmutableMultiDict([('product_ids', '1,2,3')]) response = query_params.validate_product_ids_parameter(params) assert isinstance(response, oto_response.Response) assert response.status == oto_status.OK assert response.message == [1, 2, 3] def test_validate_product_ids_parameter_handles_invalid_product_ids(): """Assert invalid data in 'product_ids' parameter is handled.""" params = datastructures.ImmutableMultiDict([('product_ids', 'invalid')]) response = query_params.validate_product_ids_parameter(params) assert response.status == oto_status.BAD_REQUEST def test_validate_product_ids_parameter_without_product_ids(): """Assert can determine when product_ids is absent.""" params = datastructures.ImmutableMultiDict([('invalid_param', '1')]) response = query_params.validate_product_ids_parameter(params) assert response.status == oto_status.BAD_REQUEST def test_validate_product_ids_parameter_rejects_another_params(): """Assert only 'product_ids' is accepted as parameter.""" params = datastructures.ImmutableMultiDict( [('invalid_param', '1'), ('product_ids', '1,2,3')]) response = query_params.validate_product_ids_parameter(params) assert response.status == oto_status.BAD_REQUEST def test_validate_correct_store_id(): """Assert can determine existing store id.""" for store_id in stores.STORE_IDS: response = query_params.validate_store_id(store_id) assert response.status == oto_status.OK assert response.message == store_id @pytest.mark.parametrize( 'store_id', [1234567890, '1234567890', 'test', [], {}, ()]) def test_validate_incorrect_store_id(store_id): """Assert can determine invalid store id.""" response = query_params.validate_store_id(store_id) assert response.status == oto_status.NOT_FOUND assert response.errors['message'] == error.ERROR_MESSAGE_STORE_NOT_FOUND