Skip to main content

Local Rule Development (ewx-public)

The ewx-public package lets you write and unit-test custom rules locally with full type hints and IDE autocompletion, before uploading them to the platform.

Installation

For prerequisites and step-by-step installation instructions (pip and Poetry, macOS / Linux and Windows), see the ewx-public package page.

Writing Rules

With ewx-public installed you can write rules in your IDE with autocompletion. For details on the available base classes and imports, see the ewx-public Package reference.

For details on rule structure, available methods, and design patterns, see:

Unit Testing Rules

Availability

The ewx_public.testing module is available from platform release 26.05 onwards (package version ewx-public==26.5.0). Pin to that version or later to use the testing utilities described below.

The ewx_public.testing module provides everything you need to test rules locally — no mocks required. Install ewx-public and you're ready to go.

Example: Testing a Flow Rule

Use make_testable() to wrap your production rule for testing:

from ewx_public.testing import (
assert_timeseries_stored,
make_datasource,
make_testable,
make_timeseries_df,
)
from rules.my_rule import MyRule


def test_stores_consumption():
rule = make_testable(
MyRule,
datasource=make_datasource(channels=["E_CONS"]),
dataframe=make_timeseries_df(columns=["E_CONS"], fill_value=100.0),
flow_properties={"threshold": 50},
)

rule.run() # runs prepare_context() then apply()

assert_timeseries_stored(rule, channel_id="E_CONS", count=1)

Key features:

  • make_testable() wraps any FlowRule subclass with an in-memory backend
  • rule.run() executes the full lifecycle (prepare_contextapply)
  • Assertion helpers check side effects: assert_timeseries_stored, assert_tags_added, assert_logged, etc.
  • Factory functions create test data with sensible defaults: make_datasource, make_timeseries_df, etc.

Example: Testing a Transform Rule

Transform rules are simpler — instantiate them directly:

import pandas as pd
from rules.my_transform import MyTransform


def test_rounds_values():
index = pd.date_range("2024-01-01", periods=3, freq="h", tz="UTC")
df = pd.DataFrame({"E_CONS": [1.23456, 2.34567, 3.45678]}, index=index)

rule = MyTransform(dataframe=df, context={"decimals": 2})
result = rule.apply()

assert result.result["E_CONS"].tolist() == [1.23, 2.35, 3.46]

Example project

For a complete working project with multiple rule types and tests, see the examples/my-ewx-rules/ directory in the ewx-public package.

Running Tests

pytest tests/ -v

For a detailed guide on testing patterns, seeding data, and assertion helpers, see Unit Testing Rules: A Practical Guide.

Uploading Rules to the Platform

Once your rule is tested locally, upload the code to the platform via the UI (rule configuration code blob) or via ewx-cli.