Test Failed
Push — master ( 312eb6...594eba )
by Ben
02:02
created

test_hdf5()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
import os
2
from contextlib import contextmanager
3
4
import matplotlib as mpl
5
import matplotlib.pyplot as plt
6
import numpy as np
7
from click.testing import CliRunner
8
from matplotlib.colors import colorConverter
9
10
from replotlib import Axes
11
from replotlib.cli import main
12
13
PATH = os.path.dirname(os.path.realpath(__file__))
14
15
16
@contextmanager
17
def ignored(*exceptions):
18
    try:
19
        yield
20
    except exceptions:
21
        pass
22
23
24
def diff(file1, file2, ignore=None):
25
    if isinstance(ignore, str):
26
        ignore = [ignore]
27
    with open(file1) as f1, open(file2) as f2:
28
        for lineno, (line1, line2) in enumerate(zip(f1, f2), 1):
29
            ignoreline = False
30
            if ignore:
31
                for ig in ignore:
32
                    if ig in line1 and ig in line2:
33
                        ignoreline = True
34
                        break
35
            if not ignoreline and line1 != line2:
36
                return lineno
37
    return None
38
39
40
def test_json():
41
    gen_orig(PATH + '/test.rplt', 'json')
42
    runner = CliRunner()
43
    runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt'])
44
    assert diff(PATH + '/test.eps', PATH + '/test_orig.eps',
45
                ['CreationDate', 'Title']) is None
46
47
48
def test_hdf5():
49
    gen_orig(PATH + '/test_h5.rplt', 'hdf5')
50
    runner = CliRunner()
51
    runner.invoke(main, ['-s', PATH + '/test_h5.eps', PATH + '/test_h5.rplt'])
52
    assert diff(PATH + '/test_h5.eps', PATH + '/test_h5_orig.eps',
53
                ['CreationDate', 'Title']) is None
54
55
56
def reset_mpl():
57
    mpl.rcParams.update(mpl.rcParamsDefault)
58
    colorConverter.cache = {}
59
60
61
def test_main():
62
    reset_mpl()
63
    runner = CliRunner()
64
    runner.invoke(main, [PATH + '/test.rplt'])
65
66
67
def test_main_save():
68
    reset_mpl()
69
    mpl.rcParams.update(mpl.rcParamsDefault)
70
    runner = CliRunner()
71
    runner.invoke(main, ['-s', PATH + '/test.eps', PATH + '/test.rplt'])
72
73
74
def test_main_style():
75
    reset_mpl()
76
    mpl.rcParams.update(mpl.rcParamsDefault)
77
    runner = CliRunner()
78
    runner.invoke(main, ['--style_file', PATH + '/style.json', '-s',
79
                         PATH + '/test_styl.eps', PATH + '/test.rplt'])
80
81
def test_main_bb():
82
    reset_mpl()
83
    mpl.rcParams.update(mpl.rcParamsDefault)
84
    runner = CliRunner()
85
    runner.invoke(main, ['--bb', '-s', PATH + '/test_bb.eps',
86
                         PATH + '/test.rplt'])
87
88
89
def gen_orig(test_file=PATH + '/test.rplt', file_type='json'):
90
    reset_mpl()
91
    plt.figure()
92
    style = {'nice_color': {'color': 'green'}}
93
    with ignored(OSError):
94
        os.remove(test_file)
95
    print(test_file, file_type)
96
    plt.rcParams['lines.linewidth'] = 3
97
    plt.rcParams['lines.color'] = 'k'
98
    ax = Axes(test_file, file_type=file_type, style=style)
99
    ax.rcParams = {'lines.linewidth': 3,
100
                   'lines.color': 'k'}
101
    style = {'strong_color': {'color': 'blue'},
102
             'errorbar': {'fmt': 'o'}}
103
    ax.style = style
104
    x = np.linspace(0, 1)
105
    ax.plot(x, x**2, color='c', label=r'$x^2$')
106
    ax.errorbar(x, x**2, x**2*0.1)
107
    ax.plot(x, x**3, style='strong_color')
108
    ax.plot(x, x**4, style='nice_color')
109
    ax.set_xlabel('x')
110
    ax.set_ylabel('y')
111
    ax.legend()
112
    plt.savefig(test_file.replace('.rplt', '_orig.eps'))
113
    reset_mpl()
114