def test_10_create_a_room_with_1_round_creates_a_round_object(self): art_category = Category(name="Art") art_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } question = Question(**data) question.save() player = Player(nick="Juan") player.save() room_data_2 = { 'owner': "Juan", 'name': "Sala 3", 'rounds_amount': 1, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) self.assertEqual(1, response_2.json['result']['rounds_amount']) self.assertEqual( 1, len( Room.objects.get( name=response_2.json['result']['_id'])['rounds']))
def test_answer_a_question_creates_an_answer_document(self): art_category = Category(name="Art") art_category.save() player = Player.objects.create(nick="tester") data = {"text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{"sentence": "Santa Martina"}, {"sentence": "Santa Marina"}, {"sentence": "Santa Maria", "correct": "True"} ], 'categories': ['Art']} question = Question(**data) question.save() question_id = question.pk room_data_2 = {'owner': "tester", 'name': "a room name", 'rounds_amount': 1, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) option = question.options.filter(correct=True).first() data = { 'id': str(option._id), 'sentence': option.sentence, 'nick': str(player.id), 'room_name': "a room name" } response = self.test_client.post(f"question/{question_id}/", json={'data': data}) self.assertEqual(200, response.status_code) round_obj = Room.objects.getRoundForAQuestion("a room name", str(question_id)) self.assertEqual(round_obj.answers.first().question_option_id, str(option._id))
def post() -> Response: data = request.get_json() post_question = Question(**data) try: post_question.save() except ValidationError as e: raise abort(400, message=e.message) return jsonify({'result': post_question})
def getRoundsFor(self, name): a_room = self.get(name=name) categories = a_room.categories round_amount = a_room.rounds_amount questions_of_category = Question.objects.filter( categories__in=categories) if len(questions_of_category) >= round_amount: questions = [ val['id'] for val in random.sample(list(questions_of_category), k=round_amount) ] else: pending_questions_amount = round_amount - len( questions_of_category) extra_questions = Question.objects( id__nin=questions_of_category.values_list('id')) if len(extra_questions) >= pending_questions_amount: extra_questions_random = random.sample( list(extra_questions), k=pending_questions_amount) questions = list( chain(questions_of_category, extra_questions_random)) else: questions = list(chain(questions_of_category, extra_questions)) rounds = [] shuffled_questions = random.sample(list(questions), k=len(questions)) for question in shuffled_questions: round = Round(question=question) rounds.append(round) return rounds
def test_get_points_for_a_player_with_1_correct_answer(self): a_player = Player(nick="a player") a_player.save() art_category = Category(name="Art") art_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } question = Question(**data) question.save() room_data_2 = { 'owner': "a player", 'name': "a room name", 'rounds_amount': 1, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) question_id = question.pk option = question.options.filter(correct=True).first() data = { 'id': str(option._id), 'sentence': option.sentence, 'nick': str(a_player.id), 'room_name': "a room name", } response = self.test_client.post(f"question/{question_id}/", json={'data': data}) self.assertEqual(200, response.status_code) self.assertEqual(2, Room.objects.getPointsFor("a room name", "a player"))
def test_13_with_5_rounds_with_4_questions_of_that_category_and_1_from_another_creates_a_room_with_5_rounds( self): art_category = Category(name="Art") art_category.save() history_category = Category(name="History") history_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } Question(**data).save() Question(**data).save() Question(**data).save() Question(**data).save() data['categories'] = ["History"] Question(**data).save() player = Player(nick="Juan") player.save() room_data_2 = { 'owner': "Juan", 'name': "Sala 3", 'rounds_amount': 5, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) self.assertEqual(5, response_2.json['result']['rounds_amount']) self.assertEqual( 5, len( Room.objects.get( name=response_2.json['result']['_id'])['rounds']))
def test_get_round_for_a_question_id(self): a_player = Player(nick="a player") a_player.save() art_category = Category(name="Art") art_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } question = Question(**data) question.save() room_data_2 = { 'owner': "a player", 'name': "a room name", 'rounds_amount': 1, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) question_id = question.pk self.assertEqual( question, Room.objects.getRoundForAQuestion("a room name", str(question_id)).question)
def get() -> Response: output = Question.objects() return jsonify({'result': output})
def test_15_re_create_a_room_old_values(self): art_category = Category(name="Art") art_category.save() history_category = Category(name="History") history_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } Question(**data).save() Question(**data).save() Question(**data).save() Question(**data).save() Question(**data).save() data['categories'] = ["History"] Question(**data).save() Question(**data).save() Question(**data).save() Question(**data).save() Question(**data).save() player = Player(nick="Juan") player.save() player_2 = Player(nick="Carlos") player_2.save() room_data_2 = { 'owner': "Juan", 'name': "Sala 3", 'rounds_amount': 5, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) self.assertEqual(5, response_2.json['result']['rounds_amount']) self.assertEqual( 5, len( Room.objects.get( name=response_2.json['result']['_id'])['rounds'])) Room.objects.add_participant( room_name=response_2.json['result']['_id'], a_participant=player) Room.objects.add_participant( room_name=response_2.json['result']['_id'], a_participant=player_2) room_data_update = {} response_update = self.test_client.post( f"/rooms/{response_2.json['result']['_id']}/update/", json=room_data_update) self.assertEqual(200, response_update.status_code) self.assertEqual(room_data_2['rounds_amount'], response_update.json['result']['rounds_amount']) self.assertEqual(10, response_update.json['result']['round_time']) self.assertEqual(room_data_2['rounds_amount'], len(response_update.json['result']['rounds'])) self.assertEqual(room_data_2['categories'], response_update.json['result']['categories']) response_all_rooms = self.test_client.get("/rooms/") self.assertEqual(200, response_all_rooms.status_code) self.assertEqual(room_data_2['categories'], response_all_rooms.json['result'][0]['categories'])
def test_get_points_for_all_players(self): a_player = Player(nick="a player") a_player.save() a_player_2 = Player(nick="a player 2") a_player_2.save() art_category = Category(name="Art") art_category.save() data = { "text": "Las 3 carabelas eran: la pinta, la niña y ...", "options": [{ "sentence": "Santa Martina" }, { "sentence": "Santa Marina" }, { "sentence": "Santa Maria", "correct": "True" }], "categories": ["Art"] } question = Question(**data) question.save() room_data_2 = { 'owner': "a player", 'name': "a room name", 'rounds_amount': 1, 'categories': ['Art'] } response_2 = self.test_client.post("/rooms/", json=room_data_2) self.assertEqual(200, response_2.status_code) a_room = Room.objects.get(name=room_data_2['name']) Room.objects.add_participant(room_name=a_room.name, a_participant=a_player) Room.objects.add_participant(room_name=a_room.name, a_participant=a_player_2) question_id = question.pk option = question.options.filter(sentence="Santa Martina").first() data = { 'id': str(option._id), 'sentence': option.sentence, 'nick': str(a_player.id), 'room_name': "a room name", } response = self.test_client.post(f"question/{question_id}/", json={'data': data}) self.assertEqual(200, response.status_code) self.assertEqual(0, Room.objects.getPointsFor("a room name", "a player")) option_correct = question.options.filter(correct=True).first() data_3 = { 'id': str(option_correct._id), 'sentence': option_correct.sentence, 'nick': str(a_player_2.id), 'room_name': "a room name", } response_3 = self.test_client.post(f"question/{question_id}/", json={'data': data_3}) self.assertEqual(200, response_3.status_code) self.assertEqual( 2, Room.objects.getPointsFor("a room name", "a player 2")) self.assertEqual( str(a_player_2.id), Room.objects.getPointsForAllPlayers("a room name")[0]['player']) self.assertEqual( 2, Room.objects.getPointsForAllPlayers("a room name")[0]['points']) self.assertEqual( str(a_player.id), Room.objects.getPointsForAllPlayers("a room name")[1]['player']) self.assertEqual( 0, Room.objects.getPointsForAllPlayers("a room name")[1]['points'])