From f505bacbe5e2a692c4f04df5037e4e1ff82d08e1 Mon Sep 17 00:00:00 2001 From: David Butenhof Date: Tue, 30 May 2023 12:59:59 -0400 Subject: [PATCH] Move asserts out of mock --- .../unit/server/test_datasets_inventory.py | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/pbench/test/unit/server/test_datasets_inventory.py b/lib/pbench/test/unit/server/test_datasets_inventory.py index a428021865..c48319eb22 100644 --- a/lib/pbench/test/unit/server/test_datasets_inventory.py +++ b/lib/pbench/test/unit/server/test_datasets_inventory.py @@ -1,9 +1,9 @@ from http import HTTPStatus from pathlib import Path +from typing import Any, Optional import pytest import requests -import werkzeug.utils from pbench.server.cache_manager import CacheManager from pbench.server.database.models.datasets import Dataset, DatasetNotFound @@ -103,30 +103,45 @@ def test_not_a_file(self, query_get_as, monkeypatch): } def test_dataset_in_given_path(self, query_get_as, monkeypatch): + mock_args: Optional[tuple[Path, dict[str, Any]]] = None + def mock_send_file(path_or_file, *args, **kwargs): - assert str(path_or_file) == "/dataset/1-default/default.csv" - assert kwargs["as_attachment"] is False - assert kwargs["download_name"] == "default.csv" + nonlocal mock_args + mock_args = (path_or_file, kwargs) return {"status": "OK"} monkeypatch.setattr(CacheManager, "find_dataset", self.mock_find_dataset) monkeypatch.setattr(Path, "is_file", lambda self: True) - monkeypatch.setattr(werkzeug.utils, "send_file", mock_send_file) + monkeypatch.setattr( + "pbench.server.api.resources.datasets_inventory.send_file", mock_send_file + ) response = query_get_as("fio_2", "1-default/default.csv", HTTPStatus.OK) assert response.status_code == HTTPStatus.OK + path, args = mock_args + assert str(path) == "/dataset/1-default/default.csv" + assert args["as_attachment"] is False + assert args["download_name"] == "default.csv" + @pytest.mark.parametrize("key", (None, "")) def test_get_result_tarball(self, query_get_as, monkeypatch, key): + mock_args: Optional[tuple[Path, dict[str, Any]]] = None + def mock_send_file(path_or_file, *args, **kwargs): - assert str(path_or_file) == "/dataset/tarball.tar.xz" - assert kwargs["as_attachment"] is True - assert kwargs["download_name"] == "tarball.tar.xz" + nonlocal mock_args + mock_args = (path_or_file, kwargs) return {"status": "OK"} monkeypatch.setattr(CacheManager, "find_dataset", self.mock_find_dataset) monkeypatch.setattr(Path, "is_file", lambda self: True) - monkeypatch.setattr(werkzeug.utils, "send_file", mock_send_file) + monkeypatch.setattr( + "pbench.server.api.resources.datasets_inventory.send_file", mock_send_file + ) response = query_get_as("fio_2", key, HTTPStatus.OK) assert response.status_code == HTTPStatus.OK + path, args = mock_args + assert str(path) == "/dataset/tarball.tar.xz" + assert args["as_attachment"] is True + assert args["download_name"] == "tarball.tar.xz"