-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathtest_geographic_distance.py
More file actions
72 lines (53 loc) · 2.27 KB
/
Copy pathtest_geographic_distance.py
File metadata and controls
72 lines (53 loc) · 2.27 KB
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
from unittest.mock import patch
import pytest
from geographic_distance import calculate_distance_and_time, main
def test_calculate_distance_and_time():
# Test with valid coordinates and speed
coord1 = (40.7128, -74.006)
coord2 = (37.7749, -122.4194)
avg_speed = 60
distance, travel_time = calculate_distance_and_time(coord1, coord2, avg_speed)
assert distance > 0
assert travel_time > 0
def test_calculate_distance_and_time_invalid_speed():
# Test with invalid speed (zero)
coord1 = (40.7128, -74.006)
coord2 = (37.7749, -122.4194)
avg_speed = 0
with pytest.raises(ValueError):
calculate_distance_and_time(coord1, coord2, avg_speed)
def test_calculate_distance_and_time_same_coordinates():
# Test with same coordinates
coord1 = (40.7128, -74.006)
coord2 = (40.7128, -74.006)
avg_speed = 60
distance, travel_time = calculate_distance_and_time(coord1, coord2, avg_speed)
assert distance == 0
assert travel_time == 0
@pytest.fixture
def mock_input():
with patch('builtins.input', side_effect=['40.7128, -74.006', '37.7749, -122.4194', '60']):
yield
def test_main(mock_input, capsys):
main()
captured = capsys.readouterr()
assert 'Distance between the two coordinates:' in captured.out
assert 'Estimated travel time:' in captured.out
def test_main_invalid_coordinates(mock_input, capsys):
with patch('builtins.input', side_effect=['abc, def', '37.7749, -122.4194', '60']):
with pytest.raises(ValueError):
main()
def test_main_invalid_speed(mock_input, capsys):
with patch('builtins.input', side_effect=['40.7128, -74.006', '37.7749, -122.4194', 'abc']):
with pytest.raises(ValueError):
main()
def test_main_zero_speed(mock_input, capsys):
with patch('builtins.input', side_effect=['40.7128, -74.006', '37.7749, -122.4194', '0']):
with pytest.raises(ValueError):
main()
def test_main_same_coordinates(mock_input, capsys):
with patch('builtins.input', side_effect=['40.7128, -74.006', '40.7128, -74.006', '60']):
main()
captured = capsys.readouterr()
assert 'Distance between the two coordinates: 0.00 kilometers' in captured.out
assert 'Estimated travel time: 0.00 hours' in captured.out