def test_car_add_reservation_twice(): car = Car("Sportback", "BMW", "X1", "5", "Diesel, 7", "AWD") reservation = Reservation("02.01.2021", "09.01.2021", "Thomas") car.add_reservation(reservation) assert car.reservation == reservation assert car.last_reserved() == f'{datetime.now():%d.%m.%Y}' assert car.add_reservation(reservation) == "Cannot reserve today"
def test_not_reserved_cars(): car = Car("Sportback", "BMW", "X1", "5", "Diesel, 7", "AWD") reservation = Reservation("02.01.2021", "27.02.2021", "Thomas") car.add_reservation(reservation) car2 = Van("Van", "Volvo", "Splinter", "2", "Diesel, 14", "FWD", "1500L") cars = [car, car2] not_reserved = not_reserved_cars(cars) assert not_reserved == [car2]
def read_from_json(file_handle): """ function used to read from database returns a list """ cars = [] data = json.load(file_handle) for item in data: seats = item['Number of seats'] name = item['Name'] fuel_consumption = item['Type of fuel and fuel Consumption per 100km'] type = item['Type'] brand = item['Brand'] drive_type = item['Drive Type'] additional = item['Additional'] reservation = item['Reservation'] last_reserved = item['Last reserved'] rent = item['Rent'] if reservation is not None: reservation_date = item['Reservation']['Reservation date'] due_reservation_date = item['Reservation']['Due Reservation Date'] who_reserved = item['Reservation']['Who reserved'] reservation = Reservation(reservation_date, due_reservation_date, who_reserved) else: reservation = None if rent is not None: rent_date = item['Rent']['Rent date'] return_date = item['Rent']['Return date'] who_rented = item['Rent']['Who rented'] overdue = item['Rent']['Is overdue'] rent = Rent(rent_date, return_date, who_rented, overdue) else: rent = None if type == "Truck": load = additional['Load'] trailer = additional['Has trailer'] volume = additional['Max cargo volume'] car = Truck(type, brand, name, seats, fuel_consumption, drive_type, volume, load, trailer, reservation, rent, last_reserved) elif type == "Van": volume = additional['Max cargo volume'] car = Van(type, brand, name, seats, fuel_consumption, drive_type, volume, reservation, rent, last_reserved) else: car = Car(type, brand, name, seats, fuel_consumption, drive_type, reservation, rent, last_reserved) cars.append(car) return cars
def _reserve_rent(self, item, name): """ used to set reservation or rent depending on input """ beginning_date = self.ui.date_begin.text() end_date = self.ui.date_end.text() person_info = self.ui.name_details.text() if name == 'rent': try: rent = Rent(beginning_date, end_date, person_info) except InvalidDateRange: dialog = Dialog('daterange', self) dialog.exec_() except InvalidDate: dialog = Dialog('date', self) dialog.exec_() if beginning_date == '' and end_date == '': self.cars[item.car_index].rent = None else: self.cars[item.car_index].rent = rent if name == 'reservation': info = '' try: reservation = Reservation(beginning_date, end_date, person_info) info = self.cars[item.car_index].add_reservation(reservation) except InvalidDateRange: dialog = Dialog('daterange', self) dialog.exec_() except InvalidDate: dialog = Dialog('date', self) dialog.exec_() if info == "Cannot reserve today": self.ui.reserve_button.setText("Cannot reserve today") dialog = Dialog('reserve', self) dialog.exec_() self.ui.reserve_button.setEnabled(False) return self.cars_dict = write_to_database(self.cars) item.car_dict = self.cars_dict[item.car_index] item.car = self.cars[item.car_index] self._setup_car_info(item) if self.action == 'overdue': self._setup_car_list(self.cars, self.action) self.ui.date_begin.setEnabled(False) self.ui.date_end.setEnabled(False) self.ui.name_details.setEnabled(False) self.ui.reserve_button.setEnabled(False) self.ui.rent_button.setEnabled(False)
def test_search_multiple(): car = Car("Sportback", "BMW", "X1", "5", "Diesel, 7", "AWD") car2 = Van("Van", "Volvo", "Splinter", "2", "Diesel, 14", "FWD", "1500L") today = datetime.today() tommorow = today + timedelta(days=2) today = f'{datetime.now():%d.%m.%Y}' tommorow = f'{tommorow:%d.%m.%Y}' reservation = Reservation("9.01.2021", today, "Thomas") rent = Rent("9.01.2021", today, "Thomas") crit = { "Rent": { "Return date": tommorow } } car.rent = rent car2.add_reservation(reservation) search = search_database(crit, [car, car2]) assert search == [car, car2]
def test_reservation_int(): with pytest.raises(InvalidDate): Reservation(123, "06.01.2021", "Thomas")
def test_reservation_invalid_str(): with pytest.raises(InvalidDate): Reservation("ddd", "06.01.2021", "Thomas")
def test_reservation_invalid_date(): with pytest.raises(InvalidDate): Reservation("02-01-2021", "06.01.2021", "Thomas")
def test_reservation_invalid_range(): with pytest.raises(InvalidDateRange): Reservation("10.01.2021", "06.01.2021", "Thomas")
def test_car_add_reservation_expired(): car = Car("Sportback", "BMW", "X1", "5", "Diesel, 7", "AWD") reservation = Reservation("02.01.2021", "06.01.2021", "Thomas") car.add_reservation(reservation) assert car.reservation.check_expiration() == "EXPIRED"