def test_param_factory_validate_spec(): with _temporary_spec( b""" id_name: mymodule name: name category: Clean parameters: - id_name: s name: s """ ) as path: with pytest.raises(ValueError, match="is not valid"): param_factory(path)
def test_param_factory_check_types(): with _temporary_spec( b""" id_name: mymodule name: name category: Clean parameters: - id_name: s name: s type: string - id_name: i name: i type: integer - id_name: f name: f type: float """ ) as path: P = param_factory(path) P(s="s", i=1, f=2.1) # ok with pytest.raises(ValueError, match="Value 1 is not a string"): P(s=1, i=1, f=2.1) with pytest.raises(ValueError, match="Value 2.1 is not an integer"): P(s="s", i=2.1, f=2.1) with pytest.raises(ValueError, match="Value 'f' is not a float"): P(s="s", i=1, f="f") P(s="s", i=1, f=2) # int is a valid float
def test_param_factory_check_unwanted_values(): with _temporary_spec( b""" id_name: mymodule name: name category: Clean parameters: - id_name: foo name: foo type: string default: foo """ ) as path: P = param_factory(path) with pytest.raises(ValueError, match="has wrong keys"): P(foo="bar", bar="baz")
def test_param_factory_defaults(): with _temporary_spec( b""" id_name: mymodule name: name category: Clean parameters: - id_name: foo name: foo type: string default: bar - id_name: moo type: integer default: 4 """ ) as path: P = param_factory(path) assert P() == {"foo": "bar", "moo": 4} assert P(foo="moo") == {"foo": "moo", "moo": 4}
def test_param_factory_defaults_do_not_nest(): with _temporary_spec( b""" id_name: mymodule name: name category: Clean parameters: - id_name: outer type: list child_parameters: - id_name: innerstring name: foo type: string default: avalue """ ) as path: P = param_factory(path) assert P() == {"outer": []} assert P(outer=[{"innerstring": "foo"}]) == {"outer": [{"innerstring": "foo"}]} with pytest.raises(ValueError, match="has wrong keys"): assert P(outer=[{}])
import datetime from pathlib import Path import pyarrow as pa from cjwmodule.arrow.testing import assert_result_equals, make_column, make_table from cjwmodule.arrow.types import ArrowRenderResult from cjwmodule.spec.testing import param_factory from converttotext import render_arrow_v1 as render P = param_factory(Path(__name__).parent.parent / "converttotext.yaml") def test_default_params_does_nothing(): assert_result_equals( render(make_table(make_column("A", [1, 2, 3])), P()), ArrowRenderResult(make_table(make_column("A", [1, 2, 3]))), ) def test_convert_str_does_nothing(): assert_result_equals( render(make_table(make_column("A", ["x"])), P(colnames=["A"])), ArrowRenderResult(make_table(make_column("A", ["x"]))), ) def test_convert_int(): assert_result_equals( render( make_table(
from pathlib import Path from typing import Any, Dict import numpy as np import pandas as pd from cjwmodule.spec.testing import param_factory from cjwmodule.testing.i18n import i18n_message from pandas.testing import assert_frame_equal import formula P = param_factory(Path(__name__).parent.parent / "formula.yaml") def _test( table: pd.DataFrame, params: Dict[str, Any] = {}, expected_table: pd.DataFrame = pd.DataFrame(), expected_error: str = "", ): result = formula.render(table, P(**params)) if expected_error: assert result == expected_error else: assert isinstance(result, pd.DataFrame) assert_frame_equal(result, expected_table) def test_python_formula_int_output(): _test( pd.DataFrame({"A": [10, 20]}),
from pathlib import Path from cjwmodule.spec.testing import param_factory from renamecolumns import migrate_params P = param_factory(Path(__file__).parent.parent / "renamecolumns.yaml") def test_v0_empty_rename_entries(): assert migrate_params({ "custom_list": False, "list_string": "A\nB\nC", "rename-entries": "" }) == P(custom_list=False, list_string="A\nB\nC", renames={}) def test_v0(): assert (migrate_params({ "custom_list": False, "list_string": "A\nB\nC", "rename-entries": '{"A":"B","B":"C"}', }) == P(custom_list=False, list_string="A\nB\nC", renames={ "A": "B", "B": "C" })) def test_v1():
from pathlib import Path from cjwmodule.spec.testing import param_factory from groupby import migrate_params P = param_factory(Path(__file__).parent.parent / "groupby.yaml") v1_defaults = { "groupby|groupby|0": "", "groupby|groupby|1": "", "active.addremove.last|groupby|1": False, "operation|operation|0": 0, "targetcolumn|operation|0": "", "outputname|operation|0": "", "operation.show-sibling|operation|1": 0, "targetcolumn.hide-with-sibling|operation|1": "", "outputname|operation|1": "", "cheat.cheat|operation|1": False, "active.addremove|operation|1": False, "operation.show-sibling|operation|2": 0, "targetcolumn.hide-with-sibling|operation|2": "", "outputname|operation|2": "", "cheat.cheat|operation|2": False, "active.addremove|operation|2": False, "operation.show-sibling|operation|3": 0, "targetcolumn.hide-with-sibling|operation|3": "", "outputname|operation|3": "", "cheat.cheat|operation|3": False, "active.addremove|operation|3": False, "operation.show-sibling|operation|4": 0,
from pathlib import Path from cjwmodule.spec.testing import param_factory from timestampmath import migrate_params P = param_factory(Path(__file__).parent.parent / "timestampmath.yaml") def test_v0_to_v1(): assert migrate_params( dict( operation="difference", colnames=["A", "B"], colname1="C", colname2="D", unit="minute", outcolname="froop", ) ) == P( operation="difference", colnames=["A", "B"], colname1="C", colname2="D", unit="minute", roundunit="hour", outcolname="froop", )
import datetime from pathlib import Path import pyarrow as pa from cjwmodule.arrow.testing import assert_result_equals, make_column, make_table from cjwmodule.arrow.types import ArrowRenderResult from cjwmodule.spec.testing import param_factory from splittimestamp import render_arrow_v1 as render P = param_factory(Path(__name__).parent.parent / "splittimestamp.yaml") def dt(year=2000, month=1, day=1, hour=0, minute=0, second=0): return datetime.datetime(year, month, day, hour, minute, second) def test_render_no_colname_is_no_op(): assert_result_equals( render( make_table(make_column("A", [dt()])), P(outputs=[dict(outcolname="B", part="date")]), ), ArrowRenderResult(make_table(make_column("A", [dt()]))), ) def test_render_no_outcolname_is_no_op(): assert_result_equals( render( make_table(make_column("A", [dt()])),
def test_param_factory_oserror(): with pytest.raises(FileNotFoundError): param_factory(Path(__file__).parent / "no-this-is-not-a-file.yaml")