From 61002dd04f0a834d85611c67bee6e46cb361a230 Mon Sep 17 00:00:00 2001 From: kwypchlo Date: Thu, 10 May 2018 18:52:38 +0200 Subject: [PATCH] fix ordering of stacked insertions --- src/cr/cube/dimension.py | 24 ++++++++++++++---------- tests/unit/test_dimension.py | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/cr/cube/dimension.py b/src/cr/cube/dimension.py index d49eaf88a..3a57e9466 100644 --- a/src/cr/cube/dimension.py +++ b/src/cr/cube/dimension.py @@ -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 diff --git a/tests/unit/test_dimension.py b/tests/unit/test_dimension.py index eef01bce5..086d51cec 100644 --- a/tests/unit/test_dimension.py +++ b/tests/unit/test_dimension.py @@ -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': { @@ -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': [], @@ -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},