def test_player(users):
    with session_scope() as sesh:
        players = sesh.query(Player).all()
        assert players[0].id == 1
        assert players[0].name == 'foo'
        assert players[1].id == 2
        assert players[1].name == 'bar'

    with pytest.raises(IntegrityError):
        with session_scope() as sesh:
            sesh.add(Player('foo'))
def test_setup(db):
    with session_scope() as sesh:
        assert sesh.execute("SELECT EXISTS ( "
                            "SELECT 1 "
                            "FROM   information_schema.tables "
                            f"WHERE  table_name = '{Time.__tablename__}' "
                            ");").fetchone() == (1, )
def test_time(users):
    with session_scope() as sesh:
        time = Time(player_id=1, time=34.5, game='easy4')
        assert dt.datetime.now(
            tz=gettz('America/New_York')).date() == time.date
        sesh.add(time)

    with session_scope() as sesh:
        time = sesh.query(Time).one()
        assert time.player_id == 1
        assert time.time == 34.5
        assert time.game == 'easy4'

    with pytest.raises(IntegrityError):
        with session_scope() as sesh:
            time = Time(player_id=1, time=25, game='easy4')
            sesh.add(time)
Exemple #4
0
async def player(request):
    try:
        with session_scope() as sesh:
            name = request.json['name']
            player = Player(name=name)
            sesh.add(player)
    except IntegrityError as e:
        raise ResourceExists(f'player {name} already registered')
    return text(f'player {name} now registered')
Exemple #5
0
def model():
    with session_scope() as sesh:
        m_model = sesh.query(Model).get(request.args['id'])
    return jsonify({
        'id': m_model.id,
        'time_created': m_model.time_created,
        'foo': m_model.foo,
        'bar': m_model.bar
    })
Exemple #6
0
def test_table_exists(db):
    with session_scope() as sesh:
        assert sesh.execute(
            "SELECT EXISTS ( "
            "SELECT 1 "
            "FROM   information_schema.tables "
            f"WHERE table_schema = '{postgres_schema}' "
            f"AND table_name = '{Model.__tablename__}' "
            ");"
        ).fetchone() == (1,)
Exemple #7
0
async def winner(request):
    with session_scope() as sesh:
        with open('src/query/winner.sql', 'r') as f:
            winners = list(
                sesh.execute(
                    f.read(), {
                        'date': dt.datetime.now(tz=nyt_tz).date(),
                        'game': request.args['game'][0]
                    }))
    winners = [
        dict(zip(('player', 'game', 'date', 'time'), w)) for w in winners
    ]
    for w in winners:
        w['date'] = w['date'].strftime('%Y-%m-%d')
    return json(winners)
Exemple #8
0
async def time(request):
    try:
        with session_scope() as sesh:
            try:
                player = sesh.query(Player).filter(
                    Player.name == request.json['player']).one()
            except KeyError as e:
                raise InvalidUsage(
                    'player parameter must be passed in request')
            except NoResultFound as e:
                raise InvalidUsage(
                    f'{request.json["player"]} is not a registered player')
            try:
                time = Time(player_id=player.id,
                            time=float(request.json['time']),
                            game=request.json['game'])
            except InvalidParameter as e:
                raise InvalidUsage(e)
            sesh.add(time)
    except IntegrityError as e:
        raise ResourceExists('time already posted')
    return text('time posted')
Exemple #9
0
def populate_db(model: t.Optional[dict] = None):
    if not model:
        model = _model
    with session_scope() as sesh:
        sesh.add(Model(foo=model["foo"], bar=model["bar"]))
Exemple #10
0
def users(db):
    with session_scope() as sesh:
        sesh.add(Player(name='foo'))
        sesh.add(Player(name='bar'))
    yield
Exemple #11
0
async def players(request):
    with session_scope() as sesh:
        players = sesh.query(Player.name).all()
        return json([p[0] for p in players])
Exemple #12
0
async def healthz(request):
    """Check if service is up.
    """
    with session_scope() as sesh:
        sesh.execute('SELECT 1')
    return json({'healthy': True})
def test_session_scope():
    with session_scope() as session:
        session.connection()