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))
示例#3
0
 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 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_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 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'])