From a3c047983d6577648937fa5f2197fd5295d5b53c Mon Sep 17 00:00:00 2001 From: Slobodan Ilic Date: Tue, 31 Jul 2018 14:53:31 +0200 Subject: [PATCH] Add tests with pruning and H&S on both dimensions --- tests/integration/fixtures/__init__.py | 3 +- .../fixtures/cubes/cat-x-cat-pruning-hs.json | 366 ++++++++++++++++++ tests/integration/test_cube_slice.py | 51 ++- 3 files changed, 405 insertions(+), 15 deletions(-) create mode 100644 tests/integration/fixtures/cubes/cat-x-cat-pruning-hs.json diff --git a/tests/integration/fixtures/__init__.py b/tests/integration/fixtures/__init__.py index 78cdc7dae..39ef11e01 100644 --- a/tests/integration/fixtures/__init__.py +++ b/tests/integration/fixtures/__init__.py @@ -138,4 +138,5 @@ def _load(cube_file): ) MR_X_CAT_X_MR_PRUNE = _load('mr-x-cat-x-mr-prune.json') HUFFPOST_ACTIONS_X_HOUSEHOLD = _load('huffpost-actions-x-household.json') -GENDER_X_WEIGHT = _load('gender-x-weight.json') \ No newline at end of file +GENDER_X_WEIGHT = _load('gender-x-weight.json') +CAT_X_CAT_PRUNING_HS = _load('cat-x-cat-pruning-hs.json') diff --git a/tests/integration/fixtures/cubes/cat-x-cat-pruning-hs.json b/tests/integration/fixtures/cubes/cat-x-cat-pruning-hs.json new file mode 100644 index 000000000..6e582beb2 --- /dev/null +++ b/tests/integration/fixtures/cubes/cat-x-cat-pruning-hs.json @@ -0,0 +1,366 @@ +{ + "query": { + "measures": { + "count": { + "function": "cube_count", + "args": [] + } + }, + "dimensions": [ + { + "variable": "000009" + }, + { + "variable": "000000" + } + ], + "weight": "http://127.0.0.1:8080/datasets/43f0d19eb1f241a39b7d5c547a0a6b6e/variables/aabdc9db95e540369e6fe9ed945af802/" + }, + "query_environment": { + "filter": [ + "http://127.0.0.1:8080/datasets/43f0d19eb1f241a39b7d5c547a0a6b6e/filters/38f5bc15591b45d885db9b41aa35cc15/", + "http://127.0.0.1:8080/datasets/43f0d19eb1f241a39b7d5c547a0a6b6e/filters/0477cb6195234daab49a26225c5b48fb/" + ] + }, + "result": { + "dimensions": [ + { + "derived": false, + "references": { + "alias": "MaritalStatus", + "view": { + "show_counts": false, + "show_numeric_values": false, + "transform": { + "insertions": [ + { + "function": "subtotal", + "args": [ + 2, + 3, + 4 + ], + "name": "left alone", + "anchor": 1 + } + ] + }, + "include_missing": false, + "column_width": null + }, + "description": "What is your marital status?", + "name": "MaritalStatus" + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "Married" + }, + { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "Separated" + }, + { + "numeric_value": 3, + "missing": false, + "id": 3, + "name": "Divorced" + }, + { + "numeric_value": 4, + "missing": false, + "id": 4, + "name": "Widowed" + }, + { + "numeric_value": 5, + "missing": false, + "id": 5, + "name": "Single" + }, + { + "numeric_value": 6, + "missing": false, + "id": 6, + "name": "Domestic partnership" + }, + { + "numeric_value": 8, + "missing": true, + "id": 8, + "name": "Skipped" + }, + { + "numeric_value": 9, + "missing": true, + "id": 9, + "name": "Not Asked" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + { + "derived": false, + "references": { + "alias": "ShutdownBlame", + "description": "If President Obama and the Republicans in Congress do not reach a budget agreement in time to avoid a shutdown of the federal government, who do you think will more to blame--President Obama or the Republican Congress?", + "name": "ShutdownBlame", + "view": { + "show_counts": false, + "column_width": null, + "transform": { + "insertions": [ + { + "function": "subtotal", + "args": [ + 1, + 2 + ], + "name": "Obama + Republicans", + "anchor": 1 + } + ] + }, + "include_missing": false, + "show_numeric_values": false + } + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "President Obama" + }, + { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "Republicans in Congress" + }, + { + "numeric_value": 3, + "missing": false, + "id": 3, + "name": "Both" + }, + { + "numeric_value": 4, + "missing": false, + "id": 4, + "name": "Neither" + }, + { + "numeric_value": 5, + "missing": false, + "id": 5, + "name": "Not sure" + }, + { + "numeric_value": 8, + "missing": true, + "id": 8, + "name": "Skipped" + }, + { + "numeric_value": 9, + "missing": true, + "id": 9, + "name": "Not Asked" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + } + ], + "missing": 0, + "measures": { + "count": { + "data": [ + 28.265438895, + 20.2582324092, + 10.0090081073, + 0.0, + 0.9708737864, + 0.0, + 0.0, + 0.0, + 1.0309278351, + 0.0, + 0.9708737864, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 3.0327294565, + 4.0036032429, + 2.0618556701, + 0.0, + 2.0018016215, + 0.0, + 0.0, + 0.0, + 3.0327294565, + 7.9471524372, + 4.9144229807, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.9708737864, + 0.9708737864, + 0.9708737864, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "n_missing": 0, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": false, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [ + 28, + 20, + 10, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 3, + 4, + 2, + 0, + 2, + 0, + 0, + 0, + 3, + 8, + 5, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "n": 91 + } +} \ No newline at end of file diff --git a/tests/integration/test_cube_slice.py b/tests/integration/test_cube_slice.py index 049ac796b..f7798a878 100644 --- a/tests/integration/test_cube_slice.py +++ b/tests/integration/test_cube_slice.py @@ -1,18 +1,41 @@ -# from unittest import TestCase -# import numpy as np +import numpy as np -# from cr.cube.cube_slice import CubeSlice +from cr.cube.crunch_cube import CrunchCube -# # from .fixtures import CAT_X_NUM_X_DATETIME -# from .fixtures import ECON_GENDER_X_IDEOLOGY_WEIGHTED -# # from .fixtures import UNIVARIATE_CATEGORICAL -# # from .fixtures import PETS_ARRAY_X_PETS -# # from .fixtures import PETS_X_PETS_ARRAY +from .fixtures import CAT_X_CAT_PRUNING_HS -# class TestCubeSlice(TestCase): -# def test_cube_slice_(self): -# cs = CubeSlice(ECON_GENDER_X_IDEOLOGY_WEIGHTED) -# expected = CAT_X_CAT['value'] -# actual = cube._table._cube -# self.assertEqual(actual, expected)gg +def test_labels_with_hs_and_pruning(): + cs = CrunchCube(CAT_X_CAT_PRUNING_HS ).slices[0] + + # Withouut pruning or H&S + expected = [ + [u'Married', u'Separated', u'Divorced', u'Widowed', u'Single', u'Domestic partnership'], + [u'President Obama', u'Republicans in Congress', u'Both', u'Neither', u'Not sure'], + ] + actual = cs.labels() + assert actual == expected + + # Apply pruning + expected = [ + [u'Married', u'Separated', u'Divorced', u'Widowed', u'Domestic partnership'], + [u'President Obama', u'Republicans in Congress', u'Both', u'Not sure'], + ] + actual = cs.labels(prune=True) + assert actual == expected + + # Apply H&S + expected = [ + [u'Married', u'left alone', u'Separated', u'Divorced', u'Widowed', u'Single', u'Domestic partnership'], + [u'President Obama', u'Obama + Republicans', u'Republicans in Congress', u'Both', u'Neither', u'Not sure'], + ] + actual = cs.labels(hs_dims=[0, 1]) + assert actual == expected + + # Apply H&S and pruning + expected = [ + [u'Married', u'left alone', u'Separated', u'Divorced', u'Widowed', u'Domestic partnership'], + [u'President Obama', u'Obama + Republicans', u'Republicans in Congress', u'Both', u'Not sure'], + ] + actual = cs.labels(prune=True, hs_dims=[0, 1]) + assert actual == expected