Skip to content

Commit

Permalink
allow cube to understand positive subtotal kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
gergness committed Mar 22, 2021
1 parent a651c03 commit 425641c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 53 deletions.
22 changes: 15 additions & 7 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,16 @@ def _iter_valid_subtotal_dicts(self):
if not {"anchor", "name"}.issubset(insertion_dict.keys()):
continue

# ---must have positive or negative elements---
positive = insertion_dict.get("args", [])
# ---use "new style" kwargs defining positive terms if available---
positive = insertion_dict.get("kwargs", {}).get("positive", [])

# ---but if missing, check for "old style" args defining positive terms---
if not positive:
positive = insertion_dict.get("args", [])

negative = insertion_dict.get("kwargs", {}).get("negative", [])

# ---must have positive or negative elements---
if not (positive or negative):
continue

Expand Down Expand Up @@ -955,11 +962,12 @@ def addend_ids(self):
Any element id not present in the dimension or present but
representing missing data is excluded.
"""
return tuple(
arg
for arg in self._subtotal_dict.get("args", [])
if arg in self._valid_elements.element_ids
)
# ---Prefer positive "kwargs" over "args" so we can migrate---
positive = self._subtotal_dict.get("kwargs", {}).get("positive", [])
if not positive:
positive = self._subtotal_dict.get("args", [])

return tuple(arg for arg in positive if arg in self._valid_elements.element_ids)

@lazyproperty
def addend_idxs(self):
Expand Down
90 changes: 44 additions & 46 deletions tests/unit/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,11 +1080,6 @@ def it_provides_the_element_ids_as_a_set_to_help(self, request, valid_elements_)
{3, 4},
(),
),
(
[{"function": "subtotal", "anchor": 9, "args": [1, 2], "name": "B"}],
{1, 2, 3, 4, 5, 8, -1},
({"function": "subtotal", "anchor": 9, "args": [1, 2], "name": "B"},),
),
(
[
{"function": "subtotal", "anchor": 9, "args": [1, 2], "name": "C"},
Expand All @@ -1097,49 +1092,9 @@ def it_provides_the_element_ids_as_a_set_to_help(self, request, valid_elements_)
{"function": "subtotal", "anchor": 9, "args": [5, 6], "name": "D"},
),
),
(
[
{
"function": "subtotal",
"anchor": 9,
"args": [1],
"kwargs": {"negative": [2]},
"name": "B",
}
],
{1, 2, 3, 4, 5, 8, -1},
(
{
"function": "subtotal",
"anchor": 9,
"args": [1],
"kwargs": {"negative": [2]},
"name": "B",
},
),
),
(
[
{
"function": "subtotal",
"anchor": 9,
"kwargs": {"negative": [1, 2]},
"name": "B",
}
],
{1, 2, 3, 4, 5, 8, -1},
(
{
"function": "subtotal",
"anchor": 9,
"kwargs": {"negative": [1, 2]},
"name": "B",
},
),
),
),
)
def it_iterates_the_valid_subtotal_insertion_dicts_to_help(
def it_removes_invalid_when_iterating_the_valid_subtotal_insertion_dicts_to_help(
self, insertion_dicts, element_ids, expected_value, _element_ids_prop_
):
_element_ids_prop_.return_value = element_ids
Expand All @@ -1149,6 +1104,47 @@ def it_iterates_the_valid_subtotal_insertion_dicts_to_help(

assert subtotal_dicts == expected_value

def but_it_accepts_valid_insertion_dicts(self, _element_ids_prop_):
_element_ids_prop_.return_value = {1, 2, 5, 8, -1}
insertion_dicts = [
{
"function": "subtotal",
"anchor": 9,
"args": [1],
"name": "old style args",
},
{
"function": "subtotal",
"anchor": 9,
"args": [1],
"kwargs": {"negative": [2]},
"name": "old style args with negative",
},
{
"function": "subtotal",
"anchor": 9,
"kwargs": {"negative": [2]},
"name": "negative only",
},
{
"function": "subtotal",
"anchor": 9,
"kwargs": {"positive": [1]},
"name": "new style kwargs",
},
{
"function": "subtotal",
"anchor": 9,
"kwargs": {"positive": [1], "negative": [2]},
"name": "new style kwargs with negative",
},
]
subtotals = _Subtotals(insertion_dicts, None)

subtotal_dicts = tuple(subtotals._iter_valid_subtotal_dicts())

assert subtotal_dicts == tuple(insertion_dicts)

def it_constructs_its_subtotal_objects_to_help(
self, request, _iter_valid_subtotal_dicts_, valid_elements_, _Subtotal_
):
Expand Down Expand Up @@ -1259,6 +1255,8 @@ def it_knows_the_insertion_anchor(
({"args": [3, 2]}, {1, 2, 3}, (3, 2)),
({"args": []}, {1, 2, 3}, ()),
({"args": [1, 2, 3]}, {}, ()),
({"kwargs": {"positive": [1, 2, 3]}}, {1, 2, 3}, (1, 2, 3)),
({"args": [1], "kwargs": {"positive": [2]}}, {1, 2, 3}, (2,)),
),
)
def it_provides_access_to_the_addend_element_ids(
Expand Down

0 comments on commit 425641c

Please sign in to comment.