def test_getOptimalValue(self): game = Game.stub(Player.stub(), getLevel(0)) game.player.getMoveCost = Mock(return_value=10) game.ensureYieldsExist = Mock() game.level['yields'][7] = [ {'value': 25.0}, {'value': 15.0}, {'value': 15.0}, {'value': 5.0}, {'value': 5.0}, {'value': 50.0}, ] game.level['yields'][0] = game.level['yields'][7] # it finds the correct overall optimal time self.assertEqual(6, game.getOptimalTime(7)) game.ensureYieldsExist.assert_called_with(7) # it finds the correct local optimal time self.assertEqual(3, game.getOptimalTime(7, True)) # it does not take into account moving cost # at the very beginning self.assertEqual(1, game.getOptimalTime(0, True)) self.assertEqual(1, game.getOptimalTime(0)) game.ensureYieldsExist.assert_called_with(0)
def test_addYield(self): # we can only really test that we add yields # with desired fish maxTime = TOTAL_TIME / FISHING_COST ym = YieldMerger(maxTime) # parameter was captured self.assertEqual(ym.n, maxTime) player = Player.stub() yieldC = 0 # for every level in the game for i in range(len(GAME['levels'])): for id, fish in getFishForLevel(i).iteritems(): ym.addYield(id, fish, 5, player) yieldC += 1 # we get one more yield: self.assertEqual(len(ym.yields), yieldC) # the size of the yield is maxTime self.assertEqual(len(ym.yields[yieldC-1]), maxTime) # check that this yield only has this fish or nothing for y in ym.yields[yieldC-1]: self.assertTrue(None == y or y['id'] == id) # for the merge part, we will check that # yieldmerger is biased for choosing a fish merged = ym.merge() self.assertEquals(len(merged), maxTime) for i in range(maxTime): empty = True for y in ym.yields: empty = empty and (y[i] == None) # none can only be selected if empty # otherwise a fish must be there self.assertEqual(empty, merged[i] == None)
def test_getFishInYield(self): game = Game.stub(Player.stub(), getLevel(0)) game.level['yields'][7] = [ {'name': 'Bass', 'weight': 1.0}, {'name': 'Bass', 'weight': 1.0}, {'name': 'Bass', 'weight': 1.0}, {'name': 'Bass', 'weight': 1.0}, None, {'name': 'Bass', 'weight': 2.0}, {'name': 'Bass', 'weight': 1.0}, ] # when we haven't fished in this location, # it returns the aggregation of the whole list fish = {'Bass': { 'weight': 7.0, 'count': 6, 'depth': 4, }} self.assertEqual(fish, game.getFishInYield(7)) # when we have fished for a while, it returns remainder game.level['timeInLoc'][7] = 2 fish = {'Bass': { 'weight': 5.0, 'count': 4, 'depth': 4, }} self.assertEqual(fish, game.getFishInYield(7))
def test_catchNoNil(self): game = Game.stub(Player.stub(), getLevel(0)) spotYield = [None, 1, 2, None, None, 3, None, None] game._common_init = Mock(return_value=(spotYield, 0)) game._common_catch_init = Mock(return_value=(0, 10, 1)) game._save_game = Mock() game.getCues = Mock(return_value=42) # it follows the prescribed logic retVal = { 'fishList': [1, 3], 'cues': 42, 'time': 6, } self.assertEqual(retVal, game.catchNoNil(['1', '0', '1'])) game._save_game.assert_called_with([1, 3], 6, 6) # it can start from the arbitrary point game._common_init = Mock(return_value=(spotYield, 2)) retVal = { 'fishList': [2, 3], 'cues': 42, 'time': 4, } self.assertEqual(retVal, game.catchNoNil(['1', '1'])) game._save_game.assert_called_with([2, 3], 4, 6) # it does not fail when exceeding the limits retVal = { 'fishList': [2, 3], 'cues': 42, 'time': 6, } self.assertEqual(retVal, game.catchNoNil(['1', '1', '1'])) game._save_game.assert_called_with([2, 3], 6, 8) # it does not fail when the time runs out game._common_init = Mock(return_value=(spotYield, 0)) game._common_catch_init = Mock(return_value=(8, 10, 1)) retVal = { 'fishList': [1], 'cues': 42, 'time': 10, } self.assertEqual(retVal, game.catchNoNil(['1', '1', '1'])) game._save_game.assert_called_with([1], 10, 2)
def test_addYield(self): # we can only really test that we add yields # with desired fish maxTime = TOTAL_TIME / FISHING_COST ym = YieldMerger(maxTime) # parameter was captured self.assertEqual(ym.n, maxTime) player = Player.stub() yieldC = 0 # for every level in the game for i in range(len(GAME['levels'])): for id, fish in getFishForLevel(i).iteritems(): ym.addYield(id, fish, 5, player) yieldC += 1 # we get one more yield: self.assertEqual(len(ym.yields), yieldC) # the size of the yield is maxTime self.assertEqual(len(ym.yields[yieldC - 1]), maxTime) # check that this yield only has this fish or nothing for y in ym.yields[yieldC - 1]: self.assertTrue(None == y or y['id'] == id) # for the merge part, we will check that # yieldmerger is biased for choosing a fish merged = ym.merge() self.assertEquals(len(merged), maxTime) for i in range(maxTime): empty = True for y in ym.yields: empty = empty and (y[i] == None) # none can only be selected if empty # otherwise a fish must be there self.assertEqual(empty, merged[i] == None)
def test_getOptEarnings(self): game = Game.stub(Player.stub(), getLevel(0)) game.getOptimalTime = Mock(return_value=2) game.getMoneyEarnedIn = Mock(return_value=1) self.assertEqual(7, game.getOptEarnings(5, 60)) self.assertEqual(7, game.getOptimalTime.call_count) game.getMoneyEarnedIn.assert_called_with(6, 2)
def test_hasEnoughFor(self): player = Player.stub() player.money = 10 self.assertTrue(player.hasEnoughFor(5)) self.assertTrue(player.hasEnoughFor(10)) self.assertFalse(player.hasEnoughFor(11))
def test_getCues(self): player = Player.stub() game = Game.stub(player, getLevel(0)) game.level['position'] = 7 # mock methods game.ensureYieldsExist = Mock() player.getCueDetail = Mock(return_value=1) # patch the generators generators[0] = Mock(return_value=0) generators[1] = Mock(return_value=1) generators[2] = Mock(return_value=2) generators[3] = Mock(return_value=3) generators[4] = Mock(return_value=4) generators[5] = Mock(return_value=5) # it makes sure the yield exists game.getCues() game.ensureYieldsExist.assert_called_with(7) # it calls the generator, specific to user self.assertEqual(1, game.getCues()) generators[1].assert_called_with(game, 7) # no matter what that user uses player.getCueDetail = Mock(return_value=4) self.assertEqual(4, game.getCues()) generators[4].assert_called_with(game, 7)
def test_update(self): player = Player.stub() player.savePlayer = Mock() player.money = 10 # does not update a non-existant target self.assertFalse(player.update('whatever')) # does not update if there is not enough money self.assertFalse(player.update('lines')) # uses up the money and saves the player when successful player.money = 3000 self.assertTrue(player.update('lines')) self.assertEqual(2000, player.money) player.savePlayer.assert_called_with() # does the same second time self.assertTrue(player.update('lines')) self.assertEqual(0, player.money) player.savePlayer.assert_called_with() # does not update past the limit player.money = 30000000 self.assertFalse(player.update('lines'))
def test_get(self): player = Player.stub() # for every level for i in range(len(GAME['levels'])): game = Game.stub(player, getLevel(i)) # for every position it shows no info for j in range(len(game.level['map'][0])): game.setYieldFor(j) # creating a yield depth = game.getDephFor(j) output = [[-1, 0]] * depth self.assertEquals(output, DepthCue(game, j).get())
def test_recalcYields(self): game = Game.stub(Player.stub(), getLevel(0)) game.setYieldFor = Mock() game.level['yields'][0] = True game.level['yields'][2] = True game.level['yields'][9] = True game.recalcYields() self.assertEqual(3, game.setYieldFor.call_count) game.setYieldFor.assert_any_call(0) game.setYieldFor.assert_any_call(2) game.setYieldFor.assert_any_call(9)
def test_getSelectedBait(self): player = Player.stub() # no bait self.assertEqual(None, player.getSelectedBait()) # jig player.modifiers['jig'] = True self.assertEqual(1.5, player.getSelectedBait()['cod']) # worm player.modifiers['jig'] = False player.modifiers['worm'] = True self.assertEqual(1.2, player.getSelectedBait()['bass'])
def test_choose(self): player = Player.stub() player.savePlayer = Mock() player.modifiers['jig'] = False player.modifiers['worm'] = True # it does not select a modifier we don't have self.assertFalse(player.choose('wobbler')) # it changes the selected modifiers when we have them self.assertTrue(player.choose('jig')) self.assertTrue(player.modifiers['jig']) self.assertFalse(player.modifiers['worm']) player.savePlayer.assert_called_with()
def test_getMoneyEarnedIn(self): game = Game.stub(Player.stub(), getLevel(0)) game.level['yields'][7] = [ {'value': 25.0}, {'value': 15.0}, {'value': 15.0}, {'value': 5.0}, None, {'value': 50.0}, ] self.assertEqual(0.0, game.getMoneyEarnedIn(7, 0)) self.assertEqual(55.0, game.getMoneyEarnedIn(7, 3)) self.assertEqual(60.0, game.getMoneyEarnedIn(7, 4)) self.assertEqual(110.0, game.getMoneyEarnedIn(7, 6))
def test_getMoveCost(self): player = Player.stub() # no boats self.assertEqual(60, player.getMoveCost()) # Row Boat player.updates['boats'] = 'Row Boat' self.assertEqual(40, player.getMoveCost()) # Motor Boat player.updates['boats'] = 'Motor Boat' self.assertEqual(20, player.getMoveCost()) # Speed Boat player.updates['boats'] = 'Speed Boat' self.assertEqual(10, player.getMoveCost())
def test_getCueDetail(self): player = Player.stub() # no cues self.assertEqual(0, player.getCueDetail()) # A map player.updates['cues'] = 'A Map' self.assertEqual(1, player.getCueDetail()) # Old Sonar player.updates['cues'] = 'Old Sonar' self.assertEqual(3, player.getCueDetail()) # mermaid player.updates['cues'] = 'A Mermaid' self.assertEqual(5, player.getCueDetail())
def test_inspect(self): game = Game.stub(Player.stub(), getLevel(0)) game.ensureYieldsExist = Mock() game.level['yields'][0] = range(96) # it returns first 3 elements when asked fishList = {'fishList': [0, 1, 2]} self.assertEqual(fishList, game.inspect(3)) # it does not return more than one can possibly fish game.level['time'] = 450 self.assertEqual(None, game.inspect(30)) # it returns fish elements from the current point game.level['timeInLoc'][0] = 1 fishList = {'fishList': [1, 2, 3]} self.assertEqual(fishList, game.inspect(3))
def test_buy(self): player = Player.stub() player.savePlayer = Mock() player.modifiers['worm'] = True player.money = 200 # it does not buy baits that don't exist self.assertFalse(player.buy('whatever')) # it does not buy baits it already has self.assertFalse(player.buy('worm')) # it buys baits when everything is ok self.assertTrue(player.buy('spinner')) self.assertEqual(0, player.money) player.savePlayer.assert_called_with() # it does not buy baits when there is no money self.assertFalse(player.buy('vobbler'))
def test_move(self): game = Game.stub(Player.stub(), getLevel(0)) game.player.getMoveCost = Mock(return_value=10) game.logPerformance = Mock() game.saveGame = Mock() game.getCues = Mock(return_value=42) # moving outside from the map returns None self.assertEqual(None, game.move('left')) # successful move changes game state retVal = { 'position': 1, 'cues': 42, 'time': 10 } self.assertEqual(retVal, game.move('right')) game.saveGame.assert_called_with() game.logPerformance.assert_called_with() # if there is no more time left, the None is returned game.player.getMoveCost = Mock(return_value=10000) self.assertEqual(None, game.move('right'))
def test_augmentProb(self): player = Player.stub() # by default: no difference self.assertEqual(0.5, player.augmentProb('bass', 0.5)) # line effect player.updates['lines'] = 'Strong Line' prob = round(player.augmentProb('bass', 0.5), 4) self.assertEqual(0.6, prob) # + bait effect player.modifiers['worm'] = True prob = round(player.augmentProb('bass', 0.5), 4) self.assertEqual(0.72, prob) # - bait effect player.modifiers['worm'] = False prob = round(player.augmentProb('bass', 0.5), 4) self.assertEqual(0.6, prob)
def test_ensureYieldsExist(self): game = Game.stub(Player.stub(), getLevel(0)) game.setYieldFor = Mock() game.saveGame = Mock() # if the yield already exists: no action game.level['yields'][7] = True game.ensureYieldsExist(7) self.assertFalse(game.setYieldFor.called) # if the yield does not exist and don't save changes game.level['yields'][7] = None game.ensureYieldsExist(7, False) game.setYieldFor.assert_called_with(7) self.assertFalse(game.saveGame.called) # if the yield does not exist and save changes game.ensureYieldsExist(7) game.setYieldFor.assert_called_with(7) game.saveGame.assert_called_with()
def test_createAllYields(self): game = Game.stub(Player.stub(), getLevel(0)) game.ensureYieldsExist = Mock() game.createAllYields() self.assertEqual(20, game.ensureYieldsExist.call_count)
def test_getDepthFor(self): game = Game.stub(Player.stub(), getLevel(0)) depth = game.level['map'][0][7] self.assertEqual(depth, game.getDephFor(7))