def test_simple_add_rows_with_clear_queue(self): """Test plain old add_rows after clearing the queue.""" all_methods = self._get_unnamed_data_methods() + self._get_named_data_methods() for method in all_methods: # Create a new data-carrying element (e.g. st.dataframe) el = method(DATAFRAME) # Make sure it has 2 rows in it. df_proto = data_frame._get_data_frame(self.get_delta_from_queue()) num_rows = len(df_proto.data.cols[0].int64s.data) self.assertEqual(num_rows, 2) # This is what we're testing: self.report_queue.clear() el.add_rows(NEW_ROWS) # Make sure there are 3 rows in the delta that got appended. ar = self.get_delta_from_queue().add_rows num_rows = len(ar.data.data.cols[0].int64s.data) self.assertEqual(num_rows, 3) # Clear the queue so the next loop is like a brand new test. get_report_ctx().reset() self.report_queue.clear()
def test_no_index_no_data_add_rows(self): """Test plain old add_rows.""" all_methods = self._get_unnamed_data_methods() for method in all_methods: # Create a new data-carrying element (e.g. st.dataframe) el = method(None) data_frame._get_data_frame(self.get_delta_from_queue()) # This is what we're testing: el.add_rows(DATAFRAME) # Make sure there are 2 rows in it now. df_proto = data_frame._get_data_frame(self.get_delta_from_queue()) num_rows = len(df_proto.data.cols[0].int64s.data) self.assertEqual(num_rows, 2) # Clear the queue so the next loop is like a brand new test. get_report_ctx().reset() self.report_queue.clear()
def test_named_add_rows(self): """Test add_rows with a named dataset.""" for method in self._get_named_data_methods(): # Create a new data-carrying element (e.g. st.dataframe) el = method(DATAFRAME) # Make sure it has 2 rows in it. df_proto = data_frame._get_data_frame(self.get_delta_from_queue()) num_rows = len(df_proto.data.cols[0].int64s.data) self.assertEqual(num_rows, 2) # This is what we're testing: el.add_rows(mydata1=NEW_ROWS) # Make sure there are 5 rows in it now. df_proto = data_frame._get_data_frame(self.get_delta_from_queue()) num_rows = len(df_proto.data.cols[0].int64s.data) self.assertEqual(num_rows, 5) # Clear the queue so the next loop is like a brand new test. get_report_ctx().reset() self.report_queue.clear()
def test_deltas_that_melt_dataframes(self): deltas = self._get_deltas_that_melt_dataframes() for delta in deltas: el = delta(DATAFRAME) el.add_rows(NEW_ROWS) # It is important that we test after this second call to add_rows # to cover the logic to compute the index. # See: # https://github.com/streamlit/streamlit/issues/748 el.add_rows(NEW_ROWS) df_proto = data_frame._get_data_frame(self.get_delta_from_queue()) num_rows = len(df_proto.data.cols[0].int64s.data) self.assertEqual(num_rows, 16) self.assertEqual( [0, 1, 0, 1, 2, 3, 4, 2, 3, 4, 5, 6, 7, 5, 6, 7], df_proto.data.cols[0].int64s.data, )
def test_get_data_frame(self): """Test streamlit.data_frame._get_data_frame.""" # Test delta not new_element or add_rows with pytest.raises(ValueError) as e: delta = Delta() data_frame._get_data_frame(delta) err_msg = "Cannot extract DataFrame from None." self.assertEqual(err_msg, str(e.value)) # Generic Data aa = AnyArray() aa.int64s.data.extend([1, 2, 3]) # Delta DataFrame delta_df = Delta() delta_df.new_element.data_frame.data.cols.extend([aa]) df = data_frame._get_data_frame(delta_df) self.assertEqual(df, delta_df.new_element.data_frame) # Delta Table delta_table = Delta() delta_table.new_element.table.data.cols.extend([aa]) df = data_frame._get_data_frame(delta_table) self.assertEqual(df, delta_table.new_element.table) # Vega-Lite Chart delta_vega = Delta() delta_vega.new_element.vega_lite_chart.data.data.cols.extend([aa]) df = data_frame._get_data_frame(delta_vega) self.assertEqual(df, delta_vega.new_element.vega_lite_chart.data) # Vega-Lite Chart w/ named dataset delta_vega_dataset = Delta() ds1 = NamedDataSet() ds1.name = "dataset 1" ds1.has_name = True ds1.data.data.cols.extend([aa]) delta_vega_dataset.new_element.vega_lite_chart.datasets.extend([ds1]) df = data_frame._get_data_frame(delta_vega_dataset, "dataset 1") self.assertEqual( df, delta_vega_dataset.new_element.vega_lite_chart.datasets[0].data) # Vega-Lite Chart w/ unnamed dataset delta_vega_unnamed_dataset = Delta() ds2 = NamedDataSet() ds2.has_name = False ds2.data.data.cols.extend([aa]) delta_vega_unnamed_dataset.new_element.vega_lite_chart.datasets.extend( [ds2]) df = data_frame._get_data_frame(delta_vega_unnamed_dataset) self.assertEqual( df, delta_vega_unnamed_dataset.new_element.vega_lite_chart. datasets[0].data) # add_rows w/ name delta_add_rows = Delta() delta_add_rows.add_rows.name = "named dataset" delta_add_rows.add_rows.has_name = True delta_add_rows.add_rows.data.data.cols.extend([aa]) df = data_frame._get_data_frame(delta_add_rows, "named dataset") self.assertEqual(df, delta_add_rows.add_rows.data) # add_rows w/out name with pytest.raises(ValueError) as e: delta_add_rows_noname = Delta() delta_add_rows_noname.add_rows.name = "named dataset" delta_add_rows_noname.add_rows.has_name = True delta_add_rows_noname.add_rows.data.data.cols.extend([aa]) df = data_frame._get_data_frame(delta_add_rows_noname) err_msg = 'No dataset found with name "None".' self.assertEqual(err_msg, str(e.value))