|
| 1 | +""" |
| 2 | +Tests for NetworkUtils class. |
| 3 | +
|
| 4 | +These tests validate the utility methods used for handling network responses. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from flashforge.api.network.utils import NetworkUtils |
| 10 | +from flashforge.models.responses import GenericResponse |
| 11 | + |
| 12 | + |
| 13 | +class TestNetworkUtilsIsOk: |
| 14 | + """Test cases for NetworkUtils.is_ok() method.""" |
| 15 | + |
| 16 | + def test_returns_true_for_code_zero(self): |
| 17 | + """Test that code 0 indicates success.""" |
| 18 | + response = GenericResponse(code=0, message="OK") |
| 19 | + assert NetworkUtils.is_ok(response) is True |
| 20 | + |
| 21 | + def test_returns_true_for_code_200(self): |
| 22 | + """Test that code 200 indicates success.""" |
| 23 | + response = GenericResponse(code=200, message="OK") |
| 24 | + assert NetworkUtils.is_ok(response) is True |
| 25 | + |
| 26 | + def test_returns_false_for_error_codes(self): |
| 27 | + """Test that error codes return False.""" |
| 28 | + for error_code in [-1, 1, 400, 401, 404, 500]: |
| 29 | + response = GenericResponse(code=error_code, message="Error") |
| 30 | + assert NetworkUtils.is_ok(response) is False, f"Code {error_code} should return False" |
| 31 | + |
| 32 | + def test_returns_false_for_none(self): |
| 33 | + """Test that None response returns False.""" |
| 34 | + assert NetworkUtils.is_ok(None) is False |
| 35 | + |
| 36 | + def test_handles_dict_response_with_code_zero(self): |
| 37 | + """Test that dict responses with code 0 return True.""" |
| 38 | + response = {"code": 0, "message": "OK"} |
| 39 | + assert NetworkUtils.is_ok(response) is True |
| 40 | + |
| 41 | + def test_handles_dict_response_with_code_200(self): |
| 42 | + """Test that dict responses with code 200 return True.""" |
| 43 | + response = {"code": 200, "message": "OK"} |
| 44 | + assert NetworkUtils.is_ok(response) is True |
| 45 | + |
| 46 | + def test_handles_dict_response_with_error_code(self): |
| 47 | + """Test that dict responses with error codes return False.""" |
| 48 | + response = {"code": 1, "message": "Error"} |
| 49 | + assert NetworkUtils.is_ok(response) is False |
| 50 | + |
| 51 | + def test_handles_dict_response_missing_code(self): |
| 52 | + """Test that dict responses without code field default to -1.""" |
| 53 | + response = {"message": "No code field"} |
| 54 | + assert NetworkUtils.is_ok(response) is False |
| 55 | + |
| 56 | + def test_handles_object_without_code_attribute(self): |
| 57 | + """Test objects without code attribute default to False.""" |
| 58 | + class MockResponse: |
| 59 | + def __init__(self): |
| 60 | + self.message = "No code attribute" |
| 61 | + |
| 62 | + response = MockResponse() |
| 63 | + assert NetworkUtils.is_ok(response) is False |
| 64 | + |
| 65 | + |
| 66 | +class TestNetworkUtilsGetErrorMessage: |
| 67 | + """Test cases for NetworkUtils.get_error_message() method.""" |
| 68 | + |
| 69 | + def test_returns_message_from_object(self): |
| 70 | + """Test extracting message from GenericResponse object.""" |
| 71 | + response = GenericResponse(code=1, message="Connection failed") |
| 72 | + assert NetworkUtils.get_error_message(response) == "Connection failed" |
| 73 | + |
| 74 | + def test_returns_message_from_dict(self): |
| 75 | + """Test extracting message from dict response.""" |
| 76 | + response = {"code": 1, "message": "File not found"} |
| 77 | + assert NetworkUtils.get_error_message(response) == "File not found" |
| 78 | + |
| 79 | + def test_returns_unknown_error_for_dict_without_message(self): |
| 80 | + """Test that dict without message returns 'Unknown error'.""" |
| 81 | + response = {"code": 1} |
| 82 | + assert NetworkUtils.get_error_message(response) == "Unknown error" |
| 83 | + |
| 84 | + def test_returns_unknown_error_for_object_without_message(self): |
| 85 | + """Test that object without message attribute returns 'Unknown error'.""" |
| 86 | + class MockResponse: |
| 87 | + def __init__(self): |
| 88 | + self.code = 1 |
| 89 | + |
| 90 | + response = MockResponse() |
| 91 | + assert NetworkUtils.get_error_message(response) == "Unknown error" |
| 92 | + |
| 93 | + def test_returns_no_response_for_none(self): |
| 94 | + """Test that None returns 'No response received'.""" |
| 95 | + assert NetworkUtils.get_error_message(None) == "No response received" |
| 96 | + |
| 97 | + def test_converts_message_to_string(self): |
| 98 | + """Test that message is converted to string.""" |
| 99 | + response = {"message": 12345} |
| 100 | + assert NetworkUtils.get_error_message(response) == "12345" |
0 commit comments