File: test_loader.py

package info (click to toggle)
kineticstools 0.6.1%2Bgit20220223.1326a4d%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,140 kB
  • sloc: python: 3,503; makefile: 202; ansic: 104; sh: 55; xml: 19
file content (129 lines) | stat: -rw-r--r-- 4,104 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os

import pytest

import kineticsTools.loader as B


def test_basic():
    expected = 'anything'
    assert expected == B.getIpdModelFilename(expected, 'foo', [])

    with pytest.raises(Exception) as excinfo:
        B.getIpdModelFilename(None, 'unknown', [])
    assert 'Chemistry cannot be identified' in str(excinfo.value)

    with pytest.raises(Exception) as excinfo:
        B.getIpdModelFilename(None, 'foo', [])
    assert 'No kinetics model available for this chemistry' in str(
        excinfo.value)


def test_path(monkeypatch):
    def isfile(fn):
        if fn in ('path1/foo.npz.gz', 'path2/foo.npz.gz'):
            return True
        if fn == 'pathmissing/foo.npz.gz':
            return False
        raise Exception('Called! {!r}'.format(fn))
    monkeypatch.setattr(os.path, 'isfile', isfile)

    chem = 'foo'

    expected = 'path1/foo.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1'])

    expected = 'path1/foo.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['pathmissing', 'path1'])

    expected = 'path1/foo.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['path1', 'pathmissing'])

    expected = 'path1/foo.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1', 'path2'])

    # Missing path first.
    expected = 'path1/foo.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['pathmissing', 'path1', 'path2'])


def test_path_with_prefixed_chem(monkeypatch):
    def isfile(fn):
        if fn in ('path1/SP2-C2.npz.gz', 'path2/SP2-C2.npz.gz',
                  'path1/SP3-C3.npz.gz', 'path2/SP3-C3.npz.gz'):
            return True
        if fn in ('pathmissing/SP2-C2.npz.gz', 'pathmissing/SP3-C3.npz.gz'):
            return False
        raise Exception('Called! {!r}'.format(fn))
    monkeypatch.setattr(os.path, 'isfile', isfile)

    chem = 'S/P1-foo'  # S/ prefix is weird for now.

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['pathmissing', 'path1'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['path1', 'pathmissing'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1', 'path2'])

    chem = 'S/P2-foo'

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['pathmissing', 'path1'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['path1', 'pathmissing'])

    expected = 'path1/SP2-C2.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1', 'path2'])

    chem = 'S/P3-foo'

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['pathmissing', 'path1'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['path1', 'pathmissing'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1', 'path2'])

    chem = 'S/foo'

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['pathmissing', 'path1'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(
        None, chem, ['path1', 'pathmissing'])

    expected = 'path1/SP3-C3.npz.gz'
    assert expected == B.getIpdModelFilename(None, chem, ['path1', 'path2'])


def test_getResourcePathSpec(monkeypatch):
    monkeypatch.setenv('SMRT_CHEMISTRY_BUNDLE_DIR', 'foo')
    assert 'foo/kineticsTools:bar' == B.getResourcePathSpec('bar')