Skip to content

Commit

Permalink
Merge pull request #407 from Crunch-io/cat-date-diff-bug-fix-188552693
Browse files Browse the repository at this point in the history
bug fix len vs any
  • Loading branch information
ernestoarbitrio authored Nov 27, 2024
2 parents 3c3b551 + c652488 commit 6935f6b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/cr/cube/matrix/subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,9 @@ def _subtotal_columns(self):

def _subtotal_column(self, subtotal, default):
"""Return (n_rows,) ndarray of values for `subtotal` column."""
if self._dimensions[1].dimension_type == DT.CAT_DATE and any(
subtotal.subtrahend_idxs
if (
self._dimensions[1].dimension_type == DT.CAT_DATE
and len(subtotal.subtrahend_idxs) > 0
):
if self._multiple_subtrahends_or_addends(subtotal):
return self._nan_subtotals(axis=0)
Expand All @@ -480,9 +481,9 @@ def _subtotal_column(self, subtotal, default):

def _subtotal_row(self, subtotal, default):
"""Return (n_cols,) ndarray of values for `subtotal` row."""

if self._dimensions[0].dimension_type == DT.CAT_DATE and any(
subtotal.subtrahend_idxs
if (
self._dimensions[0].dimension_type == DT.CAT_DATE
and len(subtotal.subtrahend_idxs) > 0
):
if self._multiple_subtrahends_or_addends(subtotal):
return self._nan_subtotals(axis=1)
Expand Down
98 changes: 98 additions & 0 deletions tests/unit/matrix/test_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import pytest

from cr.cube.dimension import Dimension, _Subtotal
from cr.cube.enums import DIMENSION_TYPE as DT
from cr.cube.matrix.subtotals import (
_BaseSubtotals,
NanSubtotals,
NegativeTermSubtotals,
PositiveTermSubtotals,
SumSubtotals,
WaveDiffSubtotal,
)

from ...unitutil import ANY, initializer_mock, instance_mock, method_mock, property_mock
Expand Down Expand Up @@ -355,6 +357,102 @@ def subtotal_(self, request):
return instance_mock(request, _Subtotal)


class TestWaveDiffSubtotals:
"""Unit test suite for `cr.cube.matrix.WaveDiffSubtotal` object."""

def test_it_provides_a_subtotal_columns_interface_method(
self, request, dimensions_, _init_
):
base_values = [[0, 4], [7, 9]]
counts = [[10, 20], [30, 40]]
default_insertions = [[0.1, 0.2], [0.3, 0.4]]
property_mock(
request,
WaveDiffSubtotal,
"_subtotal_columns",
return_value=np.array([[1, 2], [3, 4]]),
)

subtotal_columns = WaveDiffSubtotal.subtotal_columns(
base_values, counts, default_insertions, dimensions_
)

_init_.assert_called_once_with(
ANY, base_values, counts, default_insertions, dimensions_
)
assert subtotal_columns.tolist() == [[1, 2], [3, 4]]

def test_it_provides_a_subtotal_rows_interface_method(
self, request, dimensions_, _init_
):
base_values = [[4, 1], [3, 5]]
counts = [[10, 20], [30, 40]]
default_insertions = [[0.1, 0.2], [0.3, 0.4]]
property_mock(
request,
WaveDiffSubtotal,
"_subtotal_rows",
return_value=np.array([[4, 3], [2, 1]]),
)

subtotal_rows = WaveDiffSubtotal.subtotal_rows(
base_values, counts, default_insertions, dimensions_
)

_init_.assert_called_once_with(
ANY, base_values, counts, default_insertions, dimensions_
)
assert subtotal_rows.tolist() == [[4, 3], [2, 1]]

def test_renders_row_wave_diff_under_specific_conditions(
self, dimensions_, subtotal_
):
dimensions_[1].dimension_type = DT.CAT
dimensions_[0].dimension_type = DT.CAT_DATE
subtotal_.subtrahend_idxs = [0]
subtotal_.addend_idxs = [1]
base_values = np.array([[4, 1], [3, 5]])
counts = np.array([[10, 20], [30, 40]])
default_insertion = [0.1, 0.2]

subtotal = WaveDiffSubtotal(base_values, counts, default_insertion, dimensions_)

assert subtotal._subtotal_row(subtotal_, default_insertion) == pytest.approx(
np.array([7.5, -12.0])
)

def test_renders_col_wave_diff_under_specific_conditions(
self, dimensions_, subtotal_
):
dimensions_[1].dimension_type = DT.CAT_DATE
dimensions_[0].dimension_type = DT.CAT
subtotal_.subtrahend_idxs = [0]
subtotal_.addend_idxs = [1]
base_values = np.array([[4, 1], [3, 5]])
counts = np.array([[10, 20], [30, 40]])
default_insertion = [0.1, 0.2]

subtotal = WaveDiffSubtotal(base_values, counts, default_insertion, dimensions_)

assert subtotal._subtotal_column(subtotal_, default_insertion) == pytest.approx(
np.array([17.5, -2.0])
)

# --- fixture components -----------------------------------------

@pytest.fixture
def dimensions_(self, request):
return (instance_mock(request, Dimension), instance_mock(request, Dimension))

@pytest.fixture
def _init_(self, request):
return initializer_mock(request, WaveDiffSubtotal)

@pytest.fixture
def subtotal_(self, request):
return instance_mock(request, _Subtotal)


class TestSumSubtotals:
"""Unit test suite for `cr.cube.matrix.SumSubtotals` object."""

Expand Down

0 comments on commit 6935f6b

Please sign in to comment.