from functools import wraps import logging logger = logging.getLogger('peewee') class _QueryLogHandler(logging.Handler): def __init__(self, *args, **kwargs): self.queries = [] logging.Handler.__init__(self, *args, **kwargs) def emit(self, record): # Counts all entries logged to the "peewee" logger by execute_sql(). if record.name == 'peewee': self.queries.append(record) class count_queries(object): def __init__(self, only_select=False): self.only_select = only_select self.count = 0 def get_queries(self): return self._handler.queries def __enter__(self): self._handler = _QueryLogHandler() logger.setLevel(logging.DEBUG) logger.addHandler(self._handler) return self def __exit__(self, exc_type, exc_val, exc_tb): logger.removeHandler(self._handler) if self.only_select: self.count = len([q for q in self._handler.queries if q.msg[0].startswith('SELECT ')]) else: self.count = len(self._handler.queries) class assert_query_count(count_queries): def __init__(self, expected, only_select=False): super(assert_query_count, self).__init__(only_select=only_select) self.expected = expected def __call__(self, f): @wraps(f) def decorated(*args, **kwds): with self: ret = f(*args, **kwds) self._assert_count() return ret return decorated def _assert_count(self): error_msg = '%s != %s' % (self.count, self.expected) assert self.count == self.expected, error_msg def __exit__(self, exc_type, exc_val, exc_tb): super(assert_query_count, self).__exit__(exc_type, exc_val, exc_tb) self._assert_count()
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
__init__.py | File | 0 B | 0644 |
|
apsw_ext.py | File | 4.82 KB | 0644 |
|
cockroachdb.py | File | 9 KB | 0644 |
|
dataset.py | File | 14.09 KB | 0644 |
|
db_url.py | File | 4.15 KB | 0644 |
|
fields.py | File | 1.66 KB | 0644 |
|
flask_utils.py | File | 8 KB | 0644 |
|
hybrid.py | File | 1.49 KB | 0644 |
|
kv.py | File | 5.48 KB | 0644 |
|
migrate.py | File | 30.11 KB | 0644 |
|
mysql_ext.py | File | 3.17 KB | 0644 |
|
pool.py | File | 11.21 KB | 0644 |
|
postgres_ext.py | File | 14.41 KB | 0644 |
|
psycopg3_ext.py | File | 1.15 KB | 0644 |
|
reflection.py | File | 30.2 KB | 0644 |
|
shortcuts.py | File | 11.25 KB | 0644 |
|
signals.py | File | 2.46 KB | 0644 |
|
sqlcipher_ext.py | File | 3.55 KB | 0644 |
|
sqlite_changelog.py | File | 4.68 KB | 0644 |
|
sqlite_ext.py | File | 45.65 KB | 0644 |
|
sqlite_udf.py | File | 13.34 KB | 0644 |
|
sqliteq.py | File | 9.75 KB | 0644 |
|
test_utils.py | File | 1.81 KB | 0644 |
|