test_mpl_fig.py 10.1 KB
Newer Older
1
2
3
4
5
6
7
#!/usr/bin/env python

import os, sys
import unittest

sys.path.append('../')

8
from figures_tests import FigTest, BrokenFigure, EmptyFig, FigTestWithAliases
9
from figures_tests import FigTestAxesAliases
10
11
from axes_tests import AxWithKeywords
from ios_tests import read_stupid_data
12
from myplotlib import HorizontalGrid
13
14
from itertools import repeat

15
16
17
class TestMyFig(unittest.TestCase):

    def setUp(self):
18
        
19
        self.data = read_stupid_data('data')
20
        
21
        self.data_1 = [-1., 1., 1., -1.]
22

23
24
        self.formatted_data_1 = {'x_data': [self.data_1[0], self.data_1[1]],
                                 'y_data': [self.data_1[2], self.data_1[3]]}
25

26
        self.data_2 = [1., -1., -1., 1.]
27
        
28
29
        self.formatted_data_2 = {'x_data': [self.data_2[0], self.data_2[1]],
                                 'y_data': [self.data_2[2], self.data_2[3]]}
30
31
32
33
34
35
36
37

    def test_contructor_broken(self):
        '''Verify that the NotImplementedError is raised
        when addAxes method is not implemented.
        
        Done to prevent typos issues.
        '''
        with self.assertRaises(NotImplementedError):
38
            fig = BrokenFigure((self.data_1,))
39
40
41
42
43
44
45
46
47

    def test_constructor_default(self):
        '''Verify that the figure's attributes are set as defined in config.py.
        
        Test the default constructor and that default values are correctly 
        passed.
        '''
        fig = EmptyFig(())
        # test if the figure got the default attributes
48
49
        self.assertEqual(fig.rawdata, ())
        self.assertEqual(fig.fignum, -1)
50
        self.assertTrue(fig.debug)
51
52
        self.assertEqual(fig.aliases, {})
        self.assertEqual(fig.FIGSIZE, (8., 6.))
53
        self.assertFalse(fig.bounded_to_win)
54
        self.assertEqual(fig.axes, [])
55
        self.assertIsInstance(fig.grid, HorizontalGrid)
56
57
58
59
60
61
            
    def test_constructor_minimal(self):
        '''Verify that the rawdata are properly passed to the axe in the default case.

        Test if the rawdata are well passed in the case of a rawdata tuple.
        '''
62
        fig = FigTest((self.data_1,))
63
        # Test the axes attributes
64
65
        self.assertEqual(fig.rawdata, (self.data_1,))
        self.assertEqual(fig.axes[0].data, self.formatted_data_1)
66
67
68
69
70
71

    def test_constructor_with_multiple_dataset(self):
        '''Verify that the rawdata are properly passed to the axes in the case of multiple dataset.

        Test if the rawdata are well passed in the case of multiple dataset.
        '''
72
        fig = FigTestWithAliases((self.data_1, self.data_2))
73
74
        # test the axes data
        # and fig rawdata
75
76
77
        self.assertEqual(fig.rawdata, (self.data_1, self.data_2))
        self.assertEqual(fig.axes[0].data, self.formatted_data_1)
        self.assertEqual(fig.axes[1].data, self.formatted_data_2)
78
79
        
    def test_constructor_with_single_dataset(self):
80
81
        '''Verify that the rawdata are properly failing to the axes in the case of single dataset
 
82
        '''
83
84
85
86
87
88
89
90
91
92
93
        with self.assertRaises(TypeError):
            fig = FigTestWithAliases(12.)

        with self.assertRaises(TypeError):
            fig = FigTestWithAliases('dummy')            

        with self.assertRaises(TypeError):            
            fig = FigTestWithAliases(self.data_1)

        with self.assertRaises(TypeError):
            fig = FigTestWithAliases({'dummy': 42.})            
94
95
96
97
98
99
100
        
    def test_constructor_with_fig_attributes(self):
        '''Verify that the meaningful keywords are properly passed.

        Test that debug, fignum and figsize are well set accordingly to 
        the keywords given in the constructor.
        '''
