diff --git a/week05-testing/fixture.yaml b/week05-testing/fixture.yaml new file mode 100644 index 0000000..f7768d2 --- /dev/null +++ b/week05-testing/fixture.yaml @@ -0,0 +1,20 @@ +- generic: + time_range_1: ["2010-01-12 10:00:00", "2010-01-12 12:00:00"] + time_range_2: ["2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60] + 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"] +- no_overlap: + time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"] + time_range_2: ["2010-01-12 12:30:00", "2010-01-12 12:45:00", 2, 60] + expected: [] +- multiple_overlaps: + time_range_1: ["2010-01-12 10:00:00", "2010-01-12 13:00:00", 3, 900] + time_range_2: ["2010-01-12 10:40:00", "2010-01-12 11:20:00", 2, 120] + expected: + - ["2010-01-12 10:40:00","2010-01-12 10:50:00"] + - ["2010-01-12 11:05:00", "2010-01-12 11:20:00"] +- touching: + time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"] + time_range_2: ["2010-01-12 11:00:00", "2010-01-12 12:45:00"] + expected: [] \ No newline at end of file diff --git a/week05-testing/test_times.py b/week05-testing/test_times.py index 0bf15e0..acd0e24 100644 --- a/week05-testing/test_times.py +++ b/week05-testing/test_times.py @@ -1,43 +1,23 @@ -from times import time_range, compute_overlap_time import pytest +import yaml +from times import compute_overlap_time, time_range -""" def test_given_input(): - large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") - short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) - result = compute_overlap_time(large,short) - 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 +with open("fixture.yaml", 'r') as yamlfile: + fixture = yaml.safe_load(yamlfile) + #print(fixture) -def test_ranges_not_overlap(): - large = time_range("2010-01-12 10:00:00", "2010-01-12 10:30:00") - short = time_range("2010-01-12 11:30:00", "2010-01-12 12:00:00", 2, 60) - result = compute_overlap_time(large,short) - expected = [] - assert result == expected +data_list = [] +for i in range(0, len(fixture)): + first_range = time_range(*list(fixture[i].values())[0]['time_range_1']) + second_range = time_range(*list(fixture[i].values())[0]['time_range_2']) + expected = [(start, stop) for start, stop in list(fixture[i].values())[0]['expected']] + data_list.append((first_range,second_range,expected)) -def test_ranges_with_intervals(): - large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",8) - short = time_range("2010-01-12 10:30:00", "2010-01-12 11:00:00", 2) - result = compute_overlap_time(large,short) - expected = [('2010-01-12 10:30:00', '2010-01-12 10:45:00'),('2010-01-12 10:45:00', '2010-01-12 11:00:00')] - assert result == expected +@pytest.mark.parametrize("first_range,second_range,expected", data_list) +def test_time_range_overlap(first_range,second_range,expected): + assert compute_overlap_time(first_range,second_range) == expected -def test_ranges_start_end(): - large = time_range("2010-01-12 10:00:00", "2010-01-12 10:30:00") - short = time_range("2010-01-12 10:30:00", "2010-01-12 11:00:00") - result = compute_overlap_time(large,short) - expected = [] - assert result == expected """ - -def test_time_goes_backwards(): # this is the negative test - with pytest.raises(ValueError): - time_range("2010-01-12 10:15:00","2010-01-12 10:00:00") - -# parametrised test. This saves time and code -@pytest.mark.parametrize("test_input,expected",[('compute_overlap_time(time_range("2010-01-12 10:00:00", "2010-01-12 10:30:00"),time_range("2010-01-12 11:30:00", "2010-01-12 12:00:00"))',[]) - ,('compute_overlap_time(time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00",8),time_range("2010-01-12 10:30:00", "2010-01-12 11:00:00",2))',[('2010-01-12 10:30:00', '2010-01-12 10:45:00'), ('2010-01-12 10:45:00', '2010-01-12 11:00:00')]) - ,('compute_overlap_time(time_range("2010-01-12 10:00:00", "2010-01-12 10:30:00"),time_range("2010-01-12 10:30:00", "2010-01-12 11:00:00"))',[]) - ,('compute_overlap_time(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')]) - ]) # the last row is a valid one. -def test_eval(test_input,expected): - assert eval(test_input) == expected \ No newline at end of file +def test_negative_time_range(): + with pytest.raises(ValueError) as e: + time_range("2010-01-12 10:00:00", "2010-01-12 09:30:00") + assert e.match("end_time should be later than start_time") \ No newline at end of file diff --git a/week05-testing/times.py b/week05-testing/times.py index 7d8f724..f0ecab6 100644 --- a/week05-testing/times.py +++ b/week05-testing/times.py @@ -4,7 +4,7 @@ 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 end_time < start_time: + if start_time_s >= end_time_s: raise ValueError("end_time should be later than start_time") 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), @@ -13,17 +13,21 @@ def time_range(start_time, end_time, number_of_intervals=1, gap_between_interval 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: - low = max(start1, start2) - high = min(end1, end2) - if low < high: + # 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 if __name__ == "__main__": large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") - short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00") print(compute_overlap_time(large, short)) \ No newline at end of file