jseop-lim / anam-earth-be-fastapi

Refactor anam-earth backend
0 stars 0 forks source link

[테스트] 모킹과 타입 힌트 #5

Open jseop-lim opened 1 year ago

jseop-lim commented 1 year ago
@pytest.fixture()
def mock_node_repo() -> mock.Mock:
    mock_node_repo: mock.Mock = mock.Mock(spec_set=NodeRepository)
    mock_node_repo.get_all_nodes.return_value = [
        Node(name='A', point=Point(longitude=Decimal('1.0'), latitude=Decimal('2.0'))),
        Node(name='B', point=Point(longitude=Decimal('3.0'), latitude=Decimal('4.0'))),
    ]
    return mock_node_repo

@pytest.fixture()
def mock_list_nodes_presenter() -> mock.Mock:
    return mock.Mock(spec_set=ListNodesOutputBoundary)

def test_list_nodes(
    mock_node_repo: mock.Mock,
    mock_list_nodes_presenter: mock.Mock,
) -> None:
    ListNodesUseCase(
        node_repo=mock_node_repo,
        output_boundary=mock_list_nodes_presenter,
    ).execute()

    assert mock_node_repo.get_all_nodes.called
    assert mock_list_nodes_presenter.present.call_args_list == [
        mock.call(
            output_data_list=[
                ListNodesOutputData(name='A', longitude=Decimal('1.0'), latitude=Decimal('2.0')),
                ListNodesOutputData(name='B', longitude=Decimal('3.0'), latitude=Decimal('4.0')),
            ],
        ),
    ]
jseop-lim commented 1 year ago

pytest.fixture() 뒤의 ()를 누락하면 mypy 실행 결과 아래 에러가 뜬다.

Expression type contains "Any" (has type overloaded function)  [misc]
jseop-lim commented 1 year ago

mock.Mock, mock.call 관련하여 mypy 에러가 발생한다.

tests/map_admin/application/test_list_nodes.py:33: error: Expression has type "Any"  [misc]
tests/map_admin/application/test_list_nodes.py:33: error: Expression type contains "Any" (has type "list[Any]")  [misc]
tests/map_admin/application/test_list_nodes.py:54: error: Expression has type "Any"  [misc]
tests/map_admin/application/test_list_nodes.py:55: error: Expression has type "Any"  [misc]
tests/map_admin/application/test_list_nodes.py:57: error: Expression type contains "Any" (has type "list[Any]")  [misc]