def test_normal_csv(self): text = dedent( """\ "attr_a","attr_b","attr_c" 1,4,"a" 2,2.1,"bb" 3,120.9,"ccc" """ ) expected_list = [ TableData( "csv1", ["attr_a", "attr_b", "attr_c"], [[1, 4, "a"], [2, "2.1", "bb"], [3, "120.9", "ccc"]], ) ] loader = ptr.TableTextLoader(text, format_name="csv") assert loader.format_name == "csv" for tabledata, expected in zip(loader.load(), expected_list): print(dumps_tabledata(expected)) print(dumps_tabledata(tabledata)) assert tabledata.equals(expected)
def test_normal_json(self): text = dedent( """\ [ {"attr_a": 1}, {"attr_b": 2.1, "attr_c": "bb"} ]""" ) expected_list = [ TableData( "json1", ["attr_a", "attr_b", "attr_c"], [{"attr_a": 1}, {"attr_b": 2.1, "attr_c": "bb"}], ) ] loader = ptr.TableTextLoader(text, format_name="json") assert loader.format_name == "json" for table_data, expected in zip(loader.load(), expected_list): print(dumps_tabledata(expected)) print(dumps_tabledata(table_data)) assert table_data.equals(expected)
def create_text_loader( logger, text: str, format_name: str, encoding: str, type_hint_rules: Optional[TypeHintRules], ) -> AbstractTableReader: try: return ptr.TableTextLoader( text, format_name, encoding=encoding, type_hint_rules=type_hint_rules, ) except (ptr.LoaderNotFoundError) as e: logger.error(msgfy.to_error_message(e)) sys.exit(ExitCode.FAILED_LOADER_NOT_FOUND)
def test_normal_ssv(self): text = dedent( """\ USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 77664 8784 ? Ss May11 0:02 /sbin/init root 2 0.0 0.0 0 0 ? S May11 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? I< May11 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? I< May11 0:00 [mm_percpu_wq] root 7 0.0 0.0 0 0 ? S May11 0:01 [ksoftirqd/0] """ ) expected_list = [ TableData( "csv1", [ "USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND", ], [ ["root", 1, 0, 0.4, 77664, 8784, "?", "Ss", "May11", "0:02", "/sbin/init"], ["root", 2, 0, 0, 0, 0, "?", "S", "May11", "0:00", "[kthreadd]"], ["root", 4, 0, 0, 0, 0, "?", "I<", "May11", "0:00", "[kworker/0:0H]"], ["root", 6, 0, 0, 0, 0, "?", "I<", "May11", "0:00", "[mm_percpu_wq]"], ["root", 7, 0, 0, 0, 0, "?", "S", "May11", "0:01", "[ksoftirqd/0]"], ], ) ] loader = ptr.TableTextLoader(text, format_name="ssv") assert loader.format_name == "csv" for tabledata, expected in zip(loader.load(), expected_list): print(dumps_tabledata(expected)) print(dumps_tabledata(tabledata)) assert tabledata.equals(expected)
def test_exception(self, value, format_name, expected): with pytest.raises(expected): ptr.TableTextLoader(value, format_name=format_name)