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 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_06_create_a_question_difficulty_default_value(self):
        biology_category = Category(name="Biology")
        biology_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": ['Biology'],
        }
        response = self.test_client.post("/questions/", json=data)
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json['result']['difficulty'])
    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'])