diff --git a/car/service.py b/car/service.py index 4c3eb5d..b413238 100644 --- a/car/service.py +++ b/car/service.py @@ -44,7 +44,7 @@ def find_by_license_plate(car_license_plate: str) -> Car: return car @staticmethod - def find_all_car_by_user(user: User) -> Car: + def find_all_car_by_user(user: User) -> List[Car]: return Car.query.filter(Car.car_owner_id == user.id).all() @staticmethod diff --git a/database/database.py b/database/database.py index bafc76f..e63207c 100644 --- a/database/database.py +++ b/database/database.py @@ -44,4 +44,12 @@ def init_db(): # import all modules here that might define models so that # they will be registered properly on the metadata. Otherwise # you will have to import them first before calling init_db() + import car.model + import chat.model + import parking_lot.model + import payment.model + import reservation.model + import settings.model + import user.model + Base.metadata.create_all(bind=engine) diff --git a/mock/mock.py b/mock/mock.py index bd3c459..108c45f 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -2,18 +2,15 @@ mock class """ -from datetime import datetime, timedelta -from car.service import CarService from chat.service import ChatService as _ -from database.database import Base, db_session, engine, init_db -from parking_lot.service import ParkingLotService -from payment.service import PaymentService -from reservation.service import ReservationService +from database.database import db_session, engine, init_db from settings.service import SettingService -from user.service import UserService +from mock.seed_car import seed_car from mock.seed_chat import seed_chat +from mock.seed_parking_lot import seed_parking_lot +from mock.seed_reservation import seed_reservation from mock.seed_user import seed_user @@ -21,7 +18,15 @@ class Mock: # drop all @staticmethod def clean_db(): - Base.metadata.drop_all(bind=engine) + try: + with engine.connect() as conn: + conn.execute("SET FOREIGN_KEY_CHECKS=0") + conn.execute( + "DROP TABLE `cars`, `chats`, `chat_heads`, `invoices`, `parking_lots`, `reservations`, `settings`, `users`" + ) + conn.execute("SET FOREIGN_KEY_CHECKS=1") + except Exception as e: + print(e) # setup database @staticmethod @@ -39,113 +44,16 @@ def mock(): # mock chat seed_chat() - # get root user - user = UserService.find_by_email("annie@example.com") - - # mock user car - car_1 = CarService.add(user, "A11111", "Tesla") - car_2 = CarService.add(user, "A22222", "Starship") - car_3 = CarService.add(user, "A33333", "Falcon9") - - # get all car - # user_cars = CarService.find_all_car_by_user(user) - # for car in user_cars: - # print(car) + # mock car + seed_car() # mock parking lot - parking_lot_1 = ParkingLotService.add("Floor 1", True) - parking_lot_2 = ParkingLotService.add("Floor 2", True) - parking_lot_3 = ParkingLotService.add("Floor 3", False) - - for i in range(1, 11): - # mock reservation - reservation = ReservationService.create_reservation( - user, car_1, parking_lot_1, datetime.utcnow() - ) - - if i < 10: - # end created reservation - ReservationService.end_reservation( - reservation, reservation.start_time + timedelta(hours=1) - ) - - # create invoice - invoice = PaymentService.create_invoice(reservation) - invoice.charge_amount = PaymentService.calculate_charge(reservation) - PaymentService.update_invoice(invoice) - - # try: - # PaymentService.setup_payment() - # invoice = PaymentService.create_invoice(reservation) - - # intent = PaymentService.create_pay_token(invoice) + seed_parking_lot() - # print(intent.client_secret) - # print(intent.id) - - # PaymentService.handle_stripe_payment(intent) - # except Exception as err: - # print(err) - - # reservation_2 = ReservationService.create_reservation( - # user, - # car_1, - # parking_lot_1, - # datetime.utcnow() - # ) - - # reservation_2 = ReservationService.create_reservation( - # user, - # car_2, - # parking_lot_2, - # datetime.utcnow() - # ) - - # reservation_3 = ReservationService.create_reservation( - # user, - # car_3, - # parking_lot_3, - # datetime.utcnow() - # ) - - # debug - # print(ParkingLotService.is_parking_lot_available(parking_lot_1)) - # print(ParkingLotService.is_parking_lot_available(parking_lot_2)) - # print(ParkingLotService.is_parking_lot_available(parking_lot_3)) - - # try create reservation on busy parking lot - # try: - # ReservationService.create_reservation( - # user, - # car_1, - # parking_lot_1, - # datetime.utcnow() - # ) - # except Exception as e: - # print(e) - - # try: - # ReservationService.end_reservation(reservation_2) - # t2_reserve = ReservationService.create_reservation( - # user, - # car_2, - # parking_lot_2, - # datetime.utcnow() - # ) - # ReservationService.end_reservation(t2_reserve) - # except Exception as e: - # print(e) - - # try: - # t_reserve = ReservationService.create_reservation( - # user, - # car_3, - # parking_lot_3, - # datetime.utcnow() - # ) - # ReservationService.end_reservation(t_reserve) - # except Exception as e: - # print(e) + # mock reservation + seed_reservation() # remove database session db_session.remove() + + print("Mocking complete!") diff --git a/mock/seed_car.py b/mock/seed_car.py new file mode 100644 index 0000000..ffb8edc --- /dev/null +++ b/mock/seed_car.py @@ -0,0 +1,18 @@ +""" + seed car +""" + + +def seed_car(): + from car.service import CarService + from user.service import UserService + + print("Mocking car...") + + # get root user + user = UserService.find_by_email("annie@example.com") + + # mock user car + CarService.add(user, "A11111", "Tesla") + CarService.add(user, "A22222", "Starship") + CarService.add(user, "A33333", "Falcon9") diff --git a/mock/seed_parking_lot.py b/mock/seed_parking_lot.py new file mode 100644 index 0000000..03e95c1 --- /dev/null +++ b/mock/seed_parking_lot.py @@ -0,0 +1,13 @@ +""" + seed parking lot +""" + + +def seed_parking_lot(): + from parking_lot.service import ParkingLotService + + print("Mocking parking lot...") + + ParkingLotService.add("Floor 1", True) + ParkingLotService.add("Floor 2", True) + ParkingLotService.add("Floor 3", False) diff --git a/mock/seed_reservation.py b/mock/seed_reservation.py new file mode 100644 index 0000000..9d85de4 --- /dev/null +++ b/mock/seed_reservation.py @@ -0,0 +1,36 @@ +""" + seed reservation +""" + + +def seed_reservation(): + print("Mocking reservation...") + + from datetime import datetime, timedelta + + from car.service import CarService + from parking_lot.service import ParkingLotService + from payment.service import PaymentService + from reservation.service import ReservationService + from user.service import UserService + + user = UserService.find_by_id(2) + car_1 = CarService.find_by_id(1) + parking_lot_1 = ParkingLotService.find_by_id(1) + + for i in range(1, 11): + # mock reservation + reservation = ReservationService.create_reservation( + user, car_1, parking_lot_1, datetime.utcnow() + ) + + if i < 10: + # end created reservation + ReservationService.end_reservation( + reservation, reservation.start_time + timedelta(hours=1) + ) + + # create invoice + invoice = PaymentService.create_invoice(reservation) + invoice.charge_amount = PaymentService.calculate_charge(reservation) + PaymentService.update_invoice(invoice) diff --git a/mock/seed_user.py b/mock/seed_user.py index 5112e65..528cffd 100644 --- a/mock/seed_user.py +++ b/mock/seed_user.py @@ -6,7 +6,9 @@ def seed_user(): from user.service import UserService - root = UserService.register( + print("Mocking user...") + + UserService.register( "root@example.com", "root-password", "root", @@ -15,7 +17,8 @@ def seed_user(): "0000000000", "0000000000000", ) - annie = UserService.register( + + UserService.register( "annie@example.com", "annie-password", "annie", @@ -24,7 +27,8 @@ def seed_user(): "1111111111", "1111111111111", ) - bobbie = UserService.register( + + UserService.register( "bobbie@example.com", "bobbie-password", "bobbie", @@ -33,7 +37,8 @@ def seed_user(): "2222222222", "2222222222222", ) - charlie = UserService.register( + + UserService.register( "charlie@example.com", "charlie-password", "charlie", @@ -42,3 +47,4 @@ def seed_user(): "3333333333", "3333333333333", ) + diff --git a/thunder-tests/thunderclient.json b/thunder-tests/thunderclient.json index ef5e16a..8e9c985 100644 --- a/thunder-tests/thunderclient.json +++ b/thunder-tests/thunderclient.json @@ -545,11 +545,11 @@ "colId": "e9e3d3d8-d79c-43e7-b6ea-dbb24aa38d46", "containerId": "", "name": "admin/invoice", - "url": "{{BASE_API_URL}}/payment/admin/invoice?page=1&limit=5&sort=1&order_by=invoice_id", + "url": "{{BASE_API_URL}}/payment/admin/list?page=1&limit=10&sort=0&order_by=invoice_id", "method": "GET", "sortNum": 5000, "created": "2022-10-11T06:49:53.245Z", - "modified": "2022-10-19T05:38:03.672Z", + "modified": "2022-10-22T12:02:33.735Z", "headers": [], "params": [ { @@ -559,12 +559,12 @@ }, { "name": "limit", - "value": "5", + "value": "10", "isPath": false }, { "name": "sort", - "value": "1", + "value": "0", "isPath": false }, { @@ -659,11 +659,11 @@ "colId": "e858eb49-2d8b-4bc5-bc1e-5eb5dcef9be0", "containerId": "", "name": "admin/list", - "url": "{{BASE_API_URL}}/car/admin/list?order_by=id&page=1&limit=10&sort=1&search=ar", + "url": "{{BASE_API_URL}}/car/admin/list?order_by=id&page=1&limit=10&sort=1", "method": "GET", "sortNum": 5000, "created": "2022-10-19T07:31:10.650Z", - "modified": "2022-10-19T07:37:05.987Z", + "modified": "2022-10-22T11:56:26.094Z", "headers": [], "params": [ { @@ -689,6 +689,7 @@ { "name": "search", "value": "ar", + "isDisabled": true, "isPath": false } ],