def test_no_plant(self):
        '''Tests when there's no plant to eat.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        with self.assertRaises(NoPlantToEat):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID])
        database.rollback()
    def test_no_plant(self):
        '''Tests when there's no plant to eat.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        with self.assertRaises(NoPlantToEat):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID])
        database.rollback()
    def test_locked(self):
        '''Tests with a locked square.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        database.get_square(TestEatAction.LOCATION, for_update=True)

        with self.assertRaises(LockAlreadyAquiredError):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID])
        database.rollback()
    def test_locked(self):
        '''Tests with a locked square.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        database.get_square(TestEatAction.LOCATION, for_update=True)

        with self.assertRaises(LockAlreadyAquiredError):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID])
        database.rollback()
    def test_bad_argument(self):
        '''Tests when sending bad arguments to the action.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID, None])
        database.rollback()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "eat", [TestEatAction.ROBOT_ID, "", 9])
        database.rollback()
    def test_blocking_object(self):
        '''Tests moving toward a blocking object.'''
        robot_id = "test_invalid_location_8765112"
        robot = Robot(robot_id, "123")
        world = World()
        action_manager = ActionManager()
        database = MemcachedDatabase()

        world.add_robot(robot, (11, 6))
        database.commit()

        with self.assertRaises(LocationIsBlockedError):
            action_manager.do_action("123", "move", [robot_id, "W"])
    def test_bad_argument(self):
        '''Tests when sending bad arguments to the action.'''
        action_manager = ActionManager()
        database = MemcachedDatabase()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "eat",
                                     [TestEatAction.ROBOT_ID, None])
        database.rollback()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "eat",
                                     [TestEatAction.ROBOT_ID, "", 9])
        database.rollback()
    def test_bad_direction(self):
        '''Sends an invalid direction as arguments.'''
        robot_id = "test_bad_direction_18445"
        robot = Robot(robot_id, "123")
        world = World()
        action_manager = ActionManager()
        database = MemcachedDatabase()

        world.add_robot(robot, (12, 6))
        database.commit()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "move", [robot_id, "U"])

        database.rollback()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "move", [robot_id, 988])

        database.rollback()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "move", [robot_id, None])

        database.rollback()

        with self.assertRaises(InvalidArgumentsError):
            action_manager.do_action("123", "move", [robot_id])

        database.rollback()
    def test_moving_outside(self):
        '''Tests moving a robot to outside of the world.'''
        robot_id = "test_moving_outside_981165"
        robot = Robot(robot_id, "123")
        world = World()
        action_manager = ActionManager()
        database = MemcachedDatabase()

        world.add_robot(robot, (14, 2))
        database.commit()

        with self.assertRaises(InvalidLocationError):
            action_manager.do_action("123", "move", [robot_id, "E"])

        database.rollback()
Exemple #10
0
    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
                }
            })
class Communicator(Singleton):
    '''Interface between listeners and the application.'''

    def _initialize(self):
        self._database = MemcachedDatabase()
        self._action_manager = ActionManager()
        self._population_control = PopulationControl()
        self._admin_handler = AdminHandler()

    def execute_command(self, password, command, args):
        '''Execute client's command.'''

        try:
            if command in ["born", "give_birth"]:
                result = self._population_control.execute_command(password, command, args)
            elif command == "map_data":
                result = self._admin_handler.execute_command(password, command, args)
            else:
                result = self._action_manager.do_action(password, command, args)

            # Committing or rollbacking all changes after the completion of execution.
            self._database.commit()
            return result

        except Exception:
            self._database.rollback()
            raise
Exemple #12
0
    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_lock(self):
        '''Tests if location is locked.'''
        robot_id = "test_move_lock_76120"
        robot = Robot(robot_id, "123")
        world = World()
        action_manager = ActionManager()
        database = MemcachedDatabase()

        world.add_robot(robot, (13, 6))
        database.commit()

        database.get_square((13, 7), for_update=True)

        with self.assertRaises(LockAlreadyAquiredError):
            action_manager.do_action("123", "move", [robot_id, "S"])

        database.rollback()
    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_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_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)
Exemple #19
0
    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)
