|
| 1 | +import json |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from openeo.testing.io import TestDataLoader |
| 8 | + |
| 9 | + |
| 10 | +class TestTestDataLoader: |
| 11 | + def test_get_path(self, tmp_path): |
| 12 | + path = tmp_path / "hello" / "world.txt" |
| 13 | + loader = TestDataLoader(root=tmp_path) |
| 14 | + assert loader.get_path("hello/world.txt") == path |
| 15 | + assert loader.get_path(Path("hello/world.txt")) == path |
| 16 | + |
| 17 | + def test_load_bytes(self, tmp_path): |
| 18 | + path = tmp_path / "hello" / "world.txt" |
| 19 | + path.parent.mkdir(parents=True) |
| 20 | + path.write_bytes(b"Hello W\x00rld") |
| 21 | + |
| 22 | + loader = TestDataLoader(root=tmp_path) |
| 23 | + assert loader.load_bytes("hello/world.txt") == b"Hello W\x00rld" |
| 24 | + |
| 25 | + @pytest.mark.parametrize( |
| 26 | + ["preprocess", "expected"], |
| 27 | + [ |
| 28 | + (None, "Hello, World!"), |
| 29 | + (lambda s: s.lower(), "hello, world!"), |
| 30 | + (lambda s: s.replace("World", "Earth"), "Hello, Earth!"), |
| 31 | + ({"World": "Earth"}, "Hello, Earth!"), |
| 32 | + ({"Hello": "Greetings", "World": "Terra"}, "Greetings, Terra!"), |
| 33 | + ({re.compile("l+"): "|_"}, "He|_o, Wor|_d!"), |
| 34 | + ({re.compile("([A-Z])"): r"\1\1\1"}, "HHHello, WWWorld!"), |
| 35 | + ], |
| 36 | + ) |
| 37 | + def test_load_text(self, tmp_path, preprocess, expected): |
| 38 | + (tmp_path / "hello.txt").write_text("Hello, World!", encoding="utf8") |
| 39 | + |
| 40 | + loader = TestDataLoader(root=tmp_path) |
| 41 | + assert loader.load_text("hello.txt", preprocess=preprocess) == expected |
| 42 | + |
| 43 | + @pytest.mark.parametrize( |
| 44 | + ["preprocess", "expected"], |
| 45 | + [ |
| 46 | + (None, {"salutation": "Hello", "target": "World"}), |
| 47 | + (lambda s: s.upper(), {"SALUTATION": "HELLO", "TARGET": "WORLD"}), |
| 48 | + ( |
| 49 | + lambda s: s.replace("World", "Terra"), |
| 50 | + {"salutation": "Hello", "target": "Terra"}, |
| 51 | + ), |
| 52 | + ( |
| 53 | + lambda s: s.replace('"World"', '["Terra","Earth"]'), |
| 54 | + {"salutation": "Hello", "target": ["Terra", "Earth"]}, |
| 55 | + ), |
| 56 | + ( |
| 57 | + {"World": "Earth", "salutation": "say", "target": "to"}, |
| 58 | + {"say": "Hello", "to": "Earth"}, |
| 59 | + ), |
| 60 | + ( |
| 61 | + {"Hello": "Greetings", '"World"': '["Terra","Earth"]'}, |
| 62 | + {"salutation": "Greetings", "target": ["Terra", "Earth"]}, |
| 63 | + ), |
| 64 | + ( |
| 65 | + {re.compile("([aeoiu])"): r"\1\1\1"}, |
| 66 | + {"saaaluuutaaatiiiooon": "Heeellooo", "taaargeeet": "Wooorld"}, |
| 67 | + ), |
| 68 | + ], |
| 69 | + ) |
| 70 | + def test_load_json(self, tmp_path, preprocess, expected): |
| 71 | + with (tmp_path / "data.json").open("w", encoding="utf8") as f: |
| 72 | + json.dump({"salutation": "Hello", "target": "World"}, f) |
| 73 | + |
| 74 | + loader = TestDataLoader(root=tmp_path) |
| 75 | + assert loader.load_json("data.json", preprocess=preprocess) == expected |
0 commit comments