def test_badBoard2(self):
        input = {}
        board = {}
        input["board"] = board
        output = status(input)

        self.assertIn("error", output["gameStatus"])
    def test_badBoard1(self):
        input = {}
        input["board"] = "muffin"

        output = status(input)

        self.assertIn("error", output["gameStatus"])
    def test_noBoard(self):

        input = {}

        output = status(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_badBoard3(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2

        input = {}
        input["board"] = board
        output = status(input)

        self.assertIn("error", output["gameStatus"])
    def test_goodBoard1(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2] * (3 * 2)

        input = {}
        input["board"] = board
        output = status(input)

        self.assertNotIn("error", output["gameStatus"])
    def test_badBoard8(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2, "cake", 0, 0, 0, 0]

        input = {}
        input["board"] = board
        output = status(input)

        self.assertIn("error", output["gameStatus"])
Пример #7
0
def dispatch(messageJson=None):
    """
        dispatch is the microservice dispatcher for IndigoGirls, a 2048-like game.  It routes
        requests for game state transformations to the appropriate functions
        :param
            messageJson: JSON string that describes the state of the game needed for the
                        requested transformation
            :return:    A JSON string that describes the state of the game after the requested transformation
                        has taken place.
    """
    def buildErrorString(diagnostic=None):
        """
            returns a dictionary containing the specified key and accompanying diagnostic information
            :param
                diagnostic:     A string that describes the error
            :return:    A dictionary that contains the specified error key having a value that
                        consists of the specfied error string followed by a free-form diagnostic message
        """
        ERROR_PROPERTY = u'gameStatus'
        ERROR_PREFIX = u'error:  '
        return {ERROR_PROPERTY: ERROR_PREFIX + diagnostic}

    #Validate JSONness of input be converting the string to an equivalent dictionary
    try:
        messageDictionary = json.loads(messageJson)
    except:
        resultDictionary = json.dumps(
            buildErrorString('input JSON string is invalid'))
        return resultDictionary

    #Validate presence of dispatching code
    if (u"op" not in messageDictionary):
        resultDictionary = json.dumps(buildErrorString('op is missing'))
        return resultDictionary

    #Perform the game transformation as directed by the value of the "op" key
    #  input to each function:  a dictionary containing the name-value pairs of the input JSON string
    #  output of each function:  a dictionary containing name-value pairs to be encoded as a JSON string
    if (messageDictionary[u"op"] == u"initializeGame"):
        resultDictionary = initializeGame(messageDictionary)
    elif (messageDictionary[u"op"] == u"swipe"):
        resultDictionary = swipe(messageDictionary)
    elif (messageDictionary[u"op"] == u"recommend"):
        resultDictionary = recommend(messageDictionary)
    elif (messageDictionary[u"op"] == u"status"):
        resultDictionary = status(messageDictionary)
    elif (messageDictionary[u"op"] == u"predict"):
        resultDictionary = predict(messageDictionary)
    else:
        resultDictionary = buildErrorString('op is invalid')

    #Covert the dictionary back to a string in JSON format
    resultJson = json.dumps(resultDictionary)
    return resultJson
    def test_underway1(self):
        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        board["grid"] = [2, 0, 2, 1]

        input = {}
        input["board"] = board

        output = status(input)

        self.assertIn("underway", output["gameStatus"])
    def test_win2(self):
        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        board["grid"] = [4, 0, 1, 0]

        input = {}
        input["board"] = board

        output = status(input)

        self.assertIn("win", output["gameStatus"])
    def test_lose1(self):
        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        board["grid"] = [2, 3, 3, 1]

        input = {}
        input["board"] = board
        input["tile"] = 2**4

        output = status(input)

        self.assertIn("lose", output["gameStatus"])
    def test_badtile2(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2] * (3 * 2)

        input = {}
        input["board"] = board
        input["tile"] = 2**(board["rowCount"] * board["columnCount"]) + 1

        output = status(input)

        self.assertIn("error", output["gameStatus"])