Commit 1a26ef5b authored by Javier Vegas-Regidor's avatar Javier Vegas-Regidor
Browse files

Added more tests

parent ea0a61f3
......@@ -358,7 +358,7 @@ class Variable(object):
parsed[0])
return parsed[0]
if not domains[0]:
elif len(domains) == 0:
Log.warning('Variable {0} has no modeling realm defined'.format(self.short_name))
return None
else:
......@@ -374,9 +374,6 @@ class Variable(object):
self.valid_min = var_line[7].strip()
self.valid_max = var_line[8].strip()
self.grid = var_line[9].strip()
for table in var_line[10].strip().split(':'):
if table:
self.add_table(table)
def get_table(self, frequency, data_convention):
for table, priority in self.tables:
......@@ -385,7 +382,7 @@ class Variable(object):
if self.domain:
table_name = self.domain.get_table_name(frequency, data_convention)
return CMORTable(table_name, frequency, 'December 2013')
return self.tables[0]
raise ValueError('Can not get table for {0} and frequency {1}'.format(self, frequency))
@staticmethod
def _select_most_specific(parsed):
......
......@@ -6,63 +6,6 @@ from earthdiagnostics.modelingrealm import ModelingRealms
from mock import patch, Mock
# noinspection PyTypeChecker,PyTypeChecker,PyTypeChecker
class TestDiagnostic(TestCase):
# noinspection PyMissingOrEmptyDocstring
class MockDiag(Diagnostic):
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):
# noinspection PyTypeChecker
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):
# self.assertEqual(len(cls.process_options(('diag_name', ), tuple())), 0)
class TestDiagnosticOption(TestCase):
def test_good_default_value(self):
......@@ -354,3 +297,73 @@ class TestDiagnosticVariableListOption(TestCase):
mock.short_name = name
return mock
class TestDiagnostic(TestCase):
def setUp(cls):
class MockDiag(Diagnostic):
pass
TestDiagnostic.MockDiag = MockDiag
def test_str(self):
self.assertEqual(str(Diagnostic(None)), 'Developer must override base class __str__ method')
def test_compute_is_virtual(self):
with self.assertRaises(NotImplementedError):
Diagnostic(None).compute()
def test_declare_data_generated_is_virtual(self):
with self.assertRaises(NotImplementedError):
Diagnostic(None).declare_data_generated()
def test_request_data_is_virtual(self):
with self.assertRaises(NotImplementedError):
Diagnostic(None).request_data()
@patch.object(Diagnostic, 'dispatch')
def test_set_status_call_dispatch(self, dispatch_mock):
diag = Diagnostic(None)
diag.status = DiagnosticStatus.FAILED
dispatch_mock.assert_called_once_with(diag)
@patch.object(Diagnostic, 'dispatch')
def test_set_status_call_dispatch(self, dispatch_mock):
class MockDiag(Diagnostic):
pass
diag = Diagnostic(None)
diag.status = diag.status
assert not dispatch_mock.called, 'Dispatch should not have been called'
def test_register(self):
with self.assertRaises(ValueError):
Diagnostic.register(TestDiagnostic)
with self.assertRaises(ValueError):
Diagnostic.register(TestDiagnostic.MockDiag)
TestDiagnostic.MockDiag.alias = 'mock'
Diagnostic.register(TestDiagnostic.MockDiag)
def test_get_diagnostic(self):
self.assertIsNone(Diagnostic.get_diagnostic('none'))
TestDiagnostic.MockDiag.alias = 'mock'
Diagnostic.register(TestDiagnostic.MockDiag)
self.assertIs(TestDiagnostic.MockDiag, Diagnostic.get_diagnostic('mock'))
def test_generate_jobs(self):
with self.assertRaises(NotImplementedError):
Diagnostic.generate_jobs(None, [''])
def test_compute(self):
with self.assertRaises(NotImplementedError):
Diagnostic(None).compute()
def test_repr(self):
self.assertEquals(Diagnostic(None).__repr__(), str(Diagnostic(None)))
def test_empty_process_options(self):
self.assertEqual(len(Diagnostic.process_options(('diag_name',), tuple())), 0)
......@@ -2,7 +2,10 @@
from mock import Mock
from unittest import TestCase
from earthdiagnostics.variable import CMORTable, VariableAlias
from earthdiagnostics.variable import CMORTable, VariableAlias, Variable, VariableJsonException
from earthdiagnostics.modelingrealm import ModelingRealms
from earthdiagnostics.constants import Basins
from earthdiagnostics.frequency import Frequencies
class TestCMORTable(TestCase):
......@@ -24,4 +27,157 @@ class TestVariableAlias(TestCase):
alias.grid = 'grid'
self.assertEquals(str(alias), 'alias Basin: basin Grid: grid')
class TestVariable(TestCase):
def test_parse_json(self):
var = Variable()
json = {'out_name': 'out_name',
'standard_name': 'standard_name',
'long_name': 'long_name',
'modeling_realm': 'ocean',
'valid_min': 'valid_min',
'valid_max': 'valid_max',
'units': 'units',
}
var.parse_json(json, 'out_name')
self.assertEqual(var.short_name, 'out_name')
self.assertEqual(var.standard_name, 'standard_name')
self.assertEqual(var.long_name, 'long_name')
self.assertEqual(var.valid_min, 'valid_min')
self.assertEqual(var.valid_max, 'valid_max')
self.assertEqual(var.units, 'units')
self.assertEqual(var.priority, 1)
self.assertEqual(var.domain, ModelingRealms.ocean)
def test_parse_json_no_out_name(self):
var = Variable()
json = {'standard_name': 'standard_name',
'long_name': 'long_name',
'modeling_realm': 'ocean',
'valid_min': 'valid_min',
'valid_max': 'valid_max',
'units': 'units',
}
with self.assertRaises(VariableJsonException):
var.parse_json(json, 'out_name')
def test_parse_json_with_priority(self):
var = Variable()
json = {'out_name': 'out_name',
'standard_name': 'standard_name',
'long_name': 'long_name',
'modeling_realm': 'ocean',
'valid_min': 'valid_min',
'valid_max': 'valid_max',
'units': 'units',
'priority': '2',
}
var.parse_json(json, 'out_name')
self.assertEqual(var.short_name, 'out_name')
self.assertEqual(var.standard_name, 'standard_name')
self.assertEqual(var.long_name, 'long_name')
self.assertEqual(var.valid_min, 'valid_min')
self.assertEqual(var.valid_max, 'valid_max')
self.assertEqual(var.units, 'units')
self.assertEqual(var.priority, 2)
self.assertEqual(var.domain, ModelingRealms.ocean)
def test_parse_json_with_primavera_priority(self):
var = Variable()
json = {'out_name': 'out_name',
'standard_name': 'standard_name',
'long_name': 'long_name',
'modeling_realm': 'ocean',
'valid_min': 'valid_min',
'valid_max': 'valid_max',
'units': 'units',
'primavera_priority': '2',
}
var.parse_json(json, 'out_name')
self.assertEqual(var.short_name, 'out_name')
self.assertEqual(var.standard_name, 'standard_name')
self.assertEqual(var.long_name, 'long_name')
self.assertEqual(var.valid_min, 'valid_min')
self.assertEqual(var.valid_max, 'valid_max')
self.assertEqual(var.units, 'units')
self.assertEqual(var.priority, 2)
self.assertEqual(var.domain, ModelingRealms.ocean)
def test_get_modelling_realm(self):
var = Variable()
domain = var.get_modelling_realm(('ocean',))
self.assertEqual(ModelingRealms.ocean, domain)
domain = var.get_modelling_realm(('ocean', 'atmos'))
self.assertEqual(ModelingRealms.ocean, domain)
domain = var.get_modelling_realm(('ocean', 'ocnBgchem'))
self.assertEqual(ModelingRealms.ocnBgchem, domain)
domain = var.get_modelling_realm(('ocean', 'seaIce'))
self.assertEqual(ModelingRealms.seaIce, domain)
domain = var.get_modelling_realm(('atmos', 'atmosChem'))
self.assertEqual(ModelingRealms.atmosChem, domain)
domain = var.get_modelling_realm(('land', 'landIce'))
self.assertEqual(ModelingRealms.landIce, domain)
domain = var.get_modelling_realm(tuple())
self.assertIsNone(domain)
def test_parse_csv(self):
var = Variable()
var.parse_csv(['not_used', 'out_name', 'standard_name', 'long_name', 'ocean', 'global', 'units',
'valid_min', 'valid_max', 'grid', 'Amon: '])
self.assertEqual(var.short_name, 'out_name')
self.assertEqual(var.standard_name, 'standard_name')
self.assertEqual(var.long_name, 'long_name')
self.assertEqual(var.valid_min, 'valid_min')
self.assertEqual(var.valid_max, 'valid_max')
self.assertEqual(var.units, 'units')
self.assertEqual(var.grid, 'grid')
self.assertEqual(var.domain, ModelingRealms.ocean)
self.assertEqual(var.basin, Basins().Global)
def test_get_table(self):
var = Variable()
var.domain = ModelingRealms.atmos
table = var.get_table(Frequencies.monthly, 'specs')
self.assertEqual(table.frequency, Frequencies.monthly)
self.assertEqual(table.name, 'Amon')
self.assertEqual(table.date, 'December 2013')
def test_get_table_added(self):
var = Variable()
var.domain = ModelingRealms.atmos
var.add_table(CMORTable('Amon', Frequencies.monthly, 'December 2013'))
table = var.get_table(Frequencies.monthly, 'specs')
self.assertEqual(table.frequency, Frequencies.monthly)
self.assertEqual(table.name, 'Amon')
self.assertEqual(table.date, 'December 2013')
def test_get_table_not_added(self):
var = Variable()
var.domain = ModelingRealms.atmos
var.add_table(CMORTable('Amon', Frequencies.monthly, 'December 2013'))
table = var.get_table(Frequencies.daily, 'specs')
self.assertEqual(table.frequency, Frequencies.daily)
self.assertEqual(table.name, 'day')
self.assertEqual(table.date, 'December 2013')
def test_get_table_not_matching(self):
var = Variable()
with self.assertRaises(ValueError):
var.get_table(Frequencies.daily, 'specs')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment