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_specific_point(self): '''Gets information of a specific point, and check its result.''' database = MemcachedDatabase() new_robot = Robot("oie982736hhjf", "lo098173635") new_robot.set_location((9, 4)) database.add_robot(new_robot, (9, 4)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual(len(info), 9) self.assertEqual(info["9,4"], { "surface_type": MapSquareTypes.SOIL, "robot": True, "plant": None }) self.assertEqual(info["9,3"], { "surface_type": MapSquareTypes.WATER, "robot": False, "plant": None }) self.assertEqual(info["10,5"], { "surface_type": MapSquareTypes.SOIL, "robot": False, "plant": None }) self.assertEqual(info["8,4"], { "surface_type": MapSquareTypes.SOIL, "robot": False, "plant": None })
def test_specific_point(self): '''Gets information of a specific point, and check its result.''' database = MemcachedDatabase() new_robot = Robot("oie982736hhjf", "lo098173635") new_robot.set_location((9, 4)) database.add_robot(new_robot, (9, 4)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual(len(info), 9) self.assertEqual(info["9,4"], {"surface_type": MapSquareTypes.SOIL, "robot": True, "plant": None}) self.assertEqual(info["9,3"], {"surface_type": MapSquareTypes.WATER, "robot": False, "plant": None}) self.assertEqual(info["10,5"], {"surface_type": MapSquareTypes.SOIL, "robot": False, "plant": None}) self.assertEqual(info["8,4"], {"surface_type": MapSquareTypes.SOIL, "robot": False, "plant": None})
def test_corner(self): '''Tests getting a corner of the map.''' database = MemcachedDatabase() new_robot = Robot("0981kdjieu871", "oie987163") new_robot.set_location((0, 1)) database.add_robot(new_robot, (0, 1)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual(len(info), 6)
def test_blind_point(self): '''Gets information of a point, but don't care about the result.''' database = MemcachedDatabase() new_robot = Robot("1873yudhNCbueio", "ueijdnchiop") new_robot.set_location((9, 7)) database.add_robot(new_robot, (9, 7)) database.commit() action_manager = ActionManager() info = action_manager.do_action(new_robot.get_password(), "sense", [new_robot.get_id()]) self.assertEqual(len(info), 9)
def test_simple_add(self): '''Test adding a single robot to database.''' database = MemcachedDatabase() new_robot = Robot("test_simple_add_", "123") # No exception should be raise. database.add_robot(new_robot, (0, 0)) database.commit() gotted_robot = database.get_robot("test_simple_add_") self.assertEqual(gotted_robot.get_id(), new_robot.get_id()) self.assertEqual(gotted_robot.get_alive(), new_robot.get_alive()) self.assertEqual(gotted_robot.get_password(), new_robot.get_password())
def _born(self, password, args): '''Gives birth to a robot for the first time. @param args: List of arguments. The first argument can be a parent robot. If provided, the new robot will be born software near its parent. If not, it will be born on a random place. ''' parent_robot_id = None if len(args) > 0: parent_robot_id = args[0] if parent_robot_id is not None: if not isinstance(parent_robot_id, str): raise InvalidArgumentsError( "`parent_robot_id' must be `str', not {0}".format( type(parent_robot_id))) parent_robot = self._database.get_robot(parent_robot_id) born_location = parent_robot.get_location() else: world_size = self._world.get_size() born_location = (random.randint(0, world_size[0] - 1), random.randint(0, world_size[1] - 1)) robot_name = "" if len(args) == 2: robot_name = args[1] self._authenticator.authenticate_new_robot(password) new_robot = Robot(self._id_generator.get_robot_id(), self._id_generator.get_password(), name=robot_name) self._world.add_robot(new_robot, (born_location[0], born_location[1])) return { 'robot_id': new_robot.get_id(), 'password': new_robot.get_password() }
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}})