def test_locked_dbr(isolation_level): with ext_db.transaction(isolation_level): Post.create(message='p2') other_db = database_initializer.get_database( 'sqlite', db_class=SqliteExtDatabase, timeout=0.1, use_speedups=False) res = other_db.execute_sql('select message from post') return res.fetchall()
def test_locked_dbr(isolation_level): with ext_db.granular_transaction(isolation_level): Post.create(message='p2') other_db = database_initializer.get_database( 'sqlite', db_class=SqliteExtDatabase, timeout=0.1, use_speedups=False) res = other_db.execute_sql('select message from post') return res.fetchall()
def setUp(self): super(TestInsertReturningModelAPI, self).setUp() self.db = database_initializer.get_database('postgres', PostgresqlDatabase) class BaseModel(TestModel): class Meta: database = self.db self.BaseModel = BaseModel self.models = []
def setUp(self): super(TestInsertReturningModelAPI, self).setUp() self.db = database_initializer.get_database( 'postgres', PostgresqlDatabase) class BaseModel(TestModel): class Meta: database = self.db self.BaseModel = BaseModel self.models = []
def test_good_and_bad_passphrases(self): things = ('t1', 't2', 't3') for thing in things: Thing.create(name=thing) # Try to open db with wrong passphrase secure = False bad_db = database_initializer.get_database( 'sqlcipher', passphrase='wrong passphrase') self.assertRaises(DatabaseError, bad_db.get_tables) # Assert that we can still access the data with the good passphrase. query = Thing.select().order_by(Thing.name) self.assertEqual([t.name for t in query], ['t1', 't2', 't3'])
import os import re from peewee import * from peewee import create_model_tables from peewee import drop_model_tables from peewee import mysql from peewee import print_ from playhouse.reflection import * from playhouse.tests.base import database_initializer from playhouse.tests.base import PeeweeTestCase sqlite_db = database_initializer.get_database('sqlite') DATABASES = [sqlite_db] if mysql: DATABASES.append(database_initializer.get_database('mysql')) try: import psycopg2 DATABASES.append(database_initializer.get_database('postgres')) except ImportError: pass class BaseModel(Model): class Meta: database = sqlite_db class ColTypes(BaseModel): f1 = BigIntegerField(index=True)
from peewee import * from peewee import print_ from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if from playhouse.tests.base import skip_unless # Use a disk-backed db since memory dbs only exist for a single connection and # we need to share the db w/2 for the locking tests. additionally, set the # sqlite_busy_timeout to 100ms so when we test locking it doesn't take forever ext_db = database_initializer.get_database('sqlite', c_extensions=False, db_class=SqliteExtDatabase, timeout=0.1, use_speedups=False) CLOSURE_EXTENSION = os.environ.get('CLOSURE_EXTENSION') FTS5_EXTENSION = FTS5Model.fts5_installed() # Test aggregate. class WeightedAverage(object): def __init__(self): self.total_weight = 0.0 self.total_ct = 0.0 def step(self, value, wt=None): wt = wt or 1.0
def test_kdf_iter(self): db = database_initializer.get_database('sqlcipher', kdf_iter=9999) self.assertRaises(ImproperlyConfigured, db.connect)
pass from peewee import * from peewee import print_ from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if from playhouse.tests.base import skip_unless # Use a disk-backed db since memory dbs only exist for a single connection and # we need to share the db w/2 for the locking tests. additionally, set the # sqlite_busy_timeout to 100ms so when we test locking it doesn't take forever ext_db = database_initializer.get_database( 'sqlite', db_class=SqliteExtDatabase, timeout=0.1) CLOSURE_EXTENSION = os.environ.get('CLOSURE_EXTENSION') FTS5_EXTENSION = FTS5Model.fts5_installed() # test aggregate. class WeightedAverage(object): def __init__(self): self.total_weight = 0.0 self.total_ct = 0.0 def step(self, value, wt=None): wt = wt or 1.0 self.total_weight += wt
import datetime import os try: from StringIO import StringIO except ImportError: from io import StringIO import sys from peewee import * from pwiz import * from playhouse.tests.base import database_initializer from playhouse.tests.base import mock from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if db = database_initializer.get_database('sqlite') class BaseModel(Model): class Meta: database = db class User(BaseModel): username = CharField(primary_key=True) id = IntegerField(default=0) class Note(BaseModel): user = ForeignKeyField(User) text = TextField(index=True)
try: import psycopg2 except ImportError: psycopg2 = None try: import MySQLdb as mysql except ImportError: try: import pymysql as mysql except ImportError: mysql = None if mysql: mysql_db = database_initializer.get_database('mysql') else: mysql_db = None if psycopg2: pg_db = database_initializer.get_database('postgres') else: pg_db = None sqlite_db = SqliteDatabase(':memory:') class Tag(Model): tag = CharField()
import datetime from hashlib import sha1 from peewee import DatabaseError from playhouse.sqlcipher_ext import * from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase db = database_initializer.get_database('sqlcipher') ext_db = database_initializer.get_database('sqlcipher_ext', passphrase='testing sqlcipher') class BaseModel(Model): class Meta: database = db class Thing(BaseModel): name = CharField() @ext_db.func('shazam') def shazam(s): return sha1(s or '').hexdigest()[:5] class ExtModel(Model): class Meta: database = ext_db
from playhouse.sqlcipher_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase db = database_initializer.get_database('sqlcipher') class BaseModel(Model): class Meta: database = db class Thing(BaseModel): name = CharField() class SqlCipherTestCase(ModelTestCase): requires = [Thing] def test_good_and_bad_passphrases(self): things = ('t1', 't2', 't3') for thing in things: Thing.create(name=thing) # Try to open db with wrong passphrase secure = False bad_db = database_initializer.get_database( 'sqlcipher', passphrase='wrong passphrase') self.assertRaises(DatabaseError, bad_db.get_tables)
try: import psycopg2 except ImportError: psycopg2 = None try: import MySQLdb as mysql except ImportError: try: import pymysql as mysql except ImportError: mysql = None if mysql: mysql_db = database_initializer.get_database("mysql") else: mysql_db = None if psycopg2: pg_db = database_initializer.get_database("postgres") else: pg_db = None sqlite_db = SqliteDatabase(":memory:") class Tag(Model): tag = CharField()
import datetime import unittest from peewee import * from playhouse.sqlite_ext import * from playhouse.sqlite_ext import _VirtualFieldMixin from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.libs import mock db = database_initializer.get_database( ':memory:', db_class=SqliteExtDatabase, c_extensions=True) import ipdb; ipdb.set_trace() try: db.connect() except OperationalError: raise ImportError('Unable to load `_sqlite_ext` C extension.') finally: db.close() class BaseModel(Model): class Meta: database = db class Note(BaseModel): content = TextField()
from peewee import prefetch from playhouse.postgres_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if from playhouse.tests.models import TestingID class TestPostgresqlExtDatabase(PostgresqlExtDatabase): insert_returning = False PYPY = 'PyPy' in sys.version test_db = database_initializer.get_database('postgres', db_class=TestPostgresqlExtDatabase) test_ss_db = database_initializer.get_database( 'postgres', db_class=TestPostgresqlExtDatabase, server_side_cursors=True, user='******') class BaseModel(Model): class Meta: database = test_db class Testing(BaseModel): name = CharField() data = HStoreField()
def test_passphrase_length(self): db = database_initializer.get_database('sqlcipher', passphrase='x') self.assertRaises(ImproperlyConfigured, db.connect)
try: import psycopg2 except ImportError: psycopg2 = None try: import MySQLdb as mysql except ImportError: try: import pymysql as mysql except ImportError: mysql = None if mysql: mysql_db = database_initializer.get_database('mysql') else: mysql_db = None if psycopg2: pg_db = database_initializer.get_database('postgres') else: pg_db = None sqlite_db = SqliteDatabase(':memory:') class Tag(Model): tag = CharField() class Person(Model): first_name = CharField()
import os import re from peewee import * from peewee import create_model_tables from peewee import drop_model_tables from peewee import mysql from peewee import print_ from playhouse.reflection import * from playhouse.tests.base import database_initializer from playhouse.tests.base import PeeweeTestCase sqlite_db = database_initializer.get_database("sqlite") DATABASES = [sqlite_db] if mysql: DATABASES.append(database_initializer.get_database("mysql")) try: import psycopg2 DATABASES.append(database_initializer.get_database("postgres")) except ImportError: pass class BaseModel(Model): class Meta: database = sqlite_db
self).execute_sql(query, *args, **kwargs) class Master(QueryLogDatabase): name = 'master' class Slave1(QueryLogDatabase): name = 'slave1' class Slave2(QueryLogDatabase): name = 'slave2' master = database_initializer.get_database('sqlite', db_class=Master) slave1 = database_initializer.get_database('sqlite', db_class=Slave1) slave2 = database_initializer.get_database('sqlite', db_class=Slave2) # Models to use for testing read slaves. class BaseModel(ReadSlaveModel): class Meta: database = master read_slaves = [slave1, slave2] class User(BaseModel): username = CharField()
import os import shutil from peewee import IntegrityError from playhouse.berkeleydb import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase database = database_initializer.get_database('berkeleydb') class BaseModel(Model): class Meta: database = database class Person(BaseModel): name = CharField(unique=True) class Message(BaseModel): person = ForeignKeyField(Person, related_name='messages') body = TextField() class TestBerkeleyDatabase(ModelTestCase): requires = [Person, Message] def setUp(self): self.remove_db_files() super(TestBerkeleyDatabase, self).setUp()
import os import shutil from peewee import IntegrityError from playhouse.berkeleydb import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import skip_unless database = database_initializer.get_database('berkeleydb') class BaseModel(Model): class Meta: database = database class Person(BaseModel): name = CharField(unique=True) class Message(BaseModel): person = ForeignKeyField(Person, related_name='messages') body = TextField() @skip_unless(BerkeleyDatabase.check_pysqlite) class TestBerkeleyDatabase(ModelTestCase): requires = [Person, Message] def setUp(self): self.remove_db_files() super(TestBerkeleyDatabase, self).setUp()
import os try: from StringIO import StringIO except ImportError: from io import StringIO import sys from peewee import * from pwiz import * from playhouse.tests.base import database_initializer from playhouse.tests.base import mock from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if db = database_initializer.get_database('sqlite') class BaseModel(Model): class Meta: database = db class User(BaseModel): username = CharField(primary_key=True) id = IntegerField(default=0) class Note(BaseModel): user = ForeignKeyField(User) text = TextField(index=True) data = IntegerField(default=0) misc = IntegerField(default=0)
self.counter += 1 return self.counter def _close(self, conn): self.closed_counter += 1 def transaction(self): return FakeTransaction(self) class TestDB(PooledDatabase, FakeDatabase): def __init__(self, *args, **kwargs): super(TestDB, self).__init__(*args, **kwargs) self.conn_key = lambda conn: conn pooled_db = database_initializer.get_database( 'postgres', db_class=PooledPostgresqlDatabase) normal_db = database_initializer.get_database('postgres') class Number(Model): value = IntegerField() class Meta: database = pooled_db class TestPooledDatabase(PeeweeTestCase): def setUp(self): super(TestPooledDatabase, self).setUp() self.db = TestDB('testing')
from peewee import * from peewee import print_ from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if from playhouse.tests.base import skip_unless # Use a disk-backed db since memory dbs only exist for a single connection and # we need to share the db w/2 for the locking tests. additionally, set the # sqlite_busy_timeout to 100ms so when we test locking it doesn't take forever ext_db = database_initializer.get_database( 'sqlite', c_extensions=False, db_class=SqliteExtDatabase, timeout=0.1, use_speedups=False) CLOSURE_EXTENSION = os.environ.get('CLOSURE_EXTENSION') FTS5_EXTENSION = FTS5Model.fts5_installed() # Test aggregate. class WeightedAverage(object): def __init__(self): self.total_weight = 0.0 self.total_ct = 0.0 def step(self, value, wt=None):
except AttributeError: pass from peewee import * from peewee import print_ from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if # Use a disk-backed db since memory dbs only exist for a single connection and # we need to share the db w/2 for the locking tests. additionally, set the # sqlite_busy_timeout to 100ms so when we test locking it doesn't take forever ext_db = database_initializer.get_database( 'sqlite', db_class=SqliteExtDatabase, timeout=0.1) CLOSURE_EXTENSION = os.environ.get('CLOSURE_EXTENSION') # test aggregate. class WeightedAverage(object): def __init__(self): self.total_weight = 0.0 self.total_ct = 0.0 def step(self, value, wt=None): wt = wt or 1.0 self.total_weight += wt self.total_ct += wt * value
def requires_cython(method): return skip_test_unless(lambda: cython_udf is not None)(method) def requires_vtfunc(testcase): return skip_unless(lambda: vtfunc is not None)(testcase) class UDFDatabase(SqliteExtDatabase): def _add_conn_hooks(self, conn): super(UDFDatabase, self)._add_conn_hooks(conn) register_all(conn) ext_db = database_initializer.get_database('sqlite', db_class=UDFDatabase) class BaseModel(Model): class Meta: database = ext_db class User(_User): class Meta: database = ext_db class APIResponse(BaseModel): url = TextField(default='') data = TextField(default='')
def requires_cython(method): return skip_test_unless(lambda: cython_udf is not None)(method) def requires_vtfunc(testcase): return skip_unless(lambda: vtfunc is not None)(testcase) class UDFDatabase(SqliteExtDatabase): def _add_conn_hooks(self, conn): super(UDFDatabase, self)._add_conn_hooks(conn) register_all(conn) ext_db = database_initializer.get_database( 'sqlite', db_class=UDFDatabase) class BaseModel(Model): class Meta: database = ext_db class User(_User): class Meta: database = ext_db class APIResponse(BaseModel): url = TextField(default='')
import os import re from peewee import * from peewee import create_model_tables from peewee import drop_model_tables from peewee import mysql from peewee import print_ from playhouse.reflection import * from playhouse.tests.base import database_initializer from playhouse.tests.base import PeeweeTestCase sqlite_db = database_initializer.get_database('sqlite') DATABASES = [sqlite_db] if mysql: DATABASES.append(database_initializer.get_database('mysql')) try: import psycopg2 DATABASES.append(database_initializer.get_database('postgres')) except ImportError: pass class BaseModel(Model): class Meta: database = sqlite_db class ColTypes(BaseModel):
from peewee import prefetch from playhouse.postgres_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase from playhouse.tests.base import PeeweeTestCase from playhouse.tests.base import skip_if from playhouse.tests.models import TestingID class TestPostgresqlExtDatabase(PostgresqlExtDatabase): insert_returning = False PYPY = 'PyPy' in sys.version test_db = database_initializer.get_database( 'postgres', db_class=TestPostgresqlExtDatabase) test_ss_db = database_initializer.get_database( 'postgres', db_class=TestPostgresqlExtDatabase, server_side_cursors=True, user='******') class BaseModel(Model): class Meta: database = test_db class Testing(BaseModel): name = CharField() data = HStoreField()
def execute_sql(self, query, *args, **kwargs): queries.append((self.name, query)) return super(QueryLogDatabase, self).execute_sql( query, *args, **kwargs) class Master(QueryLogDatabase): name = 'master' class Slave1(QueryLogDatabase): name = 'slave1' class Slave2(QueryLogDatabase): name = 'slave2' master = database_initializer.get_database('sqlite', db_class=Master) slave1 = database_initializer.get_database('sqlite', db_class=Slave1) slave2 = database_initializer.get_database('sqlite', db_class=Slave2) # Models to use for testing read slaves. class BaseModel(ReadSlaveModel): class Meta: database = master read_slaves = [slave1, slave2] class User(BaseModel): username = CharField() class Thing(BaseModel): name = CharField()
import datetime from hashlib import sha1 from peewee import DatabaseError from playhouse.sqlcipher_ext import * from playhouse.sqlite_ext import * from playhouse.tests.base import database_initializer from playhouse.tests.base import ModelTestCase db = database_initializer.get_database('sqlcipher') ext_db = database_initializer.get_database( 'sqlcipher_ext', passphrase='testing sqlcipher') class BaseModel(Model): class Meta: database = db class Thing(BaseModel): name = CharField() @ext_db.func('shazam') def shazam(s): return sha1(s or '').hexdigest()[:5] class ExtModel(Model): class Meta: database = ext_db