def test_cleanup_challenges(db, socket): ChallengeFactory() ChallengeFactory(expire_at=datetime.utcnow() - timedelta(seconds=1)) svc = PlayService(db, socket) svc.cleanup_challenges() assert db.query(Challenge).count() == 1
def test_accept_expired_challenge(db, socket): challenge = ChallengeFactory(expire_at=datetime.utcnow() - timedelta(seconds=1)) svc = PlayService(db, socket, challenge.challengee) with pytest.raises(ChallengeExpiredError): svc.execute('accept_challenge', {'challenge_id': challenge.id})
def test_accept_ranked_challenge(db, socket): user = UserFactory(rating=1500) challenge = ChallengeFactory(is_ranked=True, owner=user) svc = PlayService(db, socket, challenge.challengee) svc.execute('accept_challenge', {'challenge_id': challenge.id}) game = db.query(Game).first() assert game.is_ranked
def test_cancel_challenge(db, socket): challenge = ChallengeFactory() svc = PlayService(db, socket, challenge.owner) socket.subscribe('challenges/' + str(challenge.owner_id)) socket.subscribe('challenges/' + str(challenge.challengee_id)) svc.execute('cancel_challenge', {'challenge_id': challenge.id}) assert db.query(Challenge).count() == 0 assert len(socket.sent_messages) == 2 assert socket.sent_messages[0]['method'] == 'challenges' assert socket.sent_messages[1]['method'] == 'challenges'
def test_challenge_again_replaces(db, socket): challenge = ChallengeFactory() other = ChallengeFactory(owner=challenge.owner, challengee=UserFactory()) svc = PlayService(db, socket, challenge.owner) svc.execute( 'challenge', { 'user_id': challenge.challengee_id, 'size': 19, 'handicap': 0, 'komi': 7.5, 'owner_is_black': True, 'speed': 'live', 'timing': 'fischer', 'maintime': 10, 'overtime': 20, 'overtime_count': 1, 'private': False }) assert db.query(Challenge).count() == 2 assert db.query(Challenge).first() == other
def test_accept_challenge_correspondence(db, socket, mails): challenge = ChallengeFactory(is_correspondence=True, owner__is_online=False, challengee__is_online=False) svc = PlayService(db, socket, challenge.challengee) svc.execute('accept_challenge', {'challenge_id': challenge.id}) game = db.query(Game).first() assert game.is_correspondence assert game.timing.capped assert len(mails) == 2 assert mails[0]['template'] == 'correspondence/challenge_started.txt' assert mails[1]['template'] == 'correspondence/challenge_started.txt' assert {mails[0]['to'], mails[1]['to'] } == {challenge.owner.email, challenge.challengee.email}