Skip to content
Snippets Groups Projects
Commit c9895c20 authored by Martin Lange's avatar Martin Lange
Browse files

add tests for chapter on adapters

parent 07806c47
No related branches found
No related tags found
No related merge requests found
Pipeline #123348 passed with stages
in 4 minutes and 6 seconds
...@@ -48,8 +48,34 @@ File ``src/scale.py``: ...@@ -48,8 +48,34 @@ File ``src/scale.py``:
.. testcode:: scale-adapter .. testcode:: scale-adapter
:hide: :hide:
from datetime import datetime, timedelta
generator = fm.modules.CallbackGenerator(
{"Value": (lambda _t: 1.0, fm.Info(time=None, grid=fm.NoGrid()))},
start=datetime(2000, 1, 1),
step=timedelta(days=1),
)
consumer = fm.modules.DebugConsumer(
{"Input": fm.Info(None, grid=fm.NoGrid())},
start=datetime(2000, 1, 1),
step=timedelta(days=1),
)
adapter = Scale(0.5) adapter = Scale(0.5)
comp = fm.Composition([generator, consumer])
comp.initialize()
generator.outputs["Value"] >> adapter >> consumer.inputs["Input"]
comp.run(datetime(2000, 1, 2))
print(fm.data.strip_data(consumer.data["Input"]))
.. testoutput:: scale-adapter
:hide:
0.5 dimensionless
In :meth:`.Adapter._get_data`, we: In :meth:`.Adapter._get_data`, we:
1. Pull the input for the requested ``time`` 1. Pull the input for the requested ``time``
...@@ -171,7 +197,10 @@ In :meth:`.Adapter._get_data`, we can now do the interpolation whenever data is ...@@ -171,7 +197,10 @@ In :meth:`.Adapter._get_data`, we can now do the interpolation whenever data is
def _get_data(self, time): def _get_data(self, time):
if self.old_data is None: if self.old_data is None:
return self.new_data[1] if self.new_data is None:
return None
else:
return self.new_data[1]
dt = (time - self.old_data[0]) / (self.new_data[0] - self.old_data[0]) dt = (time - self.old_data[0]) / (self.new_data[0] - self.old_data[0])
...@@ -183,8 +212,34 @@ In :meth:`.Adapter._get_data`, we can now do the interpolation whenever data is ...@@ -183,8 +212,34 @@ In :meth:`.Adapter._get_data`, we can now do the interpolation whenever data is
.. testcode:: time-adapter .. testcode:: time-adapter
:hide: :hide:
from datetime import datetime, timedelta
generator = fm.modules.CallbackGenerator(
{"Value": (lambda t: t.day, fm.Info(time=None, grid=fm.NoGrid()))},
start=datetime(2000, 1, 1),
step=timedelta(days=30),
)
consumer = fm.modules.DebugConsumer(
{"Input": fm.Info(None, grid=fm.NoGrid())},
start=datetime(2000, 1, 1),
step=timedelta(days=1),
)
adapter = TimeInterpolation() adapter = TimeInterpolation()
comp = fm.Composition([generator, consumer])
comp.initialize()
generator.outputs["Value"] >> adapter >> consumer.inputs["Input"]
comp.run(datetime(2000, 1, 15))
print(fm.data.strip_data(consumer.data["Input"]))
.. testoutput:: time-adapter
:hide:
14.0 dimensionless
In :meth:`.Adapter._get_data`, the following happens: In :meth:`.Adapter._get_data`, the following happens:
1. If only one data entry was received so far, we can't interpolate and simply return the available data. Otherwise... 1. If only one data entry was received so far, we can't interpolate and simply return the available data. Otherwise...
......
...@@ -416,5 +416,6 @@ Here is the final code of the completed component. ...@@ -416,5 +416,6 @@ Here is the final code of the completed component.
TestDummy().test_dummy_model() #doctest: +ELLIPSIS TestDummy().test_dummy_model() #doctest: +ELLIPSIS
.. testoutput:: .. testoutput::
:hide:
... ...
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment