Ejemplo n.º 1
0
 def test_add_empty_set_to_axis_error(self):
     with pytest.raises(ValueError):
         MdxBuilder.from_cube("Cube") \
             .add_hierarchy_set_to_column_axis(MdxHierarchySet.tm1_subset_all("Dimension1")) \
             .add_hierarchy_set_to_axis(1, MdxHierarchySet.tm1_subset_all("Dimension2")) \
             .add_empty_set_to_axis(1) \
             .to_mdx()
Ejemplo n.º 2
0
 def test_mdx_builder_multi_fail_combine_tuples_sets_on_axis(self):
     with pytest.raises(ValueError):
         MdxBuilder.from_cube("CUBE") \
             .rows_non_empty() \
             .add_member_tuple_to_axis(0, Member.of("Dim1", "Dim1", "Elem1")) \
             .add_hierarchy_set_to_axis(0, MdxHierarchySet.all_leaves("Dim1")) \
             .to_mdx()
Ejemplo n.º 3
0
    def test_mdx_builder_with_calculated_member(self):
        mdx = MdxBuilder.from_cube(cube="Cube").with_member(
            CalculatedMember.avg(
                dimension="Period",
                hierarchy="Period",
                element="AVG 2016",
                cube="Cube",
                mdx_set=MdxHierarchySet.children(member=Member.of("Period", "2016")),
                mdx_tuple=MdxTuple.of(Member.of("Dim1", "Total Dim1"),
                                      Member.of("Dim2", "Total Dim2")))) \
            .rows_non_empty() \
            .add_hierarchy_set_to_row_axis(MdxHierarchySet.all_leaves("DIM1", "DIM1")) \
            .columns_non_empty() \
            .add_member_tuple_to_columns(Member.of("Period", "AVG 2016")) \
            .where("[Dim2].[Total Dim2]") \
            .to_mdx()

        self.assertEqual(
            "WITH\r\n"
            "MEMBER [PERIOD].[PERIOD].[AVG2016] AS AVG({[PERIOD].[PERIOD].[2016].CHILDREN},"
            "[CUBE].([DIM1].[DIM1].[TOTALDIM1],[DIM2].[DIM2].[TOTALDIM2]))\r\n"
            "SELECT\r\n"
            "NON EMPTY {([PERIOD].[PERIOD].[AVG2016])} ON 0,\r\n"
            "NON EMPTY {TM1FILTERBYLEVEL({TM1SUBSETALL([DIM1].[DIM1])},0)} ON 1\r\n"
            "FROM [CUBE]\r\n"
            "WHERE ([DIM2].[DIM2].[TOTALDIM2])",
            mdx)
Ejemplo n.º 4
0
    def test_merge_without_clean_after(self):
        mdx = MdxBuilder.from_cube(self.cube_name).add_member_tuple_to_columns(
            Member.of(self.dimension_names[0], "Element1"),
            Member.of(self.dimension_names[1], "Element1"),
            Member.of(self.dimension_names[2], "Element1")) \
            .to_mdx()

        sandbox2 = Sandbox(self.sandbox_name2, True)
        self.tm1.sandboxes.create(sandbox2)

        self.tm1.cells.write_values(cube_name=self.cube_name,
                                    cellset_as_dict={
                                        ("Element1", "Element1", "Element1"): 5
                                    },
                                    sandbox_name=self.sandbox_name2)

        self.tm1.sandboxes.merge(source_sandbox_name=self.sandbox_name2,
                                 target_sandbox_name=self.sandbox_name1,
                                 clean_after=False)

        values = self.tm1.cells.execute_mdx_values(
            mdx=mdx, sandbox_name=self.sandbox_name1)
        self.assertEqual(5, values[0])

        values = self.tm1.cells.execute_mdx_values(
            mdx=mdx, sandbox_name=self.sandbox_name2)
        self.assertEqual(5, values[0])
