"""Tests for product logic.""" import pytest from logic import product from models import ows_product import const # noqa import index # noqa def test_prepare_doc_product_add( mocker, product_document_fixture, product_cloudsearch_document, converted_product_event_fixture): """Test prepare cloudsearch document.""" mocker.patch.object( ows_product, 'get_product_document', return_value=product_document_fixture) doc = product.prepare_doc(converted_product_event_fixture) assert doc == product_cloudsearch_document @pytest.mark.parametrize( 'table_name', ['release_correction', 'release_approval_queue']) def test_prepare_doc_product_add_release_correction_approval( mocker, product_document_fixture, table_name, product_cloudsearch_document, converted_product_event_fixture): """Test prepare cloudsearch document for release correction and approval.""" mocker.patch.object( ows_product, 'get_product_document', return_value=product_document_fixture) event = converted_product_event_fixture.copy() event['type'] = 'delete' event['table'] = table_name doc = product.prepare_doc(converted_product_event_fixture) assert doc['type'] == 'add' assert doc == product_cloudsearch_document def test_prepare_doc_product_delete( product_cloudsearch_document, converted_product_event_fixture): """Test prepare cloudsearch document for deletion.""" delete_event = converted_product_event_fixture.copy() delete_doc = product_cloudsearch_document.copy() delete_event['type'] = 'delete' delete_doc['type'] = 'delete' delete_doc.pop('fields') doc = product.prepare_doc(delete_event) assert doc == delete_doc def test_prepare_doc_product_soft_delete( mocker, product_document_fixture, product_cloudsearch_document, converted_product_event_fixture): """Test prepare cloudsearch document on soft deletion.""" mocker.patch.object( ows_product, 'get_product_document', return_value=product_document_fixture) delete_event = converted_product_event_fixture.copy() delete_event['data']['deletions'] = 'Y' doc = product.prepare_doc(delete_event) delete_doc = product_cloudsearch_document.copy() assert doc == delete_doc def test_prepare_doc_product_incompatible( mocker, converted_product_event_fixture): """Test prepare cloudsearch document with incompatible format.""" delete_event = converted_product_event_fixture.copy() del delete_event['type'] doc = product.prepare_doc(delete_event) assert doc == {} @pytest.mark.parametrize( 'mock_object,mock_method,exception_message', [ (ows_product, 'get_product_document', const.OWS_PROJECT_ERROR) ] ) def test_prepare_doc_product_error( mocker, converted_product_event_fixture, mock_object, mock_method, exception_message): """Test prepare cloudsearch document ows-product error.""" mocker.patch.object( mock_object, mock_method, side_effect=Exception(exception_message)) with pytest.raises(Exception, match=exception_message): product.prepare_doc(converted_product_event_fixture) # def test_prepare_doc_product_exclusion( # mocker, # product_document_fixture, # converted_delete_row_fixture, # converted_product_event_fixture, # product_id): # """Test prepare cloudsearch document on exclusion list.""" # mocker.patch.object( # ows_product, # 'get_product_document', # return_value=product_document_fixture) # event = converted_product_event_fixture.copy() # event['data']['release_id'] = product_id # doc = product.prepare_doc(event) # delete_doc = converted_delete_row_fixture # assert doc == delete_doc