101
        fig = FigTest((self.data_1,), fignum=42, debug=False, figsize=(6., 8.))
102
        # test figure attributes
103
        self.assertEqual(fig.fignum, 42)
104
        self.assertFalse(fig.debug)
105
        self.assertEqual(fig.FIGSIZE, (6., 8.))
106
107
108
109
110
111
112

    def test_constructor_with_axes_keywords(self):
        '''Verify that keywords meant for axes are well passed.

        Test that the keyword xRange given to the constructor is passed to the
        axes.
        '''
113
        fig = FigTest((self.data_1,), p1_x_range=[-1., 1.])
114
        # test axes attributes
115
        self.assertEqual(fig.axes[0].keywords['x_range'], [-1., 1.])
116
117

    def test_constructore_with_aliased_keywords(self):
118
        '''Verify that keywords meant for axes are well passed, even when aliased.
119

120
121
        Test that a non-aliased keyword goes to all axes (x_range) and that tagged
        keywords are well passed (p1_y_range)
122
        '''
123
        fig = FigTestAxesAliases((self.data_1, self.data_2), x_range=[-2., 2.], p1_y_range=[-3., 3.])
124
        # test axes attributes
125
126
127
128
129
        self.assertEqual(fig.axes[0].keywords['x_range'], [-2., 2.])
        self.assertEqual(fig.axes[1].keywords['x_range'], [-2., 2.])
        self.assertEqual(fig.axes[0].keywords['y_range'], [-3., 3.])
        self.assertEqual(fig.axes[1].keywords['y_range'], [-1., 1.])

130
    def test_update_fig_attributes(self):
131
132
133
134
        '''Verify that figure's keywords are well updated.

        Test that updatable keywords from the figure can be updated by the
        update function.
135
        '''
136
137
138

        fig = FigTestAxesAliases((self.data_1,))
        ax = fig.get_axes_by_name('p1')
139
        
140
        save_auto_keywords = fig._tagged_keywords
141
142
143
        
        fig.aliases.update({'alias_for_yrange': ('p1_y_range',)})
        
144
145
146
147
        fig.update(fignum=42.,
                   reformat=True,
                   formatted=False,
                   debug=False,
148
                   aliases={'second_alias_for_yrange': ('p1_y_range',)})
149

150
        self.assertEqual(fig.fignum, 42.)
151
152
153
        self.assertTrue(fig.reformat)
        self.assertFalse(fig.formatted)
        self.assertFalse(fig.debug)
154
155
156
157
158
159
160
161
162
        self.assertTrue("alias_for_yrange" in fig.aliases)
        self.assertTrue("second_alias_for_yrange" in fig.aliases)
        self.assertEqual(fig.aliases["alias_for_yrange"], ('p1_y_range',)) 
        self.assertEqual(fig.aliases['second_alias_for_yrange'], ('p1_y_range',))
        
        # Are the _tagged_keywords set correctly?
        
        fig.update(y_range=[12, 13], title=r"something")
        
163
164
165
        self.assertTrue("p1_x_range" in fig._tagged_keywords)
        self.assertTrue("p1_y_range" in fig._tagged_keywords)
        self.assertTrue("p1_title" in fig._tagged_keywords)
166
        
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        # Do they have the correct value?
        
        self.assertEqual(fig._tagged_keywords["p1_x_range"], (ax,'x_range'))
        self.assertEqual(fig._tagged_keywords["p1_y_range"], (ax,'y_range'))
        self.assertEqual(fig._tagged_keywords["p1_title"], (ax,'title'))
        
        # Are the axes keywords set correctly?
        
        self.assertEqual(ax.keywords["y_range"], [12, 13])
        self.assertEqual(ax.keywords["title"], r"something")

        # test passing aliases
        fig.update(alias_for_yrange=[10, 31])
        self.assertEqual(ax.keywords["y_range"], [10, 31])

