def test_water_level(self): '''Tests if water level increases after watering.''' database = MemcachedDatabase() world = World() robot = Robot("198.1287.fkdfjei", "123") robot.set_location((5, 0)) robot.set_has_water(True) plant = Plant() plant.set_water_level(30) world.plant(plant, (5, 0)) database.commit() action = WaterAction() action.do_action(robot, ["198.1287.fkdfjei"]) database.commit() updated_square = world.get_square((5, 0)) plant = updated_square.get_plant() # Checking if honor increased. self.assertEqual(robot.get_honor(), 1) self.assertEqual(plant.get_water_level(), 100) self.assertFalse(robot.get_has_water())
def test_bad_location(self): '''Tests planting on an invalid location.''' world = World() new_plant = Plant() with self.assertRaises(InvalidLocationError): world.plant(new_plant, (1881, 1998))
def test_one_cycle(self): '''Tests if plant updates correctly after one cycle.''' plant = Plant() world = World() database = MemcachedDatabase() world.plant(plant, (3, 8)) database.commit() # Sleeping one cycle. time.sleep(0.031) square = world.get_square((3, 8), for_update=False) plant = square.get_plant() self.assertEqual(plant.get_age(), 1) self.assertEqual(plant.get_water_level(), 90)
def test_maximum_age(self): '''Tests if plant die after maximum age.''' plant = Plant() world = World() database = MemcachedDatabase() world.plant(plant, (11, 8)) database.commit() square = world.get_square((11, 8), for_update=True) plant = square.get_plant() plant.set_age(40) database.commit() # Sleeping one cycle. time.sleep(0.031) square = world.get_square((11, 8), for_update=False) self.assertIsNone(square.get_plant())
def test_non_soil_location(self): '''Tests planting on a non-soil location.''' world = World() new_plant = Plant() with self.assertRaises(CannotPlantHereError): world.plant(new_plant, (2, 16)) with self.assertRaises(CannotPlantHereError): world.plant(new_plant, (3, 16))
def do_action(self, robot, args): '''Plant a plant where robot stands. @param robot: Instance of `objects.robot.Robot'. ''' if len(args) > 1: raise InvalidArgumentsError("`plant' action takes no arguments.") plant = Plant() self._world.plant(plant, robot.get_location())
def test_eating_not_matured(self): '''Tests when robot eat a non matured plant.''' world = World() database = MemcachedDatabase() action_manager = ActionManager() plant = Plant() plant.set_age(1) world.plant(plant, TestEatAction.LOCATION) database.commit() robot = database.get_robot(TestEatAction.ROBOT_ID, for_update=True) robot.set_energy(10) database.commit() action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID]) database.commit() updated_robot = database.get_robot(TestEatAction.ROBOT_ID) self.assertEqual(updated_robot.get_energy(), robot.get_energy() - 1)
def test_duplicate(self): '''Tests planting twice on a location.''' world = World() new_plant = Plant() world.plant(new_plant, (4, 16)) MemcachedDatabase().commit() with self.assertRaises(AlreadyPlantError): world.plant(new_plant, (4, 16))
def test_plant(self): '''Tests sensing a plant.''' world = World() database = MemcachedDatabase() new_robot = Robot("poeiekfm98871", "123") plant = Plant() plant.set_age(12) plant.set_water_level(60) world.plant(plant, (11, 4)) database.commit() world.add_robot(new_robot, (11, 4)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual( info["11,4"], { "surface_type": MapSquareTypes.SOIL, "robot": True, "plant": { "age": 12, "water_level": 60, "matured": True } })
def test_plant(self): '''Tests sensing a plant.''' world = World() database = MemcachedDatabase() new_robot = Robot("poeiekfm98871", "123") plant = Plant() plant.set_age(12) plant.set_water_level(60) world.plant(plant, (11, 4)) database.commit() world.add_robot(new_robot, (11, 4)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual(info["11,4"], {"surface_type": MapSquareTypes.SOIL, "robot": True, "plant": {"age": 12, "water_level": 60, "matured": True}})
def test_out_of_water(self): '''Tests if plant die after running out of water.''' plant = Plant() world = World() database = MemcachedDatabase() world.plant(plant, (4, 8)) database.commit() # Waiting for 11 cycles. time.sleep(11 * 0.03) square = world.get_square((4, 8), for_update=False) self.assertIsNone(square.get_plant())
def test_no_cycle_passed(self): '''Tests if plant not changed if no cycle passed.''' world = World() database = MemcachedDatabase() plant = Plant() plant.set_age(2) plant.set_water_level(70) world.plant(plant, (6, 8)) database.commit() # Waiting just a little. time.sleep(0.01) square = world.get_square((6, 8), for_update=False) received_plant = square.get_plant() self.assertEqual(received_plant.get_age(), plant.get_age()) self.assertEqual(received_plant.get_water_level(), plant.get_water_level()) self.assertEqual(received_plant.get_last_update(), plant.get_last_update())
def test_locked_square(self): '''Tests updating a locked square.''' plant = Plant() world = World() database = MemcachedDatabase() world.plant(plant, (7, 8)) database.commit() # Locking the square. world.get_square((7, 8), for_update=True) # Sleeping one cycle. time.sleep(0.031) with self.assertRaises(LockAlreadyAquiredError): world.get_square((7, 8), for_update=False)
def test_getting_data(self): robot = Robot("13329.12900.12213", "123", name="HappyBot") robot.set_energy(124) robot.set_honor(7) robot.set_life(3) robot.set_has_water(True) plant = Plant() plant.set_age(64) plant.set_water_level(98) database = MemcachedDatabase() database.add_robot(robot, (6, 11)) square = database.get_square((5, 11), for_update=True) square.set_plant(plant) database.commit() expected_result = { "5,11": { "surface_type": MapSquareTypes.SOIL, "plant": { "water_level": 98, "matured": True, "age": 64 }, "robot": None }, "6,11": { "surface_type": MapSquareTypes.SOIL, "plant": None, "robot": { "name": "HappyBot", "has_water": True, "energy": 124, "life": 3, "honor": 7 } }, "6,2": { "surface_type": MapSquareTypes.ROCK, "robot": None, "plant": None } } communicator = Communicator() result = communicator.execute_command("NhdEr32Qcmp0Iue3", "map_data", expected_result.keys()) self.assertCountEqual(result, expected_result) for expected_key, expected_value in expected_result.items(): self.assertDictEqual(result[expected_key], expected_value)
def test_matured(self): plant = Plant() plant.set_age(4) self.assertTrue(plant.is_matured())
def test_ok(self): '''Tests a good scenario.''' world = World() new_plant = Plant() world.plant(new_plant, (0, 16))