Skip to content

test_parkinglot

Test Parking lot's vehicle entry and exit scenarios.

TestNearestSpotAssigned

Test Case: to verify if nearest spot to entrance is assigned

Source code in parking_lot/tests/test_parkinglot.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
@pytest.mark.parametrize("num_vehicles, num_spots", [(2, 2)], indirect=True)
class TestNearestSpotAssigned:
    """Test Case: to verify if nearest spot to entrance is assigned"""

    def test_nearest_spots_assigned(
        self, num_spots, parking_lot, vehicles, park_vehicles
    ):
        """Two vehicles should be issued spots at two ends of spots, closest to entry"""
        park_vehicles(parking_lot, vehicles)
        assert (
            vehicles[0].ticket.spot_id is 0
            and vehicles[1].ticket.spot_id is num_spots - 1
        ) or (
            vehicles[1].ticket.spot_id is 0
            and vehicles[0].ticket.spot_id is num_spots - 1
        )

    def test_num_free_spots_after_vehicle_entrance(
        self, num_vehicles, num_spots, parking_lot
    ):
        """Unit Test to verify number of free spots is updated after vehicle's entry."""
        spot_type = ParkingSpotType.COMPACT
        assert parking_lot._num_free_spots[spot_type] == num_spots - num_vehicles

test_nearest_spots_assigned(num_spots, parking_lot, vehicles, park_vehicles)

Two vehicles should be issued spots at two ends of spots, closest to entry

Source code in parking_lot/tests/test_parkinglot.py
181
182
183
184
185
186
187
188
189
190
191
192
def test_nearest_spots_assigned(
    self, num_spots, parking_lot, vehicles, park_vehicles
):
    """Two vehicles should be issued spots at two ends of spots, closest to entry"""
    park_vehicles(parking_lot, vehicles)
    assert (
        vehicles[0].ticket.spot_id is 0
        and vehicles[1].ticket.spot_id is num_spots - 1
    ) or (
        vehicles[1].ticket.spot_id is 0
        and vehicles[0].ticket.spot_id is num_spots - 1
    )

test_num_free_spots_after_vehicle_entrance(num_vehicles, num_spots, parking_lot)

Unit Test to verify number of free spots is updated after vehicle's entry.

Source code in parking_lot/tests/test_parkinglot.py
194
195
196
197
198
199
def test_num_free_spots_after_vehicle_entrance(
    self, num_vehicles, num_spots, parking_lot
):
    """Unit Test to verify number of free spots is updated after vehicle's entry."""
    spot_type = ParkingSpotType.COMPACT
    assert parking_lot._num_free_spots[spot_type] == num_spots - num_vehicles

TestOneVehicleNoSpotAvailable

Test Case: to verify vehicle entry denial if no spot is available

Source code in parking_lot/tests/test_parkinglot.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
@pytest.mark.parametrize("num_vehicles, num_spots", [(1, 0)], indirect=True)
class TestOneVehicleNoSpotAvailable:
    """Test Case: to verify vehicle entry denial if no spot is available"""

    def test_vehicle_entry_denied(self, parking_lot, vehicles, park_vehicles):
        """Unit test to verify vehicle entry is denied when no spot is available"""
        park_vehicles(parking_lot, vehicles)
        assert vehicles[0].ticket is None

    def test_num_free_spots_after_vehicle_entrance(
        self, num_vehicles, num_spots, parking_lot
    ):
        """Unit Test to verify number of free spots is updated after vehicle's entry."""
        spot_type = ParkingSpotType.COMPACT
        assert parking_lot._num_free_spots[spot_type] == num_spots

test_num_free_spots_after_vehicle_entrance(num_vehicles, num_spots, parking_lot)

Unit Test to verify number of free spots is updated after vehicle's entry.

Source code in parking_lot/tests/test_parkinglot.py
150
151
152
153
154
155
def test_num_free_spots_after_vehicle_entrance(
    self, num_vehicles, num_spots, parking_lot
):
    """Unit Test to verify number of free spots is updated after vehicle's entry."""
    spot_type = ParkingSpotType.COMPACT
    assert parking_lot._num_free_spots[spot_type] == num_spots

test_vehicle_entry_denied(parking_lot, vehicles, park_vehicles)

Unit test to verify vehicle entry is denied when no spot is available

Source code in parking_lot/tests/test_parkinglot.py
145
146
147
148
def test_vehicle_entry_denied(self, parking_lot, vehicles, park_vehicles):
    """Unit test to verify vehicle entry is denied when no spot is available"""
    park_vehicles(parking_lot, vehicles)
    assert vehicles[0].ticket is None

