|
| 1 | +from square.core.api_error import ApiError |
| 2 | +from square.types.error import Error |
| 3 | + |
| 4 | + |
| 5 | +class TestResponse: |
| 6 | + def __init__(self, data): |
| 7 | + for key, value in data.items(): |
| 8 | + if isinstance(value, list): |
| 9 | + setattr( |
| 10 | + self, |
| 11 | + key, |
| 12 | + [ |
| 13 | + TestResponse(item) if isinstance(item, dict) else item |
| 14 | + for item in value |
| 15 | + ], |
| 16 | + ) |
| 17 | + else: |
| 18 | + setattr(self, key, value) |
| 19 | + |
| 20 | + |
| 21 | +def test_exception_without_body(): |
| 22 | + exception = ApiError(status_code=400, body=None) |
| 23 | + |
| 24 | + assert "status_code: 400" in str(exception) |
| 25 | + assert exception.status_code == 400 |
| 26 | + assert len(exception.errors) == 1 |
| 27 | + assert isinstance(exception.errors[0], Error) |
| 28 | + assert exception.errors[0].code == "Unknown" |
| 29 | + assert exception.errors[0].category == "API_ERROR" |
| 30 | + assert exception.errors[0].field is None |
| 31 | + assert exception.errors[0].detail is None |
| 32 | + |
| 33 | + |
| 34 | +def test_exception_with_v1_error(): |
| 35 | + error_string = """{ |
| 36 | + "type": "INVALID_REQUEST", |
| 37 | + "message": "Invalid field value", |
| 38 | + "field": "customer_id" |
| 39 | + }""" |
| 40 | + exception = ApiError(status_code=400, body=error_string) |
| 41 | + |
| 42 | + assert "status_code: 400" in str(exception) |
| 43 | + assert "body:" in str(exception) |
| 44 | + assert exception.status_code == 400 |
| 45 | + assert len(exception.errors) == 1 |
| 46 | + assert isinstance(exception.errors[0], Error) |
| 47 | + assert exception.errors[0].code == "INVALID_REQUEST" |
| 48 | + assert exception.errors[0].category == "API_ERROR" |
| 49 | + assert exception.errors[0].field == "customer_id" |
| 50 | + assert exception.errors[0].detail == "Invalid field value" |
| 51 | + |
| 52 | + |
| 53 | +def test_exception_with_v1_error_without_type(): |
| 54 | + error_string = """{ |
| 55 | + "message": "Invalid field value", |
| 56 | + "field": "customer_id" |
| 57 | + }""" |
| 58 | + exception = ApiError(status_code=400, body=error_string) |
| 59 | + |
| 60 | + assert "status_code: 400" in str(exception) |
| 61 | + assert exception.status_code == 400 |
| 62 | + assert len(exception.errors) == 1 |
| 63 | + assert isinstance(exception.errors[0], Error) |
| 64 | + assert exception.errors[0].code == "Unknown" |
| 65 | + assert exception.errors[0].category == "API_ERROR" |
| 66 | + assert exception.errors[0].field == "customer_id" |
| 67 | + assert exception.errors[0].detail == "Invalid field value" |
| 68 | + |
| 69 | + |
| 70 | +def test_exception_with_v2_error(): |
| 71 | + error_string = """{ |
| 72 | + "errors": [{ |
| 73 | + "category": "API_ERROR", |
| 74 | + "code": "BAD_REQUEST", |
| 75 | + "detail": "Invalid input", |
| 76 | + "field": "email" |
| 77 | + }] |
| 78 | + }""" |
| 79 | + exception = ApiError(status_code=400, body=error_string) |
| 80 | + |
| 81 | + assert "status_code: 400" in str(exception) |
| 82 | + assert exception.status_code == 400 |
| 83 | + assert len(exception.errors) == 1 |
| 84 | + assert isinstance(exception.errors[0], Error) |
| 85 | + assert exception.errors[0].category == "API_ERROR" |
| 86 | + assert exception.errors[0].code == "BAD_REQUEST" |
| 87 | + assert exception.errors[0].detail == "Invalid input" |
| 88 | + assert exception.errors[0].field == "email" |
| 89 | + |
| 90 | + |
| 91 | +def test_exception_with_v2_error_as_dict(): |
| 92 | + errors = { |
| 93 | + "errors": [ |
| 94 | + { |
| 95 | + "category": "API_ERROR", |
| 96 | + "code": "BAD_REQUEST", |
| 97 | + "detail": "Invalid input", |
| 98 | + "field": "email", |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + exception = ApiError(status_code=400, body=errors) |
| 103 | + |
| 104 | + assert "status_code: 400" in str(exception) |
| 105 | + assert exception.status_code == 400 |
| 106 | + assert len(exception.errors) == 1 |
| 107 | + assert isinstance(exception.errors[0], Error) |
| 108 | + assert exception.errors[0].category == "API_ERROR" |
| 109 | + assert exception.errors[0].code == "BAD_REQUEST" |
| 110 | + assert exception.errors[0].detail == "Invalid input" |
| 111 | + assert exception.errors[0].field == "email" |
| 112 | + |
| 113 | + |
| 114 | +def test_exception_with_v2_error_as_object(): |
| 115 | + errors = TestResponse( |
| 116 | + { |
| 117 | + "errors": [ |
| 118 | + { |
| 119 | + "category": "API_ERROR", |
| 120 | + "code": "BAD_REQUEST", |
| 121 | + "detail": "Invalid input", |
| 122 | + "field": "email", |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | + ) |
| 127 | + |
| 128 | + exception = ApiError(status_code=400, body=errors) |
| 129 | + |
| 130 | + assert "status_code: 400" in str(exception) |
| 131 | + assert exception.status_code == 400 |
| 132 | + assert len(exception.errors) == 1 |
| 133 | + assert isinstance(exception.errors[0], Error) |
| 134 | + assert exception.errors[0].category == "API_ERROR" |
| 135 | + assert exception.errors[0].code == "BAD_REQUEST" |
| 136 | + assert exception.errors[0].detail == "Invalid input" |
| 137 | + assert exception.errors[0].field == "email" |
| 138 | + |
| 139 | + |
| 140 | +def test_exception_with_multiple_errors(): |
| 141 | + errors = { |
| 142 | + "errors": [ |
| 143 | + { |
| 144 | + "category": "API_ERROR", |
| 145 | + "code": "BAD_REQUEST", |
| 146 | + "detail": "Invalid input", |
| 147 | + "field": "email", |
| 148 | + }, |
| 149 | + { |
| 150 | + "category": "AUTHENTICATION_ERROR", |
| 151 | + "code": "UNAUTHORIZED", |
| 152 | + "detail": "Not authorized", |
| 153 | + "field": None, |
| 154 | + }, |
| 155 | + ] |
| 156 | + } |
| 157 | + exception = ApiError(status_code=400, body=errors) |
| 158 | + |
| 159 | + assert exception.status_code == 400 |
| 160 | + assert len(exception.errors) == 2 |
| 161 | + |
| 162 | + # First error |
| 163 | + assert exception.errors[0].category == "API_ERROR" |
| 164 | + assert exception.errors[0].code == "BAD_REQUEST" |
| 165 | + assert exception.errors[0].detail == "Invalid input" |
| 166 | + assert exception.errors[0].field == "email" |
| 167 | + |
| 168 | + # Second error |
| 169 | + assert exception.errors[1].category == "AUTHENTICATION_ERROR" |
| 170 | + assert exception.errors[1].code == "UNAUTHORIZED" |
| 171 | + assert exception.errors[1].detail == "Not authorized" |
| 172 | + assert exception.errors[1].field is None |
0 commit comments