Esempio n. 1
0
def test_parser_set_name():
    data = {"cmd": "set_name",
            "team": "home",
            "arg": "Foo"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.home_team.name == "Foo"
Esempio n. 2
0
def test_parser_clock_set_max_seconds():
    data = {"cmd": "clock_set_seconds",
            "clock": "match",
            "arg": 30}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.match_clock.set_max_seconds.assert_called_with(30)
Esempio n. 3
0
def test_parser_set_timeout_flag():
    data = {"cmd": "set_timeout_flag",
            "team": "away",
            "arg": True}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.away_team.timeout_flag is True
Esempio n. 4
0
def test_parser_set_penalty_flag():
    data = {"cmd": "set_penalty_flag",
            "team": "home",
            "arg": False}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.home_team.penalty_flag is False
Esempio n. 5
0
def test_points_reset():
    data = {"cmd": "points_reset",
            "counter": "set",
            "team": "home"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.home_team.set_points.reset_points.assert_called_with()
Esempio n. 6
0
def test_parser_with_too_much_args_case():
    data = {"cmd": "clock_stop",
            "clock": "regular",
            "arg": "soap"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "Too many arguments:" in str(excinfo.value)
Esempio n. 7
0
def test_points_subtract():
    data = {"cmd": "points_subtract",
            "counter": "set",
            "team": "home",
            "arg": 2}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.home_team.set_points.subtract_point.assert_called_with(2)
Esempio n. 8
0
def test_points_set():
    data = {"cmd": "points_set",
            "counter": "regular",
            "team": "away",
            "arg": 69}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.away_team.points.set_points.assert_called_with(69)
Esempio n. 9
0
def test_points_set():
    data = {
        "cmd": "points_set",
        "counter": "regular",
        "team": "away",
        "arg": 69
    }
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.away_team.points.set_points.assert_called_with(69)
Esempio n. 10
0
def test_points_add():
    data = {
        "cmd": "points_add",
        "counter": "regular",
        "team": "away",
        "arg": 3
    }
    parser = Parser(create_game())
    parser.parse_and_execute(data)
    parser.game.away_team.points.add_point.assert_called_with(3)
Esempio n. 11
0
def test_points_subtract():
    data = {
        "cmd": "points_subtract",
        "counter": "set",
        "team": "home",
        "arg": 2
    }
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.home_team.set_points.subtract_point.assert_called_with(2)
Esempio n. 12
0
def test_parser_in_wrong_command_case():
    data = {"cmd": "wrong_command"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "No command found:" in str(excinfo.value)
Esempio n. 13
0
def test_parser_with_less_args_case():
    data = {"cmd": "clock_stop"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "I need" in str(excinfo.value)
Esempio n. 14
0
def test_parser_with_too_much_args_case():
    data = {"cmd": "clock_stop", "clock": "regular", "arg": "soap"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "Too many arguments:" in str(excinfo.value)
Esempio n. 15
0
def test_parser_clock_stop():
    data = {"cmd": "clock_stop", "clock": "match"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.match_clock.end.assert_called_with()
Esempio n. 16
0
def test_parser_clock_reset():
    data = {"cmd": "clock_reset",
            "clock": "twenty_four"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.twenty_four_clock.reset_clock.assert_called_with()
Esempio n. 17
0
def test_parser_clock_stop():
    data = {"cmd": "clock_stop",
            "clock": "match"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.match_clock.end.assert_called_with()
Esempio n. 18
0
def test_parser_set_timeout_flag():
    data = {"cmd": "set_timeout_flag", "team": "away", "arg": True}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.away_team.timeout_flag is True
Esempio n. 19
0
def test_parser_with_less_args_case():
    data = {"cmd": "clock_stop"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "I need" in str(excinfo.value)
Esempio n. 20
0
def test_parser_in_wrong_command_case():
    data = {"cmd": "wrong_command"}
    parser = Parser(MagicMock)
    with pytest.raises(ParseError) as excinfo:
        parser.parse_and_execute(data, MagicMock)
    assert "No command found:" in str(excinfo.value)
Esempio n. 21
0
def test_period_set():
    data = {"cmd": "period_set", "arg": 2}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.period.set_points.assert_called_with(2)
Esempio n. 22
0
def test_parser_clock_set_max_seconds():
    data = {"cmd": "clock_set_seconds", "clock": "match", "arg": 30}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.match_clock.set_max_seconds.assert_called_with(30)
Esempio n. 23
0
def test_parser_set_penalty_flag():
    data = {"cmd": "set_penalty_flag", "team": "home", "arg": False}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.home_team.penalty_flag is False
Esempio n. 24
0
class ServerHandler(WebSocketServerProtocol):
    second_deffer = None
    parser = Parser(game)

    def __init__(self):
        super(ServerHandler, self).__init__()
        self.user = None

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onOpen(self):
        print("WebSocket connection open.")
        self.send_data_for_second()

    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:
            print("Text message received: {0}".format(payload.decode('utf8')))
            message = payload.decode('utf8')
            try:
                data = json.loads(message)
            except ValueError:
                self.send_response(NotJson())
                return
            try:
                response = self.parser.parse_and_execute(data, self.user)
                if isinstance(response, CorrectCredentials):
                    self.user = User()
                elif isinstance(response, SignedOut):
                    self.user = None
                self.send_response(response)
            except ParseError as e:
                self.send_response(CannotParse(str(e)))
            except ParserTypeError as e:
                self.send_response(WrongDataType(str(e)))
            except:
                self.send_response(UnknownError())

    def onClose(self, wasClean, code, reason):
        if self.second_deffer is not None:
            self.second_deffer.cancel()
        print("WebSocket connection closed: {0}".format(reason))

    def send_data(self):
        dictionary = game.to_dict()
        dictionary["logged"] = True if self.user else False
        data = {"data": dictionary}
        state = json.dumps(data)
        self.sendMessage(state.encode('utf-8'), isBinary=False)

    def send_data_for_second(self):
        self.send_data()
        self.second_deffer = task.deferLater(reactor, 1.0,
                                             self.send_data_for_second)

    def send_response(self, response_object):
        data = {"response": response_object.get_response()}
        state = json.dumps(data)
        self.sendMessage(state.encode('utf-8'), isBinary=False)
Esempio n. 25
0
def test_parser_clock_reset():
    data = {"cmd": "clock_reset", "clock": "twenty_four"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.twenty_four_clock.reset_clock.assert_called_with()
Esempio n. 26
0
def test_points_reset():
    data = {"cmd": "points_reset", "counter": "set", "team": "home"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.home_team.set_points.reset_points.assert_called_with()
Esempio n. 27
0
def test_parser_set_name():
    data = {"cmd": "set_name", "team": "home", "arg": "Foo"}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    assert parser.game.home_team.name == "Foo"
Esempio n. 28
0
def test_period_set():
    data = {"cmd": "period_set",
            "arg": 2}
    parser = Parser(create_game())
    parser.parse_and_execute(data, MagicMock)
    parser.game.period.set_points.assert_called_with(2)
Esempio n. 29
0
def test_parser_clock_start():
    data = {"cmd": "clock_start", "clock": "twenty_four"}
    parser = Parser(create_game())
    parser.parse_and_execute(data)
    parser.game.twenty_four_clock.start.assert_called_with()