TECHNOLOGIESTieInn Technologies

Testing the APIs that feed your data: status codes and JSON checks

Data Quality Automation with Python · 7 questions

Short notes

Key idea

Much warehouse data comes from APIs. Test the API boundary before loading: check the status code first (with a timeout), validate every record's fields, types and allowed values, check pagination completeness, and test error responses on purpose against a mock.

Syntax

r = requests.get(url, params={"page": 1}, timeout=10)
r.raise_for_status()
data = r.json()

class Order(BaseModel):
    order_id: int
    amount: float
    status: Literal["delivered", "shipped", "pending", "cancelled", "returned"]

Order.model_validate(rec)               # lax: "2" becomes 2
Order.model_validate(rec, strict=True)  # strict: refuses conversions

Paging: received count = reported total; no duplicate ids; reconcile ids with the source.

Example

A fake orders API whose pages after the first start one row late returns 79 of 80 orders: order 26 is never sent. A strict pydantic check flags {"order_id": "2", "amount": "12145.70"}, which lax mode accepted.

Traps

  • Parsing before checking the status (404 can return {}).
  • No timeout.
  • Trusting JSON types; lax validation hiding type drift.
  • Only happy-path tests: test 400, 401, 429, 5xx and empty results.

Want the full lesson with step-by-step examples, a project and a certificate? Data Quality Automation with Python course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    Testing the APIs that feed your data: status codes and JSON checks

  1. Question 1easy

    The API returns 429. What should the pipeline do?

  2. Testing the APIs that feed your data: status codes and JSON checks

  3. Question 2easy

    What does this print?

    import json
    rec = json.loads('{"order_id": "2", "amount": "12145.70"}')
    print(isinstance(rec["amount"], float))
  4. Testing the APIs that feed your data: status codes and JSON checks

  5. Question 3medium

    How many records fail validation?

    from typing import Literal
    from pydantic import BaseModel, ValidationError
    
    class Order(BaseModel):
        order_id: int
        status: Literal["delivered", "shipped", "pending", "cancelled", "returned"]
    
    failed = 0
    for rec in [{"order_id": 1, "status": "shipped"}, {"order_id": 2, "status": "on_hold"},
                {"order_id": 3}, {"order_id": 4, "status": "returned"}]:
        try:
            Order.model_validate(rec)
        except ValidationError:
            failed += 1
    print(failed)
  6. Testing the APIs that feed your data: status codes and JSON checks

  7. Question 4hard

    What does this print?

    from pydantic import BaseModel, ValidationError
    
    class Order(BaseModel):
        order_id: int
    
    results = []
    for strict in (False, True):
        try:
            Order.model_validate({"order_id": "2"}, strict=strict)
            results.append("ok")
        except ValidationError:
            results.append("error")
    print(results.count("ok"))
  8. Testing the APIs that feed your data: status codes and JSON checks

  9. Question 5medium

    How should you test that the pipeline fails loudly on a 401 from the API?

2 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.