Skip to content

Commit

Permalink
wip tests (variable, process)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy committed Jun 26, 2017
1 parent 2013c0f commit a713ba8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
8 changes: 4 additions & 4 deletions xsimlab/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ def validate(self):
Variable.validators
"""
pass
pass # pragma: no cover

def initialize(self):
"""This method will be called once at the beginning of a model run.
Implementation is optional (by default it does nothing).
"""
pass
pass # pragma: no cover

def run_step(self, *args):
"""This method will be called at every time step of a model run.
Expand All @@ -203,14 +203,14 @@ def finalize_step(self):
Implementation is optional (by default it does nothing).
"""
pass
pass # pragma: no cover

def finalize(self):
"""This method will be called once at the end of a model run.
Implementation is optional (by default does nothing).
"""
pass
pass # pragma: no cover

@combomethod
def info(cls_or_self, buf=None):
Expand Down
82 changes: 82 additions & 0 deletions xsimlab/tests/test_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import unittest

from xsimlab.variable.base import (Variable, VariableList, VariableGroup,
diagnostic)
from xsimlab.process import Process


class MyProcess(Process):
var = Variable(())
var_list = VariableList([Variable(()), Variable(())])
var_group = VariableGroup('group')
no_var = 'this is not a variable object'

class Meta:
time_dependent = False

@diagnostic
def diag(self):
return 1


class TestProcessBase(unittest.TestCase):

def test_new(self):
with self.assertRaisesRegex(TypeError, "subclassing a subclass"):
class InvalidProcess(MyProcess):
var = Variable(())

with self.assertRaisesRegex(AttributeError, "invalid attribute"):
class InvalidProcess2(Process):
class Meta:
time_dependent = True
invalid_meta_attr = 'invalid'

# test extract variable objects vs. other attributes
self.assertTrue(getattr(MyProcess, 'no_var', False))
self.assertFalse(getattr(MyProcess, 'var', False))
self.assertEqual(set(['var', 'var_list', 'var_group', 'diag']),
set(MyProcess._variables.keys()))

# test Meta attributes
self.assertDictEqual(MyProcess._meta, {'time_dependent': False})


class TestProcess(unittest.TestCase):

def setUp(self):
self.my_process = MyProcess()

def test_constructor(self):
# test dict-like vs. attribute access
self.assertIs(self.my_process['var'], self.my_process._variables['var'])
self.assertIs(self.my_process.var, self.my_process._variables['var'])

# test deep copy variable objects
MyProcess._variables['var'].state = 2
self.assertNotEqual(self.my_process._variables['var'].state,
MyProcess._variables['var'].state)

# test assign process to diagnostics
self.assertIs(self.my_process['diag']._process_obj, self.my_process)

def test_clone(self):
cloned_process = self.my_process.clone()
self.assertIsNot(self.my_process['var'], cloned_process['var'])

def test_variables(self):
self.assertEqual(set(['var', 'var_list', 'var_group', 'diag']),
set(self.my_process.variables.keys()))

def test_meta(self):
self.assertDictEqual(self.my_process.meta, {'time_dependent': False})

def name(self):
self.assertEqual(self.my_process.name, "MyProcess")

self.my_process._name = "my_process"
self.assertEqual(self.my_process.name, "my_process")

def run_step(self):
with self.assertRaisesRegex(NotImplementedError, "no method"):
self.my_process.run_step(1)
7 changes: 6 additions & 1 deletion xsimlab/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def test_repr(self):
class TestVariableList(unittest.TestCase):

def test_constructor(self):
var_list = VariableList([Variable(()), Variable(('x'))])
self.assertIsInstance(var_list, tuple)

with self.assertRaisesRegex(ValueError, "found variables mixed"):
_ = VariableList([2, Variable(())])

Expand All @@ -179,7 +182,9 @@ def test_iter(self):
with self.assertRaisesRegex(ValueError, "cannot retrieve variables"):
_ = list(myprocess3.var)

processes_dict = OrderedDict((('p1', myprocess), ('p2', myprocess2)))
processes_dict = OrderedDict([('p1', myprocess),
('p2', myprocess2),
('p3', myprocess3)])
myprocess3.var._set_variables(processes_dict)

expected = [myprocess.var, myprocess2.var]
Expand Down
2 changes: 1 addition & 1 deletion xsimlab/variable/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def run_validators(self, xr_variable):
vfunc(xr_variable)

def validate(self, xr_variable):
pass
pass # pragma: no cover

def validate_dimensions(self, dims, ignore_dims=None):
"""Validate given dimensions for this variable.
Expand Down
2 changes: 1 addition & 1 deletion xsimlab/variable/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _check_bounds(self, xr_variable):

def validate(self, xr_variable):
self._check_bounds(xr_variable)
super(NumberVariable, self).validate(xr_variable)
super(NumberVariable, self).validate(xr_variable) # pragma: no cover


class FloatVariable(NumberVariable):
Expand Down

0 comments on commit a713ba8

Please sign in to comment.