aboutsummaryrefslogtreecommitdiff
path: root/tests/test_trace.py
blob: e7d2cf185ac5500992506128ff6f98707931d165 (plain)
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
import pytest
from qolab.data.trace import loadTrace
import numpy as np
import os


def isItExpectedTrace(tr):
    cfg = tr.getConfig()
    assert cfg["config"]["version"] == "0.1"
    assert cfg["config"]["model"] == "Trace"
    data = tr.getData()
    assert np.all((data - np.array([[1], [3], [2], [5]])) == 0)
    return True


def test_load_uncompressed_v0dot1_trace():
    tr = loadTrace("tests/trace_test_data/xtrace1.dat")
    assert isItExpectedTrace(tr) == True


def test_load_gzip_compressed_v0dot1_trace():
    tr = loadTrace("tests/trace_test_data/xtrace1.dat.gz")
    assert isItExpectedTrace(tr) == True


def test_load_bzip_compressed_v0dot1_trace():
    tr = loadTrace("tests/trace_test_data/xtrace1.dat.bz")
    assert isItExpectedTrace(tr) == True

    tr = loadTrace("tests/trace_test_data/xtrace1.dat.bz2")
    assert isItExpectedTrace(tr) == True


def test_tryCompressedIfMissing():
    fname = "tests/trace_test_data/only_compressed_file1.dat"
    # we check that the guess is working for gzip compressed file (.gz)
    tr = loadTrace(fname, tryCompressedIfMissing=True)
    assert isItExpectedTrace(tr) == True

    fname = "tests/trace_test_data/only_compressed_file2.dat"
    # we check that the guess is working for bzip compressed file (.bz)
    tr = loadTrace(fname, tryCompressedIfMissing=True)
    assert isItExpectedTrace(tr) == True

    fname = "tests/trace_test_data/only_compressed_file3.dat"
    # we check that the guess is working for bzip compressed file (.bz2)
    tr = loadTrace(fname, tryCompressedIfMissing=True)
    assert isItExpectedTrace(tr) == True

    # now we disable search for compressed version
    with pytest.raises(FileNotFoundError) as exc_info:
        tr = loadTrace(fname, tryCompressedIfMissing=False)

    tr = loadTrace("tests/trace_test_data/xtrace1.dat")
    assert isItExpectedTrace(tr) == True


def test_saving_raw():
    import tempfile

    with tempfile.NamedTemporaryFile(delete=True) as fp:
        fp.close()
        fname = fp.name
        assert not os.path.exists(fname)

        tr = loadTrace("tests/trace_test_data/xtrace1.dat")
        tr.save(fname, compressionmethod=None)
        trLoaded = loadTrace(fname)
        assert isItExpectedTrace(trLoaded)
        os.remove(fname)


def test_saving_gzip():
    import tempfile

    with tempfile.NamedTemporaryFile(delete=False) as fp:
        fp.close()
        fname = fp.name + ".bz"
        assert not os.path.exists(fname)

        tr = loadTrace("tests/trace_test_data/xtrace1.dat")
        tr.save(fname, compressionmethod="bzip")
        trLoaded = loadTrace(fname)
        assert isItExpectedTrace(trLoaded)
        os.remove(fname)