def test_repository_get_championship_invalid(self): championship_id = uuid4() robots = [uuid4(), uuid4(), uuid4(), uuid4(), uuid4()] name = 'Roborumble' events = [ { 'id': str(uuid4()), 'championship_id': str(championship_id), 'championship_name': name, 'robots': [str(r) for r in robots], 'created_at': datetime.utcnow().isoformat(), 'event': 'ChampionshipCreated' }, { 'id': str(uuid4()), 'championship_id': str(championship_id), 'created_at': datetime.utcnow().isoformat(), 'event': 'ChampionshipInvalidated' }, ] self.mock_kafka_consumer.get_all_events.return_value = events repo = ChampionshipEventRepository(self.mock_kafka_consumer, None) c = repo.get(championship_id) assert len(c.events) == 2 assert c.current_state.id == championship_id assert c.current_state.name == name assert c.current_state.status == 'invalid' assert c.current_state.robots == robots
def test_save_flushes_only_new_events(self): championship_id = uuid4() robots = [uuid4(), uuid4(), uuid4(), uuid4(), uuid4()] name = 'Roborumble' events = [ { 'id': str(uuid4()), 'championship_id': str(championship_id), 'championship_name': name, 'robots': [str(r) for r in robots], 'created_at': datetime.utcnow().isoformat(), 'event': 'ChampionshipCreated' }, { 'id': str(uuid4()), 'championship_id': str(championship_id), 'created_at': datetime.utcnow().isoformat(), 'event': 'ChampionshipValidated' }, ] self.mock_kafka_consumer.get_all_events.return_value = events repo = ChampionshipEventRepository(self.mock_kafka_consumer, self.mock_kafka_producer) c = repo.get(championship_id) c.start() repo.save(c) self.mock_kafka_producer.send.assert_called_once_with(c.events[2])
def championship_by_id(championship_id): repo = ChampionshipEventRepository( championship_event_one_time_consumer_factory(), championship_message_sender, ) championship = repo.get(championship_id) if request.method == 'GET': return json_response(championship.current_state) if request.method == 'DELETE': championship.delete() repo.save(championship)
def start_championship(championship_id): try: repo = ChampionshipEventRepository( championship_event_one_time_consumer_factory(), championship_message_sender, ) championship = repo.get(championship_id) championship.start() repo.save(championship) return json_response(championship.current_state) except Exception as e: logger.error(str(e)) raise HTTPException(description=str(e), response=Response( status=400, response=str(e), ))
def root(): data = request.get_json() try: _validate_new_data(data) robots = list(map(UUID, data['robots'])) c = Championship.create( championship_id=uuid4(), championship_name=data['name'], robots=robots, ) repo = ChampionshipEventRepository( championship_event_one_time_consumer_factory(), championship_message_sender, ) repo.save(c) return json_response(c.current_state) except Exception as e: logger.error(str(e)) raise HTTPException(description=str(e), response=Response( status=400, response=str(e), ))