aboutsummaryrefslogtreecommitdiff
path: root/tests/test_file_utils.py
blob: ca799f33848af585dd162d555623d2bd40568d4e (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
86
87
import os

from qolab.file_utils import infer_compression, save_table_with_header


def test_infer_compression():
    assert infer_compression("data.dat") is None
    assert infer_compression("data.unknown_ext") is None
    assert infer_compression("data.dat.gz") == "gzip"
    assert infer_compression("data.dat.bz") == "bzip"
    assert infer_compression("data.dat.bz2") == "bzip"


def test_save_table_with_header_match_filename_to_compression():
    import tempfile

    data = [[1, 3], [2, 5]]
    headerstr = ["header 1", "header 2"]
    with tempfile.TemporaryDirectory(prefix="qolab_test") as d:
        fname = os.path.join(d, "t.dat")
        assert save_table_with_header(
            fname,
            data,
            header=headerstr,
            compressionmethod=None,
            match_filename_to_compression=True,
        ) == (fname)
        assert save_table_with_header(
            fname,
            data,
            header=headerstr,
            compressionmethod="gzip",
            match_filename_to_compression=True,
        ) == (fname + ".gz")
        assert save_table_with_header(
            fname,
            data,
            header=headerstr,
            compressionmethod="bzip",
            match_filename_to_compression=True,
        ) == (fname + ".bz")

        # now cases when requested extension does not match the compression
        assert save_table_with_header(
            fname + ".gz",
            data,
            header=headerstr,
            compressionmethod=None,
            match_filename_to_compression=True,
        ) == (fname + ".gz.dat")
        assert save_table_with_header(
            fname + ".bz",
            data,
            header=headerstr,
            compressionmethod="gzip",
            match_filename_to_compression=True,
        ) == (fname + ".bz.gz")
        assert save_table_with_header(
            fname + ".gz",
            data,
            header=headerstr,
            compressionmethod="bzip",
            match_filename_to_compression=True,
        ) == (fname + ".gz.bz")

        # do as I told cases, extension does not match compression
        assert save_table_with_header(
            fname + ".gz",
            data,
            header=headerstr,
            compressionmethod=None,
            match_filename_to_compression=False,
        ) == (fname + ".gz")
        assert save_table_with_header(
            fname,
            data,
            header=headerstr,
            compressionmethod="gzip",
            match_filename_to_compression=False,
        ) == (fname)
        assert save_table_with_header(
            fname,
            data,
            header=headerstr,
            compressionmethod="bzip",
            match_filename_to_compression=False,
        ) == (fname)