|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from textwrap import dedent |
| 4 | + |
1 | 5 | import pytest
|
2 | 6 | from pytest_mock import MockerFixture
|
3 | 7 |
|
4 | 8 | from oc4ids_datastore_pipeline.pipeline import (
|
5 | 9 | download_json,
|
6 | 10 | process_dataset,
|
7 | 11 | validate_json,
|
| 12 | + write_json_to_file, |
8 | 13 | )
|
9 | 14 |
|
10 | 15 |
|
@@ -47,6 +52,34 @@ def test_validate_json_raises_validation_errors_exception(
|
47 | 52 | assert "Dataset has 2 validation errors" in str(exc_info.value)
|
48 | 53 |
|
49 | 54 |
|
| 55 | +def test_write_json_to_file_writes_in_correct_format() -> None: |
| 56 | + with tempfile.TemporaryDirectory() as dir: |
| 57 | + file_name = os.path.join(dir, "test_dataset.json") |
| 58 | + write_json_to_file(file_name=file_name, json_data={"key": "value"}) |
| 59 | + |
| 60 | + expected = dedent( |
| 61 | + """\ |
| 62 | + { |
| 63 | + "key": "value" |
| 64 | + }""" |
| 65 | + ) |
| 66 | + with open(file_name) as file: |
| 67 | + assert file.read() == expected |
| 68 | + |
| 69 | + |
| 70 | +def test_write_json_to_file_raises_failure_exception(mocker: MockerFixture) -> None: |
| 71 | + patch_json_dump = mocker.patch("oc4ids_datastore_pipeline.pipeline.json.dump") |
| 72 | + patch_json_dump.side_effect = Exception("Mocked exception") |
| 73 | + |
| 74 | + with pytest.raises(Exception) as exc_info: |
| 75 | + with tempfile.TemporaryDirectory() as dir: |
| 76 | + file_name = os.path.join(dir, "test_dataset.json") |
| 77 | + write_json_to_file(file_name=file_name, json_data={"key": "value"}) |
| 78 | + |
| 79 | + assert "Error while writing to JSON file" in str(exc_info.value) |
| 80 | + assert "Mocked exception" in str(exc_info.value) |
| 81 | + |
| 82 | + |
50 | 83 | def test_process_dataset_catches_exception(mocker: MockerFixture) -> None:
|
51 | 84 | patch_download_json = mocker.patch(
|
52 | 85 | "oc4ids_datastore_pipeline.pipeline.download_json"
|
|
0 commit comments