def test_duplicate_add(self):
        '''Adding two robots with same ID. Should be failed.'''
        database = MemcachedDatabase()

        new_robot = Robot("test_duplicate_add_", "123")
        database.add_robot(new_robot, (1, 1))
        database.commit()

        robot2 = Robot("test_duplicate_add_", "123")
        with self.assertRaises(CannotAddObjectError):
            database.add_robot(robot2, (1, 2))
            database.commit()
    def test_duplicate(self):
        '''Tests adding duplicate robot.'''
        database = MemcachedDatabase()
        robot = Robot("world_duplicate_robot_8722", "123")

        self._world.add_robot(robot, (5, 1))

        database.commit()

        robot_2 = Robot("world_duplicate_robot_8722", "1236")
        with self.assertRaises(CannotAddObjectError):
            self._world.add_robot(robot_2, (5, 2))
            database.commit()
    def test_for_update(self):
        '''Tests of for_update flag works correctly.'''
        database = MemcachedDatabase()

        robot_id = "test_for_update_18762"
        robot = Robot(robot_id, "123")
        database.add_robot(robot, (10, 0))
        database.commit()

        new_robot = database.get_robot(robot_id, for_update=True)

        # Testing lock
        with self.assertRaises(LockAlreadyAquiredError):
            database.get_robot(robot_id, for_update=True)

        # Testing commit.
        new_robot.set_alive(False)

        # It shouldn't be changed yet.
        new_robot_2 = database.get_robot(robot_id)
        self.assertNotEqual(new_robot.get_alive(), new_robot_2.get_alive())

        # Actually committing changes.
        database.commit()
        new_robot_2 = database.get_robot(robot_id)
        self.assertEqual(new_robot.get_alive(), new_robot_2.get_alive())

        # Lock should be freed.
        database.get_robot(robot_id, for_update=True)
        database.rollback()
Exemple #4
0
    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_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)
Exemple #6
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
        })
Exemple #7
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
                }
            })
    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_wrong_password(self):
        '''Robot sent wrong password.'''
        authenticator = Authenticator()

        robot = Robot("wrong_password_robot_authentication_test", "123")
        with self.assertRaises(AuthenticationFailedError):
            authenticator.authenticate_robot(robot, "187")
Exemple #10
0
 def setUpClass(cls):
     # Creating a robot that all the tests will use.
     database = MemcachedDatabase()
     world = World()
     robot = Robot(TestGiveBirth.ROBOT_ID, "123")
     world.add_robot(robot, (0, 14))
     database.commit()
    def test_dead_robot(self):
        '''Authenticate a dead robot.'''
        authenticator = Authenticator()

        robot = Robot("dead_robot_authentication_test", "123")
        robot.set_alive(False)
        with self.assertRaises(AuthenticationFailedError):
            authenticator.authenticate_robot(robot, "123")
    def test_invalid_location(self):
        '''Tests adding a robot to an invalid location.'''
        database = MemcachedDatabase()
        robot = Robot("invalid_location_robot_1863", "123")

        with self.assertRaises(InvalidLocationError):
            self._world.add_robot(robot, (19872, 1190))
            database.commit()
    def test_bad_args(self):
        '''Tests the action with invalid arguments.'''
        plant_action = PlantAction()

        robot = Robot("918872.18711.0092", "123")

        with self.assertRaises(InvalidArgumentsError):
            plant_action.do_action(robot, ["918872.18711.0092", None])
    def setUpClass(cls):
        # Addin a robot to the world. All the tests would use this robot.
        robot = Robot(cls.ROBOT_ID, "123")
        world = World()

        world.add_robot(robot, cls.LOCATION)

        database = MemcachedDatabase()
        database.commit()
    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_ok(self):
        '''Tests a good scenario.'''
        action = PickWaterAction()
        robot = Robot("19882ukfdjfhuoIE", "123")
        robot.set_location((0, 18))

        action.do_action(robot, ["19882ukfdjfhuoIE"])

        self.assertTrue(robot.get_has_water())
    def test_invalid_location(self):
        '''Tests if database checks for invalid locations.'''
        database = MemcachedDatabase()

        new_robot = Robot("test_invalid_location_19887", "123")

        with self.assertRaises(InvalidLocationError):
            database.add_robot(new_robot, (91872, 16652))
            database.commit()
Exemple #18
0
    def test_have_no_water(self):
        '''Tests if a robot has water.'''
        robot = Robot("1223.9887.099", "123")
        robot.set_location((6, 0))
        robot.set_has_water(False)

        action = WaterAction()

        with self.assertRaises(RobotHaveNoWaterError):
            action.do_action(robot, ["1223.9887.099"])
    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_arguments(self):
        '''Tests invalid arguments.'''
        action = PickWaterAction()
        robot = Robot("19882ukfdjfhuoIE", "123")
        robot.set_location((0, 18))

        with self.assertRaises(InvalidArgumentsError):
            action.do_action(robot, [])

        with self.assertRaises(InvalidArgumentsError):
            action.do_action(robot, ["19882ukfdjfhuoIE", ""])
    def test_rollback(self):
        '''Tests if calling rollback works correctly.'''
        database = MemcachedDatabase()

        new_robot = Robot("test_rollback_87162", "123")
        database.add_robot(new_robot, (1, 1))

        database.rollback()
        database.commit()

        with self.assertRaises(RobotNotFoundError):
            database.get_robot("test_rollback_87162")
    def test_non_water_square(self):
        '''Tests trying to pick water from a dry location.'''
        action = PickWaterAction()
        robot = Robot("19882ukfdjfhuoIE", "123")

        robot.set_location((1, 18))
        with self.assertRaises(NoWaterError):
            action.do_action(robot, ["19882ukfdjfhuoIE"])

        robot.set_location((2, 18))
        with self.assertRaises(NoWaterError):
            action.do_action(robot, ["19882ukfdjfhuoIE"])
    def test_ok(self):
        '''Tests a good scenario.'''
        row = [MapSquare(MapSquareTypes.SOIL, (0, 17))]
        database = MemcachedDatabase()
        database.add_square_row(row)

        plant_action = PlantAction()

        robot = Robot("18873.182873.1123", "123")
        robot.set_location((0, 17))

        plant_action.do_action(robot, ["18873.182873.1123"])
    def test_blocked_location(self):
        '''Tests adding the robot to a blocked square.'''
        database = MemcachedDatabase()
        robot = Robot("test_blocked_location_91882", "123")

        # There's a rock here.
        self._world.add_robot(robot, (6, 1))
        database.commit()

        received_robot = database.get_robot(robot.get_id())

        self.assertNotEqual(received_robot.get_location(), (6, 1))
Exemple #25
0
    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_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_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))
    def test_bad_password(self):
        '''When password object is not a string.'''
        authenticator = Authenticator()

        robot = Robot("bad_password_robot_authentication_test", "123")

        with self.assertRaises(AuthenticationFailedError):
            authenticator.authenticate_robot(robot, None)

        with self.assertRaises(AuthenticationFailedError):
            authenticator.authenticate_robot(robot, Robot)

        with self.assertRaises(AuthenticationFailedError):
            authenticator.authenticate_robot(robot, -1)
Exemple #29
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)
Exemple #30
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)