Python sqlalchemy.orm.scoping.scoped_session() Examples
The following are 30
code examples of sqlalchemy.orm.scoping.scoped_session().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.orm.scoping
, or try the search function
.
Example #1
Source File: crud_helpers.py From hydrus with MIT License | 7 votes |
def apply_filter(object_id: str, search_props: Dict[str, Any], triples: Graph, session: scoped_session) -> bool: """Check whether objects has properties with query values or not. :param object_id: Id of the instance. :param search_props: Dictionary of query parameters with property id and values. :param triples: All triples. :param session: sqlalchemy scoped session. :return: True if the instance has properties with given values, False otherwise. """ for prop in search_props: # For nested properties if isinstance(search_props[prop], dict): data = session.query(triples).filter( triples.GraphIII.subject == object_id, triples.GraphIII.predicate == prop).one() if apply_filter(data.object_, search_props[prop], triples, session) is False: return False else: data = session.query(triples).filter( triples.GraphIIT.subject == object_id, triples.GraphIIT.predicate == prop).one() terminal = session.query(Terminal).filter( Terminal.id == data.object_).one() if terminal.value != search_props[prop]: return False return True
Example #2
Source File: api_test.py From nekoyume with MIT License | 6 votes |
def test_post_node_status_not_200(fx_test_client: FlaskClient, fx_session: scoped_session, code: int): url = 'http://test.neko' assert not fx_session.query(Node).first() with Mocker() as m: m.get(url + '/ping', text='pong', status_code=code) res = fx_test_client.post( '/nodes', data=json.dumps({'url': url}), content_type='application/json' ) assert res.status_code == 403 data = json.loads(res.get_data()) assert data['result'] == 'failed' assert data['message'] == f'Connection to node {url} was failed.' assert not fx_session.query(Node).filter( Node.url == url ).first()
Example #3
Source File: api_test.py From nekoyume with MIT License | 6 votes |
def test_post_block_return_block_id(fx_test_client: FlaskClient, fx_user: User, fx_session: scoped_session): block = Block.create(fx_user, []) fx_session.add(block) fx_session.commit() block2 = Block.create(fx_user, []) des = block2.serialize(use_bencode=False, include_suffix=True, include_moves=True, include_hash=True) des['id'] = 3 resp = fx_test_client.post('/blocks', data=json.dumps(des), content_type='application/json') assert resp.status_code == 403 data = json.loads(resp.get_data()) assert data['result'] == 'failed' assert data['message'] == "new block isn't our next block." assert data['block_id'] == 2
Example #4
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_block_my_node(fx_session: scoped_session, fx_user: User): block = Block.create(fx_user, []) url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.flush() with Mocker() as m: m.post('http://test.neko/blocks', text='success') expected = serialized = block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ) multicast(serialized=serialized, my_node=node, broadcast=broadcast_block) expected['sent_node'] = url assert node.last_connected_at > now assert node.last_connected_at > now # check request.json value assert m.request_history[0].json() == expected
Example #5
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_block_same_node(fx_session: scoped_session, fx_user: User): block = Block.create(fx_user, []) url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.flush() multicast( serialized=block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ), sent_node=node, broadcast=broadcast_block, ) assert node.last_connected_at == now
Example #6
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_block_raise_exception( fx_session: scoped_session, fx_user: User, error: typing.Union[ConnectionError, Timeout] ): block = Block.create(fx_user, []) url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.flush() with Mocker() as m: m.post('http://test.neko/blocks', exc=error) multicast( serialized=block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ), broadcast=broadcast_block, ) assert node.last_connected_at == now
Example #7
Source File: api_test.py From nekoyume with MIT License | 6 votes |
def test_post_node(fx_test_client: FlaskClient, fx_session: scoped_session): url = 'http://test.neko' assert not fx_session.query(Node).first() with Mocker() as m: m.get(url + '/ping', text='pong') res = fx_test_client.post( '/nodes', data=json.dumps({'url': url}), content_type='application/json' ) assert res.status_code == 200 assert json.loads(res.get_data())['result'] == 'success' node = fx_session.query(Node).filter( Node.url == url ).first() assert node assert node.last_connected_at
Example #8
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_move( fx_server: WSGIServer, fx_session: scoped_session, fx_other_server: WSGIServer, fx_other_session: Session, fx_user: User, fx_novice_status: typing.Mapping[str, str], ): now = datetime.datetime.utcnow() node = Node(url=fx_server.url, last_connected_at=now) node2 = Node(url=fx_other_server.url, last_connected_at=datetime.datetime.utcnow()) move = fx_user.create_novice(fx_novice_status) fx_session.add_all([node, node2, move]) fx_session.commit() assert not fx_other_session.query(Move).get(move.id) serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) multicast(serialized=serialized, broadcast=broadcast_move) assert fx_other_session.query(Move).get(move.id) assert node.last_connected_at > now
Example #9
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_move_my_node(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str]): url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) move = fx_user.create_novice(fx_novice_status) fx_session.add_all([node, move]) fx_session.commit() with Mocker() as m: m.post('http://test.neko/moves', json={'result': 'success'}) expected = serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) multicast(serialized=serialized, my_node=node, broadcast=broadcast_move) expected['sent_node'] = 'http://test.neko' assert node.last_connected_at > now # check request.json value assert m.request_history[0].json() == expected
Example #10
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def broadcast_move_failed(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str], error): now = datetime.datetime.utcnow() move = fx_user.create_novice(fx_novice_status) node = Node(url='http://test.neko', last_connected_at=now) fx_session.add_all([node, move]) fx_session.commit() with Mocker() as m: serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) m.post('http://test.neko', exc=error) multicast(serialized=serialized, broadcast=broadcast_move) assert node.last_connected_at == now
Example #11
Source File: api_test.py From nekoyume with MIT License | 6 votes |
def test_post_node_connection_error(fx_test_client: FlaskClient, fx_session: scoped_session): url = 'http://test.neko' assert not fx_session.query(Node).first() with Mocker() as m: m.get(url + '/ping', exc=ConnectionError) res = fx_test_client.post( '/nodes', data=json.dumps({'url': url}), content_type='application/json' ) assert res.status_code == 403 data = json.loads(res.get_data()) assert data['result'] == 'failed' assert data['message'] == f'Connection to node {url} was failed.' assert not fx_session.query(Node).filter( Node.url == url ).first()
Example #12
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def broadcast_node_failed(fx_session: scoped_session, fx_other_session: Session, error): now = datetime.datetime.utcnow() node = Node(url='http://test.neko', last_connected_at=now) node2 = Node(url='http://other.neko', last_connected_at=datetime.datetime.utcnow()) fx_session.add(node) fx_session.commit() fx_other_session.add(node2) fx_other_session.commit() assert not fx_session.query(Node).filter(Node.url == node2.url).first() with Mocker() as m: m.post('http://test.neko', exc=error) multicast(serialized={'url': fx_other_server.url}, broadcast=broadcast_node) assert not fx_session.query(Node).filter(Node.url == node2.url).first() assert node.last_connected_at == now
Example #13
Source File: crud.py From hydrus with MIT License | 6 votes |
def delete_single(type_: str, session: scoped_session) -> None: """Delete instance of classes with single objects. :param type_: type of object to be deleted :param session: sqlalchemy scoped session :return: None Raises: ClassNotFound: If `type_` does not represt a valid/defined RDFClass. InstanceNotFound: If no Instance of the class exists. """ try: rdf_class = session.query(RDFClass).filter( RDFClass.name == type_).one() except NoResultFound: raise ClassNotFound(type_=type_) try: instance = session.query(Instance).filter( Instance.type_ == rdf_class.id).all()[-1] except (NoResultFound, IndexError, ValueError): raise InstanceNotFound(type_=rdf_class.name) return delete(instance.id, type_, session=session)
Example #14
Source File: pkcs11_kek_rewrap.py From sgx-kms with Apache License 2.0 | 6 votes |
def __init__(self, conf): self.dry_run = False self.db_engine = sqlalchemy.create_engine(conf.sql_connection) self._session_creator = scoping.scoped_session( orm.sessionmaker( bind=self.db_engine, autocommit=True ) ) self.crypto_plugin = p11_crypto.P11CryptoPlugin(conf) self.pkcs11 = self.crypto_plugin.pkcs11 self.plugin_name = utils.generate_fullname_for(self.crypto_plugin) self.hsm_session = self.pkcs11.get_session() self.new_mkek_label = self.crypto_plugin.mkek_label self.new_hmac_label = self.crypto_plugin.hmac_label self.new_mkek = self.crypto_plugin._get_master_key(self.new_mkek_label) self.new_mkhk = self.crypto_plugin._get_master_key(self.new_hmac_label)
Example #15
Source File: pkcs11_kek_rewrap.py From barbican with Apache License 2.0 | 6 votes |
def __init__(self, conf): self.dry_run = False self.db_engine = session.create_engine(conf.sql_connection) self._session_creator = scoping.scoped_session( orm.sessionmaker( bind=self.db_engine, autocommit=True ) ) self.crypto_plugin = p11_crypto.P11CryptoPlugin(conf) self.pkcs11 = self.crypto_plugin.pkcs11 self.plugin_name = utils.generate_fullname_for(self.crypto_plugin) self.hsm_session = self.pkcs11.get_session() self.new_mkek_label = self.crypto_plugin.mkek_label self.new_hmac_label = self.crypto_plugin.hmac_label self.new_mkek_type = self.crypto_plugin.mkek_key_type self.new_hmac_type = self.crypto_plugin.hmac_key_type self.new_mkek = self.crypto_plugin._get_master_key( self.new_mkek_type, self.new_mkek_label) self.new_mkhk = self.crypto_plugin._get_master_key( self.new_hmac_type, self.new_hmac_label)
Example #16
Source File: broadcast_test.py From nekoyume with MIT License | 6 votes |
def test_broadcast_node( fx_server: WSGIServer, fx_session: scoped_session, fx_other_server: WSGIServer, fx_other_session: Session, ): now = datetime.datetime.utcnow() node = Node(url=fx_server.url, last_connected_at=now) node2 = Node(url=fx_other_server.url, last_connected_at=datetime.datetime.utcnow()) fx_session.add(node) fx_session.commit() fx_other_session.add(node2) fx_other_session.commit() assert not fx_session.query(Node).filter(Node.url == node2.url).first() multicast(serialized={'url': fx_other_server.url}, broadcast=broadcast_node) assert fx_session.query(Node).filter(Node.url == node2.url).first() assert node.last_connected_at > now
Example #17
Source File: game_test.py From nekoyume with MIT License | 5 votes |
def test_post_move_broadcasting( fx_test_client: FlaskClient, fx_user: User, fx_private_key: PrivateKey, fx_session: scoped_session, ): with unittest.mock.patch('nekoyume.game.multicast') as m: fx_test_client.post('/login', data={ 'private_key': fx_private_key.to_hex(), 'name': 'test_user', }, follow_redirects=True) fx_test_client.post('/new') Block.create(fx_user, fx_session.query(Move).filter_by(block_id=None).all()) assert not get_unconfirmed_move(fx_user.address) res = fx_test_client.post('/session_moves', data={ 'name': 'hack_and_slash' }) assert res.status_code == 302 move = fx_session.query(Move).filter( Move.name == 'hack_and_slash', ).first() assert move serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) assert m.called args = m.call_args[1] assert serialized == args['serialized'] my_node = args['my_node'] assert isinstance(my_node, Node) assert my_node.url == 'http://localhost' broadcast = args['broadcast'] assert isinstance(broadcast, typing.Callable) assert broadcast.__name__ == 'broadcast_move'
Example #18
Source File: broadcast_test.py From nekoyume with MIT License | 5 votes |
def test_broadcast_my_node(fx_session: scoped_session): url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.commit() with Mocker() as m: m.post('http://test.neko/nodes', json={'result': 'success'}) multicast({'url': url}, my_node=node, broadcast=broadcast_node) assert node.last_connected_at > now # check request.json value assert m.request_history[0].json() == { 'url': 'http://test.neko', 'sent_node': 'http://test.neko' }
Example #19
Source File: broadcast_test.py From nekoyume with MIT License | 5 votes |
def test_broadcast_node_same_url(fx_session: scoped_session): url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.commit() with Mocker() as m: multicast(serialized={'url': url}, sent_node=node, broadcast=broadcast_node) assert not m.called assert node.last_connected_at == now
Example #20
Source File: block_test.py From nekoyume with MIT License | 5 votes |
def test_sync_node_unavailable_on_get_blocks( fx_user: User, fx_session: scoped_session, fx_server: WSGIServer, fx_other_session: Session, fx_novice_status: typing.Mapping[str, str], code: int ): move = fx_user.create_novice(fx_novice_status) block = Block.create(fx_user, [move]) Block.sync(Node(url=fx_server.url), fx_other_session) serialized = block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ) serialized['id'] = block.id + 1 with Mocker() as m: m.register_uri( 'GET', f'{fx_server.url}/blocks/last', json={'block': serialized}, status_code=200, ) m.register_uri( 'GET', f'{fx_server.url}/blocks/1', json={'block': serialized}, status_code=200, ) m.get(url=f'{fx_server.url}/blocks', status_code=code) Block.sync(Node(url=fx_server.url), fx_other_session) assert not fx_other_session.query(Block).get(serialized['id'])
Example #21
Source File: broadcast_test.py From nekoyume with MIT License | 5 votes |
def test_broadcast_block_retry( fx_session: scoped_session, fx_user: User, limit: int, blocks: int, expected: int ): for i in range(blocks): block = Block.create(fx_user, []) url = 'http://test.neko' now = datetime.datetime.utcnow() node = Node(url=url, last_connected_at=now) fx_session.add(node) fx_session.flush() patch = unittest.mock.patch('nekoyume.broadcast.BROADCAST_LIMIT', limit) with mock() as m, patch: m.register_uri('POST', 'http://test.neko/blocks', [ { 'json': { 'result': 'failed', 'block_id': 0, 'mesage': "new block isn't our next block." }, 'status_code': 403 }, { 'json': { 'result': 'success', }, 'status_code': 200 } ]) multicast( serialized=block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ), broadcast=broadcast_block, ) assert m.call_count == expected assert node.last_connected_at > now
Example #22
Source File: tasks_test.py From nekoyume with MIT License | 5 votes |
def test_block_broadcast(fx_session: scoped_session, fx_user: User): block = Block.create(fx_user, []) fx_session.add(block) fx_session.commit() with unittest.mock.patch('nekoyume.tasks.multicast') as m: block_broadcast(block.id, 'http://localhost:5000', 'http://localhost:5001', session=fx_session) serialized = block.serialize( use_bencode=False, include_suffix=True, include_moves=True, include_hash=True ) assert m.called args = m.call_args[1] assert serialized == args['serialized'] assert isinstance(args['sent_node'], Node) assert args['sent_node'].url == 'http://localhost:5000' assert isinstance(args['my_node'], Node) assert args['my_node'].url == 'http://localhost:5001' broadcast = args['broadcast'] assert isinstance(broadcast, typing.Callable) assert broadcast.__name__ == 'broadcast_block'
Example #23
Source File: tasks_test.py From nekoyume with MIT License | 5 votes |
def test_block_broadcast_no_block(fx_session: scoped_session): with unittest.mock.patch('nekoyume.tasks.multicast') as m: block_broadcast(0, 'http://localhost:5000', 'http://localhost:5001', session=fx_session) assert not m.called
Example #24
Source File: tasks_test.py From nekoyume with MIT License | 5 votes |
def test_move_broadcast(fx_session: scoped_session, fx_user: User, fx_novice_status: typing.Mapping[str, str]): move = fx_user.create_novice(fx_novice_status) fx_session.add(move) fx_session.commit() with unittest.mock.patch('nekoyume.tasks.multicast') as m: move_broadcast(move.id, 'http://localhost:5000', 'http://localhost:5001', session=fx_session) serialized = move.serialize( use_bencode=False, include_signature=True, include_id=True, ) assert m.called args = m.call_args[1] assert serialized == args['serialized'] assert isinstance(args['sent_node'], Node) assert args['sent_node'].url == 'http://localhost:5000' assert isinstance(args['my_node'], Node) assert args['my_node'].url == 'http://localhost:5001' broadcast = args['broadcast'] assert isinstance(broadcast, typing.Callable) assert broadcast.__name__ == 'broadcast_move'
Example #25
Source File: pkcs11_migrate_kek_signatures.py From sgx-kms with Apache License 2.0 | 5 votes |
def __init__(self, db_connection, library_path, login, slot_id): self.dry_run = False self.db_engine = sqlalchemy.create_engine(db_connection) self._session_creator = scoping.scoped_session( orm.sessionmaker( bind=self.db_engine, autocommit=True ) ) self.crypto_plugin = p11_crypto.P11CryptoPlugin(CONF) self.plugin_name = utils.generate_fullname_for(self.crypto_plugin) self.pkcs11 = self.crypto_plugin.pkcs11 self.session = self.pkcs11.get_session()
Example #26
Source File: pkcs11_migrate_kek_signatures.py From barbican with Apache License 2.0 | 5 votes |
def __init__(self, db_connection, library_path, login, slot_id): self.dry_run = False self.db_engine = session.create_engine(db_connection) self._session_creator = scoping.scoped_session( orm.sessionmaker( bind=self.db_engine, autocommit=True ) ) self.crypto_plugin = p11_crypto.P11CryptoPlugin(CONF) self.plugin_name = utils.generate_fullname_for(self.crypto_plugin) self.pkcs11 = self.crypto_plugin.pkcs11 self.session = self.pkcs11.get_session()
Example #27
Source File: SqliteDbAdapter.py From legion with GNU General Public License v3.0 | 5 votes |
def establishSqliteConnection(self, dbFileName: str): self.name = dbFileName self.dbsemaphore = QSemaphore(1) # to control concurrent write access to db self.engine = create_engine( 'sqlite:///{dbFileName}'.format(dbFileName=dbFileName)) self.session = scoped_session(sessionmaker()) self.session.configure(bind=self.engine, autoflush=False) self.metadata = self.base.metadata self.metadata.create_all(self.engine) self.metadata.echo = True self.metadata.bind = self.engine self.log.info(f"Established SQLite connection on file '{dbFileName}'")
Example #28
Source File: objstore_factory.py From seafobj with Apache License 2.0 | 5 votes |
def get_repo_storage_id(repo_id): if repo_id in repo_storage_id: return repo_storage_id[repo_id] else: from .db import Base from sqlalchemy.orm.scoping import scoped_session RepoStorageId = Base.classes.RepoStorageId storage_id = None session = scoped_session(objstore_factory.session) q = session.query(RepoStorageId).filter(RepoStorageId.repo_id==repo_id) r = q.first() storage_id = r.storage_id if r else None repo_storage_id[repo_id] = storage_id session.remove() return storage_id
Example #29
Source File: db.py From build-relengapi with Mozilla Public License 2.0 | 5 votes |
def session(self, dbname): # set up a session for each db; this uses scoped_session (based on the # thread ID) to ensure only one session per thread if dbname not in self._sessions: Session = orm.sessionmaker(bind=self.engine(dbname)) self._sessions[dbname] = scoping.scoped_session(Session) return self._sessions[dbname]
Example #30
Source File: crud.py From hydrus with MIT License | 5 votes |
def insert_iit(object_: Dict[str, Any], prop_name: str, instance: Instance, property_: BaseProperty, session: scoped_session) -> GraphIIT: """ Insert a GraphIIT triple in the database. :param object_: Object body. :param prop_name: Property name. :param instance: Instance for the newly added object. :param property_: Predicate in the new triple being inserted. :param session: sqlalchemy session. :return: GraphIIT triple. :raises: NotInstanceProperty """ terminal = Terminal(value=object_[prop_name]) session.add(terminal) session.flush() # Assigns ID without committing if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE": property_.type_ = "INSTANCE" session.add(property_) triple = GraphIIT( subject=instance.id, predicate=property_.id, object_=terminal.id) # Add things directly to session, if anything fails whole # transaction is aborted session.add(triple) return triple else: session.close() raise NotInstanceProperty(type_=prop_name)