Exemple #20
0
    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_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_directions(self):
        '''Tests if moving in different directions works correctly.'''
        robot_id = "test_directions_154332"
        robot = Robot(robot_id, "123")
        world = World()
        database = MemcachedDatabase()

        world.add_robot(robot, (10, 2))
        database.commit()

        action_manager = ActionManager()

        action_manager.do_action("123", "move", [robot_id, "N"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (10, 1))

        action_manager.do_action("123", "move", [robot_id, "NE"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (11, 0))

        action_manager.do_action("123", "move", [robot_id, "E"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (12, 0))

        action_manager.do_action("123", "move", [robot_id, "SE"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (13, 1))

        action_manager.do_action("123", "move", [robot_id, "S"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (13, 2))

        action_manager.do_action("123", "move", [robot_id, "SW"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (12, 3))

        action_manager.do_action("123", "move", [robot_id, "W"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (11, 3))

        action_manager.do_action("123", "move", [robot_id, "NW"])
        database.commit()
        robot = database.get_robot(robot_id)
        self.assertEqual(robot.get_location(), (10, 2))
class TestActionManager(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestActionManager, self).__init__(*args, **kwargs)

        self._action_manager = ActionManager()
        self._database = MemcachedDatabase()

    def tearDown(self):
        # Rolling back any remaining thing.
        self._database.rollback()

    def test_not_exist_robot(self):
        '''Test if ActionManager handles not-existed robot.'''
        with self.assertRaises(RobotNotFoundError):
            self._action_manager.do_action("123", "move",
                                           ["not_existed_robot_id"])

    def test_bad_robot_id(self):
        '''Test invalid robot IDs.'''
        # Note that listeners checks if `args' is a list, so action manager won't receive another type.

        with self.assertRaises(actions.exceptions.InvalidArgumentsError):
            self._action_manager.do_action("123", "move", [])

        with self.assertRaises(actions.exceptions.InvalidArgumentsError):
            self._action_manager.do_action("123", "move", [None])

    def test_invalid_password(self):
        '''Test if ActionManager authenticate passwords correctly.'''
        robot = Robot("test_invalid_password_95312", "andhue-ifue876-fkdnpw-1")
        self._database.add_robot(robot, (3, 1))
        self._database.commit()

        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("ieukjdf-ioquiwe-751io", "status",
                                           ["test_invalid_password_95312"])

    def test_dead_robot(self):
        '''Test if ActionManager checks a dead robot.'''
        robot = Robot("test_dead_robot_98176", "1234")
        robot.set_alive(False)
        self._database.add_robot(robot, (3, 2))
        self._database.commit()

        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("1234", "status",
                                           ["test_dead_robot_98176"])

    def test_bad_actions(self):
        '''Test wrong action IDs.'''
        robot = Robot("test_bad_actions_2376", "123")
        self._database.add_robot(robot, (4, 1))
        self._database.commit()

        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", "not-exist-action",
                                           ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", 5432,
                                           ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", None,
                                           ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", "",
                                           ["test_bad_actions_2376"])

    def test_ok(self):
        '''Execute a fine action.'''
        robot = Robot("test_ok_action_3278", "4467yrt-ddfjh-1u872-oiie")
        self._database.add_robot(robot, (3, 3))
        self._database.commit()

        initial_energy = robot.get_energy()
        initial_age = robot.get_life()

        result = self._action_manager.do_action("4467yrt-ddfjh-1u872-oiie",
                                                "status",
                                                ["test_ok_action_3278"])

        self.assertEqual(result['alive'], True)

        # Robot should lost energy and age.
        self._database.commit()
        robot = self._database.get_robot("test_ok_action_3278")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

    def test_losing_energy_on_error(self):
        '''Tests if ActionManager reduces energy and age after an exception.'''
        robot = Robot("test_losing_energy_on_error_981", "091oikjdmncj")
        self._database.add_robot(robot, (5, 3))
        self._database.commit()

        initial_energy = robot.get_energy()
        initial_age = robot.get_life()

        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("091oikjdmncj", "invalid_action",
                                           ["test_losing_energy_on_error_981"])

        self._database.commit()
        robot = self._database.get_robot("test_losing_energy_on_error_981")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

        # Robot shouldn't lose energy on authentication error.
        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("wrong pass", "invalid_action",
                                           ["test_losing_energy_on_error_981"])

        self._database.rollback()
        robot = self._database.get_robot("test_losing_energy_on_error_981")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

    def test_delay(self):
        '''Tests delay between robot actions.'''
        robot = Robot("test_delay_1223", "09112345")
        self._database.add_robot(robot, (6, 3))
        self._database.commit()

        self._action_manager.do_action("09112345", "sense", [robot.get_id()])
        self._database.commit()

        start_time = time.time()
        self._action_manager.do_action("09112345", "sense", [robot.get_id()])
        elapsed_time = time.time() - start_time
        self._database.commit()

        # one millisecond reduced from delay to cover error.
        self.assertGreater(elapsed_time, 0.029)
class TestActionManager(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(TestActionManager, self).__init__(*args, **kwargs)

        self._action_manager = ActionManager()
        self._database = MemcachedDatabase()

    def tearDown(self):
        # Rolling back any remaining thing.
        self._database.rollback()

    def test_not_exist_robot(self):
        '''Test if ActionManager handles not-existed robot.'''
        with self.assertRaises(RobotNotFoundError):
            self._action_manager.do_action("123", "move", ["not_existed_robot_id"])

    def test_bad_robot_id(self):
        '''Test invalid robot IDs.'''
        # Note that listeners checks if `args' is a list, so action manager won't receive another type.

        with self.assertRaises(actions.exceptions.InvalidArgumentsError):
            self._action_manager.do_action("123", "move", [])

        with self.assertRaises(actions.exceptions.InvalidArgumentsError):
            self._action_manager.do_action("123", "move", [None])

    def test_invalid_password(self):
        '''Test if ActionManager authenticate passwords correctly.'''
        robot = Robot("test_invalid_password_95312", "andhue-ifue876-fkdnpw-1")
        self._database.add_robot(robot, (3, 1))
        self._database.commit()

        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("ieukjdf-ioquiwe-751io", "status", ["test_invalid_password_95312"])

    def test_dead_robot(self):
        '''Test if ActionManager checks a dead robot.'''
        robot = Robot("test_dead_robot_98176", "1234")
        robot.set_alive(False)
        self._database.add_robot(robot, (3, 2))
        self._database.commit()

        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("1234", "status", ["test_dead_robot_98176"])

    def test_bad_actions(self):
        '''Test wrong action IDs.'''
        robot = Robot("test_bad_actions_2376", "123")
        self._database.add_robot(robot, (4, 1))
        self._database.commit()

        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", "not-exist-action", ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", 5432, ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", None, ["test_bad_actions_2376"])

        self._database.rollback()
        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("123", "", ["test_bad_actions_2376"])

    def test_ok(self):
        '''Execute a fine action.'''
        robot = Robot("test_ok_action_3278", "4467yrt-ddfjh-1u872-oiie")
        self._database.add_robot(robot, (3, 3))
        self._database.commit()

        initial_energy = robot.get_energy()
        initial_age = robot.get_life()

        result = self._action_manager.do_action("4467yrt-ddfjh-1u872-oiie", "status", ["test_ok_action_3278"])

        self.assertEqual(result['alive'], True)

        # Robot should lost energy and age.
        self._database.commit()
        robot = self._database.get_robot("test_ok_action_3278")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

    def test_losing_energy_on_error(self):
        '''Tests if ActionManager reduces energy and age after an exception.'''
        robot = Robot("test_losing_energy_on_error_981", "091oikjdmncj")
        self._database.add_robot(robot, (5, 3))
        self._database.commit()

        initial_energy = robot.get_energy()
        initial_age = robot.get_life()

        with self.assertRaises(actions.exceptions.InvalidActionError):
            self._action_manager.do_action("091oikjdmncj", "invalid_action", ["test_losing_energy_on_error_981"])

        self._database.commit()
        robot = self._database.get_robot("test_losing_energy_on_error_981")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

        # Robot shouldn't lose energy on authentication error.
        with self.assertRaises(AuthenticationFailedError):
            self._action_manager.do_action("wrong pass", "invalid_action", ["test_losing_energy_on_error_981"])

        self._database.rollback()
        robot = self._database.get_robot("test_losing_energy_on_error_981")
        self.assertEqual(robot.get_energy(), initial_energy - 1)
        self.assertEqual(robot.get_life(), initial_age - 1)

    def test_delay(self):
        '''Tests delay between robot actions.'''
        robot = Robot("test_delay_1223", "09112345")
        self._database.add_robot(robot, (6, 3))
        self._database.commit()

        self._action_manager.do_action("09112345", "sense", [robot.get_id()])
        self._database.commit()

        start_time = time.time()
        self._action_manager.do_action("09112345", "sense", [robot.get_id()])
        elapsed_time = time.time() - start_time
        self._database.commit()

        # one millisecond reduced from delay to cover error.
        self.assertGreater(elapsed_time, 0.029)