TestOneVehicleSpotAvailable

Tests: 1. Ticket issued to vehicle entry with spot available. 2. Number of free spots reduces by 1.

Source code in parking_lot/tests/test_parkinglot.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@pytest.mark.parametrize("num_vehicles, num_spots", [(1, 1), (2, 2)], indirect=True)
class TestOneVehicleSpotAvailable:
    """Tests:
    1. Ticket issued to vehicle entry with spot available.
    2. Number of free spots reduces by 1.
    """

    def test_vehicle_ticket_issued(self, parking_lot, vehicles, park_vehicles):
        """Unit test to verify vehicle entry when spot is available"""
        park_vehicles(parking_lot, vehicles)
        assert vehicles[0].ticket is not None

    def test_num_free_spots_after_vehicle_entrance(
        self, num_vehicles, num_spots, parking_lot
    ):
        """Unit Test to verify number of free spots is updated after vehicle's entry."""
        spot_type = ParkingSpotType.COMPACT
        assert parking_lot._num_free_spots[spot_type] == num_spots - num_vehicles

test_num_free_spots_after_vehicle_entrance(num_vehicles, num_spots, parking_lot)

Unit Test to verify number of free spots is updated after vehicle's entry.

Source code in parking_lot/tests/test_parkinglot.py
133
134
135
136
137
138
def test_num_free_spots_after_vehicle_entrance(
    self, num_vehicles, num_spots, parking_lot
):
    """Unit Test to verify number of free spots is updated after vehicle's entry."""
    spot_type = ParkingSpotType.COMPACT
    assert parking_lot._num_free_spots[spot_type] == num_spots - num_vehicles

test_vehicle_ticket_issued(parking_lot, vehicles, park_vehicles)

Unit test to verify vehicle entry when spot is available

Source code in parking_lot/tests/test_parkinglot.py
128
129
130
131
def test_vehicle_ticket_issued(self, parking_lot, vehicles, park_vehicles):
    """Unit test to verify vehicle entry when spot is available"""
    park_vehicles(parking_lot, vehicles)
    assert vehicles[0].ticket is not None

TestTwoVehicleOneSpotAvailable

Test Case: to verify 2 vehicles entry concurrently

Source code in parking_lot/tests/test_parkinglot.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@pytest.mark.parametrize("num_vehicles, num_spots", [(2, 1)], indirect=True)
class TestTwoVehicleOneSpotAvailable:
    """Test Case: to verify 2 vehicles entry concurrently"""

    def test_vehicle_entry_denied(self, parking_lot, vehicles, park_vehicles):
        """One vehicle should be issued ticket, other should be denied entry"""
        park_vehicles(parking_lot, vehicles)
        assert (vehicles[0].ticket is None and vehicles[1].ticket is not None) or (
            vehicles[1].ticket is None and vehicles[0].ticket is not None
        )

    def test_num_free_spots_after_vehicle_entrance(
        self, num_vehicles, num_spots, parking_lot
    ):
        """Unit Test to verify number of free spots is updated after vehicle's entry."""
        spot_type = ParkingSpotType.COMPACT
        assert parking_lot._num_free_spots[spot_type] == num_spots - (num_vehicles - 1)

test_num_free_spots_after_vehicle_entrance(num_vehicles, num_spots, parking_lot)

Unit Test to verify number of free spots is updated after vehicle's entry.

Source code in parking_lot/tests/test_parkinglot.py
169
170
171
172
173
174
def test_num_free_spots_after_vehicle_entrance(
    self, num_vehicles, num_spots, parking_lot
):
    """Unit Test to verify number of free spots is updated after vehicle's entry."""
    spot_type = ParkingSpotType.COMPACT
    assert parking_lot._num_free_spots[spot_type] == num_spots - (num_vehicles - 1)

test_vehicle_entry_denied(parking_lot, vehicles, park_vehicles)

One vehicle should be issued ticket, other should be denied entry

Source code in parking_lot/tests/test_parkinglot.py
162
163
164
165
166
167
def test_vehicle_entry_denied(self, parking_lot, vehicles, park_vehicles):
    """One vehicle should be issued ticket, other should be denied entry"""
    park_vehicles(parking_lot, vehicles)
    assert (vehicles[0].ticket is None and vehicles[1].ticket is not None) or (
        vehicles[1].ticket is None and vehicles[0].ticket is not None
    )