I’ve got the issue, that the SQLLite DB seems to forget all my tables when trying to write an entity while running my Unit Tests.
My setup looks as follows:
class TestBookingApi(unittest.TestCase):
def setUp(self):
self.engine = create_engine("sqlite:///", echo=True, connect_args={"check_same_thread": False})
Base.metadata.create_all(self.engine)
self.patch_db_session = patch('util.util.get_db_session', return_value=Session(self.engine))
self.patch_db_session.start()
def tearDown(self):
Base.metadata.drop_all(self.engine)
self.patch_db_session.stop()
def test_create_booking(self):
# when
booking = setup_booking()
# then
with Client(app) as client:
result = client.http.post(
"/api/bookings",
headers={'Content-Type': 'application/json'},
body=booking.json()
)
# expect
assert result.status_code == HTTPStatus.CREATED
body = result.json_body
api_booking = Booking(**body)
assert api_booking.name == booking.name
assert len(api_booking.hardware) == 2
def test_create_booking(self):
# when
booking = setup_booking()
# then
with Client(app) as client:
result = client.http.post(
"/api/bookings",
headers={'Content-Type': 'application/json'},
body=booking.json()
)
# expect
assert result.status_code == HTTPStatus.CREATED
body = result.json_body
api_booking = Booking(**body)
assert api_booking.name == booking.name
assert len(api_booking.hardware) == 2
The controller that writes into the db:
def create_booking():
request = api.current_request
try:
json_body = request.json_body
request_booking = parse_model(BookingRequest, json_body)
hardware = []
for hardware_id in request_booking.hardware_ids:
request_hardware = get_hardware(hardware_id)
hardware.append(request_hardware)
db_booking = convert_to_db_booking(request_booking, hardware)
bookings_db.create_booking(db_booking)
return Response(
status_code=HTTPStatus.CREATED,
headers={'Content-Type': 'application/json'},
body=db_booking.json()
)
except (ValidationError, ValueError) as e:
raise BadRequestError(str(e))
When i’m debugging now and set a debug point on bookings_db.create_booking(db_booking)
, every property on the db_booking entity has the following error:
[SQL: SELECT bookings.id AS bookings_id, bookings.name AS bookings_name, bookings.customer_id AS bookings_customer_id, bookings.booking_start AS bookings_booking_start, bookings.booking_end AS bookings_booking_end, bookings.created_at AS bookings_created_at, bookings.updated_at AS bookings_updated_at
FROM bookings
WHERE bookings.id = ?]
Anyone here with a clue what's the issue here?