Skip to content

Commit

Permalink
improve mock
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Oct 22, 2022
1 parent ec722f5 commit 22c56f2
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 123 deletions.
2 changes: 1 addition & 1 deletion car/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
132 changes: 20 additions & 112 deletions mock/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
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


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
Expand All @@ -39,113 +44,16 @@ def mock():
# mock chat
seed_chat()

# get root user
user = UserService.find_by_email("[email protected]")

# 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!")
18 changes: 18 additions & 0 deletions mock/seed_car.py
Original file line number Diff line number Diff line change
@@ -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("[email protected]")

# mock user car
CarService.add(user, "A11111", "Tesla")
CarService.add(user, "A22222", "Starship")
CarService.add(user, "A33333", "Falcon9")
13 changes: 13 additions & 0 deletions mock/seed_parking_lot.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions mock/seed_reservation.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 10 additions & 4 deletions mock/seed_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
def seed_user():
from user.service import UserService

root = UserService.register(
print("Mocking user...")

UserService.register(
"[email protected]",
"root-password",
"root",
Expand All @@ -15,7 +17,8 @@ def seed_user():
"0000000000",
"0000000000000",
)
annie = UserService.register(

UserService.register(
"[email protected]",
"annie-password",
"annie",
Expand All @@ -24,7 +27,8 @@ def seed_user():
"1111111111",
"1111111111111",
)
bobbie = UserService.register(

UserService.register(
"[email protected]",
"bobbie-password",
"bobbie",
Expand All @@ -33,7 +37,8 @@ def seed_user():
"2222222222",
"2222222222222",
)
charlie = UserService.register(

UserService.register(
"[email protected]",
"charlie-password",
"charlie",
Expand All @@ -42,3 +47,4 @@ def seed_user():
"3333333333",
"3333333333333",
)

13 changes: 7 additions & 6 deletions thunder-tests/thunderclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -559,12 +559,12 @@
},
{
"name": "limit",
"value": "5",
"value": "10",
"isPath": false
},
{
"name": "sort",
"value": "1",
"value": "0",
"isPath": false
},
{
Expand Down Expand Up @@ -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": [
{
Expand All @@ -689,6 +689,7 @@
{
"name": "search",
"value": "ar",
"isDisabled": true,
"isPath": false
}
],
Expand Down

0 comments on commit 22c56f2

Please sign in to comment.