From 58db40d67c12cb4353a825d4aa215141a51b9e6e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 22 Jul 2019 17:05:51 -0700 Subject: [PATCH] Better __repr__ for tables --- docs/python-api.rst | 2 +- sqlite_utils/db.py | 5 ++++- tests/test_introspect.py | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index 43a9087..9444df9 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -571,7 +571,7 @@ Introspection If you have loaded an existing table, you can use introspection to find out more about it:: >>> db["PlantType"] - + The ``.count`` property shows the current number of rows (``select count(*) from table``):: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 39a7416..f4829ee 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -380,7 +380,10 @@ class Table: def __repr__(self): return "
".format( - self.name, " (does not exist yet)" if not self.exists else "" + self.name, + " (does not exist yet)" + if not self.exists + else " ({})".format(", ".join(c.name for c in self.columns)), ) @property diff --git a/tests/test_introspect.py b/tests/test_introspect.py index a49028c..0dd544e 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -57,8 +57,10 @@ def test_schema(existing_db): assert "CREATE TABLE foo (text TEXT)" == existing_db["foo"].schema -def test_table_repr(existing_db): - assert "
" == repr(existing_db["foo"]) +def test_table_repr(fresh_db): + table = fresh_db["dogs"].insert({"name": "Cleo", "age": 4}) + assert "
" == repr(table) + assert "
" == repr(fresh_db["cats"]) def test_indexes(fresh_db):