Skip to content

Commit

Permalink
wip tests (model)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy committed Jun 30, 2017
1 parent bb28909 commit 46ed13a
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions xsimlab/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import unittest

import numpy as np
from numpy.testing import assert_array_equal

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

Expand Down Expand Up @@ -41,7 +42,7 @@ class SomeProcess(Process):
some_effect = Variable('x', group='effect', provided=True)

def run_step(self, dt):
self.some_effect.value = self.x * self.some_param.value + dt
self.some_effect.value = self.x.value * self.some_param.value + dt


class OtherProcess(Process):
Expand All @@ -51,15 +52,15 @@ class OtherProcess(Process):
other_effect = Variable('x', group='effect', provided=True)

def run_step(self, dt):
self.other_effect.value = self.x * self.other_param.value - dt
self.other_effect.value = self.x.value * self.other_param.value - dt

@diagnostic
def x2(self):
return self.x * 2


class MetaProcess(Process):
param = Variable(())
meta_param = Variable(())
some_param = ForeignVariable(SomeProcess, 'some_param')
other_param = ForeignVariable(OtherProcess, 'other_param')

Expand All @@ -70,6 +71,12 @@ def run_step(self, *args):

class TestModel(unittest.TestCase):

def setUp(self):
self.model = Model({'grid': Grid,
'some_process': SomeProcess,
'other_process': OtherProcess,
'quantity': Quantity})

def test_constructor(self):
# test invalid processes
with self.assertRaises(TypeError):
Expand All @@ -84,15 +91,44 @@ class OtherClass(object):
with self.assertRaisesRegex(TypeError, "is not a subclass"):
Model({'invalid_class': OtherClass})

model = Model({'grid': Grid, 'some_process': SomeProcess,
'other_process': OtherProcess, 'quantity': Quantity})

# test process ordering
sorted_process_names = list(model.keys())
sorted_process_names = list(self.model.keys())
self.assertEqual(sorted_process_names[0], 'grid')
self.assertEqual(sorted_process_names[-1], 'quantity')
self.assertIn('some_process', sorted_process_names[1:-1])
self.assertIn('other_process', sorted_process_names[1:-1])

# test dict-like vs. attribute access
self.assertIs(model['grid'], model.grid)
self.assertIs(self.model['grid'], self.model.grid)

def test_input_vars(self):
expected = {'grid': ['x_size'],
'some_process': ['some_param'],
'other_process': ['other_param'],
'quantity': ['quantity']}
actual = {k: list(v.keys()) for k, v in self.model.input_vars.items()}
self.assertDictEqual(expected, actual)

def test_is_input(self):
self.assertTrue(self.model.is_input(self.model.grid.x_size))
self.assertTrue(self.model.is_input(('grid', 'x_size')))
self.assertFalse(self.model.is_input(('quantity', 'all_effects')))

def test_initialize(self):
model = self.model.clone()
model.grid.x_size.value = 10
model.initialize()
expected = np.arange(10)
assert_array_equal(model.grid.x.value, expected)

def test_run_step(self):
model = self.model.clone()
model.grid.x_size.value = 10
model.some_process.some_param.value = 1
model.other_process.other_param.value = 1

model.initialize()
model.run_step(100)

expected = model.grid.x.value * 2
assert_array_equal(model.quantity.quantity.change, expected)

0 comments on commit 46ed13a

Please sign in to comment.