| 1 |
import sys |
| 2 |
from textwrap import dedent |
| 3 |
|
| 4 |
import yaml |
| 5 |
|
| 6 |
from pytest_regressions.testing import check_regression_fixture_workflow |
| 7 |
|
| 8 |
|
| 9 |
def test_example(data_regression): |
| 10 |
"""Basic example""" |
| 11 |
contents = {"contents": "Foo", "value": 11} |
| 12 |
data_regression.check(contents) |
| 13 |
|
| 14 |
|
| 15 |
def test_basename(data_regression): |
| 16 |
"""Basic example using basename parameter""" |
| 17 |
contents = {"contents": "Foo", "value": 11} |
| 18 |
data_regression.check(contents, basename="case.normal") |
| 19 |
|
| 20 |
|
| 21 |
def test_custom_object(data_regression): |
| 22 |
"""Basic example where we register a custom conversion to dump objects""" |
| 23 |
|
| 24 |
class Scalar: |
| 25 |
def __init__(self, value, unit): |
| 26 |
self.value = value |
| 27 |
self.unit = unit |
| 28 |
|
| 29 |
def dump_scalar(dumper, scalar): |
| 30 |
return dumper.represent_dict(dict(value=scalar.value, unit=scalar.unit)) |
| 31 |
|
| 32 |
from pytest_regressions import add_custom_yaml_representer |
| 33 |
|
| 34 |
add_custom_yaml_representer(Scalar, dump_scalar) |
| 35 |
|
| 36 |
contents = {"scalar": Scalar(10, "m")} |
| 37 |
|
| 38 |
data_regression.check(contents) |
| 39 |
|
| 40 |
|
| 41 |
def test_usage_workflow(pytester, monkeypatch): |
| 42 |
monkeypatch.setattr( |
| 43 |
sys, "testing_get_data", lambda: {"contents": "Foo", "value": 10}, raising=False |
| 44 |
) |
| 45 |
source = """ |
| 46 |
import sys |
| 47 |
def test_1(data_regression): |
| 48 |
contents = sys.testing_get_data() |
| 49 |
data_regression.check(contents) |
| 50 |
""" |
| 51 |
|
| 52 |
def get_yaml_contents(): |
| 53 |
yaml_filename = pytester.path / "test_file" / "test_1.yml" |
| 54 |
assert yaml_filename.is_file() |
| 55 |
with yaml_filename.open() as f: |
| 56 |
return yaml.safe_load(f) |
| 57 |
|
| 58 |
check_regression_fixture_workflow( |
| 59 |
pytester, |
| 60 |
source=source, |
| 61 |
data_getter=get_yaml_contents, |
| 62 |
data_modifier=lambda: monkeypatch.setattr( |
| 63 |
sys, |
| 64 |
"testing_get_data", |
| 65 |
lambda: {"contents": "Bar", "value": 20}, |
| 66 |
raising=False, |
| 67 |
), |
| 68 |
expected_data_1={"contents": "Foo", "value": 10}, |
| 69 |
expected_data_2={"contents": "Bar", "value": 20}, |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def test_data_regression_full_path(pytester, tmp_path): |
| 74 |
""" |
| 75 |
Test data_regression with ``fullpath`` parameter. |
| 76 |
""" |
| 77 |
fullpath = tmp_path.joinpath("full/path/to/contents.yaml") |
| 78 |
fullpath.parent.mkdir(parents=True) |
| 79 |
assert not fullpath.is_file() |
| 80 |
|
| 81 |
source = """ |
| 82 |
def test(data_regression): |
| 83 |
contents = {'data': [1, 2]} |
| 84 |
data_regression.check(contents, fullpath=%s) |
| 85 |
""" % ( |
| 86 |
repr(str(fullpath)) |
| 87 |
) |
| 88 |
pytester.makepyfile(test_foo=source) |
| 89 |
|
| 90 |
result = pytester.inline_run() |
| 91 |
result.assertoutcome(failed=1) |
| 92 |
|
| 93 |
|
| 94 |
assert fullpath.is_file() |
| 95 |
result = pytester.inline_run() |
| 96 |
result.assertoutcome(passed=1) |
| 97 |
|
| 98 |
|
| 99 |
def test_data_regression_no_aliases(pytester): |
| 100 |
""" |
| 101 |
YAML standard supports aliases as can be seen here: |
| 102 |
http://pyyaml.org/wiki/PyYAMLDocumentation#Aliases. |
| 103 |
|
| 104 |
Even though this is a resourceful feature, data regression intends to be as human readable as |
| 105 |
possible and it was deemed that YAML aliases make it harder for developers to understand |
| 106 |
contents. |
| 107 |
|
| 108 |
This test makes sure data regression never uses aliases when dumping expected file to YAML. |
| 109 |
""" |
| 110 |
source = """ |
| 111 |
def test(data_regression): |
| 112 |
red = (255, 0, 0) |
| 113 |
green = (0, 255, 0) |
| 114 |
blue = (0, 0, 255) |
| 115 |
|
| 116 |
contents = { |
| 117 |
'color1': red, |
| 118 |
'color2': green, |
| 119 |
'color3': blue, |
| 120 |
'color4': red, |
| 121 |
'color5': green, |
| 122 |
'color6': blue, |
| 123 |
} |
| 124 |
data_regression.Check(contents) |
| 125 |
""" |
| 126 |
pytester.makepyfile(test_file=source) |
| 127 |
|
| 128 |
result = pytester.inline_run() |
| 129 |
result.assertoutcome(failed=1) |
| 130 |
|
| 131 |
yaml_file_contents = pytester.path.joinpath("test_file/test.yml").read_text() |
| 132 |
assert yaml_file_contents == dedent( |
| 133 |
"""\ |
| 134 |
color1: |
| 135 |
- 255 |
| 136 |
- 0 |
| 137 |
- 0 |
| 138 |
color2: |
| 139 |
- 0 |
| 140 |
- 255 |
| 141 |
- 0 |
| 142 |
color3: |
| 143 |
- 0 |
| 144 |
- 0 |
| 145 |
- 255 |
| 146 |
color4: |
| 147 |
- 255 |
| 148 |
- 0 |
| 149 |
- 0 |
| 150 |
color5: |
| 151 |
- 0 |
| 152 |
- 255 |
| 153 |
- 0 |
| 154 |
color6: |
| 155 |
- 0 |
| 156 |
- 0 |
| 157 |
- 255 |
| 158 |
""" |
| 159 |
) |
| 160 |
result = pytester.inline_run() |
| 161 |
result.assertoutcome(passed=1) |
| 162 |
|
| 163 |
|
| 164 |
def test_not_create_file_on_error(pytester): |
| 165 |
"""Basic example where we serializing the object should throw an error and should not create the file""" |
| 166 |
|
| 167 |
source = """ |
| 168 |
def test(data_regression): |
| 169 |
class Scalar: |
| 170 |
def __init__(self, value, unit): |
| 171 |
self.value = value |
| 172 |
self.unit = unit |
| 173 |
|
| 174 |
contents = {"scalar": Scalar(10, "m")} |
| 175 |
data_regression.Check(contents) |
| 176 |
""" |
| 177 |
pytester.makepyfile(test_file=source) |
| 178 |
|
| 179 |
result = pytester.inline_run() |
| 180 |
result.assertoutcome(failed=1) |
| 181 |
|
| 182 |
yaml_file = pytester.path.joinpath("test_file/test.yml") |
| 183 |
assert not yaml_file.is_file() |
| 184 |
|
| 185 |
|
| 186 |
def test_regen_all(pytester, tmp_path): |
| 187 |
source = """ |
| 188 |
def test_1(data_regression): |
| 189 |
contents = {"contents": "Foo", "value": 11} |
| 190 |
data_regression.check(contents, basename="test_1_a") |
| 191 |
|
| 192 |
contents = {"contents": "Bar", "value": 12} |
| 193 |
data_regression.check(contents, basename="test_1_b") |
| 194 |
|
| 195 |
def test_2(data_regression): |
| 196 |
contents = {"contents": "Baz", "value": 33} |
| 197 |
data_regression.check(contents, basename="test_2_a") |
| 198 |
|
| 199 |
contents = {"contents": "BazFoo", "value": 34} |
| 200 |
data_regression.check(contents, basename="test_2_b") |
| 201 |
""" |
| 202 |
pytester.makepyfile(source) |
| 203 |
result = pytester.runpytest("--regen-all") |
| 204 |
|
| 205 |
result.stdout.fnmatch_lines("* 2 passed *") |
| 206 |
data_dir = pytester.path.joinpath("test_regen_all") |
| 207 |
assert {x.name for x in data_dir.iterdir()} == { |
| 208 |
"test_1_a.yml", |
| 209 |
"test_1_b.yml", |
| 210 |
"test_2_a.yml", |
| 211 |
"test_2_b.yml", |
| 212 |
} |
| 213 |
|
| 214 |
result = pytester.runpytest("--regen-all") |
| 215 |
result.stdout.fnmatch_lines("* 2 passed *") |
| 216 |
data_dir = pytester.path.joinpath("test_regen_all") |
| 217 |
assert {x.name for x in data_dir.iterdir()} == { |
| 218 |
"test_1_a.yml", |
| 219 |
"test_1_b.yml", |
| 220 |
"test_2_a.yml", |
| 221 |
"test_2_b.yml", |
| 222 |
} |
| 223 |
|
| 224 |
result = pytester.runpytest() |
| 225 |
result.stdout.fnmatch_lines("* 2 passed *") |
| 226 |
data_dir = pytester.path.joinpath("test_regen_all") |
| 227 |
assert {x.name for x in data_dir.iterdir()} == { |
| 228 |
"test_1_a.yml", |
| 229 |
"test_1_b.yml", |
| 230 |
"test_2_a.yml", |
| 231 |
"test_2_b.yml", |
| 232 |
} |