class CachingGameStore(object): def __init__(self, ladderFilePath, useCache): self._gameStore = GameStore(ladderFilePath) self._usingCache = useCache def loadGames(self, ladderTime, transforms): self._ladderTime = ladderTime useCache = self._usingCache and self._ladderTime['now'] return Transformer.transform(self._baseLoadGames, transforms, useCache) def _baseLoadGames(self): games = self._gameStore.getGames() if not self._ladderTime['now']: games = [ g for g in games if self._ladderTime['range'][0] <= g.time and g.time <= self._ladderTime['range'][1] ] return games def appendGame(self, game): self._deleteCache() self._gameStore.appendGame(game) def deleteGame(self, gameTime, deletedBy): self._deleteCache() return self._gameStore.deleteGame(gameTime, deletedBy) def _deleteCache(self): for transform in Transforms.values(): transform.deleteCache()
class CachingGameStore(object): def __init__(self, ladderFilePath, useCache): self._gameStore = GameStore(ladderFilePath) self._usingCache = useCache def loadGames(self, ladderTime, transforms): self._ladderTime = ladderTime useCache = self._usingCache and self._ladderTime['now'] return Transformer.transform(self._baseLoadGames, transforms, useCache) def _baseLoadGames(self): games = self._gameStore.getGames() if not self._ladderTime['now']: games = [g for g in games if self._ladderTime['range'][0] <= g.time and g.time <= self._ladderTime['range'][1]] return games def appendGame(self, game): self._deleteCache() self._gameStore.appendGame(game) def deleteGame(self, gameTime, deletedBy): self._deleteCache() return self._gameStore.deleteGame(gameTime, deletedBy) def _deleteCache(self): for transform in Transforms.values(): transform.deleteCache()
def testAppend(self): try: game1 = ("tlr", 0, "cjm", 10, 1445443858) game2 = ("jma", 5, "tmm", 5, 1445443859) filePath = "temp.txt" with open(filePath, 'w') as temp: temp.write("\n%s %s %s %s %.0f" % game1) sut = GameStore(filePath) sut.appendGame(self._createGame(game2)) result = sut.getGames() self.assertEqual(len(result), 2) self._assertGame(result[0], game1) self._assertGame(result[1], game2) with open(filePath, 'r') as temp: self.assertMultiLineEqual(temp.read(), "\ntlr 0 cjm 10 1445443858\njma 5 tmm 5 1445443859") finally: os.remove(filePath)
def testGetAppendDelete(self): try: game1 = ("tlr", 0, "cjm", 10, 1445443858) game2 = ("foo", 2, "bar", 8, 1445443859) filePath = "temp.txt" with open(filePath, 'w') as temp: temp.write("\n%s %s %s %s %.0f" % game1) temp.write("\n%s %s %s %s %.0f" % game2) sut = GameStore(filePath) result = sut.getGames() self.assertEqual(len(result), 2) self._assertGame(result[0], game1) self._assertGame(result[1], game2) game3 = ("bar", 8, "baz", 2, 1445443860) game4 = ("bim", 8, "bob", 2, 1445443861) sut.appendGame(self._createGame(game3)) sut.appendGame(self._createGame(game4)) result = sut.getGames() self.assertEqual(len(result), 4) self._assertGame(result[0], game1) self._assertGame(result[1], game2) self._assertGame(result[2], game3) self._assertGame(result[3], game4) sut.deleteGame(1445443860, "baz", 1445443862) result = sut.getGames() self.assertEqual(len(result), 4) self._assertGame(result[0], game1) self._assertGame(result[1], game2) self._assertGame(result[2], game3 + ("baz", 1445443862)) self._assertGame(result[3], game4) with open(filePath, 'r') as temp: self.assertMultiLineEqual( temp.read(), "\ntlr 0 cjm 10 1445443858\nfoo 2 bar 8 1445443859\nbar 8 baz 2 1445443860 baz 1445443862\nbim 8 bob 2 1445443861" ) finally: os.remove(filePath)
def testAppend(self): try: game1 = ("tlr", 0, "cjm", 10, 1445443858) game2 = ("jma", 5, "tmm", 5, 1445443859) filePath = "temp.txt" with open(filePath, 'w') as temp: temp.write("\n%s %s %s %s %.0f" % game1) sut = GameStore(filePath) sut.appendGame(self._createGame(game2)) result = sut.getGames() self.assertEqual(len(result), 2) self._assertGame(result[0], game1) self._assertGame(result[1], game2) with open(filePath, 'r') as temp: self.assertMultiLineEqual( temp.read(), "\ntlr 0 cjm 10 1445443858\njma 5 tmm 5 1445443859") finally: os.remove(filePath)
def testGetAppendDelete(self): try: game1 = ("tlr", 0, "cjm", 10, 1445443858) game2 = ("foo", 2, "bar", 8, 1445443859) filePath = "temp.txt" with open(filePath, 'w') as temp: temp.write("\n%s %s %s %s %.0f" % game1) temp.write("\n%s %s %s %s %.0f" % game2) sut = GameStore(filePath) result = sut.getGames() self.assertEqual(len(result), 2) self._assertGame(result[0], game1) self._assertGame(result[1], game2) game3 = ("bar", 8, "baz", 2, 1445443860) game4 = ("bim", 8, "bob", 2, 1445443861) sut.appendGame(self._createGame(game3)) sut.appendGame(self._createGame(game4)) result = sut.getGames() self.assertEqual(len(result), 4) self._assertGame(result[0], game1) self._assertGame(result[1], game2) self._assertGame(result[2], game3) self._assertGame(result[3], game4) sut.deleteGame(1445443860, "baz", 1445443862) result = sut.getGames() self.assertEqual(len(result), 4) self._assertGame(result[0], game1) self._assertGame(result[1], game2) self._assertGame(result[2], game3 + ("baz", 1445443862)) self._assertGame(result[3], game4) with open(filePath, 'r') as temp: self.assertMultiLineEqual(temp.read(), "\ntlr 0 cjm 10 1445443858\nfoo 2 bar 8 1445443859\nbar 8 baz 2 1445443860 baz 1445443862\nbim 8 bob 2 1445443861") finally: os.remove(filePath)