def test_get_display_value_self_describing_object(): """Check that SelfDescribingObject is displayed correctly.""" json_object = json.loads("{\"schema\" : \"Integer\", \"value\": 23}") assert _parse_json_with_schema('SelfDescribing', json_object) == 23 json_object = json.loads("{\"schema\" : \"Integer\"}") assert _parse_json_with_schema('SelfDescribing', json_object) is None
def test_get_display_value_self_describing_object(): """Check that SelfDescribingObject is displayed correctly.""" json_object = json.loads('{"schema" : "Integer", "value": 23}') assert _parse_json_with_schema("SelfDescribing", json_object) == 23 json_object = json.loads('{"schema" : "Integer"}') assert _parse_json_with_schema("SelfDescribing", json_object) is None
def test_trace_tree_list_deserialization(): raw_trace_tree_list = [ { "traceElement": { "fragments": [{ "class": TEXT_FRAGMENT, "text": "aaa" }] } }, { "traceElement": { "fragments": [{ "class": TEXT_FRAGMENT, "text": "bbb" }] } }, ] trace_tree_list = _parse_json_with_schema("List<TraceTree>", raw_trace_tree_list) assert len(trace_tree_list) == 2 assert trace_tree_list[0] == TraceTree(TraceElement([TextFragment("aaa")]), []) assert trace_tree_list[1] == TraceTree(TraceElement([TextFragment("bbb")]), [])
def _rows_to_frame(table_metadata, rows): # type: (TableMetadata, List[Row]) -> pandas.DataFrame return pandas.DataFrame.from_records( [[ _parse_json_with_schema(cm.schema, row.get(cm.name)) for cm in table_metadata.column_metadata ] for row in rows], columns=table_metadata.get_column_names())
def test_trace_tree_list_representation(): raw_trace_tree_list = [ { "traceElement": {"fragments": [{"class": TEXT_FRAGMENT, "text": "aaa"}]}, "children": [ { "traceElement": { "fragments": [{"class": TEXT_FRAGMENT, "text": "bbb"}] }, "children": [ { "traceElement": { "fragments": [{"class": TEXT_FRAGMENT, "text": "ccc"}] } } ], }, { "traceElement": { "fragments": [{"class": TEXT_FRAGMENT, "text": "ddd"}] } }, ], }, { "traceElement": {"fragments": [{"class": TEXT_FRAGMENT, "text": "eee"}]}, "children": [ { "traceElement": { "fragments": [{"class": TEXT_FRAGMENT, "text": "fff"}] } } ], }, ] trace_tree_list = _parse_json_with_schema("List<TraceTree>", raw_trace_tree_list) assert str(trace_tree_list) == "\n".join( ["- aaa", " - bbb", " - ccc", " - ddd", "- eee", " - fff"] ) html_text = trace_tree_list._repr_html_().replace(" ", "").replace("\n", "") assert html_text == "".join( [ "<ul>", "<li>aaa<ul>", "<li>bbb<ul>", "<li>ccc</li></ul></li>", "<li>ddd</li></ul></li>", "<li>eee<ul>", "<li>fff</li></ul></li>", "</ul>", ] )
def _rows_to_frame(table_metadata, rows): # type: (TableMetadata, List[Row]) -> pandas.DataFrame row_based = [[_parse_json_with_schema(cm.schema, row.get(cm.name)) for cm in table_metadata.column_metadata] for row in rows] column_names = table_metadata.get_column_names() # convert data to column format and force dtype=object on Series # This gets us consistent `None` values across columns -- no columns # are treated as numeric. col_based = {column_names[i]: pandas.Series(column, dtype='object') for i, column in enumerate(zip(*row_based))} df = pandas.DataFrame.from_dict(col_based, orient='columns', dtype='object') # Re-index to: # 1. Force ordering of columns # 2. Set columns even if the dataframe is empty return df.reindex(labels=column_names, axis='columns')
def test_get_display_value_unknown_schema(): """Check that Integer values are extracted correctly.""" assert _parse_json_with_schema('bogus', None) is None json_object = json.loads("{\"foo\" : 23}") assert _parse_json_with_schema('bogus', json_object) == json_object
def test_get_display_value_integer(): """Check that Integer values are extracted correctly.""" assert _parse_json_with_schema('Integer', "0") == 0 assert _parse_json_with_schema('Integer', 0) == 0 assert _parse_json_with_schema('Integer', -1) == -1 assert _parse_json_with_schema('Integer', "-1") == -1