예제 #1
0
 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)
예제 #2
0
 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')
예제 #3
0
 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')
예제 #4
0
 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')
예제 #5
0
 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')
예제 #6
0
 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')
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
0
 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)
예제 #10
0
 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')
예제 #11
0
 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')
예제 #12
0
 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')
예제 #13
0
    def to_table(self):
        """ Consume and extract the entire result as a :class:`.Table`
        object.

        :return: the full query result
        """
        from py2neo.export import Table
        return Table(self)
예제 #14
0
 def test_missing_keys(self):
     with self.assertRaises(ValueError):
         _ = Table([
             ["Alice", 33],
             ["Bob", 44],
             ["Carol", 55],
             ["Dave", 66],
         ])
예제 #15
0
 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.write(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" = ")
예제 #16
0
 def write_result(self, result, page_size=50):
     table = Table(result)
     table_size = len(table)
     if self.verbosity >= 0:
         for skip in range(0, table_size, page_size):
             self.result_writer(table,
                                file=self.output_file,
                                header="cyan",
                                skip=skip,
                                limit=page_size)
             self.write("\r\n", end='')
     return table_size
예제 #17
0
 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')
예제 #18
0
 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" = ")
예제 #19
0
    def preview(self, limit=1):
        """ Construct a :class:`.Table` containing a preview of
        upcoming records, including no more than the given `limit`.

        :param limit: maximum number of records to include in the
            preview
        :returns: :class:`.Table` containing the previewed records
        """
        from py2neo.export import Table
        if limit < 0:
            raise ValueError("Illegal preview size")
        records = []
        if self._fields:
            for values in self._result.peek(int(limit)):
                if self._hydrant:
                    values = self._hydrant.hydrate_list(values)
                records.append(values)
            return Table(records, self._fields)
        else:
            return None
예제 #20
0
 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")