Ejemplo n.º 5
0
def build_mdx_from_cellset(cells: Dict, cube_name: str, dimensions: Iterable[str]) -> str:
    query = MdxBuilder.from_cube(cube_name)
    for coordinates in cells:
        members = (Member.of(dimension, element) for dimension, element in zip(dimensions, coordinates))
        query.add_member_tuple_to_columns(*members)
    mdx = query.to_mdx()
    return mdx
Ejemplo n.º 6
0
def build_vmm_vmt_mdx(cube_name: str):
    return MdxBuilder.from_cube("}CubeProperties") \
        .add_member_tuple_to_rows(Member.of("}Cubes", cube_name)) \
        .add_hierarchy_set_to_column_axis(
        MdxHierarchySet.members([
            Member.of("}CubeProperties", "VMM"),
            Member.of("}CubeProperties", "VMT")])) \
        .to_mdx()
Ejemplo n.º 7
0
    def test_mdx_builder_single_axes(self):
        mdx = MdxBuilder.from_cube("cube") \
            .add_hierarchy_set_to_axis(0, MdxHierarchySet.member(Member.of("Dim1", "Elem1"))) \
            .to_mdx()

        self.assertEqual(
            "SELECT\r\n"
            "{[dim1].[dim1].[elem1]} ON 0\r\n"
            "FROM [cube]", mdx)
Ejemplo n.º 8
0
def build_mdx_and_values_from_cellset(cells: Dict, cube_name: str, dimensions: Iterable[str]) -> Tuple[str, List]:
    values = []
    query = MdxBuilder.from_cube(cube_name)
    for coordinates, value in cells.items():
        members = (Member.of(dimension, element) for dimension, element in zip(dimensions, coordinates))
        query.add_member_tuple_to_columns(*members)
        values.append(value)
    mdx = query.to_mdx()
    return mdx, values
Ejemplo n.º 9
0
 def test_add_empty_set_to_axis_happy_case(self):
     mdx = MdxBuilder.from_cube("Cube") \
         .add_hierarchy_set_to_column_axis(MdxHierarchySet.tm1_subset_all("Dimension")) \
         .add_empty_set_to_axis(1) \
         .to_mdx()
     self.assertEqual(
         mdx, "SELECT\r\n"
         "{TM1SUBSETALL([dimension].[dimension])} ON 0,\r\n"
         "{} ON 1\r\n"
         "FROM [cube]")
Ejemplo n.º 10
0
    def as_MDX(self) -> str:
        """ Build a valid MDX Query from an Existing cubeview. 
        Takes Zero suppression into account. 
        Throws an Exception when no elements are place on the columns.
        Subsets are referenced in the result-MDX through the TM1SubsetToSet Function

        :return: String, the MDX Query
        """
        if not self.columns:
            raise ValueError("Column selection must not be empty")

        query = MdxBuilder.from_cube(self.cube)
        if self._suppress_empty_rows:
            query.rows_non_empty()

        if self.suppress_empty_columns:
            query.columns_non_empty()

        axes = [self.columns]
        if self.rows:
            axes.append(self.rows)

        for axis_id, axis in enumerate(axes):
            for axis_selection in axis:
                subset = axis_selection.subset

                if isinstance(subset, AnonymousSubset):
                    if subset.expression is not None:
                        mdx_hierarchy_set = MdxHierarchySet.from_str(
                            dimension=subset.dimension_name,
                            hierarchy=subset.hierarchy_name,
                            mdx=subset.expression)

                    else:
                        members = [
                            Member.of(subset.dimension_name, element)
                            for element in subset.elements
                        ]
                        mdx_hierarchy_set = MdxHierarchySet.members(members)

                else:
                    mdx_hierarchy_set = MdxHierarchySet.tm1_subset_to_set(
                        dimension=axis_selection.dimension_name,
                        hierarchy=axis_selection.hierarchy_name,
                        subset=subset.name)
                query.add_hierarchy_set_to_axis(
                    axis=axis_id, mdx_hierarchy_set=mdx_hierarchy_set)

        for title in self._titles:
            query.add_member_to_where(
                Member.of(title.dimension_name, title.selected))

        return query.to_mdx()
