let it go

Finished
1
2import sys, pytest, collections, collections.abc, urllib3.exceptions, _pytest.pytester, numpy;
3collections.Mapping = collections.abc.Mapping;
4collections.MutableMapping = collections.abc.MutableMapping;
5collections.MutableSet = collections.abc.MutableSet;
6collections.Sequence = collections.abc.Sequence;
7collections.Callable = collections.abc.Callable;
8collections.Iterable = collections.abc.Iterable;
9collections.Iterator = collections.abc.Iterator;
10urllib3.exceptions.SNIMissingWarning = urllib3.exceptions.DependencyWarning;
11pytest.RemovedInPytest4Warning = DeprecationWarning;
12_pytest.pytester.Testdir = _pytest.pytester.Pytester;
13numpy.PINF = numpy.inf;
14numpy.unicode_ = numpy.str_;
15numpy.bytes_ = numpy.bytes_;
16numpy.float_ = numpy.float64;
17numpy.string_ = numpy.bytes_;
18numpy.NaN = numpy.nan;
19
20
21import json
22from main import RestAPI
23
24print("=== FINAL COMPREHENSIVE TEST ===")
25api = RestAPI()
26
27# Test 1: User creation and sorting
28print("\n1. Testing user creation and sorting:")
29users = ["Zoe", "Alice", "Charlie", "Bob"]
30for user in users:
31 response = api.post('/add', json.dumps({"user": user}))
32 print(f" Added {user}")
33
34response = api.get('/users')
35all_users = json.loads(response)
36user_names = [user["name"] for user in all_users["users"]]
37print(f" Users sorted by name: {user_names}")
38assert user_names == ["Alice", "Bob", "Charlie", "Zoe"], "Users not sorted correctly"
39
40# Test 2: Complex IOU scenarios with debt cancellation
41print("\n2. Testing complex IOU scenarios:")
42# Alice lends Bob $10
43response = api.post('/iou', json.dumps({"lender": "Alice", "borrower": "Bob", "amount": 10}))
44alice_state = json.loads(response)["users"][0]
45bob_state = json.loads(response)["users"][1]
46print(f" Alice lends Bob $10: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}")
47
48# Bob lends Alice $5 (partial cancellation)
49response = api.post('/iou', json.dumps({"lender": "Bob", "borrower": "Alice", "amount": 5}))
50alice_state = json.loads(response)["users"][0]
51bob_state = json.loads(response)["users"][1]
52print(f" Bob lends Alice $5: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}")
53assert alice_state["owed_by"]["Bob"] == 5.0, "Partial debt reduction failed"
54assert bob_state["owes"]["Alice"] == 5.0, "Partial debt reduction failed"
55
56# Bob lends Alice $5 (complete cancellation)
57response = api.post('/iou', json.dumps({"lender": "Bob", "borrower": "Alice", "amount": 5}))
58alice_state = json.loads(response)["users"][0]
59bob_state = json.loads(response)["users"][1]
60print(f" Bob lends Alice $5: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}")
61assert "Bob" not in alice_state["owes"], "Complete debt cancellation failed"
62assert "Alice" not in bob_state["owed_by"], "Complete debt cancellation failed"
63
64# Test 3: Edge cases and error handling
65print("\n3. Testing edge cases and error handling:")
66
67# Empty user list
68response = api.get('/users', json.dumps({"users": []}))
69assert json.loads(response) == {"users": []}, "Empty user list handling failed"
70print(" Empty user list: ✓")
71
72# Non-existent users
73response = api.get('/users', json.dumps({"users": ["NonExistent"]}))
74assert json.loads(response) == {"users": []}, "Non-existent user handling failed"
75print(" Non-existent users: ✓")
76
77# Error conditions
78error_tests = [
79 ("Missing user name", lambda: api.post('/add', json.dumps({})), "User name is required"),
80 ("Duplicate user", lambda: api.post('/add', json.dumps({"user": "Alice"})), "already exists"),
81 ("Same lender/borrower", lambda: api.post('/iou', json.dumps({"lender": "Alice", "borrower": "Alice", "amount": 10})), "cannot be the same"),
82 ("Negative amount", lambda: api.post('/iou', json.dumps({"lender": "Alice", "borrower": "Bob", "amount": -5})), "non-negative"),
83 ("Non-existent user", lambda: api.post('/iou', json.dumps({"lender": "Alice", "borrower": "NonExistent", "amount": 5})), "does not exist")
84]
85
86for test_name, test_func, expected_error in error_tests:
87 try:
88 test_func()
89 print(f" {test_name}: ✗ (should have failed)")
90 except ValueError as e:
91 if expected_error in str(e):
92 print(f" {test_name}: ✓")
93 else:
94 print(f" {test_name}: ✗ (wrong error: {e})")
95
96# Test 4: Floating point precision
97print("\n4. Testing floating point precision:")
98response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.1}))
99response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.2}))
100response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.3}))
101charlie_state = json.loads(response)["users"][0]
102zoe_state = json.loads(response)["users"][1]
103print(f" Charlie lends Zoe 0.1+0.2+0.3: Charlie balance={charlie_state['balance']}, Zoe balance={zoe_state['balance']}")
104assert charlie_state["balance"] == 0.6, "Floating point precision failed"
105assert zoe_state["balance"] == -0.6, "Floating point precision failed"
106
107# Test 5: Final state verification
108print("\n5. Final state verification:")
109response = api.get('/users')
110final_state = json.loads(response)
111print(" Final user balances:")
112for user in final_state["users"]:
113 print(f" {user['name']}: balance={user['balance']}, owes={user['owes']}, owed_by={user['owed_by']}")
114
115print("\n=== ALL TESTS PASSED! ===")
Test NameStatus
test_add_user
Pass
test_borrower_has_negative_balance
Pass
test_both_users_have_0_balance
Pass
test_get_single_user
Pass
test_lender_has_negative_balance
Pass
test_lender_owes_borrower
Pass
test_lender_owes_borrower_less_than_new_loan
Pass
test_lender_owes_borrower_same_as_new_loan
Pass
test_no_users
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.