182
        # test if explicit keywords have higher priority than aliases
183
184
        fig.update(alias_for_yrange=[10, 31], p1_y_range=[31, 10])
        self.assertEqual(ax.keywords["y_range"], [31, 10])
185

186
187
188
        # test if aliases have higher priority than keywords
        fig.update(y_range=[31, 10], alias_for_yrange=[10, 31])
        self.assertEqual(ax.keywords["y_range"], [10, 31])
189

190
191
192
193
        # test if explicit keywords have higher priority than keywords
        fig.update(y_range=[12, 13], p1_y_range=[15, 20])
        self.assertEqual(ax.keywords["y_range"], [15, 20])
        
194
    def test_set_rawdata(self):
195
196
        '''Verify the well function of set_rawdata

197
        Test if set_rawdata works with a tuple, a MplData obj, and 
198
        return a false status with an array.
199
        '''
200
        fig = FigTest((self.data_1,))
201

202
203
        fig.update(formatted=False)
        status = False
204
        status = fig.set_rawdata((self.data_2,))
205
        self.assertTrue(fig.formatted)
206
207
        self.assertEqual(fig.rawdata, (self.data_2,))
        self.assertEqual(fig.axes[0].data, self.formatted_data_2)
208
209
210
        self.assertTrue(status)

        fig.update(formatted=False)
211
212
        with self.assertRaises(TypeError):
            status = fig.set_rawdata(self.data_1)
213
214
215

        fig.update(formatted=False)
        status = False
216
        status = fig.set_rawdata((12., 14., 15., 16.))
217
218
        self.assertFalse(fig.formatted)
        self.assertIsNone(fig.rawdata)
219
        self.assertEqual(fig.axes[0].data, self.formatted_data_2)
220
        self.assertFalse(status)
221

222
    def test_update_rawdata(self):
223
        '''Verify that the special keyword rawdata is well passed
224
        '''
Yori Fournier's avatar
Yori Fournier committed
225
        fig = FigTest((self.data_1,))
226
        fig.update(rawdata=(self.data_2,))
227
        self.assertTrue(fig.formatted)
228
        self.assertEqual(fig.rawdata, (self.data_2,))
229
230

    def test_update_axes_keywords(self):
231
        '''Verify that axes' keywords are well passed
232
        '''
233
234
235
        fig = FigTest((self.data_1,), x_range=[-2., 2.])
        fig.update(x_range=[-1., 1.])
        self.assertEqual(fig.axes[0].keywords['x_range'], [-1., 1.])
236

237
    def test_add_axes(self):
238
239
        '''
        '''
240
        fig = EmptyFig(())
241
        dynamic_axes = AxWithKeywords(fig)
Yori Fournier's avatar
Yori Fournier committed
242
        fig.add_axes(dynamic_axes, "dynamic_plot")
243
        self.assertEqual(len(fig.axes), 1)
244
        self.assertEqual(fig.axes[0], dynamic_axes)
245

246
247
248
    def test_get_axes_by_name(self):
        '''
        '''
249
250
        fig = FigTestWithAliases((self.data_1, self.data_2))
        ax = fig.get_axes_by_name("p2")
251
        self.assertEqual(ax, fig.axes[1])
252
253
254
255

    def test_format_rawdata(self):
        '''
        '''
256
        pass
257
258
259
260
261
262
263
264
265
266
267
268
269

    def test_plot(self):
        '''
        '''
        implemented=False
        self.assertFalse(implemented)

    def test_reset(self):
        '''
        '''
        implemented=False
        self.assertFalse(implemented)                

270
    def test_print_to_file(self):
271
272
273
        '''
        '''
        implemented=False
274
275
276
277
278
279
280
281
        self.assertFalse(implemented)

    def test_layout(self):
        '''
        '''
        implemented=False
        self.assertFalse(implemented)

282
    def test_set_grid(self):
283
284
285
286
        '''
        '''
        implemented=False
        self.assertFalse(implemented)
287

288
289
if __name__ == '__main__':
    unittest.main()