3.15 Random Values Homework: SFI Backend QA Simulator

Build a reusable randomized QA simulator for the SFI automotive backend.

The simulator should work with

  • structured SFI part records
  • multiple backend actions (GET/search, POST/create, PUT/update, DELETE/remove)
  • existing spec numbers
  • repeated test runs (test_count)
  • duplicate-spec handling during creation

Requirements

  • Randomly pick a complete part record and an action with random.choice
  • Clearly show which record and action were selected and what backend-style result occurred
  • Reject a create when the spec number already exists
  • Run multiple tests and keep the code self-contained
  • Explain the full possible output set (3 parts x 4 actions = 12 possible pairs, and repeats are normal)

Grading

Homework Simulator: 0.40 of the 1.0 total (Popcorn Hacks 0.45 and MCQ 0.15 are graded separately).

Code Runner Challenge

Homework Hack - SFI Backend QA Simulator

View IPYNB Source
%%python

# CODE_RUNNER: Homework Hack - SFI Backend QA Simulator

import random

parts = [
    {
        "product_name": "Replacement Flywheels",
        "category": "Auto Racing",
        "spec_number": "1.1"
    },
    {
        "product_name": "Multiple Disc Clutch Assemblies",
        "category": "Drag Racing",
        "spec_number": "1.2"
    },
    {
        "product_name": "Racing Flywheel Record",
        "category": "Auto Racing",
        "spec_number": "2.1"
    }
]

actions = [
    "GET/search",
    "POST/create",
    "PUT/update",
    "DELETE/remove"
]

existing_spec_numbers = ["1.1", "2.1"]
test_count = 5

# choice() picks a whole record, so its fields stay together.
# There are 3 parts x 4 actions = 12 possible pairs, and repeats are normal.
for run in range(1, test_count + 1):
    part = random.choice(parts)
    action = random.choice(actions)
    spec = part["spec_number"]
    in_database = spec in existing_spec_numbers

    print("Run", run, "|", action, "|", part["product_name"], "(spec " + spec + ")")

    if action == "GET/search":
        if in_database:
            print("  Result: record found")
        else:
            print("  Result: not found in database")
    elif action == "POST/create":
        if in_database:
            print("  Result: REJECTED - duplicate spec number")
        else:
            existing_spec_numbers.append(spec)   # the record is now stored
            print("  Result: record created")
    elif action == "PUT/update":
        if in_database:
            print("  Result: record updated")
        else:
            print("  Result: cannot update, record does not exist")
    else:
        if in_database:
            existing_spec_numbers.remove(spec)
            print("  Result: record removed")
        else:
            print("  Result: cannot remove, record does not exist")

print("Existing spec numbers at end:", existing_spec_numbers)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...