I have a university task to create a flask app for car rental. When I manually(with Postman) create new order in the database, everything works good, but when I create the entry using test, it creates 1 extra(invalid) entry with almost the same info(except carId and userId). It happens when I try to run both tests(test_place_order and test_unauthorized).
test_flask.py:
class TestPlaceOrder(BaseTestCase):
def setUp(self):
super().setUp()
self.car_1 = dbu.create_entry(car_table, **self.car_1_data)
self.order_1 = dbu.create_entry(order_table, **self.order_1_data)
self.user = dbu.create_entry(user_table, **self.user_1_credentials)
def test_place_order(self):
resp = self.client.post(
url_for("api.place_order", carId=self.car_1.carId),
json={"shipDate": self.order_1_data["shipDate"], "returnDate": self.order_1_data["returnDate"]},
headers=self.get_auth_headers(self.user_1_credentials),
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json, {
'id': ANY
})
self.assertTrue(
Session().query(order_table).filter_by(shipDate=self.order_1.shipDate,
returnDate=self.order_1.returnDate).one()
)
Post method:
@api_blueprint.route("/cars/car/<int:carId>/order", methods=["POST"])
@jwt_required
def place_order(carId):
with session_scope():
current_user = get_jwt_identity()
user = dbu.get_entry_by_username(user_table, current_user)
if current_user:
order_data = OrderQuery().load(request.json)
order = dbu.create_entry(order_table,
userId=user.id,
carId=carId,
shipDate=order_data["shipDate"],
returnDate=order_data["returnDate"],
status="placed",
complete=False)
return jsonify({"id": OrderDetails().dump(order)["id"]})
else:
return jsonify(code=401, type="UNAUTHORIZED_ACCESS"), 401