def test_re_initParkingLot_raises_Exception(self): # Arrange s = EntryService() s.InitializeParkingLot(6) # Act # Re initialize Parking lot throws exception with self.assertRaises(Exception) as ex: s.InitializeParkingLot(6) # Assert self.assertEqual('Already Initialized Parking Lot', str(ex.exception))
def test_check_car_already_parked(self): s = EntryService() s.InitializeParkingLot(4) s.lotHandler.IsCarParked = MagicMock(return_value=True) result = s.CheckCarParkedAlready(Car('KA-01-HH-1234', 'White')) self.assertTrue(result)
def test_Reg_nums_by_color(self): s = EntryService() s.InitializeParkingLot(4) s.lotHandler.GetRegNumsByColor = MagicMock( return_value=['KA-01-HH-1234', 'KA-01-HH-9999', 'KA-01-P-333']) status = s.RegNumsByColor('Orange') self.assertEqual(status, 'KA-01-HH-1234, KA-01-HH-9999, KA-01-P-333')
def test_init_parking_lot(self): # Arrange s = EntryService() # Act status, _ = s.InitializeParkingLot(6) # Assert self.assertEqual(status, 'Created a parking lot with 6 slots')
def test_allow_entry_none(self): # Arrange s = EntryService() s.InitializeParkingLot(6) # Act with self.assertRaises(Exception) as ex: s.allow(None) # Assert self.assertEqual(InvalidInput, str(ex.exception))
def test_allow_entry_parking_lot_full(self): # Arrange car = Car('KA-01-HH-1234', 'White') s = EntryService() s.InitializeParkingLot(6) s.lotHandler.IsLotFull = MagicMock(return_value=True) # Act status = s.allow(car) # Assert self.assertEqual(status, 'Sorry, parking lot is full')
def test_allow_re_park_car(self): # Arrange car = Car('KA-01-HH-1234', 'White') s = EntryService() s.InitializeParkingLot(6) s.lotHandler.IsCarParked = MagicMock(return_value=True) # Act status = s.allow(car) # Assert self.assertEqual( 'Cannot park already Parked Car: {0}'.format(car.RegNum), status)
def test_allow_slot_unavailable(self): # Arrange car = Car('KA-01-HH-1234', 'White') s = EntryService() s.InitializeParkingLot(6) s.lotHandler.IsCarParked = MagicMock(return_value=False) s.lotHandler.IsLotFull = MagicMock(return_value=False) s.lotHandler.GetNearestSlot = MagicMock(return_value=1) s.lotHandler.IsSlotAvailable = MagicMock(return_value=False) # Act with self.assertRaises(ValueError) as ex: s.allow(car) # Assert self.assertEqual('Slot Not Available'.format(car.RegNum), str(ex.exception))
def test_allow_park_car(self): # Arrange car = Car('KA-01-HH-1234', 'White') s = EntryService() s.InitializeParkingLot(6) s.lotHandler.IsCarParked = MagicMock(return_value=False) s.lotHandler.IsLotFull = MagicMock(return_value=False) s.lotHandler.GetNearestSlot = MagicMock(return_value=1) s.lotHandler.IsSlotAvailable = MagicMock(return_value=True) slot = Slot(1) s.lotHandler.FillSlot = MagicMock(return_value=slot) # Act status = s.allow(car) # Assert self.assertEqual(status, 'Allocated slot number: {0}'.format(1))
def test_slot_fro_reg_num(self): s = EntryService() s.InitializeParkingLot(4) s.lotHandler.GetSlotNumByRegNum = MagicMock(return_value='2') status = s.SlotNumForRegNum('KA-01-HH-9999') self.assertEqual(status, '2')
def test_slot_nums_by_color(self): s = EntryService() s.InitializeParkingLot(4) s.lotHandler.GetSlotNumsByColor = MagicMock(return_value=['2', '4']) status = s.SlotNumsByColor('Red') self.assertEqual(status, '2, 4')
class ProcessInput(object): def __init__(self): self.entrysvc = EntryService() self.exsvc = ExitService() self.serviceInstance = None self.valid_actions = [ 'create_parking_lot', 'park', 'leave', 'status', 'registration_numbers_for_cars_with_colour', 'slot_numbers_for_cars_with_colour', 'slot_number_for_registration_number' ] def interactive_mode(self): try: while True: inp = input() if inp.lower() == 'exit': break else: result = self.process(inp) print(result) except (KeyboardInterrupt, SystemExit): return except Exception as ex: raise ex def file_input_mode(self, input_file): if not os.path.exists(input_file): raise FileNotFoundError( 'File {} does not exists'.format(input_file)) with open(input_file, 'r') as fp: lines = fp.readlines() lines = [line.rstrip('\n') for line in lines] for inp in lines: res = self.process(inp) print(res) def process(self, inp): try: result = None cmds = inp.split() cmdlen = len(cmds) if cmdlen == 0: raise ValueError(UnknownErrorOccured) action = cmds[0] if action not in self.valid_actions: return TryAgain if action != 'create_parking_lot' and not self.entrysvc.IsInitialized: return MustCreateParkingLot if action == 'create_parking_lot': if cmdlen != 2: return action + ' Error. ' + TryAgain capacity = int(cmds[1]) result, self.serviceInstance = self.entrysvc.InitializeParkingLot( capacity) return result elif action == 'park': if cmdlen != 3: return action + ' Error. ' + TryAgain regNum = cmds[1] color = cmds[2] car = Car(regNum, color) result = self.entrysvc.allow(car) elif action == 'leave': if cmdlen != 2: return action + ' Error. ' + TryAgain slotNum = cmds[1] self.exsvc.lotHandler = self.serviceInstance result = self.exsvc.allow(slotNum) elif action == 'status': if cmdlen != 1: return action + ' Error. ' + TryAgain slots = self.entrysvc.ShowStatus() status = 'Slot No.{0}Registration No{0}Colour{0}\n'.format( '\t') for slot in slots: if slot.ParkedCar is not None: status += '{}\t{}\t{}\n'.format( slot.SlotNum, slot.ParkedCar.RegNum, slot.ParkedCar.Color) result = status elif action == 'registration_numbers_for_cars_with_colour': if cmdlen != 2: return action + ' Error. ' + TryAgain color = cmds[1] result = self.entrysvc.RegNumsByColor(color) elif action == 'slot_numbers_for_cars_with_colour': if cmdlen != 2: return action + ' Error. ' + TryAgain color = cmds[1] result = self.entrysvc.SlotNumsByColor(color) elif action == 'slot_number_for_registration_number': if cmdlen != 2: return action + ' Error. ' + TryAgain regNum = cmds[1] result = self.entrysvc.SlotNumForRegNum(regNum) else: return InvalidAction return result except Exception as ex: raise ex