How to Implement a Manual Approval Gate Mock in Python
Simulates a manual approval workflow with threshold-based rules, random decisions for medium amounts, and logs each result with timing.
Python code
24 linesimport random
import time
def approve_request(amount: float) -> bool:
if amount <= 1000:
return True
if amount <= 5000:
return random.random() < 0.7
return False
def main():
requests = [500, 1200, 7500, 3000, 50]
for amount in requests:
start = time.perf_counter()
approved = approve_request(amount)
elapsed = time.perf_counter() - start
status = "APPROVED" if approved else "REJECTED"
print(f"${amount:>7,.2f} -> {status} ({elapsed:.4f}s)")
if __name__ == "__main__":
main()
Output
$ 500.00 -> APPROVED (0.0000s)
$ 1,200.00 -> APPROVED (0.0000s)
$ 7,500.00 -> REJECTED (0.0000s)
$ 3,000.00 -> REJECTED (0.0000s)
$ 50.00 -> APPROVED (0.0000s)
How it works
This mock uses simple threshold checks to mimic an approval gate: amounts under $1,000 auto-approve, amounts over $5,000 auto-reject, and mid-range amounts pass with 70% probability. The random.random() call simulates a human or external decision, and time.perf_counter() measures the latency introduced by the gate. This pattern is useful for testing pipelines that must handle asynchronous or manual approval steps.
Common mistakes
- Using `random.random() < 0.7` without seeding can make tests non-deterministic.
- Forgetting that `time.perf_counter()` measures wall-clock time, not just CPU time.
- Hardcoding thresholds instead of reading them from config or environment variables.
Variations
- Use `secrets.randbelow()` or `random.SystemRandom()` for more security-sensitive decisions.
- Replace the boolean return with an enum like `ApprovalStatus.APPROVED` for richer state handling.
Real-world use cases
- Testing a payment orchestration service that must pause and wait for an approval webhook.
- Simulating a human-in-the-loop review step in a CI/CD deployment pipeline.
- Creating a fallback decision engine when the real approval service is unavailable.
Sponsored
More from Production deployment patterns
- Auto Rollback on Error Rate Exceeded in Python medium
- Automate Semantic Versioning with Conventional Commits in Python medium
- Design a Data Helper for Beginners in Python easy
- Docker healthcheck CMD mock in Python easy
- Generate a Mock Artifact Version Tag in Python easy
- Generate a docker-compose.yml with mock services in Python easy
Keep learning
Related tutorials and quizzes for this topic.