def test_create_session_read_only_mode(admin_flag, is_superuser, doom_calls, monkeypatch, pyramid_services): get = pretend.call_recorder(lambda *a: admin_flag) session_obj = pretend.stub(close=lambda: None, query=lambda *a: pretend.stub(get=get)) session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) connection = pretend.stub( connection=pretend.stub( get_transaction_status=lambda: pretend.stub(), set_session=lambda **kw: None, rollback=lambda: None, ), info={}, close=lambda: None, ) engine = pretend.stub(connect=pretend.call_recorder(lambda: connection)) request = pretend.stub( find_service=pyramid_services.find_service, registry={"sqlalchemy.engine": engine}, tm=pretend.stub(doom=pretend.call_recorder(lambda: None)), read_only=False, add_finished_callback=lambda callback: None, user=pretend.stub(is_superuser=is_superuser), ) assert _create_session(request) is session_obj assert get.calls == [pretend.call("read-only")] assert request.tm.doom.calls == doom_calls
def test_create_session(monkeypatch, predicates): session_obj = pretend.stub() session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) engine = pretend.stub() request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), matched_route=pretend.stub(predicates=predicates), add_finished_callback=pretend.call_recorder(lambda callback: None), ) request2 = pretend.stub( db=pretend.stub( close=pretend.call_recorder(lambda: None), ), ) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert session_cls.calls == [pretend.call(bind=engine)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ] assert request.add_finished_callback.calls == [pretend.call(mock.ANY)] request.add_finished_callback.calls[0].args[0](request2) assert request2.db.close.calls == [pretend.call()]
def test_create_session_read_only_mode( admin_flag, is_superuser, doom_calls, monkeypatch): get = pretend.call_recorder(lambda *a: admin_flag) session_obj = pretend.stub( close=lambda: None, query=lambda *a: pretend.stub(get=get), ) session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) connection = pretend.stub( connection=pretend.stub( get_transaction_status=lambda: pretend.stub(), set_session=lambda **kw: None, rollback=lambda: None, ), info={}, close=lambda: None, ) engine = pretend.stub(connect=pretend.call_recorder(lambda: connection)) request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(doom=pretend.call_recorder(lambda: None)), read_only=False, add_finished_callback=lambda callback: None, user=pretend.stub(is_superuser=is_superuser), ) assert _create_session(request) is session_obj assert get.calls == [pretend.call('read-only')] assert request.tm.doom.calls == doom_calls
def test_create_session(monkeypatch, predicates): session_obj = pretend.stub() session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) engine = pretend.stub() request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), matched_route=pretend.stub(predicates=predicates), add_finished_callback=pretend.call_recorder(lambda callback: None), ) request2 = pretend.stub(db=pretend.stub( close=pretend.call_recorder(lambda: None), ), ) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert session_cls.calls == [pretend.call(bind=engine)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ] assert request.add_finished_callback.calls == [pretend.call(mock.ANY)] request.add_finished_callback.calls[0].args[0](request2) assert request2.db.close.calls == [pretend.call()]
def test_creates_readonly_session(monkeypatch): session_obj = pretend.stub(execute=pretend.call_recorder(lambda sql: None)) session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) engine = pretend.stub() request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), matched_route=pretend.stub( predicates=[db.ReadOnlyPredicate(True, None)], ), add_finished_callback=pretend.call_recorder(lambda callback: None), ) request2 = pretend.stub(db=pretend.stub( close=pretend.call_recorder(lambda: None), ), ) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert session_cls.calls == [pretend.call(bind=engine)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ] assert session_obj.execute.calls == [ pretend.call(""" SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE """), ] assert request.add_finished_callback.calls == [pretend.call(mock.ANY)] request.add_finished_callback.calls[0].args[0](request2) assert request2.db.close.calls == [pretend.call()]
def test_raises_db_available_error(pyramid_services, metrics): def raiser(): raise OperationalError("foo", {}, psycopg2.OperationalError()) engine = pretend.stub(connect=raiser) request = pretend.stub( find_service=pyramid_services.find_service, registry={"sqlalchemy.engine": engine}, ) with pytest.raises(DatabaseNotAvailable): _create_session(request) assert metrics.increment.calls == [ pretend.call("warehouse.db.session.start"), pretend.call("warehouse.db.session.error", tags=["error_in:connecting"]), ]
def test_raises_db_available_error(pyramid_services, metrics): def raiser(): raise OperationalError("foo", {}, psycopg2.OperationalError()) engine = pretend.stub(connect=raiser) request = pretend.stub( find_service=pyramid_services.find_service, registry={"sqlalchemy.engine": engine}, ) with pytest.raises(DatabaseNotAvailableError): _create_session(request) assert metrics.increment.calls == [ pretend.call("warehouse.db.session.start"), pretend.call("warehouse.db.session.error", tags=["error_in:connecting"]), ]
def test_create_session(monkeypatch, read_only, tx_status): session_obj = pretend.stub( close=pretend.call_recorder(lambda: None), query=lambda *a: pretend.stub(get=lambda *a: None), ) session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) connection = pretend.stub( connection=pretend.stub( get_transaction_status=pretend.call_recorder(lambda: tx_status), set_session=pretend.call_recorder(lambda **kw: None), rollback=pretend.call_recorder(lambda: None), ), info={}, close=pretend.call_recorder(lambda: None), ) engine = pretend.stub(connect=pretend.call_recorder(lambda: connection)) request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), read_only=read_only, add_finished_callback=pretend.call_recorder(lambda callback: None), ) request2 = pretend.stub() register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert connection.connection.get_transaction_status.calls == [ pretend.call(), ] assert session_cls.calls == [pretend.call(bind=connection)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ] assert request.add_finished_callback.calls == [pretend.call(mock.ANY)] request.add_finished_callback.calls[0].args[0](request2) assert session_obj.close.calls == [pretend.call()] assert connection.close.calls == [pretend.call()] if read_only: assert connection.info == {"warehouse.needs_reset": True} assert connection.connection.set_session.calls == [ pretend.call( isolation_level="SERIALIZABLE", readonly=True, deferrable=True, ) ] if tx_status != psycopg2.extensions.TRANSACTION_STATUS_IDLE: connection.connection.rollback.calls == [pretend.call()]
def test_create_session(monkeypatch): session_obj = pretend.stub() session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "_Session", session_cls) engine = pretend.stub() request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), ) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert session_cls.calls == [pretend.call(bind=engine)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ]
def test_creates_readonly_session(monkeypatch): session_obj = pretend.stub(execute=pretend.call_recorder(lambda sql: None)) session_cls = pretend.call_recorder(lambda bind: session_obj) monkeypatch.setattr(db, "Session", session_cls) engine = pretend.stub() request = pretend.stub( registry={"sqlalchemy.engine": engine}, tm=pretend.stub(), matched_route=pretend.stub( predicates=[db.ReadOnlyPredicate(True, None)], ), add_finished_callback=pretend.call_recorder(lambda callback: None), ) request2 = pretend.stub( db=pretend.stub( close=pretend.call_recorder(lambda: None), ), ) register = pretend.call_recorder(lambda session, transaction_manager: None) monkeypatch.setattr(zope.sqlalchemy, "register", register) assert _create_session(request) is session_obj assert session_cls.calls == [pretend.call(bind=engine)] assert register.calls == [ pretend.call(session_obj, transaction_manager=request.tm), ] assert session_obj.execute.calls == [ pretend.call( """ SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE """ ), ] assert request.add_finished_callback.calls == [pretend.call(mock.ANY)] request.add_finished_callback.calls[0].args[0](request2) assert request2.db.close.calls == [pretend.call()]