import unittest from munch import Munch from mosfet.info import blocks from mosfet.jobs.gather_crop import GatherCropStates class CropSectionCacheTests(unittest.TestCase): def make_world(self, crops): class FakeWorld: def __init__(self): self.crops = dict(crops) self.index_calls = 0 def find_blocks_indexed(self, _center, block_ids, _distance, sort=True): self.index_calls += 1 return [position for position, state in self.crops.items() if state in block_ids] @staticmethod def filter_work_positions(positions): return list(positions) def block_at(self, *position): return self.crops.get(tuple(position), blocks.AIR) return FakeWorld() def test_reuses_active_section_without_rebuilding_world_indexes(self): first = (1, 64, 0) second = (2, 64, 0) unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] world = self.make_world({ first: blocks.MATURE_WHEAT_ID, second: unripe_wheat, }) state = GatherCropStates(Munch(world=world, pos=(0, 64, 0))) state.find_new_crop() self.assertEqual(state.crop, first) self.assertEqual(state.active_section_positions, {first, second}) self.assertEqual(world.index_calls, 2) world.crops[first] = unripe_wheat world.crops[second] = blocks.MATURE_WHEAT_ID state.find_new_crop() self.assertEqual(state.crop, second) self.assertEqual(world.index_calls, 2) self.assertEqual(state.active_section_maturity, (1, 2)) def test_rebuilds_cache_when_a_cached_crop_disappears(self): first = (1, 64, 0) second = (2, 64, 0) unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] world = self.make_world({ first: blocks.MATURE_WHEAT_ID, second: unripe_wheat, }) state = GatherCropStates(Munch(world=world, pos=(0, 64, 0))) state.find_new_crop() self.assertEqual(world.index_calls, 2) world.crops.pop(first) world.crops[second] = blocks.MATURE_WHEAT_ID state.find_new_crop() self.assertEqual(state.crop, second) self.assertEqual(world.index_calls, 4) self.assertEqual(state.active_section_positions, {second}) if __name__ == '__main__': unittest.main()