"""Tests for account note logic.""" from unittest.mock import call import pytest from product_review.logic import account_note as account_note_logic from product_review.models import account_note as account_note_model from product_review.models import subaccount_note as subaccount_note_model def test_get_account_note(mocker): """Test get_account_note.""" mocker.patch.object( account_note_model, "get_account_note", return_value={"foo": "bar"} ) result = account_note_logic.get_account_note(1) assert result.message == {"foo": "bar"} assert result.status == 200 def test_get_account_note_not_found(mocker): """Test get_account_note not found.""" result = None mocker.patch.object(account_note_model, "get_account_note", return_value=None) with pytest.raises(Exception) as er: result = account_note_logic.get_account_note(1) assert ( str(getattr(er, "value", None)) == '{"status": 404, "code": "not_found", "message": "Not Found"}' ) assert result is None def test_get_subaccount_note(mocker): """Test get_subaccount_note.""" mocker.patch.object( subaccount_note_model, "get_subaccount_note", return_value={"foo": "bar"} ) result = account_note_logic.get_subaccount_note(1) assert result.message == {"foo": "bar"} assert result.status == 200 def test_get_subaccount_note_not_found(mocker): """Test get_subaccount_note not found.""" result = None mocker.patch.object(subaccount_note_model, "get_subaccount_note", return_value=None) with pytest.raises(Exception) as er: result = account_note_logic.get_subaccount_note(1) assert ( str(getattr(er, "value", None)) == '{"status": 404, "code": "not_found", "message": "Not Found"}' ) assert result is None @pytest.mark.parametrize( ( "test_description", "mock_get_model_result", "expected_update_model_calls", "expected_create_model_calls", "expected_response_message", ), [ ( "create new note", None, [], [call(1, "abc", 123)], "Successfully created account note.", ), ( "update existing note", {"foo": "bar"}, [call(1, "abc", 123)], [], "Successfully updated account note.", ), ], ) def test_add_account_note( mocker, test_description, mock_get_model_result, expected_update_model_calls, expected_create_model_calls, expected_response_message, ): """Test add_account_note.""" mocker.patch.object( account_note_model, "get_account_note", return_value=mock_get_model_result ) mocker.patch.object(account_note_model, "update_account_note") mocker.patch.object(account_note_model, "create_account_note") result = account_note_logic.add_account_note(1, "abc", 123) assert result.status == 200 assert result.message == expected_response_message account_note_model.get_account_note.assert_called_once_with(1) assert ( account_note_model.update_account_note.mock_calls == expected_update_model_calls ) assert ( account_note_model.create_account_note.mock_calls == expected_create_model_calls ) @pytest.mark.parametrize( ( "test_description", "mock_get_model_result", "expected_update_model_calls", "expected_create_model_calls", "expected_response_message", ), [ ( "create new note", None, [], [call(1, "abc", 123)], "Successfully created subaccount note.", ), ( "update existing note", {"foo": "bar"}, [call(1, "abc", 123)], [], "Successfully updated subaccount note.", ), ], ) def test_add_subaccount( mocker, test_description, mock_get_model_result, expected_update_model_calls, expected_create_model_calls, expected_response_message, ): """Test add_subaccount_note.""" mocker.patch.object( subaccount_note_model, "get_subaccount_note", return_value=mock_get_model_result ) mocker.patch.object(subaccount_note_model, "update_subaccount_note") mocker.patch.object(subaccount_note_model, "create_subaccount_note") result = account_note_logic.add_subaccount_note(1, "abc", 123) assert result.status == 200 assert result.message == expected_response_message subaccount_note_model.get_subaccount_note.assert_called_once_with(1) assert ( subaccount_note_model.update_subaccount_note.mock_calls == expected_update_model_calls ) assert ( subaccount_note_model.create_subaccount_note.mock_calls == expected_create_model_calls )