Exemplo n.º 1
0
class UnitTestCreateParkingLotMethod(TestCase):
    def setUp(self):
        self.dao = MagicMock()
        self.service = ParkingLotService(self.dao)

    def test_with_valid_size(self):
        self.dao.create_slots = MagicMock(return_value=True)
        num_slots = self.service.create_parking_lot_of_size(6)
        self.assertEqual(6, num_slots)
        self.dao.create_slots.assert_called_once_with(6)

    def test_with_invalid_size(self):
        self.dao.create_slots = MagicMock(return_value=False)
        with self.assertRaises(ValueError) as e:
            num_slots = self.service.create_parking_lot_of_size(-6)
            output = self.command_processor.process(command)
        self.dao.create_slots.assert_not_called()

        with self.assertRaises(ValueError) as e:
            num_slots = self.service.create_parking_lot_of_size(6.3)
            output = self.command_processor.process(command)
        self.dao.create_slots.assert_not_called()

        with self.assertRaises(ValueError) as e:
            num_slots = self.service.create_parking_lot_of_size("6")
            output = self.command_processor.process(command)
        self.dao.create_slots.assert_not_called()
Exemplo n.º 2
0
 def test_interface(self):
     self.dao = MagicMock()
     mockVehicle = Vehicle("mock", Driver(15))
     self.dao.unpark_vehicle_at_slot_number = MagicMock(
         return_value=mockVehicle)
     self.service = ParkingLotService(self.dao)
     output = self.service.empty_slot(3)
     self.dao.unpark_vehicle_at_slot_number.assert_called_once_with(3)
     self.assertEqual(output, mockVehicle)
Exemplo n.º 3
0
class IntegrationTestParkVehicle(TestCase):
    def setUp(self):
        self.dao = ParkingLotDao()
        self.service = ParkingLotService(self.dao)

    def test_with_valid_vehicle(self):
        num_slots = self.service.create_parking_lot_of_size(2)
        self.assertEqual(2, num_slots)
        driver = Driver(22)
        vehicle1 = Vehicle("pb08-11-22-random", driver)
        parked_slot = self.service.park_vehicle(vehicle1)
        self.assertEqual(parked_slot.number, 1)
        self.assertEqual(parked_slot.parked_vehicle.number,
                         "pb08-11-22-random")
        driver = Driver(23)
        vehicle1clone = Vehicle("pb08-11-22-random", driver)
        with self.assertRaises(DuplicateVehicleError):
            parked_slot = self.service.park_vehicle(vehicle1clone)

        driver = Driver(23)
        vehicle2 = Vehicle("pb08-11-23-random", driver)
        parked_slot = self.service.park_vehicle(vehicle2)
        self.assertEqual(parked_slot.number, 2)
        self.assertEqual(parked_slot.parked_vehicle.number,
                         "pb08-11-23-random")

        driver = Driver(23)
        vehicle4 = Vehicle("pb08-11-34-random", driver)
        with self.assertRaises(ParkingFullError):
            parked_slot = self.service.park_vehicle(vehicle4)
            self.assertEqual(len(self.dao.empty_slots_heap), 0)

    def test_with_none_vehicle(self):
        num_slots = self.service.create_parking_lot_of_size(2)
        self.assertEqual(2, num_slots)
        with self.assertRaises(NoneVehicleError):
            parked_slot = self.service.park_vehicle(None)

    def test_with_invalid_driver(self):
        num_slots = self.service.create_parking_lot_of_size(2)
        self.assertEqual(2, num_slots)
        with self.assertRaises(InvalidDriverError):
            driver = None
            v = Vehicle("rndomplate", driver)
            parked_slot = self.service.park_vehicle(v)
        with self.assertRaises(InvalidDriverError):
            driver = Driver(None)
            v = Vehicle("rndomplate", driver)
            parked_slot = self.service.park_vehicle(v)
