Skip to content

Commit

Permalink
Merge branch 'empty-subtotal-fix-156884530' into rel-5.1.111
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed May 3, 2018
2 parents 9e0e54a + 57a069c commit f7d93bd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,13 @@ def _insertions(self, result, dimension, dimension_index):
[ind_subtotal_elements]
)
axis = dimension_index
value = np.sum(result[ind], axis=axis)
insertions.append((ind_insertion, value))

# no indices are provided (should never get here)
if len(indices['inds']) == 0:
value = 0
else:
value = np.sum(result[ind], axis=axis)
insertions.append((ind_insertion, value))

return insertions

Expand Down
3 changes: 3 additions & 0 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def hs_indices(self):
if el['id'] in subtotal.args],
} for subtotal in self.subtotals]

# filter where indices aren't available to sum
indices = [ind for ind in indices if len(ind['inds']) > 0]

return indices

@property
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,19 @@ def test_margin_pruned_indices_with_insertions_and_nans(self):

actual = CrunchCube._margin_pruned_indices(table, insertions)
np.testing.assert_array_equal(actual, expected)

def test_insertions_with_empty_indices(self):
cc = CrunchCube({})
class DummyDimension:

@property
def hs_indices(self):
return [{'anchor_ind': 0, 'inds': []}]

result = Mock()
dimension_index = 0
dimension = DummyDimension()

insertions = cc._insertions(result, dimension, dimension_index)
assert insertions == [], insertions

30 changes: 30 additions & 0 deletions tests/unit/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,36 @@ def test_hs_indices_with_bad_data(self, mock_type):
actual = dim.hs_indices
self.assertEqual(actual, expected)

@patch('cr.cube.dimension.Dimension._elements', [
{'id': 1}, {'id': 2}, {'id': 5}, {'id': 4}
])
@patch('cr.cube.dimension.Dimension._get_type')
def test_hs_indices_with_empty_indices(self, mock_type):

mock_type.return_value = None
dim_data = {
'references': {
'view': {
'transform': {'insertions': [{
"function": "subtotal",
"args": [
7,
8,
9,
10,
11
],
"anchor": "bottom",
"name": "test subtotal"
}]}
}
}
}
dim = Dimension(dim_data)
expected = []
actual = dim.hs_indices
self.assertEqual(actual, expected)

@patch('cr.cube.dimension.Dimension._get_type')
def test_subtotals(self, mock_type):
mock_type.return_value = None
Expand Down

0 comments on commit f7d93bd

Please sign in to comment.