def test_curly_braces(self): self.assertEqual( MDXUtils.curly_braces("something"), "{something}") self.assertEqual( MDXUtils.curly_braces("something}"), "{something}") self.assertEqual( MDXUtils.curly_braces("{something"), "{something}") self.assertEqual( MDXUtils.curly_braces("{something}"), "{something}")
def test_build_pandas_dataframe_empty_cellset(self): self.tm1.cubes.cells.write_value( value=0, cube_name=self.cube_name, element_tuple=(self.dim1_element_names[0], self.dim2_element_names[0], self.dim3_element_names[0], self.dim4_element_names[0]), dimensions=(self.dim1_name, self.dim2_name, self.dim3_name, self.dim4_name)) rows = [ DimensionSelection(dimension_name=self.dim1_name, elements=(self.dim1_element_names[0], )), DimensionSelection(dimension_name=self.dim2_name, elements=(self.dim2_element_names[0], )) ] columns = [ DimensionSelection(dimension_name=self.dim3_name, elements=(self.dim3_element_names[0], )), DimensionSelection(dimension_name=self.dim4_name, elements=(self.dim4_element_names[0], )) ] suppress = "Both" mdx = MDXUtils.construct_mdx(cube_name=self.cube_name, rows=rows, columns=columns, suppress=suppress) empty_cellset = self.tm1.cubes.cells.execute_mdx(mdx) self.assertRaises(ValueError, Utils.build_pandas_dataframe_from_cellset, empty_cellset, True) self.assertRaises(ValueError, Utils.build_pandas_dataframe_from_cellset, empty_cellset, False)
def test_build_pandas_dataframe_from_cellset(self): rows = [ DimensionSelection(dimension_name=self.dim1_name), DimensionSelection(dimension_name=self.dim2_name, elements=self.dim2_element_names) ] columns = [ DimensionSelection(dimension_name=self.dim3_name, expression="TM1SubsetAll([{}])".format( self.dim3_name)), DimensionSelection(dimension_name=self.dim4_name, subset=self.dim4_subset_Name) ] suppress = None mdx = MDXUtils.construct_mdx(cube_name=self.cube_name, rows=rows, columns=columns, suppress=suppress) cellset = self.tm1.cubes.cells.execute_mdx(mdx) df = Utils.build_pandas_dataframe_from_cellset(cellset, multiindex=False) self.assertTrue(df.shape[0] == 1000) # cater for potential Sandboxes dimension on first position if df.columns[0] == "Sandboxes": self.assertTrue(df.shape[1] == 6) else: self.assertTrue(df.shape[1] == 5) self.assertIsInstance(df, pd.DataFrame) cellset = Utils.build_cellset_from_pandas_dataframe(df) self.assertTrue(len(cellset.keys()) == 1000) self.assertIsInstance(cellset, Utils.CaseAndSpaceInsensitiveTuplesDict)
def test_cellset_and_pandas_df(self): rows = {self.dim1_name: None, self.dim2_name: self.dim2_element_names} columns = { self.dim3_name: "{{TM1SubsetAll([{}])}}".format(self.dim3_name) } contexts = {self.dim4_name: self.dim4_element_names[0]} suppress = None mdx = MDXUtils.construct_mdx(cube_name=self.cube_name, rows=rows, columns=columns, contexts=contexts, suppress=suppress) cellset = self.tm1.cubes.cells.execute_mdx(mdx) df = Utils.build_pandas_dataframe_from_cellset(cellset, multiindex=True) self.assertIsInstance(df, pd.DataFrame) self.assertTrue(df.shape[0] == 1000) self.assertTrue(df.shape[1] == 1) cellset = Utils.build_cellset_from_pandas_dataframe(df) self.assertTrue(len(cellset.keys()) == 1000) self.assertIsInstance(cellset, Utils.CaseAndSpaceInsensitiveTuplesDict) df = Utils.build_pandas_dataframe_from_cellset(cellset, multiindex=False) self.assertTrue(df.shape[0] == 1000) self.assertTrue(df.shape[1] == 5) self.assertIsInstance(df, pd.DataFrame) cellset = Utils.build_cellset_from_pandas_dataframe(df) self.assertTrue(len(cellset.keys()) == 1000) self.assertIsInstance(cellset, Utils.CaseAndSpaceInsensitiveTuplesDict)
def test_read_cube_name_from_mdx(self): all_cube_names = self.tm1.cubes.get_all_names() for cube_name in all_cube_names: private_views, public_views = self.tm1.cubes.views.get_all(cube_name) for view in private_views + public_views: mdx = view.MDX self.assertEquals( cube_name.upper().replace(" ", ""), MDXUtils.read_cube_name_from_mdx(mdx))
def test_read_cube_name_from_mdx(self): all_cube_names = self.tm1.cubes.get_all_names() all_cube_names_normalized = [ cube_name.upper().replace(" ", "") for cube_name in all_cube_names ] for cube_name in all_cube_names: private_views, public_views = self.tm1.cubes.views.get_all( cube_name) for view in private_views + public_views: mdx = view.MDX cube_name = MDXUtils.read_cube_name_from_mdx(mdx) self.assertIn(cube_name, all_cube_names_normalized)
def test_construct_mdx(self): rows = [DimensionSelection(dimension_name=self.dim1_name), DimensionSelection(dimension_name=self.dim2_name, elements=self.dim2_element_names)] columns = [DimensionSelection( dimension_name=self.dim3_name, expression="TM1SubsetAll([{}])".format(self.dim3_name))] contexts = {self.dim4_name: self.dim4_element_names[0]} mdx = MDXUtils.construct_mdx( cube_name=self.cube_name, rows=rows, columns=columns, contexts=contexts, suppress=None) content = self.tm1.cubes.cells.execute_mdx(mdx) number_cells = len(content.keys()) self.assertEqual(number_cells, 1000)
def test_construct_mdx(self): rows = {self.dim1_name: None, self.dim2_name: self.dim2_element_names} columns = { self.dim3_name: "{{TM1SubsetAll([{}])}}".format(self.dim3_name) } contexts = {self.dim4_name: self.dim4_element_names[0]} suppress = None mdx = MDXUtils.construct_mdx(cube_name=self.cube_name, rows=rows, columns=columns, contexts=contexts, suppress=suppress) content = self.tm1.cubes.cells.execute_mdx(mdx) number_cells = len(content.keys()) self.assertEqual(number_cells, 1000)
def test_construct_mdx_suppress_zeroes(self): rows = [DimensionSelection(dimension_name=self.dim1_name), DimensionSelection(dimension_name=self.dim2_name, elements=self.dim2_element_names)] columns = [ DimensionSelection( dimension_name=self.dim3_name, expression="TM1SubsetAll([{}])".format(self.dim3_name)), DimensionSelection( dimension_name=self.dim4_name, subset=self.dim4_subset_Name)] mdx = MDXUtils.construct_mdx( cube_name=self.cube_name, rows=rows, columns=columns, suppress="BOTH") content = self.tm1.cubes.cells.execute_mdx(mdx) number_cells = len(content.keys()) self.assertLess(number_cells, 1000)