| 1 | ||
| 2 | import sys, pytest, collections, collections.abc, urllib3.exceptions, _pytest.pytester, numpy; | |
| 3 | collections.Mapping = collections.abc.Mapping; | |
| 4 | collections.MutableMapping = collections.abc.MutableMapping; | |
| 5 | collections.MutableSet = collections.abc.MutableSet; | |
| 6 | collections.Sequence = collections.abc.Sequence; | |
| 7 | collections.Callable = collections.abc.Callable; | |
| 8 | collections.Iterable = collections.abc.Iterable; | |
| 9 | collections.Iterator = collections.abc.Iterator; | |
| 10 | urllib3.exceptions.SNIMissingWarning = urllib3.exceptions.DependencyWarning; | |
| 11 | pytest.RemovedInPytest4Warning = DeprecationWarning; | |
| 12 | _pytest.pytester.Testdir = _pytest.pytester.Pytester; | |
| 13 | numpy.PINF = numpy.inf; | |
| 14 | numpy.unicode_ = numpy.str_; | |
| 15 | numpy.bytes_ = numpy.bytes_; | |
| 16 | numpy.float_ = numpy.float64; | |
| 17 | numpy.string_ = numpy.bytes_; | |
| 18 | numpy.NaN = numpy.nan; | |
| 19 | ||
| 20 | ||
| 21 | import json | |
| 22 | from main import RestAPI | |
| 23 | ||
| 24 | print("=== FINAL COMPREHENSIVE TEST ===") | |
| 25 | api = RestAPI() | |
| 26 | ||
| 27 | # Test 1: User creation and sorting | |
| 28 | print("\n1. Testing user creation and sorting:") | |
| 29 | users = ["Zoe", "Alice", "Charlie", "Bob"] | |
| 30 | for user in users: | |
| 31 | response = api.post('/add', json.dumps({"user": user})) | |
| 32 | print(f" Added {user}") | |
| 33 | ||
| 34 | response = api.get('/users') | |
| 35 | all_users = json.loads(response) | |
| 36 | user_names = [user["name"] for user in all_users["users"]] | |
| 37 | print(f" Users sorted by name: {user_names}") | |
| 38 | assert user_names == ["Alice", "Bob", "Charlie", "Zoe"], "Users not sorted correctly" | |
| 39 | ||
| 40 | # Test 2: Complex IOU scenarios with debt cancellation | |
| 41 | print("\n2. Testing complex IOU scenarios:") | |
| 42 | # Alice lends Bob $10 | |
| 43 | response = api.post('/iou', json.dumps({"lender": "Alice", "borrower": "Bob", "amount": 10})) | |
| 44 | alice_state = json.loads(response)["users"][0] | |
| 45 | bob_state = json.loads(response)["users"][1] | |
| 46 | print(f" Alice lends Bob $10: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}") | |
| 47 | ||
| 48 | # Bob lends Alice $5 (partial cancellation) | |
| 49 | response = api.post('/iou', json.dumps({"lender": "Bob", "borrower": "Alice", "amount": 5})) | |
| 50 | alice_state = json.loads(response)["users"][0] | |
| 51 | bob_state = json.loads(response)["users"][1] | |
| 52 | print(f" Bob lends Alice $5: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}") | |
| 53 | assert alice_state["owed_by"]["Bob"] == 5.0, "Partial debt reduction failed" | |
| 54 | assert bob_state["owes"]["Alice"] == 5.0, "Partial debt reduction failed" | |
| 55 | ||
| 56 | # Bob lends Alice $5 (complete cancellation) | |
| 57 | response = api.post('/iou', json.dumps({"lender": "Bob", "borrower": "Alice", "amount": 5})) | |
| 58 | alice_state = json.loads(response)["users"][0] | |
| 59 | bob_state = json.loads(response)["users"][1] | |
| 60 | print(f" Bob lends Alice $5: Alice balance={alice_state['balance']}, Bob balance={bob_state['balance']}") | |
| 61 | assert "Bob" not in alice_state["owes"], "Complete debt cancellation failed" | |
| 62 | assert "Alice" not in bob_state["owed_by"], "Complete debt cancellation failed" | |
| 63 | ||
| 64 | # Test 3: Edge cases and error handling | |
| 65 | print("\n3. Testing edge cases and error handling:") | |
| 66 | ||
| 67 | # Empty user list | |
| 68 | response = api.get('/users', json.dumps({"users": []})) | |
| 69 | assert json.loads(response) == {"users": []}, "Empty user list handling failed" | |
| 70 | print(" Empty user list: ✓") | |
| 71 | ||
| 72 | # Non-existent users | |
| 73 | response = api.get('/users', json.dumps({"users": ["NonExistent"]})) | |
| 74 | assert json.loads(response) == {"users": []}, "Non-existent user handling failed" | |
| 75 | print(" Non-existent users: ✓") | |
| 76 | ||
| 77 | # Error conditions | |
| 78 | error_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 | ||
| 86 | for 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 | |
| 97 | print("\n4. Testing floating point precision:") | |
| 98 | response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.1})) | |
| 99 | response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.2})) | |
| 100 | response = api.post('/iou', json.dumps({"lender": "Charlie", "borrower": "Zoe", "amount": 0.3})) | |
| 101 | charlie_state = json.loads(response)["users"][0] | |
| 102 | zoe_state = json.loads(response)["users"][1] | |
| 103 | print(f" Charlie lends Zoe 0.1+0.2+0.3: Charlie balance={charlie_state['balance']}, Zoe balance={zoe_state['balance']}") | |
| 104 | assert charlie_state["balance"] == 0.6, "Floating point precision failed" | |
| 105 | assert zoe_state["balance"] == -0.6, "Floating point precision failed" | |
| 106 | ||
| 107 | # Test 5: Final state verification | |
| 108 | print("\n5. Final state verification:") | |
| 109 | response = api.get('/users') | |
| 110 | final_state = json.loads(response) | |
| 111 | print(" Final user balances:") | |
| 112 | for user in final_state["users"]: | |
| 113 | print(f" {user['name']}: balance={user['balance']}, owes={user['owes']}, owed_by={user['owed_by']}") | |
| 114 | ||
| 115 | print("\n=== ALL TESTS PASSED! ===") |
| Test Name | Status |
|---|---|
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.