Exemplo n.º 4
0
    def setUp(self):
        self.dao = ParkingLotDao()
        self.service = ParkingLotService(self.dao)
        self.service.create_parking_lot_of_size(3)

        d1 = Driver(18)
        v1 = Vehicle("num1", d1)
        self.service.park_vehicle(v1)

        d2 = Driver(18)
        v2 = Vehicle("num2", d2)
        self.service.park_vehicle(v2)

        d3 = Driver(20)
        v3 = Vehicle("num3", d3)
        self.service.park_vehicle(v3)
Exemplo n.º 5
0
class IntegrationTestCreateParkingLotMethod(TestCase):
    def setUp(self):
        self.dao = ParkingLotDao()
        self.service = ParkingLotService(self.dao)

    def test_with_valid_size(self):
        num_slots = self.service.create_parking_lot_of_size(6)
        self.assertEqual(6, num_slots)
Exemplo n.º 6
0
class UnitTestParkVehicleMethod(TestCase):
    def setUp(self):
        self.dao = MagicMock()
        self.dao.create_slots = MagicMock(return_value=True)
        self.service = ParkingLotService(self.dao)

    def test_with_valid_vehicle(self):
        driver = Driver(33)
        vehicle = Vehicle("random", driver)
        slot = ParkingSlot(1, vehicle)
        self.dao.park_vehicle_at_closest_empty_slot = MagicMock(
            return_value=slot)
        res = self.service.park_vehicle(vehicle)
        self.dao.park_vehicle_at_closest_empty_slot.assert_called_once_with(
            vehicle)
        self.assertEqual(res, slot)
Exemplo n.º 7
0
 def setUp(self):
     self.dao = ParkingLotDao()
     self.service = ParkingLotService(self.dao)
Exemplo n.º 8
0
 def setUp(self):
     self.dao = MagicMock()
     self.dao.create_slots = MagicMock(return_value=True)
     self.service = ParkingLotService(self.dao)
Exemplo n.º 9
0
 def setUp(self):
     self.dao = MagicMock()
     self.service = ParkingLotService(self.dao)
Exemplo n.º 10
0
class IntegrationTestGovtRegulationQueries(TestCase):
    def setUp(self):
        self.dao = ParkingLotDao()
        self.service = ParkingLotService(self.dao)
        self.service.create_parking_lot_of_size(3)

        d1 = Driver(18)
        v1 = Vehicle("num1", d1)
        self.service.park_vehicle(v1)

        d2 = Driver(18)
        v2 = Vehicle("num2", d2)
        self.service.park_vehicle(v2)

        d3 = Driver(20)
        v3 = Vehicle("num3", d3)
        self.service.park_vehicle(v3)

    def test_vehicle_number_to_slot_number(self):
        self.assertEqual(
            self.service.get_slot_number_for_vehicle_number("num1"), 1)
        self.assertEqual(
            self.service.get_slot_number_for_vehicle_number("num2"), 2)
        self.assertEqual(
            self.service.get_slot_number_for_vehicle_number("num3"), 3)

    def test_slot_numbers_for_age(self):
        slot_nums = self.service.get_slot_numbers_for_driver_age(18)
        self.assertEqual(slot_nums[0], 1)
        self.assertEqual(slot_nums[1], 2)

        slot_nums = self.service.get_slot_numbers_for_driver_age(18)
        self.assertEqual(slot_nums[0], 1)

        slot_nums = self.service.get_slot_numbers_for_driver_age(14)
        self.assertEqual(len(slot_nums), 0)

    def test_parked_vehicle_number_for_age(self):
        vehicle_nums = self.service.get_parked_vehicle_numbers_of_driver_age(
            18)
        self.assertEqual(vehicle_nums[0], "num1")
        self.assertEqual(vehicle_nums[1], "num2")

        vehicle_nums = self.service.get_parked_vehicle_numbers_of_driver_age(
            20)
        self.assertEqual(vehicle_nums[0], "num3")

        vehicle_nums = self.service.get_parked_vehicle_numbers_of_driver_age(
            12)
        self.assertEqual(len(vehicle_nums), 0)
 def __init__(self):
     dao = ParkingLotDao()
     self.parking_lot_service = ParkingLotService(dao)
