Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .gitignore
Empty file.
34 changes: 34 additions & 0 deletions week05-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from times import compute_overlap_time, time_range


@pytest.mark.parametrize("first_range, second_range, expected_overlap",
[(time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00"),
time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60),
[("2010-01-12 10:30:00","2010-01-12 10:37:00"), ("2010-01-12 10:38:00", "2010-01-12 10:45:00")]),
(time_range("2010-01-12 10:00:00", "2010-01-12 11:00:00"),
time_range("2010-01-12 12:30:00", "2010-01-12 12:45:00", 2, 60),
[]),
(time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 3, 900),
time_range("2010-01-12 10:40:00", "2010-01-12 11:20:00", 2, 120),
[("2010-01-12 10:40:00","2010-01-12 10:50:00"), ("2010-01-12 11:05:00", "2010-01-12 11:20:00")]),
(time_range("2010-01-12 10:00:00", "2010-01-12 11:00:00"),
time_range("2010-01-12 11:00:00", "2010-01-12 12:45:00"),
[])
])
def test_time_range_overlap(first_range, second_range, expected_overlap):
assert compute_overlap_time(first_range, second_range) == expected_overlap

# two ways of doing the same negative test. Only one is needed.
def test_negative_time_range():
# first possible solution
with pytest.raises(ValueError) as e:
time_range("2010-01-12 10:00:00", "2010-01-12 09:30:00")
# lines after the error is raised are not executed in the pytest.raises context, so the assertion has to be outside the "with"
assert e.match('The end of the time range has to come strictly after its start.')

def test_negative_time_range_alternative():
# an alternative solution for using pytest.raises to check that the error message is as expected
expected_error_message = 'The end of the time range has to come strictly after its start.'
with pytest.raises(ValueError, match=expected_error_message):
time_range("2010-01-12 10:00:00", "2010-01-12 09:30:00")
27 changes: 27 additions & 0 deletions week05-testing/times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import datetime


def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0):
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
if(start_time_s>=end_time_s):
raise ValueError('The end of the time range has to come strictly after its start.')
d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1)
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)]
return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range]


def compute_overlap_time(range1, range2):
overlap_time = []
for start1, end1 in range1:
for start2, end2 in range2:
# both ranges need to start before the other ends, otherwise there is no overlap!
if start1 <= end2 and start2 <= end1:
low = max(start1, start2)
high = min(end1, end2)
if high == low:
continue
overlap_time.append((low, high))
return overlap_time