def test_multitable_definition(self, mock_info, mock_definition, mock_ds_definition): """Test that multitable definition function returns proper dictionary.""" __definition = { 'id': '3D6D912611E9F40400000080EF3580D0', 'name': 'IRIS', 'result': { 'definition': { 'availableObjects': { 'tables': [{ 'id': 'F256FBE616DFD979E7548694E3363106', 'name': 'IRIS', 'type': 15 }, { 'id': 'F256FBE616DFD979E7548694E3363109', 'name': 'IRIS2', 'type': 15 }], 'columns': [{ 'tableId': 'F256FBE616DFD979E7548694E3363106', 'tableName': 'IRIS', 'columnId': '0621CC6C11E9F65400000080EF35AAE7', 'columnName': 'Id', 'dataType': 33, 'precision': 0, 'scale': 0 }, { 'tableId': 'F256FBE616DFD979E7548694E3363106', 'tableName': 'IRIS', 'columnId': '0621CF3C11E9F65400000080EF35AAE7', 'columnName': 'PetalWidthCm', 'dataType': 33, 'precision': 0, 'scale': 0 }, { 'tableId': 'F256FBE616DFD979E7548694E3363109', 'tableName': 'IRIS2', 'columnId': '0621D0EA11E9F65400000080EF35AAE7', 'columnName': 'SepalWidthCm', 'dataType': 33, 'precision': 0, 'scale': 0 }] } } } } __returned_dictionary = { 'IRIS': ['Id', 'PetalWidthCm'], 'IRIS2': ['SepalWidthCm'] } mock_ds_definition.return_value = Mock(ok=True) mock_ds_definition.return_value.json.return_value = __definition cube = Cube(connection=self.connection, cube_id=self.cube_id) cube._Cube__multitable_definition() self.assertEqual(cube.table_definition, __returned_dictionary)
def test_to_dataframe_multi_data_frame_true(self, mock_ds_definition, mock_info, mock_definition, mock_attr_element, mock_instance, mock_instance_id): mock_ds_definition.return_value = Mock(ok=True) mock_ds_definition.return_value.json.return_value = self.__ds_definition mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.__instance mock_instance_id.return_value = Mock(ok=True) mock_instance_id.return_value.json.return_value = self.__instance_id cube = Cube(connection=self.connection, cube_id=self.cube_id) dfs = cube.to_dataframe(limit=2, multi_df=True) self.assertTrue(mock_instance.called) self.assertTrue(mock_instance_id.called) self.assertIsInstance(dfs, list) self.assertIsInstance(cube.dataframe, pandas.core.frame.DataFrame) self.assertTrue(cube.dataframe.equals(self.__dataframe)) for i, df in enumerate(dfs): colnames = self.__ds_definition['result']['definition'][ 'availableObjects']['tables'][i]['columns'] self.assertIsInstance(df, pandas.core.frame.DataFrame) self.assertTrue(df.equals(self.__dataframe[colnames]))
def test_apply_filter_no_list(self, mock_info, mock_definition, mock_attr_element, mock_attr_element_coroutine): """Test that selected objects passed as strings are assigned properly when filter is applied.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements mock_attr_element_coroutine.return_value = Mock() mock_attr_element_coroutine.return_value.result.return_value = Mock( ok=True, headers=self.__headers) mock_attr_element_coroutine.return_value.result.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) cube.apply_filters(attributes=self.__selected_attr[0], metrics=self.__selected_metrs[0], attr_elements=self.__selected_elem[0]) self.assertEqual(cube.selected_attributes, self.__selected_attr) self.assertEqual(cube.selected_metrics, self.__selected_metrs) self.assertEqual(cube.selected_attr_elements, [self.__selected_elem[0]])
def test_to_dataframe(self, mock_info, mock_definition, mock_attr_element, mock_instance, mock_instance_id): """Test that data is retrieved and parsed properly when to_dataframe() is called. Result should be saved to Cube.dataframe property. """ mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.__instance mock_instance_id.return_value = Mock(ok=True) mock_instance_id.return_value.json.return_value = self.__instance_id cube = Cube(connection=self.connection, cube_id=self.cube_id) df = cube.to_dataframe(limit=2) self.assertTrue(mock_instance.called) self.assertTrue(mock_instance_id.called) self.assertIsInstance(df, pandas.core.frame.DataFrame) self.assertIsInstance(cube.dataframe, pandas.core.frame.DataFrame) self.assertTrue(df.equals(self.__dataframe))
def test_apply_filters_for_incorrect_assignments( self, mock_info, mock_definition, mock_attr_element, mock_attr_element_coroutine): """Test that incorrectly assigned selected objects are assigned properly when filter is applied.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements mock_attr_element_coroutine.return_value = Mock() mock_attr_element_coroutine.return_value.result.return_value = Mock( ok=True, headers=self.__headers) mock_attr_element_coroutine.return_value.result.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) # attributes assigned selected_metrs, metrics assigned selected_elem and attr_elements assigned selected_attr cube.apply_filters(attributes=self.__selected_metrs, metrics=self.__selected_elem, attr_elements=self.__selected_attr) self.assertEqual(cube.selected_attributes, self.__selected_attr) self.assertEqual(cube.selected_metrics, self.__selected_metrs) self.assertEqual(cube.selected_attr_elements, self.__selected_elem)
def test_clear_filters(self, mock_info, mock_definition, mock_attr_element, mock_attr_element_coroutine): """Test that selected objects are assigned with empty lists when filter is cleared.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements mock_attr_element_coroutine.return_value = Mock() mock_attr_element_coroutine.return_value.result.return_value = Mock( ok=True, headers=self.__headers) mock_attr_element_coroutine.return_value.result.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) cube.apply_filters(self.__selected_attr, self.__selected_metrs, self.__selected_elem) self.assertEqual(cube.selected_attributes, self.__selected_attr) self.assertEqual(cube.selected_metrics, self.__selected_metrs) self.assertEqual(cube.selected_attr_elements, self.__selected_elem) cube.clear_filters() self.assertIsNone(cube.selected_attributes) self.assertIsNone(cube.selected_metrics) self.assertIsNone(cube.selected_attr_elements)
def test_apply_filters_invalid_elements(self, mock_info, mock_definition, mock_attr_element): """Test that invalid id passed to a filter raises ValueError.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) self.assertRaises(ValueError, cube.apply_filters, attributes='INV123456')
def test_init_cube(self, mock_info, mock_definition, mock_attr_element): """Test that definition of the cube is assigned properly when cube is initialized.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) self.assertTrue(mock_info.called) self.assertTrue(mock_definition.called) self.assertEqual(cube._connection, self.connection) self.assertEqual(cube._cube_id, self.cube_id) self.assertEqual(cube.name, self.cube_name) self.assertEqual(cube.size, self.__info['cubesInfos'][0]['size']) self.assertEqual(cube.status, self.__info['cubesInfos'][0]['status']) self.assertEqual(cube.path, self.__info['cubesInfos'][0]['path']) self.assertEqual(cube.owner_id, self.__info['cubesInfos'][0]['ownerId']) self.assertEqual(cube.last_modified, self.__info['cubesInfos'][0]['modificationTime']) self.assertEqual(cube.attributes, [{ 'name': 'Name', 'id': '089FC10C11E9CA4D39700080EF15B5B9' }]) self.assertEqual(cube.metrics, [{ 'name': 'Age', 'id': '089FB58611E9CA4D39700080EF15B5B9' }, { 'name': 'Row Count - table1', 'id': '089DE7BA11E9CA4D085B0080EFC515B9' }]) self.assertEqual(cube.attr_elements, []) self.assertIsNone(cube.selected_attributes) self.assertIsNone(cube.selected_metrics) self.assertIsNone(cube.selected_attr_elements) self.assertIsNone(cube._dataframe) with self.assertWarns(Warning): cube.dataframe
def test_apply_filters(self, mock_info, mock_definition, mock_attr_element): """Test that selected objects are assigned properly when filter is applied.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) cube.apply_filters(self.__selected_attr, self.__selected_metrs, self.__selected_elem) self.assertTrue(mock_attr_element.called) self.assertEqual(cube.selected_attributes, self.__selected_attr) self.assertEqual(cube.selected_metrics, self.__selected_metrs) self.assertEqual(cube.selected_attr_elements, self.__selected_elem) cube.clear_filters() cube.apply_filters(attributes=[], metrics=[]) self.assertEqual(cube.attr_elements, [{ 'attribute_name': 'Name', 'attribute_id': '089FC10C11E9CA4D39700080EF15B5B9', 'elements': [{ 'id': '089FC10C11E9CA4D39700080EF15B5B9:jack', 'formValues': ['jack'] }, { 'id': '089FC10C11E9CA4D39700080EF15B5B9:krish', 'formValues': ['krish'] }, { 'id': '089FC10C11E9CA4D39700080EF15B5B9:nick', 'formValues': ['nick'] }, { 'id': '089FC10C11E9CA4D39700080EF15B5B9:Tom', 'formValues': ['Tom'] }] }]) self.assertEqual(cube.selected_attributes, None) self.assertEqual(cube.selected_metrics, None)
def test_apply_filters(self, mock_info, mock_definition, mock_attr_element): """Test that selected objects are assigned properly when filter is applied.""" mock_info.return_value = Mock(ok=True) mock_info.return_value.json.return_value = self.__info mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(ok=True, headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements cube = Cube(connection=self.connection, cube_id=self.cube_id) cube.apply_filters(self.__selected_attr, self.__selected_metrs, self.__selected_elem) self.assertEqual(cube.selected_attributes, self.__selected_attr) self.assertEqual(cube.selected_metrics, self.__selected_metrs) self.assertEqual(cube.selected_attr_elements, self.__selected_elem) cube.clear_filters() cube.apply_filters(attributes=[], metrics=[]) self.assertEqual(cube.selected_attributes, []) self.assertEqual(cube.selected_metrics, [])
# get connection to an environment base_url = "https://<>/MicroStrategyLibrary/api" username = "******" password = "******" connection = Connection(base_url, username, password, project_name="MicroStrategy Tutorial", login_mode=1) cube_id = "some_cube_id" report_id = "some_report_id" # get cube based on its id and store it in data frame my_cube = Cube(connection=connection, cube_id=cube_id) my_cube_df = my_cube.to_dataframe # get report based on its id and store it in data frame my_report = Report(connection=connection, report_id=report_id, parallel=False) my_report_df = my_report.to_dataframe # get list of ids of metrics, attributes or attribue elements available within Cube or Report my_cube.metrics my_cube.attributes my_cube.attr_elements # by default all elements are shown in the data frame. To choose elements you have to pass proper IDs # to function 'apply_filters()' which is available for Cube and Report my_cube.apply_filters(attributes=[ "A598372E11E9910D1CBF0080EFD54D63", "A59855D811E9910D1CC50080EFD54D63"
def get_connected_cubes(self): """Lists cubes used by this document.""" response = documents.get_cubes_used_by_document( self.connection, self.id).json() return [Cube(self.connection, el['id']) for el in response]