class CommandProcessor:
    def __init__(self):
        dao = ParkingLotDao()
        self.parking_lot_service = ParkingLotService(dao)

    def _read_input(self, input_arr, index):
        try:
            return input_arr[index]
        except IndexError:
            raise MalformedCommandError("Missing command arguments.")

    def process(self, command):
        input_arr = command.split()
        operationCommand = self._read_input(input_arr, 0)
        operation = commands_to_operation_map.get(operationCommand)
        if operation is None:
            raise MalformedCommandError(command + " missing valid operation.")
        if operation == "INIT":
            try:
                size = int(self._read_input(input_arr, 1))
            except ValueError:
                raise MalformedCommandError(
                    "{} is not int. Slot size should be int.".format(
                        input_arr[1]))
            try:
                num_slots = self.parking_lot_service.create_parking_lot_of_size(
                    size)
            except ValueError as e:
                raise MalformedCommandError(
                    "Lot size should be positive integer")
            return "Created parking of {} slots".format(num_slots)
        elif operation == "PARK":
            if self._read_input(input_arr, 2) != "driver_age":
                raise MalformedCommandError("Missing command arguments.")
            number_plate = self._read_input(input_arr, 1)
            try:
                driver_age = int(self._read_input(input_arr, 3))
                if driver_age <= 0:
                    raise MalformedCommandError(
                        "{} is negative. Age should be positive.".format(
                            input_arr[3]))
            except ValueError:
                raise MalformedCommandError(
                    "{} is not int. Drive age should be int.".format(
                        input_arr[3]))
            driver = Driver(driver_age)
            vehicle_to_park = Vehicle(number_plate, driver)
            try:
                parked_slot = self.parking_lot_service.park_vehicle(
                    vehicle_to_park)
                return 'Car with vehicle registration number "{}" has been parked at slot number {}'.format(
                    parked_slot.parked_vehicle.number, parked_slot.number)
            except DuplicateVehicleError as e:
                return "Vehicle with this registration number is already parked."
            except ParkingFullError as e:
                return "Cannot park more vehicles because parking is full."
        elif operation == "VEHICLE_NUM_TO_SLOT_NUM":
            vehicle_number = self._read_input(input_arr, 1)
            slot_num = self.parking_lot_service.get_slot_number_for_vehicle_number(
                vehicle_number)
            if slot_num is None:
                return ""
            return str(slot_num)
        elif operation == "AGE_TO_SLOT_NUMS":
            try:
                age = int(self._read_input(input_arr, 1))
                if age <= 0:
                    raise MalformedCommandError(
                        "{} is negative. Age should be positive.".format(
                            input_arr[1]))
            except ValueError:
                raise MalformedCommandError(
                    "{} is not int. Age should be int.".format(input_arr[1]))
            slot_nums = self.parking_lot_service.get_slot_numbers_for_driver_age(
                age)
            return ",".join([str(item) for item in slot_nums])
        elif operation == "AGE_TO_VEHICLE_NUMS":
            try:
                age = int(self._read_input(input_arr, 1))
                if age <= 0:
                    raise MalformedCommandError(
                        "{} is negative. Age should be positive.".format(
                            input_arr[1]))
            except ValueError:
                raise MalformedCommandError(
                    "{} is not int. Age should be int.".format(input_arr[1]))
            nums = self.parking_lot_service.get_parked_vehicle_numbers_of_driver_age(
                age)
            return ",".join([str(item) for item in nums])
        elif operation == "UNPARK":
            try:
                slot_num = int(self._read_input(input_arr, 1))
                if slot_num <= 0:
                    raise MalformedCommandError(
                        "{} is negative. Slot Number should be positive.".
                        format(input_arr[1]))
                vehicle = self.parking_lot_service.empty_slot(slot_num)
            except ValueError:
                raise MalformedCommandError(
                    "{} is not int. Slot Number should be int.".format(
                        input_arr[1]))
            except SlotNotPresentError:
                return 'Slot number not present.'
            if vehicle is None:
                return "Slot already vacant"
            return 'Slot number {} vacated, the car with vehicle registration number "{}" left the space, the driver of the car was of age {}'.format(
                slot_num, vehicle.number, vehicle.driver.age)