from functools import wraps import logging from peewee import create_model_tables from peewee import drop_model_tables logger = logging.getLogger('peewee') class test_database(object): def __init__(self, db, models, create_tables=True, drop_tables=True, fail_silently=False): if not isinstance(models, (list, tuple, set)): raise ValueError('%r must be a list or tuple.' % models) self.db = db self.models = models self.create_tables = create_tables self.drop_tables = drop_tables self.fail_silently = fail_silently def __enter__(self): self.orig = [] for m in self.models: self.orig.append(m._meta.database) m._meta.database = self.db if self.create_tables: create_model_tables(self.models, fail_silently=self.fail_silently) def __exit__(self, exc_type, exc_val, exc_tb): if self.create_tables and self.drop_tables: drop_model_tables(self.models, fail_silently=self.fail_silently) for i, m in enumerate(self.models): m._meta.database = self.orig[i] class _QueryLogHandler(logging.Handler): def __init__(self, *args, **kwargs): self.queries = [] logging.Handler.__init__(self, *args, **kwargs) def emit(self, record): 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 |
|
_speedups.cpython-38-x86_64-linux-gnu.so | File | 239.53 KB | 0755 |
|
_sqlite_ext.cpython-38-x86_64-linux-gnu.so | File | 72.59 KB | 0755 |
|
_sqlite_udf.cpython-38-x86_64-linux-gnu.so | File | 88.42 KB | 0755 |
|
apsw_ext.py | File | 4.94 KB | 0644 |
|
berkeleydb.py | File | 4.04 KB | 0644 |
|
csv_loader.py | File | 75 B | 0644 |
|
csv_utils.py | File | 11.34 KB | 0644 |
|
dataset.py | File | 10.74 KB | 0644 |
|
db_url.py | File | 3.8 KB | 0644 |
|
djpeewee.py | File | 7.72 KB | 0644 |
|
fields.py | File | 11.53 KB | 0644 |
|
flask_utils.py | File | 5.53 KB | 0644 |
|
gfk.py | File | 5.89 KB | 0644 |
|
hybrid.py | File | 1.44 KB | 0644 |
|
kv.py | File | 4.69 KB | 0644 |
|
migrate.py | File | 23.49 KB | 0644 |
|
pool.py | File | 8.01 KB | 0644 |
|
postgres_ext.py | File | 13.13 KB | 0644 |
|
read_slave.py | File | 1.42 KB | 0644 |
|
reflection.py | File | 21.33 KB | 0644 |
|
shortcuts.py | File | 7.03 KB | 0644 |
|
signals.py | File | 2.2 KB | 0644 |
|
sqlcipher_ext.py | File | 4.12 KB | 0644 |
|
sqlite_ext.py | File | 34.94 KB | 0644 |
|
sqlite_udf.py | File | 13.26 KB | 0644 |
|
sqliteq.py | File | 8.34 KB | 0644 |
|
test_utils.py | File | 2.73 KB | 0644 |
|