def test_nested_transaction_on_error(session):
    hook = SQLAlchemyTransactionHook(nested=True)
    resp = http.Response(content='test')

    txn = session.begin_nested()

    hook.on_request(session)
    puppy = PuppyModel()
    session.add(puppy)
    hook.on_error(resp)

    txn.rollback()
Example #2
0
def create_app(config=DefaultConfig):
    """Set-up the web API.

    :rtype: apistar.App
    """
    if inspect.isclass(config):
        config = config()  # To load configuration from environment variables

    database_url = f'{config.DATABASE_URL}'
    components = [SQLAlchemySessionComponent(url=database_url)]
    event_hooks = [SQLAlchemyTransactionHook()]

    return App(
        routes=routes,
        template_dir=str(TEMPLATES_DIR),
        components=components,
        event_hooks=event_hooks,
    )
Example #3
0
    name = validators.String()
    category = validators.String()
    description = validators.String()


# View
def get_products(session: Session) -> List[MyProductType]:
    return [MyProductType(product) for product in session.query(MyProduct).all()]



# Routes
routes = [
    Route('/api/', 'GET', get_products),
]


components = [
    SQLAlchemySessionComponent(url='postgresql://*****:*****@192.168.99.100/api_test'),
]

event_hooks = [
    SQLAlchemyTransactionHook(),
]

app = App(routes=routes, components=components, event_hooks=event_hooks)



if __name__ == '__main__':
    app.serve('127.0.0.1', 5000, debug=True)
Example #4
0
    input_type = PuppyInputType
    output_type = PuppyOutputType
    methods = ("create", "retrieve", "update", "delete", "list", "drop")

    @classmethod
    def list(
        cls, session: Session, page: http.QueryParam, page_size: http.QueryParam, name: http.QueryParam
    ) -> typing.List[PuppyOutputType]:
        return PageNumberResponse(page=page, page_size=page_size, content=cls._filter(session=session, name=name))


routes = [Include("/puppy", "puppy", PuppyResource.routes)]

sqlalchemy_component = SQLAlchemySessionComponent(url="sqlite://")
components = [sqlalchemy_component]
event_hooks = [SQLAlchemyTransactionHook()]

app = App(routes=routes, components=components, event_hooks=event_hooks)
async_app = ASyncApp(routes=routes, components=components, event_hooks=event_hooks)


class TestCaseSQLAlchemyCRUD:
    @pytest.fixture(scope="function", params=[app, async_app])
    def client(self, request):
        database.Base.metadata.create_all(sqlalchemy_component.engine)
        yield TestClient(request.param)
        database.Base.metadata.drop_all(sqlalchemy_component.engine)

    @pytest.fixture
    def puppy(self):
        return {"name": "canna"}