def test_get_rounds_with_a_room_name(self):
     a_player = Player(nick="a player")
     a_player.save()
     a_room = Room(name="a room name", owner=a_player, rounds_amount=1)
     a_room.save()
     self.assertEqual(a_room.rounds,
                      Room.objects.getRoundsFor("a room name"))
 def test_get_points_for_a_player_no_answers(self):
     a_player = Player(nick="a player")
     a_player.save()
     a_room = Room(name="a room name", owner=a_player, rounds_amount=1)
     a_room.save()
     self.assertEqual(0, Room.objects.getPointsFor("a room name",
                                                   "a player"))
 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']))
Esempio n. 4
0
 def test_get_basic_ranking(self):
     player_1 = Player(nick='player_1', points=15).save()
     player_2 = Player(nick='player_2', points=2).save()
     player_3 = Player(nick='player_3', points=80).save()
     response = self.test_client.get("/ranking/players/")
     self.assertEqual(200, response.status_code)
     self.assertEqual(player_3.nick, response.json['result'][0]['_id'])
     self.assertEqual(player_1.nick, response.json['result'][1]['_id'])
     self.assertEqual(player_2.nick, response.json['result'][2]['_id'])
 def test_08_create_a_room_default_amount_value(self):
     player = Player(nick="Juan")
     player.save()
     room_data_2 = {
         'owner': "Juan",
         'name': "Sala 3",
     }
     response_2 = self.test_client.post("/rooms/", json=room_data_2)
     self.assertEqual(200, response_2.status_code)
     self.assertEqual(4, response_2.json['result']['rounds_amount'])
 def test_12_create_a_room_with_15_seconds_time(self):
     player = Player(nick="Juan")
     player.save()
     room_data = {'owner': "Juan", 'name': "Sala 1", 'round_time': 15}
     response = self.test_client.post("/rooms/", json=room_data)
     self.assertEqual(200, response.status_code)
     self.assertEqual(player.nick, response.json['result']['owner'])
     self.assertEqual(room_data['name'], response.json['result']['_id'])
     self.assertEqual(room_data['round_time'],
                      response.json['result']['round_time'])
 def test_09_create_a_room_with_1_rounds_amount_set_it_properly(self):
     player = Player(nick="Juan")
     player.save()
     room_data_2 = {
         'owner': "Juan",
         'name': "Sala 3",
         'rounds_amount': 1,
     }
     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'])
 def test_search_owner_nick_and_room_name_1_found(self):
     player = Player(nick="Juan").save()
     player_2 = Player(nick="Milagros").save()
     room = Room(name="Sala de Juan", owner=player, participants=[player_2])
     room.save()
     response = self.test_client.get("/rooms/search/?q=juan")
     self.assertEqual(200, response.status_code)
     self.assertEqual(1, len(response.json['result']))
     self.assertEqual(room.name, response.json['result'][0]['_id'])
     self.assertEqual(player.nick, response.json['result'][0]['owner'])
     self.assertEqual(player_2.nick,
                      response.json['result'][0]['participants'][0])
 def test_11_create_a_room_without_sending_time_goes_to_default_10_secs(
         self):
     player = Player(nick="Juan")
     player.save()
     room_data = {
         'owner': "Juan",
         'name': "Sala 1",
     }
     response = self.test_client.post("/rooms/", json=room_data)
     self.assertEqual(200, response.status_code)
     self.assertEqual(player.nick, response.json['result']['owner'])
     self.assertEqual(room_data['name'], response.json['result']['_id'])
     self.assertEqual(10, response.json['result']['round_time'])
 def test_02_get_all_rooms_1_found(self):
     player = Player(nick="Juan")
     player_2 = Player(nick="Milagros")
     room = Room(name="sala del test 2",
                 owner=player,
                 participants=[player_2])
     room.save()
     response = self.test_client.get("/rooms/")
     self.assertEqual(200, response.status_code)
     self.assertEqual(room.name, response.json['result'][0]['_id'])
     self.assertEqual(player.nick, response.json['result'][0]['owner'])
     self.assertEqual(player_2.nick,
                      response.json['result'][0]['participants'][0])
Esempio n. 11
0
 def post() -> Response:
     data = request.get_json()
     password = data.get('password', None)
     if not password or password == "":
         abort(400, message="No envió una password")
     try:
         post_player = Player.objects.get(nick=data['nick'])
     except DoesNotExist:
         post_player = Player(**data)
         try:
             post_player.save()
         except ValidationError as e:
             abort(400, message=e.message)
     return jsonify({'result': post_player})
 def test_06_create_a_room_with_art_category(self):
     player = Player(nick="Juan")
     player.save()
     art_category = Category(name="Art")
     art_category.save()
     room_data = {
         'owner': "Juan",
         'name': "Sala 2",
         'categories': ['Art'],
     }
     response = self.test_client.post("/rooms/", json=room_data)
     self.assertEqual(200, response.status_code)
     self.assertEqual(player.nick, response.json['result']['owner'])
     self.assertEqual(room_data['name'], response.json['result']['_id'])
     self.assertEqual(room_data['categories'],
                      response.json['result']['categories'])
 def test_03_get_one_room_1_found(self):
     player = Player(nick="Juan")
     room = Room(name="sala del test 3", owner=player)
     room.save()
     response = self.test_client.get(f"/rooms/{room.name}/")
     self.assertEqual(200, response.status_code)
     self.assertEqual(room.name, response.json['result']['_id'])
     self.assertEqual(player.nick, response.json['result']['owner'])
Esempio n. 14
0
 def get(self, room_id):
     try:
         output = Room.objects.get(name=room_id)
         players = Player.objects(
             nick__in=[p.id for p in output.participants])
     except DoesNotExist:
         raise abort(404)
     return jsonify({'result': output, 'players': players})
 def test_07_create_a_room_with_biology_and_history_category(self):
     player = Player(nick="Juan")
     player.save()
     biology_category = Category(name="Biology")
     history_category = Category(name="History")
     biology_category.save()
     history_category.save()
     room_data_2 = {
         'owner': "Juan",
         'name': "Sala 3",
         'categories': ['Biology', 'History'],
     }
     response_2 = self.test_client.post("/rooms/", json=room_data_2)
     self.assertEqual(200, response_2.status_code)
     self.assertEqual(player.nick, response_2.json['result']['owner'])
     self.assertEqual(room_data_2['name'], response_2.json['result']['_id'])
     self.assertEqual(room_data_2['categories'],
                      response_2.json['result']['categories'])
    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 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'])
    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_12_cannot_create_a_room_with_more_than_60_seconds(self):
     player = Player(nick="Juan")
     player.save()
     room_data = {'owner': "Juan", 'name': "Sala 1", 'round_time': 61}
     response = self.test_client.post("/rooms/", json=room_data)
     self.assertEqual(400, response.status_code)
Esempio n. 22
0
 def get(self):
     output = Room.objects()
     players = Player.objects()
     return jsonify({'result': output, 'players': players})