blob: 2f51cb2e692a0ebfb1bbd8baec92d5998b0f7e81 [file] [log] [blame]
Javi Merino7f3c8a62014-06-13 11:38:58 +01001#!/usr/bin/python
2
3import unittest
Javi Merino8e8e3212014-06-26 15:24:34 +01004import matplotlib
5import pandas as pd
Javi Merino7f3c8a62014-06-13 11:38:58 +01006
Javi Merino076277d2014-07-02 18:48:18 +01007from test_thermal import BaseTestThermal
Javi Merino6a5c88d2014-06-19 09:43:18 +01008import cr2
Javi Merino7f3c8a62014-06-13 11:38:58 +01009import plot_utils
10
11class TestPlotUtils(unittest.TestCase):
Javi Merino7f3c8a62014-06-13 11:38:58 +010012 def test_normalize_title(self):
13 """Test normalize_title"""
14 self.assertEquals(plot_utils.normalize_title("Foo", ""), "Foo")
15 self.assertEquals(plot_utils.normalize_title("Foo", "Bar"), "Bar - Foo")
Javi Merino3a736552014-06-19 19:22:44 +010016
Javi Merinoa1561272014-06-21 17:49:02 +010017 def test_set_lim(self):
18 """Test set_lim()"""
19
20 class GetSet(object):
21 def __init__(self):
22 self.min = 1
23 self.max = 2
24
25 def get(self):
26 return (self.min, self.max)
27
28 def set(self, minimum, maximum):
29 self.min = minimum
30 self.max = maximum
31
32 gs = GetSet()
33
34 plot_utils.set_lim("default", gs.get, gs.set)
35 self.assertEquals(gs.min, 1)
36 self.assertEquals(gs.max, 2)
37
38 plot_utils.set_lim("range", gs.get, gs.set)
39 self.assertEquals(gs.min, 0.9)
40 self.assertEquals(gs.max, 2.1)
41
42 plot_utils.set_lim((0, 100), gs.get, gs.set)
43 self.assertEquals(gs.min, 0)
44 self.assertEquals(gs.max, 100)
45
46 def test_set_ylim(self):
47 """Test that set_ylim() doesn't bomb"""
48
Javi Merino5981ee82014-08-11 17:45:38 +010049 _, ax = matplotlib.pyplot.subplots()
Javi Merinoa1561272014-06-21 17:49:02 +010050
51 plot_utils.set_ylim(ax, "default")
52 plot_utils.set_ylim(ax, (0, 5))
53
54 def test_set_xlim(self):
55 """Test that set_xlim() doesn't bomb"""
56
Javi Merino5981ee82014-08-11 17:45:38 +010057 _, ax = matplotlib.pyplot.subplots()
Javi Merinoa1561272014-06-21 17:49:02 +010058
59 plot_utils.set_xlim(ax, "default")
60 plot_utils.set_xlim(ax, (0, 5))
61
Javi Merinof9ac5782014-06-21 19:02:50 +010062 def test_pre_plot_setup(self):
63 """Test that plot_utils.pre_plot_setup() doesn't bomb"""
64 plot_utils.pre_plot_setup(None, None)
65 plot_utils.pre_plot_setup(height=9, width=None)
66 plot_utils.pre_plot_setup(height=None, width=9)
67 plot_utils.pre_plot_setup(3, 9)
68
Javi Merino7f88ae32014-08-11 16:53:35 +010069 axis = plot_utils.pre_plot_setup(ncols=2)
70 self.assertEquals(len(axis), 2)
71
Javi Merino270caac2014-08-12 10:41:38 +010072 axis = plot_utils.pre_plot_setup(nrows=2, ncols=3)
73 self.assertEquals(len(axis), 2)
74 self.assertEquals(len(axis[0]), 3)
75 self.assertEquals(len(axis[1]), 3)
76
Javi Merino3a736552014-06-19 19:22:44 +010077 def test_post_plot_setup(self):
78 """Test that post_plot_setup() doesn't bomb"""
79
Javi Merino5981ee82014-08-11 17:45:38 +010080 _, ax = matplotlib.pyplot.subplots()
Javi Merino3a736552014-06-19 19:22:44 +010081
82 plot_utils.post_plot_setup(ax)
83 plot_utils.post_plot_setup(ax, title="Foo")
84 plot_utils.post_plot_setup(ax, ylim=(0, 72))
Javi Merinoa1561272014-06-21 17:49:02 +010085 plot_utils.post_plot_setup(ax, ylim="range")
Javi Merinof9d43af2014-06-21 16:32:04 +010086 plot_utils.post_plot_setup(ax, xlabel="Bar")
Javi Merinod04643f2014-06-21 17:00:16 +010087 plot_utils.post_plot_setup(ax, xlim=(0, 100))
Javi Merinoa1561272014-06-21 17:49:02 +010088 plot_utils.post_plot_setup(ax, xlim="default")
Javi Merino6a5c88d2014-06-19 09:43:18 +010089
Javi Merinoed977c12014-06-25 17:46:17 +010090 def test_plot_hist(self):
91 """Test that plost_hist doesn't bomb"""
92 data = pd.Series([1, 1, 2, 4])
93
94 plot_utils.plot_hist(data, "Foo", 20, "numbers", (0, 4), "default")
95
Javi Merino076277d2014-07-02 18:48:18 +010096class TestPlotUtilsNeedTrace(BaseTestThermal):
Javi Merino6bf48832014-08-11 17:15:35 +010097 def __init__(self, *args, **kwargs):
98 super(TestPlotUtilsNeedTrace, self).__init__(*args, **kwargs)
99 self.map_label = {"0000000f": "A7", "000000f0": "A15"}
100
Javi Merino2919e8d2014-06-26 15:16:05 +0100101 def test_plot_temperature(self):
102 """Test that plot_utils.plot_temperature() doesn't bomb"""
103
Javi Merino1cc4bfc2014-08-11 16:29:11 +0100104 run1 = cr2.Run(name="first")
105 run2 = cr2.Run(name="second")
106 runs = [run1, run2]
Javi Merino2919e8d2014-06-26 15:16:05 +0100107
Javi Merino1cc4bfc2014-08-11 16:29:11 +0100108 plot_utils.plot_temperature(runs, ylim="default")
Javi Merino8e8e3212014-06-26 15:24:34 +0100109 matplotlib.pyplot.close('all')
Javi Merino6bf48832014-08-11 17:15:35 +0100110
111 def test_plot_load(self):
112 """Test that plot_utils.plot_load() doesn't bomb"""
113
114 run1 = cr2.Run(name="first")
115 run2 = cr2.Run(name="second")
116 runs = [run1, run2]
117
118 plot_utils.plot_load(runs, self.map_label, height=5)
119 matplotlib.pyplot.close('all')
Javi Merinoaf05f312014-08-11 17:47:59 +0100120
121 def test_plot_load_single_run(self):
122 """plot_utils.plot_load() can be used with a single run"""
123 run = cr2.Run()
124
125 plot_utils.plot_load([run], self.map_label)
126 matplotlib.pyplot.close('all')
Javi Merino0c484262014-08-12 11:55:04 +0100127
128 def test_plot_allfreqs(self):
129 """Test that plot_utils.plot_allfreqs() doesn't bomb"""
130
131 run1 = cr2.Run(name="first")
132 run2 = cr2.Run(name="second")
133 runs = [run1, run2]
134
135 plot_utils.plot_allfreqs(runs, self.map_label, width=20)
136 matplotlib.pyplot.close('all')
137
138 plot_utils.plot_allfreqs([run1], self.map_label, width=20)
139 matplotlib.pyplot.close('all')