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_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_ok(self): '''Test status of a good robot object.''' robot = Robot("status_test_robot", "123") action = StatusAction() result = action.do_action(robot, []) self.assertEqual(result['alive'], robot.get_alive())
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_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_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_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_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_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_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_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))
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_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_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_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_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_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_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)
def test_ok(self): '''Adds a good robot object to the world.''' database = MemcachedDatabase() robot = Robot("world_ok_robot_38364", "123") self._world.add_robot(robot, (5, 0)) database.commit() gotted_robot = database.get_robot(robot.get_id()) self.assertEqual(gotted_robot.get_alive(), robot.get_alive()) all_robots = database.get_all_robot_ids() self.assertIn(robot.get_id(), all_robots)
def test_info(self): '''Test received information.''' action = InfoAction() world = World() configs = Configs() robot = Robot("test_info_action_1293", "123") info = action.do_action(robot, [robot.get_id()]) self.assertEqual(info['world_size'], '{0},{1}'.format(*world.get_size())) self.assertEqual(info['plant_max_age'], configs.get_plant_max_age()) self.assertEqual(info['plant_matured_age'], configs.get_plant_matured_age()) self.assertEqual(info['action_delay'], configs.get_robots_actions_delay()) self.assertEqual(info['birth_required_honor'], configs.get_robots_birth_required_honor())
def test_locked_location(self): '''Tests adding a robot to a locked location.''' database = MemcachedDatabase() robot = Robot("test_locked_location_0023", "123") # Locking 8,1 database.get_square((8, 1), for_update=True) self._world.add_robot(robot, (8, 1)) database.commit() received_robot = database.get_robot(robot.get_id()) self.assertNotEqual(received_robot.get_location(), (8, 1))
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 test_bad_arguments(self): '''Calls the action with invalid arguments.''' action = InfoAction() database = MemcachedDatabase() robot = Robot("test_info_action_1293", "123") with self.assertRaises(InvalidArgumentsError): action.do_action(robot, [robot.get_id(), None]) database.rollback() with self.assertRaises(InvalidArgumentsError): action.do_action(robot, [robot.get_id(), "", "09"]) database.rollback() with self.assertRaises(InvalidArgumentsError): action.do_action(robot, [robot.get_id(), []])
def test_locked(self): '''Tests with a locked square.''' database = MemcachedDatabase() world = World() robot = Robot("test_locked_robot_190083", "123") # Setting the energy to zero, so the updater tries to update the square too. robot.set_energy(0) world.add_robot(robot, (5, 9)) database.commit() world.get_square((5, 9), for_update=True) with self.assertRaises(LockAlreadyAquiredError): database.get_robot(robot.get_id(), for_update=True)
def test_out_of_energy_robot(self): '''Tests when a robot ran out of energy.''' database = MemcachedDatabase() world = World() robot = Robot("test_out_of_energy_robot_18773", "123") robot.set_energy(0) world.add_robot(robot, (1, 9)) database.commit() got_robot = database.get_robot("test_out_of_energy_robot_18773", for_update=False) self.assertFalse(got_robot.get_alive()) square = world.get_square((1, 9)) self.assertIsNone(square.get_robot_id())
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 command(self, userId, args): if not args or len(args) == 0: resultString = ['Usage: command [command name] [args]', 'where command like go, attack, harvest'] else: robot = Robot(self.db.robots.find_one({'owner': userId})) tmpres = robot.add_command(args[0], args[1:]) if tmpres == 'OK': resultString = ['ACCEPTED'] self.db.robots.update( {'name': robot.name, 'owner': robot.owner}, {'$set': robot.to_update()} ) else: resultString = [tmpres] return resultString
def test_out_of_life_robot(self): '''Tests when a robot ran out of life.''' database = MemcachedDatabase() world = World() robot = Robot("test_out_of_life_robot_9022", "123") robot.set_life(0) world.add_robot(robot, (0, 9)) database.commit() received_robot = database.get_robot("test_out_of_life_robot_9022", for_update=False) self.assertFalse(received_robot.get_alive()) square = world.get_square((0, 9)) self.assertIsNone(square.get_robot_id())
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)
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")
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()
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_locked_square(self): '''Tests with a already-locked square.''' database = MemcachedDatabase() robot = Robot("oi981872yuweu.9887", "123") robot.set_location((5, 0)) robot.set_has_water(True) database.get_square((5, 0), for_update=True) action = WaterAction() with self.assertRaises(LockAlreadyAquiredError): action.do_action(robot, ["oi981872yuweu.9887"]) # Freeing lock. database.rollback()
def ls(self, userId, args): robot = self.db.robots.find_one({'owner': userId}) if not robot: self.db.robots.insert({ "name": generate_name(), "inventory": [], "x": 0, "y": 0, "owner": userId, "actions": [], "instructions": {}, "messages": [], "instructions_stage": 0, }) robot = self.db.robots.find_one({'owner': userId}) robot = Robot(robot) return robot.to_terminal()
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 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 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_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()
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_concurrent_add_failure(self): '''Tests the behavior of Database class, when concurrent add fails.''' # Mocking `cas' method, making it always return False. def mocked_cas(*args): return False mc_connection = MemcachedConnection().get_connection() original_cas = mc_connection.cas mc_connection.cas = unittest.mock.Mock(side_effect=mocked_cas) try: new_robot = Robot("test_concurrent_add_failure_9865", "123") database = MemcachedDatabase() with self.assertRaises(CouldNotSetValueBecauseOfConcurrencyError): database.add_robot(new_robot, (1, 1)) database.commit() finally: # Setting back the original cas method. mc_connection.cas = original_cas # Checking to see added robot is clearly rolled back. self.assertFalse(mc_connection.get(new_robot.get_id()))