def aioSetUp(self): from aiorm import registry from aiorm.orm.dialect.postgresql import Dialect, CreateTableDialect registry.register(Dialect) registry.register(CreateTableDialect) yield from registry.connect('/sample')
def aiotest(): from aiorm import registry from aiorm.orm.query import statements as stmt driver.DummyCursor.return_many = [[range(3), range(10, 13)], ] yield from registry.connect('/sample') groups = yield from (stmt.Select(sample.Group) .join(sample.User) .left_join(sample.UserPreference, sample.User.id == sample.UserPreference.user_id) .where(Mock()).run() ) yield from registry.disconnect('sample') groups = [group for group in groups] self.assertEqual(len(groups), 2) self.assertEqual(groups[0].to_dict(), {'id': 1, 'created_at': 0, 'name': 2}) self.assertEqual(groups[1].to_dict(), {'id': 11, 'created_at': 10, 'name': 12})
def includeme(config): from aiorm.driver.postgresql.aiopg import Driver from aiorm.orm.dialect.postgresql import Dialect config.add_tween('pyramid_aiorm.transaction.tween_factory', under=EXCVIEW) settings = {key:val for key, val in config.get_settings().items() if key.startswith('aiorm.')} driver_cls = settings.pop('aiorm.driver', 'aiorm.driver.postgresql.aiopg') driver_cls = getattr(importlib.import_module(driver_cls), 'Driver') registry.register(driver_cls) dialect = settings.pop('aiorm.driver', 'aiorm.orm.dialect.postgresql') dialect = importlib.import_module(dialect) dialect_cls = getattr(dialect, 'Dialect') registry.register(dialect_cls) dialect_cls = getattr(dialect, 'CreateTableDialect') registry.register(dialect_cls) ignored = len('aiorm.db.') for key, url in settings.items(): if not key.startswith('aiorm.db.'): continue name = key[ignored:] yield from registry.connect(url, name=name) scanmods = aslist(settings['aiorm.scan'], flatten=True) for scanmod in scanmods: orm.scan(scanmod)
def aiotest(): from aiorm import registry from aiorm.orm.query import statements as stmt driver.DummyCursor.return_many = [ [range(3), range(10, 13)], ] yield from registry.connect('/sample') groups = yield from (stmt.Select(sample.Group).join( sample.User).left_join( sample.UserPreference, sample.User.id == sample.UserPreference.user_id).where( Mock()).run()) yield from registry.disconnect('sample') groups = [group for group in groups] self.assertEqual(len(groups), 2) self.assertEqual(groups[0].to_dict(), { 'id': 1, 'created_at': 0, 'name': 2 }) self.assertEqual(groups[1].to_dict(), { 'id': 11, 'created_at': 10, 'name': 12 })
def aiotest(): from aiorm import registry driver = yield from registry.connect("protocol://host/db", name="other") self.assertEqual(DummyDriver.last_url, "protocol://host/db") self.assertEqual(driver.database, "db") self.assertEqual(registry.get_driver("other"), driver) registry._drivers.pop("other") DummyDriver.last_url = None
def aiotest(): from aiorm import registry driver = yield from registry.connect('protocol://host/db', name='other') self.assertEqual(DummyDriver.last_url, 'protocol://host/db') self.assertEqual(driver.database, 'db') self.assertEqual(registry.get_driver('other'), driver) registry._drivers.pop('other') DummyDriver.last_url = None
def aiotest(): from aiorm import registry from aiorm.orm import Transaction yield from registry.connect('/sample') transaction = Transaction('sample') self.assertIsInstance(transaction.driver, driver.DummyDriver) self.assertEqual(transaction.driver.database, 'sample') yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm.query.schema import CreateTable dummy_dialect = dialect.DummyCreateTableDialect() yield from registry.connect('/sample') yield from CreateTable(sample.Group).run() dummy_dialect.render_sql.assert_called_once() # last query is a mock, we just ensure that a query has been # executed, not that the query is valid, this is a dialect test self.assertIsNotNone(driver.DummyCursor.last_query) yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm.query.schema import CreateTable dummy_dialect = dialect.DummyCreateTableDialect() yield from registry.connect('/sample') yield from CreateTable(sample.Group).run() dummy_dialect.render_create_table.assert_called_once_with(sample.Group) # last query is a mock, we just ensure that a query has been # executed, not that the query is valid, this is a dialect test self.assertIsNotNone(driver.DummyCursor.last_query) yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm.query import statements as stmt driver.DummyCursor.return_one = [range(3)] yield from registry.connect('/sample') group = yield from stmt.Get(sample.Group, 1).run() yield from registry.disconnect('sample') self.assertEqual(group.to_dict(), {'id': 1, 'created_at': 0, 'name': 2})
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') transaction = yield from Transaction('sample').begin() yield from transaction.execute('dummy_query') self.assertEqual(driver.DummyCursor.last_query, 'dummy_query') yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') driver.DummyCursor.return_one = ['one'] transaction = yield from Transaction('sample').begin() result = yield from transaction.fetchone() self.assertEqual(result, 'one') yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') driver.DummyCursor.return_many = [[('one',), ('two',)]] transaction = yield from Transaction('sample').begin() result = yield from transaction.fetchall() self.assertEqual(result, [('one',), ('two',)]) yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm.query.schema import CreateSchema dummy_dialect = dialect.DummyCreateTableDialect() yield from registry.connect('/sample') yield from CreateSchema('sample').run() dummy_dialect.render_sql.assert_called() # last query is a mock, we just ensure that at least, a query # has been executed, not that the query is valid, this is a # dialect test self.assertIsNotNone(driver.DummyCursor.last_query) yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') driver.DummyCursor.return_many = [[('one', ), ('two', )]] transaction = yield from Transaction('sample').begin() result = yield from transaction.fetchall() self.assertEqual(result, [('one', ), ('two', )]) yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm.query import statements as stmt driver.DummyCursor.return_one = [None] yield from registry.connect('/sample') groups = yield from stmt.Get(sample.Group, 1).where(Mock()).run() yield from registry.disconnect('sample') driver.DummyDriver.mock = driver.DummyCursor() self.assertIsNone(groups)
def aiotest(): from aiorm import registry from aiorm.orm.query import statements as stmt driver.DummyCursor.return_one = [range(3)] yield from registry.connect('/sample') group = yield from stmt.Get(sample.Group, 1).run() yield from registry.disconnect('sample') self.assertEqual(group.to_dict(), { 'id': 1, 'created_at': 0, 'name': 2 })
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') cursor = registry.get(interfaces.IDialect)() cursor.configure_mock(**{'render_rollback_transaction.return_value': 'rollback_transaction'}) transaction = yield from Transaction('sample').begin() yield from transaction.rollback() cursor.render_rollback_transaction.assert_called_once_with() self.assertEqual(driver.DummyCursor.last_query, 'rollback_transaction') yield from registry.disconnect('sample')
def aiotest(): from aiorm import registry from aiorm.orm import Transaction from aiorm.orm import interfaces yield from registry.connect('/sample') cursor = registry.get(interfaces.IDialect)() cursor.configure_mock( ** {'render_begin_transaction.return_value': 'begin_transaction'}) transaction = Transaction('sample') yield from transaction.begin() cursor.render_begin_transaction.assert_called_once_with() self.assertEqual(driver.DummyCursor.last_query, 'begin_transaction') yield from registry.disconnect('sample')