"""Tests for context logic.""" from typing import Any from unittest.mock import MagicMock, call import pytest from flask import g from pytest_mock import MockerFixture from video.api import app from video.constants import header from video.logic import context from video.models.ows import account from video.models.sql import queries @pytest.mark.parametrize( ( "method", "headers", "get_subaccount_calls", "persist_context_asserter", "persist_context_asserter_args", "expected_context", ), [ ( "POST", { header.GRASS_ACCOUNT_TYPE: "subaccount", header.GRASS_ACCOUNT_ID: 456, header.ORCHARD_USER_ID: "alw:343", }, [call(456)], lambda mock, args: mock.assert_called_once_with(*args), [ { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", }, ], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", "id": 333, }, ), ( "POST", {}, [], lambda mock, args: mock.assert_called_once_with(*args), [ { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", }, ], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "id": 333, }, ), ( "PUT", { header.GRASS_ACCOUNT_TYPE: "subaccount", header.GRASS_ACCOUNT_ID: 456, header.ORCHARD_USER_ID: "alw:343", }, [call(456)], lambda mock, args: mock.assert_called_once_with(*args), [ { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", }, ], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", "id": 333, }, ), ( "PATCH", { header.GRASS_ACCOUNT_TYPE: "subaccount", header.GRASS_ACCOUNT_ID: 456, header.ORCHARD_USER_ID: "alw:343", }, [call(456)], lambda mock, args: mock.assert_called_once_with(*args), [ { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", }, ], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", "id": 333, }, ), ( "DELETE", { header.GRASS_ACCOUNT_TYPE: "subaccount", header.GRASS_ACCOUNT_ID: 456, header.ORCHARD_USER_ID: "alw:343", }, [call(456)], lambda mock, args: mock.assert_called_once_with(*args), [ { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", }, ], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", "id": 333, }, ), ( "GET", { header.GRASS_ACCOUNT_TYPE: "subaccount", header.GRASS_ACCOUNT_ID: 456, header.ORCHARD_USER_ID: "alw:343", }, [call(456)], lambda mock, args: assert_not_called(mock), [], { "correlation_id": "32fe92d2-9b25-11e8-af41-acbc32b98ae7", "vendor_id": 123, "subaccount_id": 456, "user_id": "alw:343", }, ), ], ) def test_create_context( mocker: MockerFixture, method: Any, headers: Any, get_subaccount_calls: Any, persist_context_asserter: Any, persist_context_asserter_args: Any, expected_context: Any, ) -> None: """Test create_context.""" mocker.patch.object( queries, "persist_context", return_value=expected_context.get("id"), autospec=True, ) mock_get_subaccount: MagicMock = mocker.patch.object( account, "get_subaccount", return_value={"vendor_id": 123}, autospec=True ) with app.test_request_context(method=method, headers=headers): g.correlation_id = "32fe92d2-9b25-11e8-af41-acbc32b98ae7" context.create_context() mock_get_subaccount.assert_has_calls(get_subaccount_calls) persist_context_asserter(queries.persist_context, persist_context_asserter_args) assert expected_context == g.context def assert_not_called(mock: Any) -> None: """Assert mock has not been called. Args: mock (unittest.mock): Mock to check. """ assert not mock.called @pytest.mark.parametrize( ( "method", "persist_context_asserter", "persist_context_asserter_args", "existing_context", "expected_context", ), [ ( "POST", lambda mock, args: mock.assert_called_once_with(*args), [{"id": 333, "applesauce": "bananas"}], {"id": 333, "abc": 123}, {"id": 333, "abc": 123, "applesauce": "bananas"}, ), ( "PUT", lambda mock, args: mock.assert_called_once_with(*args), [{"id": 333, "applesauce": "bananas"}], {"id": 333, "abc": 123}, {"id": 333, "abc": 123, "applesauce": "bananas"}, ), ( "PATCH", lambda mock, args: mock.assert_called_once_with(*args), [{"id": 333, "applesauce": "bananas"}], {"id": 333, "abc": 123}, {"id": 333, "abc": 123, "applesauce": "bananas"}, ), ( "DELETE", lambda mock, args: mock.assert_called_once_with(*args), [{"id": 333, "applesauce": "bananas"}], {"id": 333, "abc": 123}, {"id": 333, "abc": 123, "applesauce": "bananas"}, ), ( "GET", lambda mock, args: assert_not_called(mock), [], {"abc": 123}, {"abc": 123, "applesauce": "bananas"}, ), ], ) def test_add_to_context( mocker: MockerFixture, method: Any, persist_context_asserter: Any, persist_context_asserter_args: Any, existing_context: Any, expected_context: Any, ) -> None: """Test add_to_context.""" mocker.patch.object(queries, "persist_context", return_value=234, autospec=True) with app.test_request_context(method=method): g.context = existing_context context.add_to_context({"applesauce": "bananas"}) persist_context_asserter(queries.persist_context, persist_context_asserter_args) assert expected_context == g.context