def test_table_answer_element_excluded_rows(): """Check deserialization creates empty table when no rows are present.""" answer = { "answerElements": [{ "metadata": { "columnMetadata": [ { "name": "col1", "schema": "String" } ] }, "excludedRows": [{ "exclusionName": "myEx", "rows": [{ "col1": "stringValue" }] }] }] } table = TableAnswer(answer) assert len(table.excluded_rows) == 1 assert len(table.excluded_rows["myEx"]) == 1 assert table.excluded_frame("myEx").size == 1
def test_table_answer_element_deser(): """Check proper deserialization for a valid table answer.""" answer = { "answerElements": [{ "metadata": { "columnMetadata": [ { "name": "col1", "schema": "String" }, { "name": "col2", "schema": "String" }, ] }, "rows": [{ "col1": "value1", "col2": "value2" }], }] } table = TableAnswer(answer) assert len(table.metadata.column_metadata) == 2 assert table.metadata.column_metadata[0].name == "col1" assert len(table.rows) == 1 assert table.rows[0].get("col1") == "value1" assert table.rows[0].get("col2") == "value2" assert not table.frame().empty
def test_table_answer_element_no_metadata(): """Exception is raised when metadata field is not present.""" answer = {"answerElements": [{"nometadata": {}}]} with pytest.raises(ValueError): TableAnswer(answer) answer = {"answerElements": [{}]} with pytest.raises(ValueError): TableAnswer(answer)
def test_table_answer_immutable_lists(): answer = { "answerElements": [{ "metadata": { "columnMetadata": [{ "name": "col1", "schema": "List<String>" }] }, "rows": [{ "col1": ["e1", "e2", "e3"] }], }] } table = TableAnswer(answer) assert isinstance(table.frame().loc[0]["col1"], ListWrapper)
def test_table_answer_element_deser_no_rows(): """Check deserialization creates empty table when no rows are present.""" answer = { "answerElements": [{ "metadata": { "columnMetadata": [{ "name": "col1", "schema": "Node" }] } }] } table = TableAnswer(answer) assert len(table.metadata.column_metadata) == 1 assert table.metadata.column_metadata[0].name == "col1" assert len(table.rows) == 0 assert table.frame().empty
def from_string(json_string): # type: (str) -> Answer """Take a string representing a Batfish answer, return answer object. :returns either an old :py:class:`Answer` or new :py:class:`TableAnswer` class. """ o = json.loads(json_string) if "answerElements" in o and "metadata" in o["answerElements"][0]: return TableAnswer(o) else: return Answer(o)
def bf_str_answer(answer_json): """Convert the Json answer to a string.""" try: if "answerElements" in answer_json and "metadata" in \ answer_json["answerElements"][0]: table_answer = TableAnswer(answer_json) return table_answer.table_data.to_string() else: return get_answer_text(answer_json) except Exception as error: return "Error getting answer text: {}\n Original Json:\n {}".format( error, json.dumps(answer_json, indent=2))
def test_table_answer_dtype_object(): """ Ensure table answer frames have dtype of object. This also means converting missing values to None (and not ``numpy.nan``) """ answer = { "answerElements": [{ "metadata": { "columnMetadata": [ { "name": "col1", "schema": "String" }, { "name": "col2", "schema": "Integer" }, ] }, "rows": [ { "col1": "v1", "col2": 1 }, { "col1": "v2", "col2": None }, { "col1": "v3", "col2": -1 }, { "col1": "v4", "col2": "-1" }, ], }] } df = TableAnswer(answer).frame() assert df["col1"].dtype == "object" assert df["col2"].dtype == "object" assert df["col2"][1] is None assert str(df["col2"][1]) == "None"
def test_table_answer_no_answer_elements(): """Exception is raised when answer elements are empty or not present.""" with pytest.raises(ValueError): TableAnswer({}) with pytest.raises(ValueError): TableAnswer({"answerElements": []})