async def engine(pg_engine: Engine): # injects ... async with pg_engine.acquire() as conn: # a 'me' user user_id = await conn.scalar(users.insert().values(**random_user( name=USERNAME)).returning(users.c.id)) # has a project 'parent' await conn.execute(projects.insert().values( **random_project(prj_owner=user_id, name=PARENT_PROJECT_NAME))) yield pg_engine
async def user_id(pg_engine: Engine) -> int: async with pg_engine.acquire() as conn: # a 'me' user uid = await conn.scalar( users.insert().values(**(random_user())).returning(users.c.id)) yield uid # cleanup async with pg_engine.acquire() as conn: # a 'me' user uid = await conn.execute(users.delete().where(users.c.id == uid))
async def user(pg_engine: Engine) -> RowProxy: # some user async with pg_engine.acquire() as conn: result: Optional[ResultProxy] = await conn.execute( users.insert().values(**random_user( name=USERNAME)).returning(users)) assert result.rowcount == 1 _user: Optional[RowProxy] = await result.first() assert _user assert _user.name == USERNAME return _user
def user_db(postgres_db: sa.engine.Engine, user_id: PositiveInt) -> Dict: with postgres_db.connect() as con: result = con.execute(users.insert().values( id=user_id, name="test user", email="*****@*****.**", password_hash="testhash", status=UserStatus.ACTIVE, role=UserRole.USER, ).returning(literal_column("*"))) user = result.first() yield dict(user) con.execute(users.delete().where(users.c.id == user["id"]))
async def engine(loop, postgres_db: sa.engine.Engine): # pylint: disable=no-value-for-parameter async with aiopg.sa.create_engine(str(postgres_db.url)) as pg_engine: # injects TABLES ... async with pg_engine.acquire() as conn: # a 'me' user user_id = await conn.scalar(users.insert().values(**random_user( name=USERNAME)).returning(users.c.id)) # has a project 'parent' await conn.execute(projects.insert().values(**random_project( prj_owner=user_id, name=PARENT_PROJECT_NAME, uuid=PROJECT_UUID))) # has a project 'another' await conn.execute(projects.insert().values(**random_project( prj_owner=user_id, name="another", uuid=ANOTHER_UUID))) yield pg_engine
def user_id(postgres_db: sa.engine.Engine) -> Iterable[int]: # inject user in db # NOTE: Ideally this (and next fixture) should be done via webserver API but at this point # in time, the webserver service would bring more dependencies to other services # which would turn this test too complex. # pylint: disable=no-value-for-parameter stmt = users.insert().values(**random_user(name="test")).returning( users.c.id) print(str(stmt)) with postgres_db.connect() as conn: result = conn.execute(stmt) [usr_id] = result.fetchone() yield usr_id with postgres_db.connect() as conn: conn.execute(users.delete().where(users.c.id == usr_id))
def user_db(postgres_db: sa.engine.Engine, user_id: PositiveInt) -> Dict: with postgres_db.connect() as con: # removes all users before continuing con.execute(users.delete()) con.execute(users.insert().values( id=user_id, name="test user", email="*****@*****.**", password_hash="testhash", status=UserStatus.ACTIVE, role=UserRole.USER, ).returning(literal_column("*"))) # this is needed to get the primary_gid correctly result = con.execute(select([users]).where(users.c.id == user_id)) user = result.first() yield dict(user) con.execute(users.delete().where(users.c.id == user_id))
def creator(**user_kwargs) -> Dict[str, Any]: with postgres_db.connect() as con: # removes all users before continuing user_config = { "id": len(created_user_ids) + 1, "name": faker.name(), "email": faker.email(), "password_hash": faker.password(), "status": UserStatus.ACTIVE, "role": UserRole.USER, } user_config.update(user_kwargs) con.execute(users.insert().values(user_config).returning( sa.literal_column("*"))) # this is needed to get the primary_gid correctly result = con.execute( sa.select([users]).where(users.c.id == user_config["id"])) user = result.first() assert user created_user_ids.append(user["id"]) return dict(user)