def test_bad_typed_field_selector(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) with self.assertRaises(TypeError): _ = table.field(object)
def test_write_csv_with_limit(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write_csv(out, limit=2) self.assertEqual(out.getvalue(), u'Alice,33\r\n' u'Bob,44\r\n')
def test_write_with_skip_and_limit(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write(out, skip=1, limit=2) self.assertEqual(out.getvalue(), u' Bob | 44 \r\n' u' Carol | 55 \r\n')
def test_write_csv_with_quotes_in_value(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave \"Nordberg\" Smith", 66], ], keys=["name", "age"]) out = StringIO() table.write_csv(out) self.assertEqual( out.getvalue(), u'Alice,33\r\n' u'Bob,44\r\n' u'Carol,55\r\n' u'"Dave ""Nordberg"" Smith",66\r\n')
def test_write_csv_with_none_in_value(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", None], ], keys=["name", "age"]) out = StringIO() table.write_csv(out) self.assertEqual( out.getvalue(), u'Alice,33\r\n' u'Bob,44\r\n' u'Carol,55\r\n' u'Dave,\r\n')
def test_mixed_types(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55.5], ["Dave", 66.6], ], keys=["name", "age"]) self.assertEqual(table.keys(), ["name", "age"]) name_field = table.field(0) self.assertEqual(name_field["type"], str) self.assertEqual(name_field["optional"], False) age_field = table.field(1) self.assertEqual(set(age_field["type"]), {int, float}) self.assertEqual(age_field["optional"], False)
def test_write_tsv(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write_tsv(out) self.assertEqual( out.getvalue(), u'Alice\t33\r\n' u'Bob\t44\r\n' u'Carol\t55\r\n' u'Dave\t66\r\n')
def test_optional_fields(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", None], [None, 66], ], keys=["name", "age"]) self.assertEqual(table.keys(), ["name", "age"]) name_field = table.field(0) self.assertEqual(name_field["type"], str) self.assertEqual(name_field["optional"], True) age_field = table.field(1) self.assertEqual(age_field["type"], int) self.assertEqual(age_field["optional"], True)
def test_simple_usage(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) self.assertEqual(table.keys(), ["name", "age"]) name_field = table.field(0) self.assertEqual(name_field["type"], str) self.assertEqual(name_field["optional"], False) age_field = table.field(1) self.assertEqual(age_field["type"], int) self.assertEqual(age_field["optional"], False)
def to_table(self): """ Consume and extract the entire result as a :class:`.Table` object. :return: the full query result """ return Table(self)
def test_write_with_newline_in_value(self): table = Table([ ["Alice\nSmith", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write(out) self.assertEqual( out.getvalue(), u' Alice | 33 \r\n' u' Smith | \r\n' u' Bob | 44 \r\n' u' Carol | 55 \r\n' u' Dave | 66 \r\n')
def test_write_csv_with_header_style(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write_csv(out, header={"fg": "cyan"}) self.assertEqual( out.getvalue(), u'name,age\r\n' u'Alice,33\r\n' u'Bob,44\r\n' u'Carol,55\r\n' u'Dave,66\r\n')
def test_missing_keys(self): with self.assertRaises(ValueError): _ = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ])
def test_write_with_style(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = StringIO() table.write(out, header={"fg": "red"}) self.assertEqual( out.getvalue(), u' name | age \r\n' u'-------|-----\r\n' u' Alice | 33 \r\n' u' Bob | 44 \r\n' u' Carol | 55 \r\n' u' Dave | 66 \r\n')
def config(self, **kwargs): result = self.graph.run("CALL dbms.listConfig") records = None last_category = None for record in result: name = record["name"] category, _, _ = name.partition(".") if category != last_category: if records is not None: Table(records, ["name", "value"]).write(auto_align=False, padding=0, separator=u" = ") self.echo(u"") records = [] records.append((name, record["value"])) last_category = category if records is not None: Table(records, ["name", "value"]).write(auto_align=False, padding=0, separator=u" = ")
def write_result(self, result, page_size=50): table = Table(result) table_size = len(table) for skip in range(0, table_size, page_size): self.result_writer(table, file=self.output_file, header={ "fg": "cyan", "bold": True }, skip=skip, limit=page_size) self.echo("\r\n", nl=False) return table_size
def test_repr(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) out = repr(table) self.assertEqual( out, u' name | age \r\n' u'-------|-----\r\n' u' Alice | 33 \r\n' u' Bob | 44 \r\n' u' Carol | 55 \r\n' u' Dave | 66 \r\n')
def kernel(self, **kwargs): result = self.graph.run( "CALL dbms.queryJmx", {"query": "org.neo4j:instance=kernel#0,name=Kernel"}) records = [] for record in result: attributes = record["attributes"] for key, value_dict in sorted(attributes.items()): value = value_dict["value"] if key.endswith("Date") or key.endswith("Time"): try: value = datetime.fromtimestamp(value / 1000).isoformat(" ") except: pass records.append((key, value)) Table(records, ["key", "value"]).write(auto_align=False, padding=0, separator=u" = ")
def test_fields_by_name_usage(self): table = Table([ ["Alice", 33], ["Bob", 44], ["Carol", 55], ["Dave", 66], ], keys=["name", "age"]) self.assertEqual(table.keys(), ["name", "age"]) name_field = table.field("name") self.assertEqual(name_field["type"], str) self.assertEqual(name_field["optional"], False) age_field = table.field("age") self.assertEqual(age_field["type"], int) self.assertEqual(age_field["optional"], False) with self.assertRaises(KeyError): _ = table.field("gender")