Skip to content

Commit 0d5163a

Browse files
committed
feat(chapter08): implement track_summary function and add tests
1 parent f3aec76 commit 0d5163a

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: test test-all clean clean-coverage clean-results combine-coverage merge-junit
22

3-
CHAPTERS = chapter01 chapter04 chapter06 chapter07 chapter09 chapter10 chapter11 chapter12
3+
CHAPTERS = chapter01 chapter04 chapter06 chapter07 chapter08 chapter09 chapter10 chapter11 chapter12
44
TEST_RESULTS_DIR = test-results
55
COVERAGE_DIR = $(TEST_RESULTS_DIR)/coverage
66
JUNIT_DIR = $(TEST_RESULTS_DIR)/junit

chapter08/src/case01/case01.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
3+
4+
def track_summary(points):
5+
def calculate_distance():
6+
result = 0
7+
for i in range(1, len(points)):
8+
result += distance(points[i - 1], points[i])
9+
return result
10+
11+
def distance(p1, p2):
12+
# 두 지점 간의 거리를 계산 (하버사인 공식 사용)
13+
EARTH_RADIUS = 3959 # 마일 단위
14+
dlat = radians(p2["lat"] - p1["lat"])
15+
dlon = radians(p2["lon"] - p1["lon"])
16+
a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(
17+
radians(p1["lat"])
18+
) * math.cos(radians(p2["lat"])) * math.sin(dlon / 2) * math.sin(dlon / 2)
19+
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
20+
return EARTH_RADIUS * c
21+
22+
def radians(degrees):
23+
# 각도를 라디안으로 변환
24+
return degrees * math.pi / 180
25+
26+
def calculate_time():
27+
# 현실적인 구현을 위해 총 소요 시간을 계산하는 간단한 예시
28+
# (여기서는 임의로 이동 거리에 따른 시간을 반환)
29+
return calculate_distance() * 10 # 마일당 10분 소요
30+
31+
total_time = calculate_time()
32+
total_distance = calculate_distance()
33+
pace = total_time / 60 / total_distance if total_distance > 0 else 0
34+
return {"time": total_time, "distance": total_distance, "pace": pace}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from chapter08.src.case01.case01 import track_summary
2+
3+
4+
def test_track_summary_calculation():
5+
"""테스트를 위한 샘플 포인트 데이터로 track_summary 함수 테스트"""
6+
# 테스트를 위한 샘플 포인트 데이터
7+
points = [
8+
{"lat": 37.7749, "lon": -122.4194}, # San Francisco
9+
{"lat": 34.0522, "lon": -118.2437}, # Los Angeles
10+
{"lat": 36.7783, "lon": -119.4179}, # Fresno
11+
]
12+
13+
# track_summary 함수 호출
14+
result = track_summary(points)
15+
16+
# 결과가 예상한 키를 포함하는지 검증
17+
assert "time" in result
18+
assert "distance" in result
19+
assert "pace" in result
20+
21+
# 결과 값이 올바른 타입인지 검증
22+
assert isinstance(result["time"], (int, float))
23+
assert isinstance(result["distance"], (int, float))
24+
assert isinstance(result["pace"], (int, float))
25+
26+
# 거리가 0보다 큰지 검증
27+
assert result["distance"] > 0
28+
29+
30+
# 추가 테스트: 거리 계산 정확성 테스트
31+
def test_zero_distance():
32+
"""동일한 위치에 대한 거리 계산 테스트"""
33+
# 동일한 위치의 포인트
34+
points = [
35+
{"lat": 37.7749, "lon": -122.4194}, # San Francisco
36+
{"lat": 37.7749, "lon": -122.4194}, # 동일한 위치
37+
]
38+
39+
result = track_summary(points)
40+
41+
# 거의 0에 가까운 거리가 계산되어야 함 (오차 허용)
42+
assert result["distance"] < 0.001

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ testpaths =
44
chapter04/tests
55
chapter06/tests
66
chapter07/tests
7+
chapter08/tests
78
chapter09/tests
89
chapter10/tests
910
chapter11/tests

0 commit comments

Comments
 (0)