CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/postgresql-python-best-practices

PostgreSQL patterns for Python with psycopg and asyncpg — connection pooling,

99

1.15x
Quality

99%

Does it follow best practices?

Impact

99%

1.15x

Average score across 5 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-2/

Inventory Management API

Problem/Feature Description

A warehouse management company needs an inventory tracking API built with Python, FastAPI, and PostgreSQL. The API manages products across multiple warehouse locations and tracks stock movements.

The PostgreSQL database has these tables:

  • warehouses -- id, name, city, capacity_units, created_at
  • products -- id, sku (unique), name, category, weight_grams, created_at, updated_at
  • stock -- id, warehouse_id (FK), product_id (FK), quantity, min_quantity (reorder threshold), updated_at
  • stock_movements -- id, product_id (FK), from_warehouse_id (FK, nullable for inbound), to_warehouse_id (FK, nullable for outbound), quantity, reason, created_at

The API needs these endpoints/functions:

  1. Import product catalog -- Accept a CSV-like list of products (500-5000 items) with sku, name, category, weight. Products that already exist (by SKU) should be updated; new products should be created.

  2. Transfer stock -- Move a quantity of a product from one warehouse to another. This must decrease stock in the source warehouse, increase stock in the destination warehouse, and record a stock movement -- all atomically. If the source warehouse doesn't have enough stock, the transfer must fail without making any changes.

  3. Low stock report -- Find all stock entries where quantity is below min_quantity, showing the product name, warehouse name, current quantity, and deficit. The result should include products from all warehouses.

  4. Warehouse inventory export -- Given a warehouse ID, stream all products and their quantities for that warehouse. Some warehouses have 50,000+ products, so the response must handle large result sets efficiently.

The application uses asyncpg with FastAPI.

Output Specification

Produce:

  • app/db.py -- Async database pool setup
  • app/inventory.py -- The four functions: import_catalog, transfer_stock, low_stock_report, export_warehouse_inventory
  • app/main.py -- FastAPI application with pool lifecycle and route definitions

evals

tile.json