Skip to content

Commit b0e60b2

Browse files
Xarvalusmsiemens
authored andcommitted
Feature: __repr__ methods for classes TinyDB, Table and Query (#229)
Closes #226 Closes #230
1 parent c3b35b1 commit b0e60b2

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pip-log.txt
2626
.coverage
2727
.tox
2828
nosetests.xml
29+
.pytest_cache/
2930

3031
# Translations
3132
*.mo

tests/test_queries.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,10 @@ def test_orm_usage():
329329
query2 = User.age.year == 2000
330330
assert query1(data)
331331
assert query2(data)
332+
333+
334+
def test_repr():
335+
Fruit = Query()
336+
337+
assert repr(Fruit) == "Query()"
338+
assert repr(Fruit.type == 'peach') == "QueryImpl('==', ('type',), 'peach')"

tests/test_tables.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import re
23

34
from tinydb import where
45

@@ -105,3 +106,13 @@ def test_table_name(db):
105106

106107
with pytest.raises(AttributeError):
107108
table.name = 'foo'
109+
110+
111+
def test_table_repr(db):
112+
name = 'table4'
113+
table = db.table(name)
114+
115+
assert re.match(
116+
r"<Table name=\'table4\', total=0, "
117+
"storage=<tinydb\.database\.StorageProxy object at [a-z0-9]+>>",
118+
repr(table))

tests/test_tinydb.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding=utf-8
22
import sys
3+
import re
34

45
import pytest
56

@@ -687,3 +688,12 @@ def _get_doc_id(self, document):
687688
table.insert({'abc': 10})
688689
assert table.get(doc_id='1')['abc'] == 10
689690
assert table._last_id == 1
691+
692+
693+
def test_repr(tmpdir):
694+
path = str(tmpdir.join('db.json'))
695+
696+
assert re.match(
697+
r"<TinyDB tables=\[u?\'_default\'\], tables_count=1, "
698+
"default_table_documents_count=0, all_tables_documents_count=\[\'_default=0\'\]>",
699+
repr(TinyDB(path)))

tinydb/database.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ def __init__(self, *args, **kwargs):
159159
self._table_cache = {}
160160
self._table = self.table(default_table)
161161

162+
def __repr__(self):
163+
args = [
164+
'tables={}'.format(list(self.tables())),
165+
'tables_count={}'.format(len(self.tables())),
166+
'default_table_documents_count={}'.format(self.__len__()),
167+
'all_tables_documents_count={}'.format(
168+
['{}={}'.format(table, len(self.table(table))) for table in self.tables()]),
169+
]
170+
171+
return '<{} {}>'.format(type(self).__name__, ', '.join(args))
172+
162173
def table(self, name=DEFAULT_TABLE, **options):
163174
"""
164175
Get access to a specific table.
@@ -276,6 +287,15 @@ def __init__(self, storage, name, cache_size=10):
276287
data = self._read()
277288
self._init_last_id(data)
278289

290+
def __repr__(self):
291+
args = [
292+
'name={!r}'.format(self.name),
293+
'total={}'.format(self.__len__()),
294+
'storage={}'.format(self._storage),
295+
]
296+
297+
return '<{} {}>'.format(type(self).__name__, ', '.join(args))
298+
279299
def _init_last_id(self, data):
280300
if data:
281301
self._last_id = max(i for i in data)

tinydb/queries.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class Query(object):
107107
def __init__(self):
108108
self._path = []
109109

110+
def __repr__(self):
111+
return '{}()'.format(type(self).__name__)
112+
110113
def __getattr__(self, item):
111114
query = Query()
112115
query._path = self._path + [item]

0 commit comments

Comments
 (0)