Newer
Older
from earthdiagnostics.diagnostic import *
from earthdiagnostics.modelingrealm import ModelingRealms
# noinspection PyTypeChecker,PyTypeChecker,PyTypeChecker
class TestDiagnostic(TestCase):
# noinspection PyMissingOrEmptyDocstring
def request_data(self):
pass
def declare_data_generated(self):
pass
def compute(self):
pass
@classmethod
def generate_jobs(cls, diags, options):
pass
alias = 'mockdiag'
def setUp(self):
self.diagnostic = Diagnostic(None)
Diagnostic.register(TestDiagnostic.MockDiag)
def test_register(self):
with self.assertRaises(ValueError):
Diagnostic.register(str)
with self.assertRaises(ValueError):
Diagnostic.register(Diagnostic)
Diagnostic.register(TestDiagnostic.MockDiag)
def test_get_diagnostic(self):
self.assertIsNone(Diagnostic.get_diagnostic('none'))
self.assertIs(TestDiagnostic.MockDiag, Diagnostic.get_diagnostic('mockdiag'))
def test_generate_jobs(self):
with self.assertRaises(NotImplementedError):
Diagnostic.generate_jobs(None, [''])
def test_compute(self):
with self.assertRaises(NotImplementedError):
self.diagnostic.compute()
def test_str(self):
self.assertEquals('Developer must override base class __str__ method', str(self.diagnostic))
def test_repr(self):
self.assertEquals(self.diagnostic.__repr__(), str(self.diagnostic))
def test_empty_process_options(self):
self.assertEqual(len(Diagnostic.process_options(('diag_name',), tuple())), 0)
# def test_empty_process_options(self):
Javier Vegas-Regidor
committed
# self.assertEqual(len(cls.process_options(('diag_name', ), tuple())), 0)
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class TestDiagnosticOption(TestCase):
def test_good_default_value(self):
diag = DiagnosticOption('option', 'default')
self.assertEqual('default', diag.parse(''))
def test_no_default_value(self):
diag = DiagnosticOption('option')
with self.assertRaises(DiagnosticOptionError):
self.assertEqual('default', diag.parse(''))
def test_parse_value(self):
diag = DiagnosticOption('option')
self.assertEqual('value', diag.parse('value'))
class TestDiagnosticFloatOption(TestCase):
def test_float_default_value(self):
diag = DiagnosticFloatOption('option', 3.0)
self.assertEqual(3.0, diag.parse(''))
def test_str_default_value(self):
diag = DiagnosticFloatOption('option', '3')
self.assertEqual(3.0, diag.parse(''))
def test_bad_default_value(self):
diag = DiagnosticFloatOption('option', 'default')
with self.assertRaises(ValueError):
self.assertEqual('default', diag.parse(''))
def test_no_default_value(self):
diag = DiagnosticFloatOption('option')
with self.assertRaises(DiagnosticOptionError):
self.assertEqual('default', diag.parse(''))
def test_parse_value(self):
diag = DiagnosticFloatOption('option')
self.assertEqual(3.25, diag.parse('3.25'))
class TestDiagnosticDomainOption(TestCase):
def test_domain_default_value(self):
diag = DiagnosticDomainOption('option', ModelingRealms.ocean)
self.assertEqual(ModelingRealms.ocean, diag.parse(''))
def test_str_default_value(self):
diag = DiagnosticDomainOption('option', 'atmos')
self.assertEqual(ModelingRealms.atmos, diag.parse(''))
def test_bad_default_value(self):
diag = DiagnosticDomainOption('option', 'default')
with self.assertRaises(ValueError):
diag.parse('')
def test_no_default_value(self):
diag = DiagnosticDomainOption('option')
with self.assertRaises(DiagnosticOptionError):
diag.parse('')
def test_parse_value(self):
diag = DiagnosticDomainOption('option')
self.assertEqual(ModelingRealms.seaIce, diag.parse('seaice'))
class TestDiagnosticIntOption(TestCase):
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
diag = DiagnosticIntOption('option', 3)
self.assertEqual(3, diag.parse(''))
def test_str_default_value(self):
diag = DiagnosticIntOption('option', '3')
self.assertEqual(3, diag.parse(''))
def test_bad_default_value(self):
diag = DiagnosticIntOption('option', 'default')
with self.assertRaises(ValueError):
diag.parse('')
def test_no_default_value(self):
diag = DiagnosticIntOption('option')
with self.assertRaises(DiagnosticOptionError):
diag.parse('')
def test_parse_value(self):
diag = DiagnosticIntOption('option')
self.assertEqual(3, diag.parse('3'))
def test_parse_bad_value(self):
diag = DiagnosticIntOption('option')
with self.assertRaises(ValueError):
diag.parse('3.5')
def test_good_low_limit(self):
diag = DiagnosticIntOption('option', None, 0)
self.assertEqual(1, diag.parse('1'))
def test_bad_low_limit(self):
diag = DiagnosticIntOption('option', None, 0)
with self.assertRaises(DiagnosticOptionError):
diag.parse('-1')
def test_good_high_limit(self):
diag = DiagnosticIntOption('option', None, None, 0)
self.assertEqual(-1, diag.parse('-1'))
def test_bad_high_limit(self):
diag = DiagnosticIntOption('option', None, None, 0)
with self.assertRaises(DiagnosticOptionError):
diag.parse('1')
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class TestDiagnosticBoolOption(TestCase):
def test_bool_default_value(self):
diag = DiagnosticBoolOption('option', True)
self.assertEqual(True, diag.parse(''))
def test_str_default_value(self):
diag = DiagnosticBoolOption('option', 'False')
self.assertEqual(False, diag.parse(''))
def test_no_default_value(self):
diag = DiagnosticBoolOption('option')
with self.assertRaises(DiagnosticOptionError):
diag.parse('')
def test_parse_True(self):
diag = DiagnosticBoolOption('option')
self.assertTrue(diag.parse('true'))
def test_parse_true(self):
diag = DiagnosticBoolOption('option')
self.assertTrue(diag.parse('true'))
def test_parse_t(self):
diag = DiagnosticBoolOption('option')
self.assertTrue(diag.parse('t'))
def test_parse_yes(self):
diag = DiagnosticBoolOption('option')
self.assertTrue(diag.parse('YES'))
def test_parse_bad_value(self):
diag = DiagnosticBoolOption('option')
self.assertFalse(diag.parse('3.5'))
class TestDiagnosticComplexStrOption(TestCase):
def test_complex_default_value(self):
diag = DiagnosticComplexStrOption('option', 'default&.str&;&.working')
self.assertEqual('default str, working', diag.parse(''))
def test_simple_default_value(self):
diag = DiagnosticComplexStrOption('default str, working', 'default str, working')
self.assertEqual('default str, working', diag.parse(''))
def test_no_default_value(self):
diag = DiagnosticComplexStrOption('option')
with self.assertRaises(DiagnosticOptionError):
diag.parse('')
def test_parse_value(self):
diag = DiagnosticComplexStrOption('option')
self.assertEqual('complex string, for testing', diag.parse('complex&.string&;&.for&.testing'))
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class TestDiagnosticListIntOption(TestCase):
def test_tuple_default_value(self):
diag = DiagnosticListIntOption('option', (3,))
self.assertEqual((3,), diag.parse(''))
def test_list_default_value(self):
diag = DiagnosticListIntOption('option', [3])
self.assertEqual([3], diag.parse(''))
def test_str_default_value(self):
diag = DiagnosticListIntOption('option', '3-4')
self.assertEqual([3, 4], diag.parse(''))
def test_bad_default_value(self):
diag = DiagnosticListIntOption('option', 'default')
with self.assertRaises(ValueError):
diag.parse('')
def test_no_default_value(self):
diag = DiagnosticListIntOption('option')
with self.assertRaises(DiagnosticOptionError):
diag.parse('')
def test_parse_value(self):
diag = DiagnosticListIntOption('option')
self.assertEqual([3, 2], diag.parse('3-2'))
def test_parse_single_value(self):
diag = DiagnosticListIntOption('option')
self.assertEqual([3], diag.parse('3'))
def test_too_low(self):
diag = DiagnosticListIntOption('option', min_limit=5)
with self.assertRaises(DiagnosticOptionError):
diag.parse('3')
def test_too_high(self):
diag = DiagnosticListIntOption('option', max_limit=5)
with self.assertRaises(DiagnosticOptionError):
diag.parse('8')
def test_parse_bad_value(self):
diag = DiagnosticListIntOption('option')
with self.assertRaises(ValueError):
diag.parse('3.5')
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
class TestDiagnosticChoiceOption(TestCase):
def test_choice_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'))
self.assertEqual('a', diag.parse('a'))
def test_choice_default_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'), default_value='a')
self.assertEqual('a', diag.parse(''))
def test_bad_default_value(self):
with self.assertRaises(DiagnosticOptionError):
DiagnosticChoiceOption('option', ('a', 'b'), default_value='c')
def test_ignore_case_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'))
self.assertEqual('b', diag.parse('b'))
self.assertEqual('b', diag.parse('B'))
diag = DiagnosticChoiceOption('option', ('a', 'b'), ignore_case=False)
self.assertEqual('b', diag.parse('b'))
with self.assertRaises(DiagnosticOptionError):
self.assertEqual('b', diag.parse('B'))
class TestDiagnosticVariableOption(TestCase):
def get_var_mock(self, name):
mock = Mock()
mock.short_name = name
return mock
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_not_recognized(self, get_variable_mock):
get_variable_mock.return_value = None
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
class TestDiagnosticVariableListOption(TestCase):
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse_multiple(self, get_variable_mock):
get_variable_mock.side_effect = (self.get_var_mock('var1'), self.get_var_mock('var2'))
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1', 'var2'], diag.parse('var1-var2'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse_one(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1'], diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_not_recognized(self, get_variable_mock):
get_variable_mock.return_value = None
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1'], diag.parse('var1'))
def get_var_mock(self, name):
mock = Mock()
mock.short_name = name
return mock