Skip to content

Commit 889fe73

Browse files
added tests for get_gateway
1 parent 593d786 commit 889fe73

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

test/test_chirpstack_client.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,134 @@ def test_get_device_activation_other_grpc_error(self, mock_insecure_channel, moc
954954

955955
self.assertEqual(result, {})
956956

957+
958+
959+
960+
961+
class TestGetGateway(unittest.TestCase):
962+
963+
@patch('chirpstack_api_wrapper.api.GatewayServiceStub')
964+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
965+
def test_get_gateway_happy_path(self, mock_insecure_channel, mock_gw_service_stub):
966+
"""
967+
Test get_gateway() method's happy path
968+
"""
969+
970+
# Mock the gRPC channel and login response
971+
mock_channel = Mock()
972+
mock_insecure_channel.return_value = mock_channel
973+
974+
# Mock the GatewayServiceStub
975+
mock_gw_service_stub_instance = mock_gw_service_stub.return_value
976+
mock_gw_service_stub_instance.Get.return_value = Mock(gw_info="mock_gw_info")
977+
978+
# Create a ChirpstackClient instance
979+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
980+
981+
# Mock the gateway id
982+
mock_gw_id = "mock_gw_id"
983+
984+
# Call method in teseting
985+
gw_info = client.get_gateway(mock_gw_id)
986+
987+
# Assert the result
988+
self.assertEqual(gw_info.gw_info, "mock_gw_info")
989+
990+
@patch('chirpstack_api_wrapper.api.GatewayServiceStub')
991+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
992+
@patch("chirpstack_api_wrapper.time.sleep", return_value=None) #dont time.sleep() for test case
993+
def test_get_gateway_unauthenticated_grpc_error(self, mock_sleep, mock_insecure_channel, mock_gw_service_stub):
994+
"""
995+
Test get_gateway() when grpc error is raised for UNAUTHENTICATED and token needs to be refreshed
996+
"""
997+
# Mock the gRPC channel and login response
998+
mock_channel = Mock()
999+
mock_insecure_channel.return_value = mock_channel
1000+
1001+
# Mock the get_device method to raise grpc.RpcError()
1002+
mock_rpc_error = grpc.RpcError()
1003+
mock_rpc_error.code = lambda: grpc.StatusCode.UNAUTHENTICATED
1004+
mock_rpc_error.details = lambda: 'ExpiredSignature'
1005+
1006+
# Mock the GatewayServiceStub
1007+
mock_gw_service_stub_instance = mock_gw_service_stub.return_value
1008+
mock_gw_service_stub_instance.Get.side_effect = mock_rpc_error
1009+
1010+
# Create a ChirpstackClient instance
1011+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
1012+
1013+
# Mock the gateway id
1014+
mock_gw_id = "mock_gw_id"
1015+
1016+
# Mock the login method to return a dummy token
1017+
with patch.object(client, "login", return_value="dummy_token"):
1018+
1019+
#mock refresh token successfully logging in and retrying the method in testing
1020+
with patch.object(client, "refresh_token", return_value="mock_gw_info"):
1021+
# Call the method in testing
1022+
result = client.get_gateway(mock_gw_id)
1023+
1024+
# assertations
1025+
self.assertEqual(result, "mock_gw_info")
1026+
1027+
@patch('chirpstack_api_wrapper.api.GatewayServiceStub')
1028+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
1029+
def test_get_gateway_not_found_grpc_error(self, mock_insecure_channel, mock_gw_service_stub):
1030+
"""
1031+
Test get_gateway() when grpc error is raised for NOT_FOUND
1032+
"""
1033+
# Mock the gRPC channel and login response
1034+
mock_channel = Mock()
1035+
mock_insecure_channel.return_value = mock_channel
1036+
1037+
# Mock the get_device method to raise grpc.RpcError()
1038+
mock_rpc_error = grpc.RpcError()
1039+
mock_rpc_error.code = lambda: grpc.StatusCode.NOT_FOUND
1040+
mock_rpc_error.details = lambda: 'Object does not exist'
1041+
1042+
# Mock the GatewayServiceStub
1043+
mock_gw_service_stub_instance = mock_gw_service_stub.return_value
1044+
mock_gw_service_stub_instance.Get.side_effect = mock_rpc_error
1045+
1046+
# Create a ChirpstackClient instance
1047+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
1048+
1049+
# Mock the gateway id
1050+
mock_gw_id = "mock_gw_id"
1051+
1052+
result = client.get_gateway(mock_gw_id)
1053+
1054+
self.assertEqual(result, {})
1055+
1056+
@patch("chirpstack_api_wrapper.api.GatewayServiceStub")
1057+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
1058+
def test_get_gateway_other_grpc_error(self, mock_insecure_channel, mock_gw_service_stub):
1059+
"""
1060+
Test get_gateway() when grpc error is not NOT_FOUND or UNAUTHENTICATED
1061+
"""
1062+
# Mock the gRPC channel and login response
1063+
mock_channel = Mock()
1064+
mock_insecure_channel.return_value = mock_channel
1065+
1066+
# Mock the get_device method to raise grpc.RpcError()
1067+
mock_rpc_error = grpc.RpcError()
1068+
mock_rpc_error.code = lambda: grpc.StatusCode.INTERNAL
1069+
mock_rpc_error.details = lambda: 'other'
1070+
1071+
# Mock the GatewayServiceStub
1072+
mock_gw_service_stub_instance = mock_gw_service_stub.return_value
1073+
mock_gw_service_stub_instance.Get.side_effect = mock_rpc_error
1074+
1075+
# Create a ChirpstackClient instance
1076+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
1077+
1078+
# Mock the gateway id
1079+
mock_gw_id = "mock_gw_id"
1080+
1081+
result = client.get_gateway(mock_gw_id)
1082+
1083+
self.assertEqual(result, {})
1084+
9571085
class TestListAggPagination(unittest.TestCase):
9581086

9591087
@patch('chirpstack_api_wrapper.api.DeviceServiceStub')

0 commit comments

Comments
 (0)