Ejemplo n.º 11
0
    def test_mdx_builder_multi_no_where(self):
        mdx = MdxBuilder.from_cube("cube") \
            .rows_non_empty() \
            .add_hierarchy_set_to_row_axis(MdxHierarchySet.all_leaves("Dim1")) \
            .columns_non_empty() \
            .add_hierarchy_set_to_column_axis(MdxHierarchySet.member(Member.of("Dim2", "Elem2"))) \
            .to_mdx()

        self.assertEqual(
            "SELECT\r\n"
            "NON EMPTY {[dim2].[dim2].[elem2]} ON 0,\r\n"
            "NON EMPTY {TM1FILTERBYLEVEL({TM1SUBSETALL([dim1].[dim1])},0)} ON 1\r\n"
            "FROM [cube]", mdx)
Ejemplo n.º 12
0
    def test_mdx_builder_simple(self):
        mdx = MdxBuilder.from_cube("CUBE") \
            .rows_non_empty() \
            .add_hierarchy_set_to_row_axis(MdxHierarchySet.all_leaves("Dim1")) \
            .columns_non_empty() \
            .add_hierarchy_set_to_column_axis(MdxHierarchySet.member(Member.of("Dim2", "Elem2"))) \
            .where(Member.of("Dim3", "Elem3"), Member.of("Dim4", "Elem4")) \
            .to_mdx()

        self.assertEqual(
            "SELECT\r\n"
            "NON EMPTY {[DIM2].[DIM2].[ELEM2]} ON 0,\r\n"
            "NON EMPTY {TM1FILTERBYLEVEL({TM1SUBSETALL([DIM1].[DIM1])},0)} ON 1\r\n"
            "FROM [CUBE]\r\n"
            "WHERE ([DIM3].[DIM3].[ELEM3],[DIM4].[DIM4].[ELEM4])", mdx)
Ejemplo n.º 13
0
    def test_mdx_builder_tm1_ignore_bad_tuples(self):
        mdx = MdxBuilder.from_cube("cube") \
            .tm1_ignore_bad_tuples() \
            .rows_non_empty() \
            .add_hierarchy_set_to_row_axis(MdxHierarchySet.all_leaves("Dim1")) \
            .columns_non_empty() \
            .add_hierarchy_set_to_column_axis(MdxHierarchySet.member(Member.of("Dim2", "Elem2"))) \
            .where(Member.of("Dim3", "Elem3"), Member.of("Dim4", "Elem4")) \
            .to_mdx()

        self.assertEqual(
            "SELECT\r\n"
            "NON EMPTY TM1IGNORE_BADTUPLES {[dim2].[dim2].[elem2]} ON 0,\r\n"
            "NON EMPTY TM1IGNORE_BADTUPLES {TM1FILTERBYLEVEL({TM1SUBSETALL([dim1].[dim1])},0)} ON 1\r\n"
            "FROM [cube]\r\n"
            "WHERE ([dim3].[dim3].[elem3],[dim4].[dim4].[elem4])", mdx)
