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)
def test_mdx_set_cross_joins(self): mdx_set = MdxSet.cross_joins([ MdxHierarchySet.children(Member.of("Dimension", "element1")), MdxHierarchySet.member(Member.of("Dimension", "element2")), MdxHierarchySet.member(Member.of("Dimension", "element3")) ]) self.assertEqual( "{{[dimension].[dimension].[element1].CHILDREN}" " * {[dimension].[dimension].[element2]}" " * {[dimension].[dimension].[element3]}}", mdx_set.to_mdx())
def test_mdx_set_unions_allow_duplicates(self): hierarchy_set = MdxSet.unions([ MdxHierarchySet.children(Member.of("Dimension", "element1")), MdxHierarchySet.member(Member.of("Dimension", "element2")), MdxHierarchySet.member(Member.of("Dimension", "element3")) ], True) self.assertEqual( "{{[dimension].[dimension].[element1].CHILDREN}," "{[dimension].[dimension].[element2]}," "{[dimension].[dimension].[element3]}}", hierarchy_set.to_mdx())
def test_mdx_hierarchy_set_unions_no_duplicates(self): hierarchy_set = MdxHierarchySet.unions([ MdxHierarchySet.children(Member.of("Dimension", "element1")), MdxHierarchySet.member(Member.of("Dimension", "element2")), MdxHierarchySet.member(Member.of("Dimension", "element3")) ]) self.assertEqual( "{{[DIMENSION].[DIMENSION].[ELEMENT1].CHILDREN}" " + {[DIMENSION].[DIMENSION].[ELEMENT2]}" " + {[DIMENSION].[DIMENSION].[ELEMENT3]}}", hierarchy_set.to_mdx())
def test_calculated_member_sum(self): calculated_member = CalculatedMember.sum( dimension="Period", hierarchy="Period", element="SUM 2016", cube="Cube", mdx_set=MdxHierarchySet.children(Member.of("Period", "2016")), mdx_tuple=MdxTuple.of(Member.of("Dimension1", "Element1"), Member.of("Dimension2", "Element2"))) self.assertEqual( calculated_member.to_mdx(), "MEMBER [PERIOD].[PERIOD].[SUM2016] AS SUM({[PERIOD].[PERIOD].[2016].CHILDREN},[CUBE]." "([DIMENSION1].[DIMENSION1].[ELEMENT1],[DIMENSION2].[DIMENSION2].[ELEMENT2]))")
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)
def test_calculated_member_sum(self): calculated_member = CalculatedMember.sum( dimension="Period", hierarchy="Period", element="SUM 2016", cube="Cube", mdx_set=MdxHierarchySet.children(Member.of("Period", "2016")), mdx_tuple=MdxTuple.of(Member.of("Dimension1", "Element1"), Member.of("Dimension2", "Element2"))) self.assertEqual( calculated_member.to_mdx(), "MEMBER [period].[period].[sum2016] AS SUM({[period].[period].[2016].CHILDREN},[cube]." "([dimension1].[dimension1].[element1],[dimension2].[dimension2].[element2]))" )
def test_mdx_hierarchy_set_children(self): hierarchy_set = MdxHierarchySet.children(Member.of("Dimension", "Element")) self.assertEqual( "{[DIMENSION].[DIMENSION].[ELEMENT].CHILDREN}", hierarchy_set.to_mdx())