diff --git a/tests/test_elements_worker/test_elements.py b/tests/test_elements_worker/test_elements.py index 370cbb6b06fbe2691f2767c8da7d5580cb6b0482..ddb49f3a6e6c3fcc1808236e2b233ef57958a4b7 100644 --- a/tests/test_elements_worker/test_elements.py +++ b/tests/test_elements_worker/test_elements.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- import json -import os -import tempfile from argparse import Namespace from uuid import UUID @@ -15,54 +13,55 @@ from arkindex_worker.worker import ElementsWorker from . import BASE_API_CALLS -def test_list_elements_elements_list_arg_wrong_type(monkeypatch, mock_elements_worker): - _, path = tempfile.mkstemp() - with open(path, "w") as f: - json.dump({}, f) +def test_list_elements_elements_list_arg_wrong_type( + monkeypatch, tmp_path, mock_elements_worker +): + elements_path = tmp_path / "elements.json" + elements_path.write_text("{}") - monkeypatch.setenv("TASK_ELEMENTS", path) + monkeypatch.setenv("TASK_ELEMENTS", str(elements_path)) worker = ElementsWorker() worker.configure() - os.unlink(path) with pytest.raises(AssertionError) as e: worker.list_elements() assert str(e.value) == "Elements list must be a list" -def test_list_elements_elements_list_arg_empty_list(monkeypatch, mock_elements_worker): - _, path = tempfile.mkstemp() - with open(path, "w") as f: - json.dump([], f) +def test_list_elements_elements_list_arg_empty_list( + monkeypatch, tmp_path, mock_elements_worker +): + elements_path = tmp_path / "elements.json" + elements_path.write_text("[]") - monkeypatch.setenv("TASK_ELEMENTS", path) + monkeypatch.setenv("TASK_ELEMENTS", str(elements_path)) worker = ElementsWorker() worker.configure() - os.unlink(path) with pytest.raises(AssertionError) as e: worker.list_elements() assert str(e.value) == "No elements in elements list" -def test_list_elements_elements_list_arg_missing_id(monkeypatch, mock_elements_worker): - _, path = tempfile.mkstemp() - with open(path, "w") as f: +def test_list_elements_elements_list_arg_missing_id( + monkeypatch, tmp_path, mock_elements_worker +): + elements_path = tmp_path / "elements.json" + with elements_path.open("w") as f: json.dump([{"type": "volume"}], f) - monkeypatch.setenv("TASK_ELEMENTS", path) + monkeypatch.setenv("TASK_ELEMENTS", str(elements_path)) worker = ElementsWorker() worker.configure() - os.unlink(path) elt_list = worker.list_elements() assert elt_list == [] -def test_list_elements_elements_list_arg(monkeypatch, mock_elements_worker): - _, path = tempfile.mkstemp() - with open(path, "w") as f: +def test_list_elements_elements_list_arg(monkeypatch, tmp_path, mock_elements_worker): + elements_path = tmp_path / "elements.json" + with elements_path.open("w") as f: json.dump( [ {"id": "volumeid", "type": "volume"}, @@ -73,10 +72,9 @@ def test_list_elements_elements_list_arg(monkeypatch, mock_elements_worker): f, ) - monkeypatch.setenv("TASK_ELEMENTS", path) + monkeypatch.setenv("TASK_ELEMENTS", str(elements_path)) worker = ElementsWorker() worker.configure() - os.unlink(path) elt_list = worker.list_elements() @@ -103,9 +101,9 @@ def test_list_elements_element_arg(mocker, mock_elements_worker): assert elt_list == ["volumeid", "pageid"] -def test_list_elements_both_args_error(mocker, mock_elements_worker): - _, path = tempfile.mkstemp() - with open(path, "w") as f: +def test_list_elements_both_args_error(mocker, mock_elements_worker, tmp_path): + elements_path = tmp_path / "elements.json" + with elements_path.open("w") as f: json.dump( [ {"id": "volumeid", "type": "volume"}, @@ -120,7 +118,7 @@ def test_list_elements_both_args_error(mocker, mock_elements_worker): return_value=Namespace( element=["anotherid", "againanotherid"], verbose=False, - elements_list=open(path), + elements_list=elements_path.open(), database=None, dev=False, ), @@ -128,7 +126,6 @@ def test_list_elements_both_args_error(mocker, mock_elements_worker): worker = ElementsWorker() worker.configure() - os.unlink(path) with pytest.raises(AssertionError) as e: worker.list_elements() @@ -847,7 +844,7 @@ def test_create_elements(responses, mock_elements_worker_with_cache, tmp_path): assert created_ids == [{"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08"}] # Check that created elements were properly stored in SQLite cache - assert os.path.isfile(tmp_path / "db.sqlite") + assert (tmp_path / "db.sqlite").is_file() assert list(CachedElement.select()) == [ CachedElement(