Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__repr__ methods for classes TinyDB, Table and Query #229

Merged
merged 5 commits into from Sep 13, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -26,6 +26,7 @@ pip-log.txt
.coverage
.tox
nosetests.xml
.pytest_cache/

# Translations
*.mo
@@ -329,3 +329,10 @@ def test_orm_usage():
query2 = User.age.year == 2000
assert query1(data)
assert query2(data)


def test_repr():
Fruit = Query()

assert repr(Fruit) == "Query()"
assert repr(Fruit.type == 'peach') == "QueryImpl('==', ('type',), 'peach')"
@@ -1,4 +1,5 @@
import pytest
import re

from tinydb import where

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

with pytest.raises(AttributeError):
table.name = 'foo'


def test_table_repr(db):
name = 'table4'
table = db.table(name)

assert re.match(
r"<Table name=\'table4\', total=0, "
"storage=<tinydb\.database\.StorageProxy object at [a-z0-9]+>>",
repr(table))
@@ -1,5 +1,6 @@
# coding=utf-8
import sys
import re

import pytest

@@ -687,3 +688,12 @@ def _get_doc_id(self, document):
table.insert({'abc': 10})
assert table.get(doc_id='1')['abc'] == 10
assert table._last_id == 1


def test_repr(tmpdir):
path = str(tmpdir.join('db.json'))

assert re.match(
r"<TinyDB tables=\[u?\'_default\'\], tables_count=1, "
"default_table_documents_count=0, all_tables_documents_count=\[\'_default=0\'\]>",
repr(TinyDB(path)))
@@ -159,6 +159,17 @@ def __init__(self, *args, **kwargs):
self._table_cache = {}
self._table = self.table(default_table)

def __repr__(self):
args = [
'tables={}'.format(list(self.tables())),
'tables_count={}'.format(len(self.tables())),
'default_table_documents_count={}'.format(self.__len__()),
'all_tables_documents_count={}'.format(
['{}={}'.format(table, len(self.table(table))) for table in self.tables()]),
]

return '<{} {}>'.format(type(self).__name__, ', '.join(args))

def table(self, name=DEFAULT_TABLE, **options):
"""
Get access to a specific table.
@@ -276,6 +287,15 @@ def __init__(self, storage, name, cache_size=10):
data = self._read()
self._init_last_id(data)

def __repr__(self):
args = [
'name={!r}'.format(self.name),
'total={}'.format(self.__len__()),
'storage={}'.format(self._storage),
]

return '<{} {}>'.format(type(self).__name__, ', '.join(args))

def _init_last_id(self, data):
if data:
self._last_id = max(i for i in data)
@@ -107,6 +107,9 @@ class Query(object):
def __init__(self):
self._path = []

def __repr__(self):
return '{}()'.format(type(self).__name__)

def __getattr__(self, item):
query = Query()
query._path = self._path + [item]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.