def start_participant(self, user, duration, password=None, ): if self.course \ and self.course.require_password() \ and password != self.course.password: raise StatementPasswordIsWrong if self.participants.filter(Participant.user_id == user.id).count(): raise StatementCanOnlyStartOnce if user.get_active_participant(): raise StatementOnlyOneOngoing new_participant = Participant( user_id=user.id, statement_id=self.id, start=int(time.time()), duration=duration, ) db.session.add(new_participant) return new_participant
def test_only_one_ongoing(self): participant = Participant( user_id=self.user.id, statement_id=self.statements[0].id, start=self.now - self.duration, duration=self.duration * 2, ) db.session.add(participant) assert_that( calling(self.statement.start_participant).with_args( user=self.user, duration=self.duration, ), raises(StatementOnlyOneOngoing))
def test_can_only_start_once(self): participant = Participant( user_id=self.user.id, statement_id=self.statement.id, start=self.now - self.duration, duration=self.duration, ) db.session.add(participant) assert_that( calling(self.statement.start_participant).with_args( user=self.user, duration=self.duration, ), raises(StatementCanOnlyStartOnce))
def setUp(self): super(TestAPI__statement_finish_virtual, self).setUp() self.virtual_statement = Statement( virtual_olympiad=1, virtual_duration=300, ) db.session.add(self.virtual_statement) self.user = User() db.session.add(self.user) db.session.flush() self.actual_duration = 10 self.participant = Participant( user_id=self.user.id, statement_id=self.virtual_statement.id, start=time.time() - self.actual_duration, duration=300, ) db.session.add(self.participant)
def test_simple(self): participant = Participant( user_id=self.user.id, statement_id=self.statements[0].id, start=self.now - self.duration, duration = self.duration * 2, ) db.session.add(participant) finished = self.statements[0].finish_participant(self.user) assert_that( finished.user_id, equal_to(self.user.id) ) assert_that( finished.statement_id, equal_to(self.statements[0].id) ) assert_that( finished.start, equal_to(self.now - self.duration) ) assert_that( finished.duration, close_to(self.duration, 2) ) db_participant = db.session.query(Participant).filter( Participant.user_id == self.user.id ).filter( Participant.statement_id == self.statements[0].id ).filter( Participant.start == self.now - self.duration ).one() assert_that( db_participant.duration, equal_to(finished.duration) )
def setUp(self): super(TestModel__statement_serialize, self).setUp() self.course = Course(id=123) db.session.add(self.course) self.statement = Statement(course=self.course) self.user = User() db.session.add_all([self.statement, self.user]) db.session.flush() self.course_module = CourseModule( instance_id=self.statement.id, module=19, course_id=self.course.id, ) self.participant = Participant( user_id=self.user.id, statement_id=self.statement.id, ) db.session.add_all([self.course_module, self.participant])
def setUp(self): super(TestAPI__statement_finish, self).setUp() self.user = User() db.session.add(self.user) self.now = time.time() self.duration = 290 self.statement = Statement( olympiad=1, time_start=self.now - 10, time_stop=self.now + self.duration, ) db.session.add(self.statement) db.session.flush() self.actual_duration = 5 self.participant = Participant( user_id=self.user.id, statement_id=self.statement.id, start=int(self.now - self.actual_duration), duration=self.duration, ) db.session.add(self.participant)
def test_with_active_virtual(self): participant = Participant( user=self.users[0], statement=self.statements[0], start=int(time.time()), duration=456, ) db.session.add(participant) self.set_session({'user_id': self.users[0].id}) response = self.client.get('/bootstrap') assert_that( response.json, has_entries({ 'user': has_entries({ 'active_virtual': { 'start': participant.start, 'duration': participant.duration, 'statement_id': participant.statement_id, } }), }))