-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_yspeed.py
125 lines (103 loc) · 4.97 KB
/
test_yspeed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Test file for the yspeed.py file
"""
import unittest
from unittest.mock import Mock, patch, MagicMock
from contextlib import contextmanager
import sys
sys.path.append("Yspeed")
from yspeed import Yspeed, gather_network_info, print_network_info
@contextmanager
def progress_context_manager():
"""
Context manager for the progress bar
"""
progress_mock = MagicMock()
progress_mock.__enter__.return_value = progress_mock
yield progress_mock
progress_mock.__exit__.assert_called_once()
class TestYourClass(unittest.TestCase):
@patch('yspeed.Halo')
@patch('yspeed.time.sleep', MagicMock(return_value=None))
def test_run_speedtest(self, mock_halo):
""" Test the run_speedtest method"""
# Replace 'YourClass' with the actual name of the class containing the `run_speedtest` function
speedtest_obj = Yspeed()
# Mock the get_speedtest method to return a predefined dictionary
mock_speedtest = {'download': '100 Mbps', 'upload': '50 Mbps', 'ping': '20 ms'}
speedtest_obj.get_speedtest = MagicMock(return_value=mock_speedtest)
# Call the run_speedtest function and get the results
result = speedtest_obj.run_speedtest()
# Assert that the result is as expected
self.assertEqual(result, mock_speedtest)
# Assert that the Halo spinner is called with the expected parameters
mock_halo.assert_called_with(spinner={'interval': 100, 'frames': ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']}, text="Démarrage du Speedtest", color="red", text_color="yellow")
def setUp(self):
"""
Setup the test class
"""
self.yspeed = Yspeed()
def test_get_ip_info(self):
"""
Test the get_ip_info method
"""
with patch('requests.get') as mock_get:
mock_get.return_value.json.return_value = {
'ip': '1.2.3.4',
'city': 'Test City',
'region': 'Test Region',
'country': 'Test Country',
'org': 'Test Operator'
}
result = self.yspeed.get_ip_info()
self.assertIsInstance(result, dict)
self.assertEqual(result["ip"], "1.2.3.4")
self.assertEqual(result["city"], "Test City")
self.assertEqual(result["region"], "Test Region")
self.assertEqual(result["country"], "Test Country")
self.assertEqual(result["operator"], "Test Operator")
def test_gather_network_info(self):
"""
Test the gather_network_info method
"""
# Mock Yspeed to return test data
speedtest_mock = Mock()
speedtest_mock.get_ip_info.return_value = {"ip": "1.2.3.4"}
speedtest_mock.best_serveur.return_value = {"Serveur": "Test Server"}
speedtest_mock.get_speedtest.return_value = {"download": "100 Mbps"}
with progress_context_manager() as progress_mock:
result = gather_network_info(speedtest_mock, progress_mock)
self.assertIn("ip", result)
self.assertIn("Serveur", result)
self.assertIn("download", result)
def test_print_network_info(self):
"""
Test the print_network_info method
"""
# Mock Console to capture printed output
console_mock = Mock()
info = {
"operator": "Test Operator",
"ip": "1.2.3.4",
"city": "Test City",
"region": "Test Region",
"country": "Test Country",
"fournisseur": "Test Fournisseur",
"Serveur": "Test Server",
"download": "100 Mbps",
"upload": "50 Mbps",
"ping": "10 ms"
}
print_network_info(console_mock, info)
console_mock.print.assert_any_call("Operator: [bold green]Test Operator[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("IP: [bold green]1.2.3.4[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Server: [bold green]Test Server[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Download: [bold green]100 Mbps[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Upload: [bold green]50 Mbps[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Ping: [bold green]10 ms[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("City: [bold green]Test City[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Region: [bold green]Test Region[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Country: [bold green]Test Country[/bold green]", style="blue", justify="center")
console_mock.print.assert_any_call("Fournisseur: [bold green]Test Fournisseur[/bold green]", style="blue", justify="center")
if __name__ == '__main__':
unittest.main()