def test_valid_response(self): """Tests on happy path, validating response and iterations over it.""" response = KustoResponseDataSetV2(json.loads(RESPONSE_TEXT)) # Test that basic iteration works assert len(response) == 3 assert len(list(response.primary_results[0])) == 3 table = list(response.tables[0]) assert 1 == len(table) expected_table = [ [ datetime(2016, 6, 6, 15, 35, tzinfo=tzutc()), "foo", 101, 3.14, False, timedelta(days=4, hours=1, minutes=2, seconds=3, milliseconds=567) ], [ datetime(2016, 6, 7, 16, tzinfo=tzutc()), "bar", 555, 2.71, True, timedelta() ], [None, str(""), None, None, None, None], ] columns = [ "Timestamp", "Name", "Altitude", "Temperature", "IsFlying", "TimeFlying" ] # Test access by index and by column name primary_table = response.primary_results[0] for row in primary_table: # Test all types for i, expected_type in enumerate( [datetime, str, int, float, bool, timedelta]): assert row[i] == row[columns[i]] assert row[i] is None or isinstance(row[i], expected_type) for row_index, row in enumerate(primary_table): expected_row = expected_table[row_index] for col_index, value in enumerate(row): assert value == expected_row[col_index]
def test_dataframe_from_result_table(self): """Test conversion of KustoResultTable to pandas.DataFrame, including fixes for certain column types""" with open( os.path.join(os.path.dirname(__file__), "input", "dataframe.json"), "r") as response_file: data = response_file.read() response = KustoResponseDataSetV2(json.loads(data)) df = dataframe_from_result_table(response.primary_results[0]) assert df.iloc[0].RecordName == "now" assert type( df.iloc[0].RecordTime) is pandas._libs.tslibs.timestamps.Timestamp assert all( getattr(df.iloc[0].RecordTime, k) == v for k, v in { "year": 2021, "month": 12, "day": 22, "hour": 11, "minute": 43, "second": 00 }.items()) assert type(df.iloc[0].RecordBool) is numpy.bool_ assert df.iloc[0].RecordBool == True assert type(df.iloc[0].RecordInt) is numpy.int32 assert df.iloc[0].RecordInt == 5678 assert type(df.iloc[0].RecordReal) is numpy.float64 assert df.iloc[0].RecordReal == 3.14159 # Kusto datetime(0000-01-01T00:00:00Z), which Pandas can't represent. assert df.iloc[1].RecordName == "earliest datetime" assert type( df.iloc[1].RecordTime) is pandas._libs.tslibs.nattype.NaTType assert pandas.isnull(df.iloc[1].RecordReal) # Kusto datetime(9999-12-31T23:59:59Z), which Pandas can't represent. assert df.iloc[2].RecordName == "latest datetime" assert type( df.iloc[2].RecordTime) is pandas._libs.tslibs.nattype.NaTType assert type(df.iloc[2].RecordReal) is numpy.float64 assert df.iloc[2].RecordReal == numpy.inf # Pandas earliest datetime assert df.iloc[3].RecordName == "earliest pandas datetime" assert type( df.iloc[3].RecordTime) is pandas._libs.tslibs.timestamps.Timestamp assert type(df.iloc[3].RecordReal) is numpy.float64 assert df.iloc[3].RecordReal == -numpy.inf # Pandas latest datetime assert df.iloc[4].RecordName == "latest pandas datetime" assert type( df.iloc[4].RecordTime) is pandas._libs.tslibs.timestamps.Timestamp # Kusto 600000000 ticks timedelta assert df.iloc[5].RecordName == "timedelta ticks" assert type( df.iloc[5].RecordTime) is pandas._libs.tslibs.timestamps.Timestamp assert type(df.iloc[5].RecordOffset ) is pandas._libs.tslibs.timestamps.Timedelta assert df.iloc[5].RecordOffset == pandas.to_timedelta("00:01:00") # Kusto timedelta(1.01:01:01.0) == assert df.iloc[6].RecordName == "timedelta string" assert type( df.iloc[6].RecordTime) is pandas._libs.tslibs.timestamps.Timestamp assert type(df.iloc[6].RecordOffset ) is pandas._libs.tslibs.timestamps.Timedelta assert df.iloc[6].RecordOffset == pandas.to_timedelta( "1 days 01:01:01")
def test_row_equality(self): """Tests the rows are idempotent.""" response = KustoResponseDataSetV2(json.loads(RESPONSE_TEXT)) table = response.primary_results[0] for row_index, row in enumerate(table): assert table[row_index] == row
def test_iterating_after_end(self): """Tests StopIteration is raised when the response ends.""" response = KustoResponseDataSetV2(json.loads(RESPONSE_TEXT)) assert sum(1 for _ in response.primary_results[0]) == 3
def test_column_dont_exist(self): """Tests accessing column that doesn't exists.""" response = KustoResponseDataSetV2(json.loads(RESPONSE_TEXT)) row = response.primary_results[0][0] self.assertRaises(IndexError, row.__getitem__, 10) self.assertRaises(LookupError, row.__getitem__, "NonexistentColumn")
def test_invalid_table(self): """Tests calling of table with index that doesn't exists.""" response = KustoResponseDataSetV2(json.loads(RESPONSE_TEXT)) self.assertRaises(IndexError, response.__getitem__, 7) self.assertRaises(IndexError, response.__getitem__, -6)