def test_checkUpperBoundRowCount(self): message = {'op': 'initializeGame', 'rowCount' : 100} answer = initializeGame(message) assert(answer['gameStatus'] == 'underway') assert(answer['board']['rowCount'] == 100) message = {'op': 'initializeGame', 'rowCount' : 101} answer = initializeGame(message) assert(answer['gameStatus'] == 'error: rowCount is not a valid number. ')
def test_checkLowerBoundColumnCount(self): message = {'op': 'initializeGame', 'columnCount' : 2} answer = initializeGame(message) self.assertTrue(answer['gameStatus'] == 'underway') self.assertTrue(answer['board']['columnCount'] == 2) message = {'op': 'initializeGame', 'columnCount' : 1} answer = initializeGame(message) self.assertTrue(answer['gameStatus'] == 'error: columnCount is not a valid number. ')
def test_gridContents2(self): message = {'op': 'initializeGame'} numberOfOnes = 0 numberOfTwos = 0 i = 0 numberOfTests = 10000 def elementsWithValue(array, value): count = 0 for e in array: if (e == value): count = count + 1 return count while (i < numberOfTests): i = i + 1 answer = initializeGame(message) grid = answer['board']['grid'] numberOfOnes = numberOfOnes + elementsWithValue(grid, 1) numberOfTwos = numberOfTwos + elementsWithValue(grid, 2) self.assertTrue(numberOfOnes + numberOfTwos == numberOfTests * 2) self.assertAlmostEqual((float(numberOfOnes) / float(numberOfTests * 2)), 0.75, 1)
def test_checkPassDictionaryKeys(self): message = {'op' : 'initializeGame'} answer = initializeGame(message) self.assertTrue('score' in answer) self.assertTrue('board' in answer) self.assertTrue('gameStatus' in answer)
def test_checkBoardKeys(self): message = {'op': 'initializeGame'} answer = initializeGame(message) board = answer.get('board') self.assertTrue('columnCount' in board) self.assertTrue('rowCount' in board) self.assertTrue('grid' in board)
def test_checkFailedDictionaryKeys(self): message = {'op': 'initializeGame', 'columnCount': '3'} answer = initializeGame(message) self.assertTrue(answer['gameStatus'] == 'error: columnCount is not a valid number. ') self.assertTrue('score' not in answer) self.assertTrue('board' not in answer) self.assertTrue('gameStatus' in answer)
def test_GridSize3(self): message = {'op': 'initializeGame', 'columnCount': 2, 'rowCount' : 7} answer = initializeGame(message) grid = answer['board']['grid'] self.assertTrue(len(grid) == (2*7)) self.assertTrue(answer['board']['columnCount'] == 2) self.assertTrue(answer['board']['rowCount'] == 7)
def test_GridSize2(self): message = {'op': 'initializeGame', 'columnCount' : 5} answer = initializeGame(message) grid = answer['board']['grid'] self.assertTrue(len(grid) == (4*5)) self.assertTrue(answer['board']['columnCount'] == 5) self.assertTrue(answer['board']['rowCount'] == 4)
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_gridContents1(self): message = {'op':'initializeGame'} answer = initializeGame(message) grid = answer['board']['grid'] count = 0; for e in grid: if (e != 0): count = count + 1 self.assertTrue(count == 2)
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. """ #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') #Convert the dictionary back to a string in JSON format resultJson = json.dumps(resultDictionary) return resultJson