예제 #1
0
    def it_knows_the_display_order_for_a_dimension(
        self, order, xtop, xbot, element_vals, empty_idxs, expected_value
    ):
        subtot_vals = [60, 40]
        dimension = Dimension(
            dimension_dict={
                "type": {
                    "categories": [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}],
                    "class": "categorical",
                }
            },
            dimension_type=DT.CAT,
            dimension_transforms={
                "order": {
                    "direction": "ascending" if order == "A" else "descending",
                    "fixed": {"top": xtop, "bottom": xbot},
                },
                "prune": True,
            },
        )

        display_order = SortByValueCollator.display_order(
            dimension, element_vals, subtot_vals, empty_idxs
        )

        assert display_order == expected_value
예제 #2
0
    def it_knows_its_transformed_description(self, dimension_dict):
        dimension_transforms = {"description": "foobar"}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        description = dimension.description

        assert description == "foobar"
예제 #3
0
    def it_knows_whether_it_should_be_pruned(self, dimension_dict):
        dimension_transforms = {"prune": True}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        prune = dimension.prune

        assert prune is True
예제 #4
0
    def it_provides_access_to_all_elements_in_its_collection(
            self, dimension_dict):
        dimension = Dimension(dimension_dict, DT.CAT)

        elements = dimension.all_elements

        assert isinstance(elements, _AllElements)
예제 #5
0
    def but_it_uses_the_dimension_name_when_no_transform(self, dimension_dict):
        dimension_transforms = {}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        name = dimension.name

        assert name == "ShutdownBlame"
예제 #6
0
    def it_knows_its_transformed_name(self, dimension_dict):
        dimension_transforms = {"name": "barfoo"}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        name = dimension.name

        assert name == "barfoo"
예제 #7
0
    def it_knows_its_selected_categories_labels(self, _dimensions_prop_):
        _dimensions_prop_.return_value = [
            Dimension({"references": {}}, None, None)
        ]
        strand_ = _Strand(None, None, None, None, None, None)

        assert strand_.selected_category_labels == ()
예제 #8
0
    def but_it_uses_transforms_insertions_instead_when_present(
            self, dimension_dict):
        dimension_transforms = {"insertions": []}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        subtotals = dimension.subtotals

        assert len(subtotals) == 0
예제 #9
0
    def it_provides_access_to_its_inserted_subtotal_specs(
            self, dimension_dict):
        dimension_transforms = {}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        subtotals = dimension.subtotals

        assert len(subtotals) == 1
예제 #10
0
    def and_it_uses_alias_when_no_name(self, dimension_dict):
        dimension_dict["references"].pop("name")
        dimension_transforms = {}
        dimension = Dimension(dimension_dict, None, dimension_transforms)

        name = dimension.name

        assert name == "ShutdownBlame"
예제 #11
0
    def it_knows_its_selected_category_labels(self, _dimensions_prop_,
                                              dimensions_dicts,
                                              expected_value):
        _dimensions_prop_.return_value = [
            Dimension(dim_dict, None, None) for dim_dict in dimensions_dicts
        ]
        slice_ = _Slice(None, None, None, None, None)

        assert slice_.selected_category_labels == expected_value
예제 #12
0
    def but_it_uses_element_description_when_not_transformed(
            self, dimension_dict):
        dimension = Dimension(dimension_dict, None)

        description = dimension.description

        assert 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 Congres"
            "s?")
예제 #13
0
    def test_numeric_values(self):
        dimension_dict = {
            "type": {
                "categories": [
                    {
                        "id": 42,
                        "missing": False,
                        "numeric_value": 1
                    },
                    {
                        "id": 43,
                        "missing": False,
                        "numeric_value": 2
                    },
                    {
                        "id": 44,
                        "missing": True,
                        "numeric_value": 3
                    },
                    {
                        "id": 45,
                        "missing": False,
                        "numeric_value": None
                    },
                    {
                        "id": 46,
                        "missing": False
                    },
                ],
                "class":
                "categorical",
            }
        }
        dimension = Dimension(dimension_dict, DT.CAT)

        numeric_values = dimension.numeric_values

        assert numeric_values == (1, 2, np.nan, np.nan)
예제 #14
0
    def test_subtotals(self):
        dimension_dict = {
            "references": {
                "view": {
                    "transform": {
                        "insertions": [
                            {
                                "anchor": 101,
                                "name": "This is respondent ideology"
                            },
                            {
                                "anchor": 2,
                                "args": [1, 2],
                                "function": "subtotal",
                                "name": "Liberal net",
                            },
                            {
                                "anchor": 5,
                                "args": [5, 4],
                                "function": "subtotal",
                                "name": "Conservative net",
                            },
                            {
                                "anchor": "fake anchor",
                                "args": ["fake_arg_1", "fake_arg_2"],
                                "function": "fake_fcn_name_not_subtotal",
                                "name": "Fake Name",
                            },
                        ]
                    }
                }
            },
            "type": {
                "categories": [{
                    "id": 1
                }, {
                    "id": 5
                }, {
                    "id": 8
                }, {
                    "id": 9
                }, {
                    "id": -1
                }],
                "class":
                "categorical",
            },
        }
        dimension = Dimension(dimension_dict, DT.CAT)

        subtotals = dimension.subtotals

        assert len(subtotals) == 2

        subtotal = subtotals[0]
        assert isinstance(subtotal, _Subtotal)
        assert subtotal.anchor == "bottom"
        assert subtotal.addend_ids == (1, )
        assert subtotal.label == "Liberal net"

        subtotal = subtotals[1]
        assert isinstance(subtotal, _Subtotal)
        assert subtotal.anchor == 5
        assert subtotal.addend_ids == (5, )
        assert subtotal.label == "Conservative net"