Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_parameterizable_essential_auxiliary.py: 100%

1 from typing import Any

2

3 from mixinforge.parameterizable_mixin import ParameterizableMixin

4 from mixinforge.json_processor import loadjs

5

6

7 class BasicParam(ParameterizableMixin):

8 def __init__(self, a: int, b: int = 2, c: str = "x") -> None:

9 self.a = a

10 self.b = b

11 self.c = c

12

13 def get_params(self) -> dict[str, Any]:

14 return {"a": self.a, "b": self.b, "c": self.c}

15

16

17 def test_default_essential_is_all_and_auxiliary_is_empty():

18 obj = BasicParam(a=1, b=20, c="ok")

19

20 # By default, everything is essential

21 assert obj.essential_param_names == {"a", "b", "c"}

22 assert obj.get_essential_params() == obj.get_params()

23

24 # Auxiliary is empty by default

25 assert obj.auxiliary_param_names == set()

26 assert obj.get_auxiliary_params() == {}

27

28 # JSON accessors mirror the dict ones

29 assert loadjs(obj.get_essential_jsparams()) == obj.get_params()

30 assert loadjs(obj.get_auxiliary_jsparams()) == {}

31

32

33 class SplitParam(BasicParam):

34 # Make only a subset essential

35 @property

36 def essential_param_names(self) -> set[str]:

37 return {"a", "b"}

38

39

40 def test_overridden_essential_and_auxiliary_behave_consistently():

41 obj = SplitParam(a=10, b=30, c="rest")

42 expected_params = {"a": 10, "b": 30, "c": "rest"}

43

44 # Sanity: get_params unchanged from base

45 assert obj.get_params() == expected_params

46

47 # Now only a and b are essential

48 assert obj.essential_param_names == {"a", "b"}

49 assert obj.get_essential_params() == {"a": 10, "b": 30}

50 assert loadjs(obj.get_essential_jsparams()) == {"a": 10, "b": 30}

51

52 # Auxiliary contains the rest

53 assert obj.auxiliary_param_names == {"c"}

54 assert obj.get_auxiliary_params() == {"c": "rest"}

55 assert loadjs(obj.get_auxiliary_jsparams()) == {"c": "rest"}