|
| 1 | +import contextlib |
| 2 | +import dataclasses |
| 3 | +import pathlib |
| 4 | +import tempfile |
| 5 | +from collections.abc import Callable, Generator |
| 6 | +from typing import Any |
| 7 | +from unittest.mock import Mock |
| 8 | + |
| 9 | +import pytest |
| 10 | +import yaml |
| 11 | + |
| 12 | +from tavern._core import exceptions |
| 13 | +from tavern._core.pytest.file import YamlFile |
| 14 | +from tavern._core.pytest.item import YamlItem |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="function") |
| 18 | +def tavern_test_content(): |
| 19 | + """return some example tests""" |
| 20 | + |
| 21 | + test_docs = [ |
| 22 | + {"test_name": "First test", "stages": [{"name": "stage 1"}]}, |
| 23 | + {"test_name": "Second test", "stages": [{"name": "stage 2"}]}, |
| 24 | + {"test_name": "Third test", "stages": [{"name": "stage 3"}]}, |
| 25 | + ] |
| 26 | + |
| 27 | + return test_docs |
| 28 | + |
| 29 | + |
| 30 | +@contextlib.contextmanager |
| 31 | +def tavern_test_file(test_content: list[Any]) -> Generator[pathlib.Path, Any, None]: |
| 32 | + """Create a temporary YAML file with multiple documents""" |
| 33 | + |
| 34 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | + file_path = pathlib.Path(tmpdir) / "test.yaml" |
| 36 | + |
| 37 | + # Write the documents to the file |
| 38 | + with file_path.open("w", encoding="utf-8") as f: |
| 39 | + for doc in test_content: |
| 40 | + yaml.dump(doc, f) |
| 41 | + f.write("---\n") |
| 42 | + |
| 43 | + yield file_path |
| 44 | + |
| 45 | + |
| 46 | +@dataclasses.dataclass |
| 47 | +class Opener: |
| 48 | + """Simple mock for generating items because pytest makes it hard to wrap |
| 49 | + their internal functionality""" |
| 50 | + |
| 51 | + path: pathlib.Path |
| 52 | + _generate_items: Callable[[dict], Any] |
| 53 | + |
| 54 | + |
| 55 | +class TestGenerateFiles: |
| 56 | + @pytest.mark.parametrize("with_merge_down_test", (True, False)) |
| 57 | + def test_multiple_documents(self, tavern_test_content, with_merge_down_test): |
| 58 | + """Verify that multiple documents in a YAML file result in multiple tests""" |
| 59 | + |
| 60 | + # Collect all tests |
| 61 | + if with_merge_down_test: |
| 62 | + tavern_test_content.insert(0, {"includes": []}) |
| 63 | + |
| 64 | + def generate_yamlitem(test_spec): |
| 65 | + mock = Mock(spec=YamlItem) |
| 66 | + mock.name = test_spec["test_name"] |
| 67 | + yield mock |
| 68 | + |
| 69 | + with tavern_test_file(tavern_test_content) as filename: |
| 70 | + tests = list( |
| 71 | + YamlFile.collect( |
| 72 | + Opener( |
| 73 | + path=filename, |
| 74 | + _generate_items=generate_yamlitem, |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + assert len(tests) == 3 |
| 80 | + |
| 81 | + # Verify each test has the correct name |
| 82 | + expected_names = ["First test", "Second test", "Third test"] |
| 83 | + for test, expected_name in zip(tests, expected_names): |
| 84 | + assert test.name == expected_name |
| 85 | + |
| 86 | + @pytest.mark.parametrize( |
| 87 | + "content, exception", |
| 88 | + ( |
| 89 | + ({"kookdff": "?A?A??"}, exceptions.BadSchemaError), |
| 90 | + ({"test_name": "name", "stages": [{"name": "lflfl"}]}, TypeError), |
| 91 | + ), |
| 92 | + ) |
| 93 | + def test_reraise_exception( |
| 94 | + self, tavern_test_content, content: dict, exception: BaseException |
| 95 | + ): |
| 96 | + """Verify that exceptions are properly reraised when loading YAML test files. |
| 97 | +
|
| 98 | + Test that when an exception occurs during test generation, it is properly |
| 99 | + reraised as a schema error if the schema is bad.""" |
| 100 | + |
| 101 | + def raise_error(test_spec): |
| 102 | + raise TypeError |
| 103 | + |
| 104 | + tavern_test_content.insert(0, content) |
| 105 | + |
| 106 | + with tavern_test_file(tavern_test_content) as filename: |
| 107 | + with pytest.raises(exception): |
| 108 | + list( |
| 109 | + YamlFile.collect( |
| 110 | + Opener( |
| 111 | + path=filename, |
| 112 | + _generate_items=raise_error, |
| 113 | + ) |
| 114 | + ) |
| 115 | + ) |
0 commit comments