Skip to content

Commit

Permalink
Move asserts out of mock
Browse files Browse the repository at this point in the history
  • Loading branch information
dbutenhof committed May 30, 2023
1 parent b700f5e commit f505bac
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/pbench/test/unit/server/test_datasets_inventory.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"

0 comments on commit f505bac

Please sign in to comment.