|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# TODO: move tests one out of src. Pythonnet doesn't run... |
| 3 | + |
| 4 | +"""Helpers for testing.""" |
| 5 | + |
| 6 | +import io |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +import pytest |
| 11 | +import clr |
| 12 | + |
| 13 | +sys.path.append('C:/testdir/') |
| 14 | +clr.AddReference("Python.Test") |
| 15 | +clr.AddReference("System.Collections") |
| 16 | +clr.AddReference("System.Data") |
| 17 | + |
| 18 | +DIR_PATH = os.path.dirname(__file__) |
| 19 | +FILES_DIR = os.path.join(DIR_PATH, 'files') |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def filepath(): |
| 24 | + """Returns full file path for test files.""" |
| 25 | + |
| 26 | + def make_filepath(filename): |
| 27 | + # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture |
| 28 | + # Alternate solution is to use paramtrization `inderect=True` |
| 29 | + # http://stackoverflow.com/a/33879151 |
| 30 | + # Syntax is noisy and requires specific variable names |
| 31 | + return os.path.join(FILES_DIR, filename) |
| 32 | + |
| 33 | + return make_filepath |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture() |
| 37 | +def load_file(filepath): |
| 38 | + """Opens filename with encoding and return its contents.""" |
| 39 | + |
| 40 | + def make_load_file(filename, encoding='utf-8'): |
| 41 | + # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture |
| 42 | + # Alternate solution is to use paramtrization `inderect=True` |
| 43 | + # http://stackoverflow.com/a/33879151 |
| 44 | + # Syntax is noisy and requires specific variable names |
| 45 | + # And seems to be limited to only 1 argument. |
| 46 | + with io.open(filepath(filename), encoding=encoding) as f: |
| 47 | + return f.read().strip() |
| 48 | + |
| 49 | + return make_load_file |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture() |
| 53 | +def get_stream(filepath): |
| 54 | + def make_stream(filename, encoding='utf-8'): |
| 55 | + return io.open(filepath(filename), encoding=encoding) |
| 56 | + |
| 57 | + return make_stream |
0 commit comments