CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/flask-testing

Write correct Flask tests -- app factory with test config, application context fixtures, database isolation, file uploads, auth testing, error handlers, mock.patch placement, and essential API test patterns

98

1.15x
Quality

99%

Does it follow best practices?

Impact

97%

1.15x

Average score across 5 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-2/

E-commerce Checkout Flow Tests

Write tests for a Flask e-commerce checkout API. The app uses the factory pattern, SQLAlchemy, and integrates with an external payment service.

Models

  • Product: id, name, price_cents (integer), stock (integer)
  • Cart: id, user_id (FK to User), created_at
  • CartItem: id, cart_id (FK to Cart), product_id (FK to Product), quantity (integer)
  • Order: id, user_id (FK to User), status (string: 'pending', 'confirmed', 'shipped', 'cancelled'), total_cents (integer), created_at
  • User: id, username, email, password_hash

Checkout Flow

The checkout endpoint is POST /api/checkout and accepts {"cart_id": <int>}. The view is in app/routes/orders.py and does:

  1. Validates the cart exists and belongs to the authenticated user
  2. Checks all items are in stock
  3. Calls charge_card(amount_cents, user) imported in app/routes/orders.py as from app.services.payment import charge_card
  4. Creates an Order with status='confirmed'
  5. Decrements product stock
  6. Calls send_confirmation_email(order) imported in app/routes/orders.py as from app.services.email import send_confirmation_email
  7. Returns 201 with order data

If charge_card raises PaymentError, the order is created with status='pending' and no email is sent.

Other Endpoints

  • POST /auth/login -- log in
  • GET /api/cart -- get current user's cart (authenticated)
  • POST /api/cart/items -- add item to cart (authenticated)
  • GET /api/orders -- list user's orders (authenticated)
  • GET /api/orders/<id> -- get order detail (authenticated)

Output

Produce test files under tests/ with comprehensive test coverage for the checkout flow. Include tests/conftest.py. Assume app code exists; you are only writing the tests.

evals

tile.json