Skip to content

Commit f66afae

Browse files
Merge pull request #8 from cocukadam12221-ship-it/test-levh-mahfuz-3641639504766514578
Add unit tests for Modul_LevhMahfuzTarama.calculate_shift_date
2 parents 06da0d3 + 675e33e commit f66afae

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__/
2+
*.pyc

test_modul_levhmahfuz.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import sys
2+
from unittest.mock import MagicMock
3+
import unittest
4+
from datetime import date, datetime, timedelta
5+
6+
# Mock dependencies to avoid ModuleNotFoundError when importing simulasyon_11
7+
sys.modules['pandas'] = MagicMock()
8+
sys.modules['numpy'] = MagicMock()
9+
sys.modules['scipy'] = MagicMock()
10+
sys.modules['scipy.stats'] = MagicMock()
11+
12+
# Import the module under test
13+
try:
14+
from simulasyon_11 import Modul_LevhMahfuzTarama
15+
except ImportError:
16+
sys.path.append('.')
17+
from simulasyon_11 import Modul_LevhMahfuzTarama
18+
19+
class TestModulLevhMahfuzTarama(unittest.TestCase):
20+
def setUp(self):
21+
self.modul = Modul_LevhMahfuzTarama()
22+
23+
def test_calculate_shift_date_zero_shift(self):
24+
"""Test that a shift of 0 years returns the original date."""
25+
target_date = date(2023, 1, 1)
26+
result = self.modul.calculate_shift_date(target_date, 0)
27+
self.assertEqual(result, target_date)
28+
29+
def test_calculate_shift_date_positive_one_year(self):
30+
"""Test a positive shift of 1 year.
31+
32+
Formula: target_date - timedelta(days=1 * 365.2422)
33+
timedelta(days=365.2422) creates a timedelta of 365 days, 5 hours, 48 minutes.
34+
35+
CRITICAL NOTE:
36+
When subtracting a timedelta from a standard Python `date` object (not `datetime`),
37+
the fractional day/time component of the timedelta is ignored/truncated by the `date` class arithmetic.
38+
It effectively subtracts `timedelta.days` (365).
39+
40+
This behavior is verified by Python's `datetime` module documentation and experimentation.
41+
If `datetime` objects were used, the time component would be adjusted correctly.
42+
"""
43+
target_date = date(2023, 1, 1)
44+
result = self.modul.calculate_shift_date(target_date, 1)
45+
46+
# Expected: 2023-01-01 - 365 days = 2022-01-01
47+
# The 0.2422 days (5.8 hours) are ignored by date subtraction.
48+
expected = target_date - timedelta(days=365)
49+
self.assertEqual(result, expected)
50+
51+
def test_calculate_shift_date_negative_one_year(self):
52+
"""Test a negative shift of 1 year.
53+
54+
Formula: target_date - timedelta(days=-1 * 365.2422)
55+
timedelta(days=-365.2422) normalizes to days=-366 and seconds=65954.
56+
57+
When subtracting from a `date` object, only `timedelta.days` (-366) is considered.
58+
target_date - (-366 days) = target_date + 366 days.
59+
"""
60+
target_date = date(2023, 1, 1)
61+
result = self.modul.calculate_shift_date(target_date, -1)
62+
63+
# Expected: 2023-01-01 + 366 days = 2024-01-02
64+
expected = target_date + timedelta(days=366)
65+
self.assertEqual(result, expected)
66+
67+
def test_calculate_shift_date_fractional_positive(self):
68+
"""Test fractional positive shift (0.5 years).
69+
0.5 * 365.2422 = 182.6211
70+
timedelta(days=182.6211) -> days=182, seconds=...
71+
date subtraction uses days=182.
72+
"""
73+
target_date = date(2023, 1, 1)
74+
result = self.modul.calculate_shift_date(target_date, 0.5)
75+
expected = target_date - timedelta(days=182)
76+
self.assertEqual(result, expected)
77+
78+
def test_calculate_shift_date_fractional_negative(self):
79+
"""Test fractional negative shift (-0.5 years).
80+
-0.5 * 365.2422 = -182.6211
81+
timedelta(days=-182.6211) -> days=-183, seconds=...
82+
date subtraction uses days=-183.
83+
"""
84+
target_date = date(2023, 1, 1)
85+
result = self.modul.calculate_shift_date(target_date, -0.5)
86+
expected = target_date + timedelta(days=183)
87+
self.assertEqual(result, expected)
88+
89+
def test_calculate_shift_date_with_datetime(self):
90+
"""Test with datetime object to verify time precision is preserved.
91+
Unlike 'date', 'datetime' operations respect the full resolution of timedelta.
92+
"""
93+
target_dt = datetime(2023, 1, 1, 12, 0, 0)
94+
shift = 1
95+
96+
result = self.modul.calculate_shift_date(target_dt, shift)
97+
98+
# Calculate manually with full precision
99+
shift_days = 1 * 365.2422
100+
expected = target_dt - timedelta(days=shift_days)
101+
102+
self.assertEqual(result, expected)
103+
# Verify the time component has changed due to the fractional day subtraction
104+
self.assertNotEqual(result.time(), target_dt.time())
105+
106+
if __name__ == '__main__':
107+
unittest.main()

0 commit comments

Comments
 (0)