Skip to content

Commit

Permalink
Merge branch 'fix-formatting-hs-in-3d-cubes-157293490' into rel-5.1.117
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed May 7, 2018
2 parents e8ebca9 + 2f9fc95 commit b36ea29
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,24 @@ def _elements(self):
@property
def inserted_hs_indices(self):
if self.type == 'categorical_array':
# For CA subvariables, we don't do H&S insertions.
return []
return [] # For CA subvariables, we don't do H&S insertions

element_ids = [element['id'] for element in self.elements()]

tops = [st for st in self.subtotals if st.anchor == 'top']
bottoms = [st for st in self.subtotals if st.anchor == 'bottom']
middles = [st for st in self.subtotals if st.anchor not in ['top', 'bottom']]

top_inds = [i for i, _ in enumerate(tops)]
middle_inds = [i + m.anchor + len(tops) for i, m in enumerate(middles)]
bottom_inds = [i + len(tops) + len(middles) + len(self.elements())
for i, _ in enumerate(bottoms)]
return top_inds + middle_inds + bottom_inds
top_indexes = list(range(len(tops)))
middle_indexes = [
index + element_ids.index(insertion.anchor) + len(tops) + 1
for index, insertion in enumerate(middles)
]
bottom_indexes = [
index + len(tops) + len(middles) + len(self.elements())
for index, insertion in enumerate(bottoms)
]
return top_indexes + middle_indexes + bottom_indexes

def _transform_anchor(self, subtotal):

Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,61 @@ def test_subtotals(self, mock_type):
self.assertEqual(type(actual[1]), Subtotal)
self.assertEqual(actual[1]._data, self.insertions_with_bad_data[2])

@patch('cr.cube.dimension.Dimension._elements', [
{'id': 111}, {'id': 222}, {'id': 333}, {'id': 444}, {'id': 555}
])
@patch('cr.cube.dimension.Dimension._get_type')
def test_inserted_hs_indices(self, mock_type):
mock_type.return_value = None
dim_data = {
'references': {
'view': {
'transform': {
'insertions': [
{
u'anchor': u'bottom',
u'args': [],
u'function': u'subtotal',
u'name': u'bottoms up one',
},
{
u'anchor': u'bottom',
u'args': [],
u'function': u'subtotal',
u'name': u'bottoms up two',
},
{
u'anchor': u'top',
u'args': [],
u'function': u'subtotal',
u'name': u'on top one',
},
{
u'anchor': u'top',
u'args': [],
u'function': u'subtotal',
u'name': u'on top two',
},
{
u'anchor': 333,
u'args': [],
u'function': u'subtotal',
u'name': u'in the middle one',
},
{
u'anchor': 333,
u'args': [],
u'function': u'subtotal',
u'name': u'in the middle two',
}
]
}
}
}
}
dim = Dimension(dim_data)
self.assertEqual(dim.inserted_hs_indices, [0, 1, 5, 6, 9, 10])

@patch('cr.cube.dimension.Dimension._elements', [
{'numeric_value': 1},
{'numeric_value': 2, 'missing': False},
Expand Down

0 comments on commit b36ea29

Please sign in to comment.