def test_apply_filters_no_list(self, mock_definition, mock_attr_element_coroutine, mock_attr_element, mock_instance): """Test that selected objects passed as strings are assigned properly when filter is applied.""" 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 mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.instance report = Report(connection=self.connection, report_id=self.report_id) report.apply_filters(attributes=self.__selected_attr[0], metrics=self.__selected_metrs[0], attr_elements=self.__selected_elem[0]) self.assertEqual(report.selected_attributes, self.__selected_attr) self.assertEqual(report.selected_metrics, self.__selected_metrs) self.assertEqual(report.selected_attr_elements, self.__selected_elem[:1])
def test_apply_filters_for_incorrect_assignments( self, mock_definition, mock_attr_element_coroutine, mock_attr_element, mock_instance): """Test that incorrectly assigned selected objects are assigned properly when filter is applied.""" 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 mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.instance report = Report(connection=self.connection, report_id=self.report_id) # attributes assigned selected_metrs, metrics assigned selected_elem and attr_elements assigned selected_attr report.apply_filters(attributes=self.__selected_metrs, metrics=self.__selected_elem, attr_elements=self.__selected_attr) self.assertEqual(report.selected_attributes, self.__selected_attr) self.assertEqual(report.selected_metrics, self.__selected_metrs) self.assertEqual(report.selected_attr_elements, self.__selected_elem)
def test_to_dataframe(self, mock_definition, mock_attr_element_coroutine, mock_attr_element, mock_instance, mock_instance_id_coroutine, mock_instance_id): """Test that data is retrieved and parsed properly when to_dataframe() is called. Result should be saved to Report.dataframe property. """ 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 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 mock_instance_id_coroutine.return_value = Mock() mock_instance_id_coroutine.return_value.result.return_value = Mock( ok=True, headers=self.__headers) mock_instance_id_coroutine.return_value.result.return_value.json.return_value = self.instance_id report = Report(connection=self.connection, report_id=self.report_id) df = report.to_dataframe(limit=3) self.assertTrue(mock_instance.called) self.assertTrue(mock_instance_id_coroutine.called) self.assertIsInstance(df, pandas.core.frame.DataFrame) self.assertIsInstance(report.dataframe, pandas.core.frame.DataFrame) self.assertEqual(df.IQ.sum(), 29349845.567647062) self.assertEqual(df.salary.sum(), 4145784.7926787906)
def test_apply_filters(self, mock_definition, mock_attr_element_coroutine, mock_attr_element): """Test that selected objects are assigned properly when filter is applied.""" 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 report = Report(connection=self.connection, report_id=self.report_id) report.apply_filters(self.__selected_attr, self.__selected_metrs, self.__selected_elem) self.assertTrue(mock_attr_element_coroutine.called) self.assertEqual(report.selected_attributes, self.__selected_attr) self.assertEqual(report.selected_metrics, self.__selected_metrs) self.assertEqual(report.selected_attr_elements, self.__selected_elem) report = Report(connection=self.connection, report_id=self.report_id) report.apply_filters(attributes=[], metrics=[]) self.assertEqual(report.selected_attr_elements, None) self.assertEqual(report._filter.attr_selected, []) self.assertEqual(report._filter.metr_selected, [])
def test_init_report(self, mock_definition, mock_attr_element, mock_instance): """Test that definition of the report is assigned properly when report is initialized.""" mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.definition mock_attr_element.return_value = Mock(headers=self.__headers) mock_attr_element.return_value.json.return_value = self.attr_elements report = Report(connection=self.connection, report_id=self.report_id) self.assertEqual(report._connection, self.connection) self.assertEqual(report._report_id, self.report_id) self.assertEqual(report.name, self.report_name) self.assertEqual(report.attributes, self.__attributes) self.assertEqual(report.metrics, self.__metrics) self.assertEqual(report._attr_elements, []) self.assertIsNone(report.selected_attributes) self.assertIsNone(report.selected_metrics) self.assertIsNone(report.selected_attr_elements) self.assertIsNone(report._dataframe) with self.assertWarns(Warning): report.dataframe
def test_init_report(self, mock_definition, mock_attr_element): """Test that definition of the report is assigned properly when report is initialized.""" mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.__definition mock_attr_element.return_value = Mock(headers=self.__headers) mock_attr_element.return_value.json.return_value = self.__attr_elements report = Report(connection=self.connection, report_id=self.report_id) self.assertTrue(mock_definition.called) self.assertTrue(mock_attr_element.called) self.assertEqual(report._connection, self.connection) self.assertEqual(report._report_id, self.report_id) self.assertEqual(report.name, self.report_name) self.assertEqual(report.attributes, [{ 'name': 'Name', 'id': '089FC10C11E9CA4D39700080EF15B5B9' }]) self.assertEqual(report.metrics, [{ 'name': 'Age', 'id': '089FB58611E9CA4D39700080EF15B5B9' }, { 'name': 'Row Count - table1', 'id': '089DE7BA11E9CA4D085B0080EFC515B9' }]) self.assertEqual(report.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.assertIsNone(report.selected_attributes) self.assertIsNone(report.selected_metrics) self.assertIsNone(report.selected_attr_elements) self.assertIsNone(report._dataframe) with self.assertWarns(Warning): report.dataframe
def test_clear_filters(self, mock_definition, mock_attr_element_coroutine, mock_attr_element, mock_instance): """Test that selected objects are assigned with empty lists when filter is cleared.""" 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 mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.instance report = Report(connection=self.connection, report_id=self.report_id) report.apply_filters(self.__selected_attr, self.__selected_metrs, self.__selected_elem) self.assertEqual(report.selected_attributes, self.__selected_attr) self.assertEqual(report.selected_metrics, self.__selected_metrs) self.assertEqual(report.selected_attr_elements, self.__selected_elem) report.clear_filters() self.assertIsNone(report.selected_attributes) self.assertIsNone(report.selected_metrics) self.assertIsNone(report.selected_attr_elements)
def test_apply_filters_invalid_elements(self, mock_definition, mock_attr_element): """Test that invalid id passed to a filter raises ValueError.""" 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 report = Report(connection=self.connection, report_id=self.report_id) self.assertRaises(ValueError, report.apply_filters, attributes='INV123456')
def test_to_dataframe(self, 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 Report.dataframe property. """ 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 report = Report(connection=self.connection, report_id=self.report_id) df = report.to_dataframe(limit=2) self.assertTrue(mock_instance.called) self.assertTrue(mock_instance_id.called) self.assertIsInstance(df, pandas.core.frame.DataFrame) self.assertIsInstance(report.dataframe, pandas.core.frame.DataFrame) self.assertTrue(df.equals(self.__dataframe))
def test_subtotals(self, mock_definition, mock_instance): """Test that subtotals are correctly turned off from requests""" mock_definition.return_value = Mock(ok=True) mock_definition.return_value.json.return_value = self.sub_def mock_instance.return_value = Mock(ok=True) mock_instance.return_value.json.return_value = self.sub_inst_off report = Report(connection=self.connection2, report_id="7405D8DE11EA482779D20080EF152878") df = report.to_dataframe() self.assertTupleEqual(df.shape, (26, 7)) mock_instance.return_value.json.return_value = self.sub_inst_on report_with_subtotals = Report( connection=self.connection, report_id="7405D8DE11EA482779D20080EF152878") df_ws = report_with_subtotals.to_dataframe() self.assertTupleEqual(df_ws.shape, (35, 7)) self.assertFalse(df.equals(df_ws))
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" ], metrics=["B4054F5411E9910D672E0080EFC5AE5B"], attr_elements=[ "A598372E11E9910D1CBF0080EFD54D63:Los Angeles",