Ejemplo n.º 14
0
    def test_mdx_builder_multi_axes(self):
        mdx = MdxBuilder.from_cube("cube") \
            .add_hierarchy_set_to_axis(0, MdxHierarchySet.member(Member.of("Dim1", "Elem1"))) \
            .add_hierarchy_set_to_axis(1, MdxHierarchySet.member(Member.of("Dim2", "Elem2"))) \
            .add_hierarchy_set_to_axis(2, MdxHierarchySet.member(Member.of("Dim3", "Elem3"))) \
            .add_hierarchy_set_to_axis(3, MdxHierarchySet.member(Member.of("Dim4", "Elem4"))) \
            .add_hierarchy_set_to_axis(4, MdxHierarchySet.member(Member.of("Dim5", "Elem5"))) \
            .to_mdx()

        self.assertEqual(
            "SELECT\r\n"
            "{[dim1].[dim1].[elem1]} ON 0,\r\n"
            "{[dim2].[dim2].[elem2]} ON 1,\r\n"
            "{[dim3].[dim3].[elem3]} ON 2,\r\n"
            "{[dim4].[dim4].[elem4]} ON 3,\r\n"
            "{[dim5].[dim5].[elem5]} ON 4\r\n"
            "FROM [cube]", mdx)
Ejemplo n.º 15
0
    def test_publish(self):
        mdx = MdxBuilder.from_cube(self.cube_name).add_member_tuple_to_columns(
            Member.of(self.dimension_names[0], "Element1"),
            Member.of(self.dimension_names[1], "Element1"),
            Member.of(self.dimension_names[2], "Element1")) \
            .to_mdx()

        self.tm1.cells.write_values(cube_name=self.cube_name,
                                    cellset_as_dict={
                                        ("Element1", "Element1", "Element1"): 1
                                    },
                                    sandbox_name=self.sandbox_name1)

        values = self.tm1.cells.execute_mdx_values(mdx=mdx)
        self.assertEqual(None, values[0])

        self.tm1.sandboxes.publish(self.sandbox_name1)

        values = self.tm1.cells.execute_mdx_values(mdx=mdx)
        self.assertEqual(1, values[0])
Ejemplo n.º 16
0
    def test_mdx_build_with_multi_calculated_member(self):
        mdx = MdxBuilder.from_cube(cube="Cube").with_member(
            CalculatedMember.avg(
                dimension="Period",
                hierarchy="Period",
                element="AVG 2016",
                cube="Cube",
                mdx_set=MdxHierarchySet.children(member=Member.of("Period", "2016")),
                mdx_tuple=MdxTuple.of(Member.of("Dim1", "Total Dim1"),
                                      Member.of("Dim2", "Total Dim2")))) \
            .with_member(
            CalculatedMember.sum(
                dimension="Period",
                hierarchy="Period",
                element="SUM 2016",
                cube="Cube",
                mdx_set=MdxHierarchySet.children(member=Member.of("Period", "2016")),
                mdx_tuple=MdxTuple.of(Member.of("Dim1", "Total Dim1"),
                                      Member.of("Dim2", "Total Dim2")))) \
            .rows_non_empty() \
            .add_hierarchy_set_to_row_axis(MdxHierarchySet.all_leaves("dim1", "dim1")) \
            .columns_non_empty() \
            .add_hierarchy_set_to_column_axis(
            MdxHierarchySet.members(members=[Member.of("Period", "AVG 2016"), Member.of("Period", "SUM 2016")])) \
            .where(Member.of("Dim2", "Total Dim2")) \
            .to_mdx()

        self.assertEqual(
            "WITH\r\n"
            "MEMBER [period].[period].[avg2016] AS AVG({[period].[period].[2016].CHILDREN},"
            "[cube].([dim1].[dim1].[totaldim1],[dim2].[dim2].[totaldim2]))\r\n"
            "MEMBER [period].[period].[sum2016] AS SUM({[period].[period].[2016].CHILDREN},"
            "[cube].([dim1].[dim1].[totaldim1],[dim2].[dim2].[totaldim2]))\r\n"
            "SELECT\r\n"
            "NON EMPTY {[period].[period].[avg2016],[period].[period].[sum2016]} ON 0,\r\n"
            "NON EMPTY {TM1FILTERBYLEVEL({TM1SUBSETALL([dim1].[dim1])},0)} ON 1\r\n"
            "FROM [cube]\r\n"
            "WHERE ([dim2].[dim2].[totaldim2])", mdx)