"""Tests for canned response logic.""" from product_review.logic import canned_response as canned_response_logic from product_review.models import canned_response as canned_response_model from product_review.models import ( canned_response_category as canned_response_category_model, ) from product_review.models import canned_response_note as canned_response_note_model def test_get_canned_response_items_default_success(mocker, test_canned_response_models): """Test get canned response items success.""" canned_response_items_arr = [item.to_dict() for item in test_canned_response_models] expected_return_value = test_canned_response_models, len( test_canned_response_models ) expected_response_obj = { "items": canned_response_items_arr, "pagination": { "type": "standard", "offset": 0, "limit": 2, "total_records": len(test_canned_response_models), }, } mocker.patch.object( canned_response_model, "get_items", return_value=expected_return_value, autospec=True, ) results = canned_response_logic.get_canned_response_items() canned_response_model.get_items.assert_called_with( category_id=None, modified_by_user_id=None, review_context=None, language_code=None, page_limit=None, page_offset=None, ) assert results.status == 200 assert results.message == expected_response_obj for item in results.message["items"]: assert "canned_response_category" in item def test_get_canned_response_items_with_pagination(mocker, test_canned_response_models): """Test get canned response items with pagination limits.""" canned_response_items_arr = [test_canned_response_models[0].to_dict()] expected_return_value = [test_canned_response_models[0]], 1 expected_response_obj = { "items": canned_response_items_arr, "pagination": { "type": "standard", "offset": 1, "limit": 1, "total_records": 1, }, } mocker.patch.object( canned_response_model, "get_items", return_value=expected_return_value, autospec=True, ) results = canned_response_logic.get_canned_response_items( page_limit=1, page_offset=1 ) canned_response_model.get_items.assert_called_with( category_id=None, modified_by_user_id=None, review_context=None, language_code=None, page_limit=1, page_offset=1, ) assert results.status == 200 assert results.message == expected_response_obj for item in results.message["items"]: assert "canned_response_category" in item def test_get_canned_response_categories(mocker): """Test get canned response categories.""" mocker.patch.object( canned_response_category_model, "get_items", return_value=[{"foo": "bar"}], autospec=True, ) results = canned_response_logic.get_canned_response_categories() canned_response_category_model.get_items.assert_called_once() assert results.status == 200 assert results.message == [{"foo": "bar"}] def test_get_canned_response(mocker, test_canned_response_models): """Test get canned response.""" expected_item = test_canned_response_models[0].to_dict( include_note_for_language="en" ) mocker.patch.object( canned_response_model, "get_canned_response_item", return_value=expected_item, autospec=True, ) result = canned_response_logic.get_canned_response(1, language_code="en") canned_response_model.get_canned_response_item.assert_called_with(1, "en") assert result.status == 200 actual_item = result.message assert actual_item == expected_item assert "canned_response_category" in actual_item assert "note" in actual_item def test_soft_delete_canned_response(mocker): """Test soft delete canned response.""" mocker.patch.object( canned_response_model, "update_canned_response", return_value={"foo": "bar"}, autospec=True, ) result = canned_response_logic.soft_delete_canned_response(1, "user-1") canned_response_model.update_canned_response.assert_called_with( 1, "user-1", for_deletion=True ) assert result == {"foo": "bar"} def test_dataload_canned_response_notes(mocker, test_canned_response_note_models): """Test dataload canned response notes.""" mocker.patch.object( canned_response_note_model, "get_canned_response_notes", return_value=test_canned_response_note_models, autospec=True, ) result = canned_response_logic.dataload_canned_response_notes( [ {"canned_response_id": 1, "language_code": "en"}, {"canned_response_id": 10, "language_code": "en"}, {"canned_response_id": 2, "language_code": "en"}, {"canned_response_id": 2, "language_code": "fr"}, ] ) canned_response_note_model.get_canned_response_notes.assert_called_with( [[1, "en"], [10, "en"], [2, "en"], [2, "fr"]] ) assert result == [ test_canned_response_note_models[0].to_dict(), None, test_canned_response_note_models[1].to_dict(), test_canned_response_note_models[2].to_dict(), ]