From 5a2802237ee2c38bca2b5505e64b3a6905ddbcf4 Mon Sep 17 00:00:00 2001 From: cristinafni Date: Wed, 4 Nov 2020 10:26:05 +0000 Subject: [PATCH] new commit testing week5 --- week05-testing/test_times.py | 16 ++++++++++++++++ week05-testing/times.py | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/week05-testing/test_times.py b/week05-testing/test_times.py index 40de8b6..b1f5614 100644 --- a/week05-testing/test_times.py +++ b/week05-testing/test_times.py @@ -1,10 +1,26 @@ from times import compute_overlap_time, time_range +import pytest def test_given_input(): + # Assert 1 - test should pass range1 = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") range2 = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) result = compute_overlap_time(range1, range2) expected = [('2010-01-12 10:30:00', '2010-01-12 10:37:00'), ('2010-01-12 10:38:00', '2010-01-12 10:45:00')] assert result == expected + # Assert 2 - test should work + range5 = time_range("2001-01-12 10:00:00", "2010-01-12 12:00:00") + range6 = time_range("2001-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) + result = compute_overlap_time(range5, range6) + expected = [('2001-01-12 10:30:00', '2005-07-13 22:37:00'), ('2005-07-13 22:38:00', '2010-01-12 10:45:00')] + assert result == expected + + # Assert 2 - test should fail (value error in range time / instead of -) + range3 = time_range("2010-01-12 10:00:00", "2010-01/12 12:00:00") + range4 = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) + result = compute_overlap_time(range3, range4) + expected = [('2010-01-12 10:30:00', '2010-01-12 10:37:00'), ('2010-01-12 10:38:00', '2010-01-12 10:45:00')] + assert result == expected + diff --git a/week05-testing/times.py b/week05-testing/times.py index 3226477..9cfa502 100644 --- a/week05-testing/times.py +++ b/week05-testing/times.py @@ -5,13 +5,16 @@ # d is the time difference # sec_range gives the difference interval in seconds # number of intervals [t2,t3] -def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): # what's intervals? +def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): # gaps in seconds - possible in the interval(s) start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1) + print('d') + print(d) sec_range = [(start_time_s + datetime.timedelta(seconds=i * d + i * gap_between_intervals_s), start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s)) for i in range(number_of_intervals)] + print(sec_range) return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range] # from object datetime to string - output: 7200sec @@ -25,9 +28,10 @@ def compute_overlap_time(range1, range2): overlap_time.append((low, high)) return overlap_time + if __name__ == "__main__": large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") # range 1 [t1,t2] - short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) # range 2 [t3,t4] + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) # range 2 [t3,t6] -> [t3,t4][t5,t6] print(large) print(short) print(compute_overlap_time(large, short)) \ No newline at end of file