[rset] Add a line break the first result in repr of multiple rows rset
Improve readability of rset while debugging by aligning the first value with the
others if the rset have multiple row. This first line was usually not read as
it' was on the same line that the first part of the repr ("<rset <RQL>, (%i
rows) ...")
--- a/rset.py Mon Jun 07 18:50:24 2010 +0200
+++ b/rset.py Mon Jun 07 19:01:21 2010 +0200
@@ -76,10 +76,16 @@
rows = self.rows
if len(rows) > 10:
rows = rows[:10] + ['...']
+ if len(rows) > 1:
+ # add a line break before first entity if more that one.
+ pattern = '<resultset %r (%s rows):\n%s>'
+ else:
+ pattern = '<resultset %r (%s rows): %s>'
+
if not self.description:
- return '<resultset %r (%s rows): %s>' % (self.rql, len(self.rows),
+ return pattern % (self.rql, len(self.rows),
'\n'.join(str(r) for r in rows))
- return '<resultset %r (%s rows): %s>' % (self.rql, len(self.rows),
+ return pattern % (self.rql, len(self.rows),
'\n'.join('%s (%s)' % (r, d)
for r, d in zip(rows, self.description)))
--- a/test/unittest_rset.py Mon Jun 07 18:50:24 2010 +0200
+++ b/test/unittest_rset.py Mon Jun 07 19:01:21 2010 +0200
@@ -401,5 +401,19 @@
rset = self.execute('Any D, COUNT(U) GROUPBY D WHERE U is CWUser, U creation_date D')
self.assertEquals(rset.related_entity(0,0), (None, None))
+ def test_str(self):
+ rset = self.execute('(Any X,N WHERE X is CWGroup, X name N)')
+ self.assertIsInstance(str(rset), basestring)
+ self.assertEquals(len(str(rset).splitlines()), 1)
+
+ def test_repr(self):
+ rset = self.execute('(Any X,N WHERE X is CWGroup, X name N)')
+ self.assertIsInstance(repr(rset), basestring)
+ self.assertTrue(len(repr(rset).splitlines()) > 1)
+
+ rset = self.execute('(Any X WHERE X is CWGroup, X name "managers")')
+ self.assertIsInstance(str(rset), basestring)
+ self.assertEquals(len(str(rset).splitlines()), 1)
+
if __name__ == '__main__':
unittest_main()