class TestParkingLotCar(unittest.TestCase): @staticmethod def prepare_status(): return [ str_status_header, str_status_raw.format('1', 'ABC', 'White'), str_status_raw.format('2', 'MNO', 'Gray'), str_status_raw.format('3', 'PQR', 'Black'), str_status_raw.format('4', 'XYZ', 'White') ] def setUp(self): self.pk = ParkingLot(4) _, m1 = Car.create_and_park(self.pk, 'ABC', 'White') _, m2 = Car.create_and_park(self.pk, 'MNO', 'Gray') _, m3 = Car.create_and_park(self.pk, 'PQR', 'Black') _, m4 = Car.create_and_park(self.pk, 'XYZ', 'White') self.messages = [m1, m2, m3, m4] self.str_status = self.prepare_status() def test_allocated_slot_number_message(self): self.assertEquals(str_allocated_slot_number.format(1), self.messages[0]) self.assertEquals(str_allocated_slot_number.format(2), self.messages[1]) self.assertEquals(str_allocated_slot_number.format(3), self.messages[2]) self.assertEquals(str_allocated_slot_number.format(4), self.messages[3]) def test_status_message(self): messages = self.pk.status() self.assertEquals(self.str_status, messages) def test_registration_numbers_for_cars_with_colour(self): white_cars = self.pk.registration_numbers_for_cars_with_colour('White') self.assertEqual(['ABC', 'XYZ'], white_cars) self.assertNotEqual(['ABC', 'MNO'], white_cars) def test_slot_numbers_for_cars_with_colour(self): slot_numbers = self.pk.slot_numbers_for_cars_with_colour('White') self.assertEqual(['1', '4'], slot_numbers) self.assertNotEqual(['1', '3'], slot_numbers) def test_slot_numbers_for_registration_number(self): slot_numbers = self.pk.slot_number_for_registration_number('ABC') self.assertEqual('1', slot_numbers) def test_slot_numbers_for_registration_number_not_found(self): slot_numbers = self.pk.slot_number_for_registration_number('XXX') self.assertEqual('Not found', slot_numbers) def test_leave(self): message = self.pk.leave(1) self.assertEquals(str_leave.format(2), message) def test_already_left(self): message = self.pk.leave(1) self.assertRaises(ParkingSlotEmpty, self.pk.leave, 1)
if line[0] == 'create_parking_lot': pk = ParkingLot(int(line[1])) elif line[0] == 'park': try: _, message = Car.create_and_park(pk, line[1], line[2]) print(message) except Exception as e: print(e) elif line[0] == 'leave': try: message = pk.leave(int(line[1]) - 1) print(message) except Exception as e: print(e) elif line[0] == 'status': messages = pk.status() for message in messages: print(message) elif line[0] == 'registration_numbers_for_cars_with_colour': arr = pk.registration_numbers_for_cars_with_colour(line[1]) print(', '.join(arr)) elif line[0] == 'slot_numbers_for_cars_with_colour': arr = pk.slot_numbers_for_cars_with_colour(line[1]) print(', '.join(arr)) elif line[0] == 'slot_number_for_registration_number': print(pk.slot_number_for_registration_number(line[1])) line_str = raw_input('') except EOFError: sys.exit(0)
from parking_lot import ParkingLot, Car parking_lot = ParkingLot() cars = [ Car('KA-01-HH-1234', 'White'), Car('KA-01-HH-9999', 'White'), Car('KA-01-BB-0001', 'Black'), Car('KA-01-HH-7777', 'Red'), Car('KA-01-HH-2701', 'Blue'), Car('KA-01-HH-3141', 'Black'), ] assert parking_lot.create_parking_lot(6) is True for i in range(0, len(cars)): assert parking_lot.park(cars[i]) == i + 1 assert parking_lot.leave(4) is True assert parking_lot.status() is True assert len(parking_lot.available_parking_lots) == 1 assert parking_lot.park(Car('KA-01-P-333', 'White')) == 4 assert parking_lot.registration_numbers_for_cars_with_colour('White') == ['KA-01-HH-1234', 'KA-01-HH-9999', 'KA-01-P-333'] assert parking_lot.slot_numbers_for_cars_with_colour('White') == [1, 2, 4] assert parking_lot.slot_number_for_registration_number('KA-01-HH-3141') == 6 assert parking_lot.slot_number_for_registration_number('MH-04-AY-1111') is None