Skip to content

Commit

Permalink
fix ordering of stacked insertions
Browse files Browse the repository at this point in the history
  • Loading branch information
kwypchlo committed May 10, 2018
1 parent 88ac037 commit 61002dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,23 @@ def labels(self, include_missing=False, include_transforms=False,

def _update_with_subtotals(self, labels_with_cat_ids):
for subtotal in self.subtotals:
# If anchor is 0, the default value of 'next' (which is -1) will
# add with 1, and amount to the total of 0, which will be the
# correct index value for the new subtotal.
ind_insert = next((
i for (i, x) in enumerate(labels_with_cat_ids)
if x.get('id') == subtotal.anchor
), subtotal.anchor)
if ind_insert == 'top':
already_inserted_with_the_same_anchor = [
index for (index, item) in enumerate(labels_with_cat_ids)
if 'anchor' in item and item['anchor'] == subtotal.anchor
]

if len(already_inserted_with_the_same_anchor):
ind_insert = already_inserted_with_the_same_anchor[-1] + 1
elif subtotal.anchor == 'top':
ind_insert = 0
elif ind_insert == 'bottom':
elif subtotal.anchor == 'bottom':
ind_insert = len(labels_with_cat_ids)
else:
ind_insert += 1
ind_insert = next(
index for (index, item) in enumerate(labels_with_cat_ids)
if item.get('id') == subtotal.anchor
) + 1

labels_with_cat_ids.insert(ind_insert, subtotal.data)

return labels_with_cat_ids
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_subtotals(self, mock_type):
{'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):
def test_inserted_hs_indices_and_order(self, mock_type):
mock_type.return_value = None
dim_data = {
'references': {
Expand All @@ -318,6 +318,12 @@ def test_inserted_hs_indices(self, mock_type):
u'function': u'subtotal',
u'name': u'bottoms up two',
},
{
u'anchor': u'bottom',
u'args': [],
u'function': u'subtotal',
u'name': u'bottoms up three',
},
{
u'anchor': u'top',
u'args': [],
Expand Down Expand Up @@ -348,7 +354,13 @@ def test_inserted_hs_indices(self, mock_type):
}
}
dim = Dimension(dim_data)
self.assertEqual(dim.inserted_hs_indices, [0, 1, 5, 6, 9, 10])
self.assertEqual(dim.inserted_hs_indices, [0, 1, 5, 6, 9, 10, 11])
labels = [
u'on top one', u'on top two', None, None, None,
u'in the middle one', u'in the middle two', None, None,
u'bottoms up one', u'bottoms up two', u'bottoms up three'
]
self.assertEqual(labels, dim.labels(include_transforms=True))

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

0 comments on commit 61002dd

Please sign in to comment.