Testing helpers

Helpers for testing studies, hooks and plugins without a database or solver.

class flexorchestrator.testing.FakeModel(spec, overrides=<factory>, solved=False)[source]

Bases: object

What fake_build() returns: the spec and overrides it was given.

Parameters:
spec

A deep copy of the spec.

Type:

dict

overrides

The overrides.

Type:

dict[str, Any]

solved

Set by fake_solve().

Type:

bool

flexorchestrator.testing.fake_build(spec, **overrides)[source]

Build a FakeModel.

Parameters:
  • spec (dict) – Any JSON spec.

  • **overrides (Any) – Recorded on the model.

Returns:

The fake model.

Return type:

FakeModel

Example

>>> fake_build({"a": 1}, x=2).overrides
{'x': 2}
flexorchestrator.testing.fake_solve(model, *, status='ok', **options)[source]

Pretend to solve, ending however status says.

Parameters:
  • model (Any) – Usually a FakeModel; marked solved when it is one.

  • status (Literal['ok', 'failed', 'error']) – "ok" (optimal), "failed" (infeasible), or "error", which raises the way a crashing solver would.

  • **options (Any) – Ignored.

Returns:

The outcome.

Raises:

RuntimeError – If status is "error".

Return type:

SolveOutcome

Example

>>> fake_solve(None, status="failed").termination
'infeasible'
flexorchestrator.testing.memory_orchestrator(*, study='test', plugins=None, hooks=None, **kwargs)[source]

Return an orchestrator on a fresh in-memory backend.

Parameters:
  • study (str | None) – A study to create and select; None for none.

  • plugins (Any) – Plugins to load; their orchestrator subclass is built. FLEXORCH_PLUGINS is ignored, so tests don’t depend on it.

  • hooks (HookSet | None) – The hooks; FAKE_HOOKS by default.

  • **kwargs (Any) – Passed to the orchestrator.

Return type:

Any

Example

>>> orch = memory_orchestrator()
>>> orch.study
'test'
flexorchestrator.testing.run_command_in_test(cmd, params=None, *, orch=None, progress=None, **fields)[source]

Run a command on a memory orchestrator and return what it returned.

Parameters:
  • cmd (Any) – A Command (registered first if the orchestrator doesn’t have it) or a command name.

  • params (Any) – Its parameters.

  • orch (Any) – The orchestrator; a fresh memory_orchestrator() if omitted.

  • progress (Any) – A Progress to report to.

  • **fields (Any) – More parameters.

Return type:

Any

Example

>>> from flexorchestrator.ext import command
>>> @command()
... def ping(orch) -> str:
...     return "pong"
>>> run_command_in_test(ping)
'pong'
flexorchestrator.testing.orchestrators.FAKE_HOOKS

Build and solve without a model library or a solver.

Type:

HookSet

Specs

flex-pse specs as plain data, for tests that build a model.

flexorchestrator.testing.specs.DEMO_SPEC: dict

A six-hour version of flex-pse’s plant_config_demo.json (schema 0.1.1) with a flat energy price instead of a tariff file, so it builds from any working directory.

Type:

dict

flexorchestrator.testing.specs.LINEAR_POWER: dict

A multilinear surrogate for demo.pump’s power relation: 0.6 kWh/m^3, no offset.

Type:

dict

flexorchestrator.testing.specs.LOGIC_SPEC: dict

Three RO trains as an indexed unit (p.ro[0..2], over the set element p.trains = [0, 1, 2]) with status and startup/shutdown logic; 6 points.

Type:

dict

flexorchestrator.testing.specs.NETWORK_SPEC: dict

A network of two plants (net.a, net.b); 24 points.

Type:

dict

flexorchestrator.testing.specs.SURROGATE_DISPATCH_SPEC: dict

A pump with a multilinear surrogate (coefficient_vars indexed by coefficient name) and a battery with a dispatched power_charge; 6 points.

Type:

dict