File: conftest.py

package info (click to toggle)
python-pint 0.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,940 kB
  • sloc: python: 20,478; makefile: 148
file content (98 lines) | stat: -rw-r--r-- 2,246 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# pytest fixtures
from __future__ import annotations

import pathlib

import pytest

import pint

_TINY = """
yocto- = 1e-24 = y-
zepto- = 1e-21 = z-
atto- =  1e-18 = a-
femto- = 1e-15 = f-
pico- =  1e-12 = p-
nano- =  1e-9  = n-
micro- = 1e-6  = µ- = μ- = u- = mu- = mc-
milli- = 1e-3  = m-
centi- = 1e-2  = c-
deci- =  1e-1  = d-
deca- =  1e+1  = da- = deka-
hecto- = 1e2   = h-
kilo- =  1e3   = k-
mega- =  1e6   = M-
giga- =  1e9   = G-
tera- =  1e12  = T-
peta- =  1e15  = P-
exa- =   1e18  = E-
zetta- = 1e21  = Z-
yotta- = 1e24  = Y-

meter = [length] = m = metre
second = [time] = s = sec

angstrom = 1e-10 * meter = Å = ångström = Å
minute = 60 * second = min
"""


@pytest.fixture(scope="session")
def tmppath_factory(tmpdir_factory) -> pathlib.Path:
    tmp = tmpdir_factory.mktemp("pint")
    return pathlib.Path(tmp)


@pytest.fixture(scope="session")
def tiny_definition_file(tmppath_factory: pathlib.Path) -> pathlib.Path:
    folder = tmppath_factory / "definitions"
    folder.mkdir(exist_ok=True, parents=True)
    path = folder / "tiny.txt"
    if not path.exists():
        path.write_text(_TINY, encoding="utf-8")
    return path


@pytest.fixture
def registry_empty():
    return pint.UnitRegistry(None)


@pytest.fixture
def registry_tiny(tiny_definition_file: pathlib.Path):
    return pint.UnitRegistry(tiny_definition_file)


@pytest.fixture
def func_registry():
    return pint.UnitRegistry()


@pytest.fixture(scope="class")
def class_registry():
    """Only use for those test that do not modify the registry."""
    return pint.UnitRegistry()


@pytest.fixture(scope="module")
def module_registry():
    """Only use for those test that do not modify the registry."""
    return pint.UnitRegistry()


@pytest.fixture(scope="session")
def sess_registry():
    """Only use for those test that do not modify the registry."""
    return pint.UnitRegistry()


@pytest.fixture(scope="class")
def class_tiny_app_registry():
    ureg_bak = pint.get_application_registry()
    ureg = pint.UnitRegistry(None)
    ureg.define("foo = []")
    ureg.define("bar = foo / 2")
    pint.set_application_registry(ureg)
    assert pint.get_application_registry() is ureg
    yield ureg
    pint.set_application_registry(ureg_bak)