|
1 | 1 | ''' |
2 | 2 | Copyright (c) 2022 Skyflow, Inc. |
3 | 3 | ''' |
| 4 | +import platform |
| 5 | +import sys |
4 | 6 | import unittest |
5 | | -from skyflow._utils import http_build_query |
6 | | - |
| 7 | +from unittest import mock |
| 8 | +from skyflow._utils import http_build_query, getMetrics |
| 9 | +from version import SDK_VERSION |
7 | 10 |
|
8 | 11 | class TestUrlEncoder(unittest.TestCase): |
9 | 12 | def setUp(self) -> None: |
@@ -50,3 +53,63 @@ def test_encoder_array(self): |
50 | 53 |
|
51 | 54 | self.assertEqual( |
52 | 55 | http_data, "key=value&nested%5Barray%5D%5B0%5D=one&nested%5Barray%5D%5B1%5D=two&nested%5Bkey%5D=value") |
| 56 | + |
| 57 | + # Test Case 1: Success case |
| 58 | + def test_get_metrics(self): |
| 59 | + expected = { |
| 60 | + 'sdk_name_version': "skyflow-python@" + SDK_VERSION, |
| 61 | + 'sdk_client_device_model': platform.node(), |
| 62 | + 'sdk_client_os_details': sys.platform, |
| 63 | + 'sdk_runtime_details': "Python " + sys.version, |
| 64 | + } |
| 65 | + actual = getMetrics() |
| 66 | + self.assertEqual(actual, expected) |
| 67 | + |
| 68 | + @mock.patch('platform.node', return_value='') |
| 69 | + def test_getMetrics_no_device_model(self, mock_node): |
| 70 | + expected_output = { |
| 71 | + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, |
| 72 | + 'sdk_client_device_model': '', |
| 73 | + 'sdk_client_os_details': sys.platform, |
| 74 | + 'sdk_runtime_details': "Python " + sys.version |
| 75 | + } |
| 76 | + |
| 77 | + actual_output = getMetrics() |
| 78 | + expected_output['sdk_client_device_model'] = '' |
| 79 | + self.assertEqual(actual_output, expected_output) |
| 80 | + |
| 81 | + @mock.patch('platform.node', return_value='Mocked Device Model') |
| 82 | + def test_getMetrics_with_device_model(self, mock_node): |
| 83 | + expected_output = { |
| 84 | + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, |
| 85 | + 'sdk_client_device_model': 'Mocked Device Model', |
| 86 | + 'sdk_client_os_details': sys.platform, |
| 87 | + 'sdk_runtime_details': "Python " + sys.version |
| 88 | + } |
| 89 | + |
| 90 | + actual_output = getMetrics() |
| 91 | + self.assertEqual(actual_output, expected_output) |
| 92 | + |
| 93 | + @mock.patch('sys.platform', return_value='mocked_os') |
| 94 | + def test_getMetrics_with_os_details(self, mock_platform): |
| 95 | + expected_output = { |
| 96 | + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, |
| 97 | + 'sdk_client_device_model': platform.node(), |
| 98 | + 'sdk_client_os_details': sys.platform, |
| 99 | + 'sdk_runtime_details': "Python " + sys.version |
| 100 | + } |
| 101 | + actual_output = getMetrics() |
| 102 | + self.assertEqual(actual_output, expected_output) |
| 103 | + |
| 104 | + def test_getMetrics_with_runtime_details(self): |
| 105 | + expected_output = { |
| 106 | + 'sdk_name_version': 'skyflow-python@' + SDK_VERSION, |
| 107 | + 'sdk_client_device_model': platform.node(), |
| 108 | + 'sdk_client_os_details': sys.platform, |
| 109 | + 'sdk_runtime_details': 'Python ' + 'mocked_version' |
| 110 | + } |
| 111 | + |
| 112 | + with mock.patch('sys.version', 'mocked_version'), \ |
| 113 | + mock.patch('sys.version_info', new=(3, 11, 2)): |
| 114 | + actual_output = getMetrics() |
| 115 | + self.assertEqual(actual_output, expected_output) |
0 commit comments