diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..44f9878 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\umitozm\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe" +} \ No newline at end of file diff --git a/week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc b/week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc index 0afc867..9a1f52c 100644 Binary files a/week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc and b/week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc differ diff --git a/week05-testing/__pycache__/times.cpython-37.pyc b/week05-testing/__pycache__/times.cpython-37.pyc new file mode 100644 index 0000000..7a0549a Binary files /dev/null and b/week05-testing/__pycache__/times.cpython-37.pyc differ diff --git a/week05-testing/__pycache__/times.cpython-38.pyc b/week05-testing/__pycache__/times.cpython-38.pyc index 70a5993..48b8b13 100644 Binary files a/week05-testing/__pycache__/times.cpython-38.pyc and b/week05-testing/__pycache__/times.cpython-38.pyc differ diff --git a/week05-testing/fixture.yaml b/week05-testing/fixture.yaml new file mode 100644 index 0000000..fec76af --- /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: [] diff --git a/week05-testing/test_times.py b/week05-testing/test_times.py index b39bc19..806967b 100644 --- a/week05-testing/test_times.py +++ b/week05-testing/test_times.py @@ -1,21 +1,27 @@ -from times import time_range ,compute_overlap_time, compute_overlap_helper import pytest +import yaml +from times import compute_overlap_time, time_range -testdata = [[('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:38:00', '2010-01-12 12:20:00'),tuple()] #test_no_overlap_helper - ,(('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:30:00', '2010-01-12 10:35:00'),('2010-01-12 10:30:00', '2010-01-12 10:35:00')) #test_inside_edge_overlap_helper - ,(('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:30:00', '2010-01-12 10:37:00')) #test_same_overlap_helper - ,(('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:37:00', '2010-01-12 10:39:00'),tuple()) #test_outside_edge_overlap_helper - ,(('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:34:00', '2010-01-12 10:42:00'),('2010-01-12 10:34:00', '2010-01-12 10:37:00')) #test_partial_overlap_helper - ,(('2010-01-12 10:30:00', '2010-01-12 10:37:00'),('2010-01-12 10:32:00', '2010-01-12 10:36:30'),('2010-01-12 10:32:00', '2010-01-12 10:36:30'))#test_subset_overlap_helper -] - -@pytest.mark.parametrize( - "time_range1,time_range2,expected",testdata -) +with open("fixture.yaml", 'r') as yamlfile: + fixture = yaml.safe_load(yamlfile) + print(fixture) + +@pytest.mark.parametrize("test_name", fixture) +# fixture is a list of dictionaries [{'generic':...}, {'no_overlap':...}, ...] -def test_eval(time_range1,time_range2,expected): - assert compute_overlap_helper(time_range1,time_range2) == expected +def test_time_range_overlap(test_name): + # test_name will be a dictionary, e.g. for the first case: {'generic': {'time_range_1':..., 'time_range2':..., 'expected':...} + properties = list(test_name.values())[0] + first_range = time_range(*properties['time_range_1']) + second_range = time_range(*properties['time_range_2']) + expected_overlap = [(start, stop) for start, stop in properties['expected']] + assert compute_overlap_time(first_range, second_range) == expected_overlap +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('The end of the time range has to come strictly after its start.') +""" def test_given_input(): large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") @@ -24,16 +30,8 @@ def test_given_input(): 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 -""" -def test_subset_overlap_helper(): - assert( - compute_overlap_helper( - ('2010-01-12 10:30:00', '2010-01-12 10:37:00'), - ('2010-01-12 10:32:00', '2010-01-12 10:36:30') - ) - == ('2010-01-12 10:32:00', '2010-01-12 10:36:30') - ) -""" + def test_negative(): with pytest.raises(ValueError): time_range("2010-01-12 12:00:00", "2010-01-12 10:00:00") +""" diff --git a/week05-testing/times.py b/week05-testing/times.py index b721729..4feed7a 100644 --- a/week05-testing/times.py +++ b/week05-testing/times.py @@ -1,32 +1,34 @@ 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("Start time is later than end time") - + 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_helper(tuple1, tuple2): - start1, end1 = tuple1 - start2, end2 = tuple2 - if(start1 >= end2 or start2 >= end1): - return tuple() - else: - low = max(start1, start2) - high = min(end1, end2) - return((low, high)) def compute_overlap_time(range1, range2): overlap_time = [] - for tuple1 in range1: - for tuple2 in range2: - if(overlap := compute_overlap_helper(tuple1, tuple2)): - overlap_time.append((overlap)) - return(overlap_time) \ No newline at end of file + 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 + +""" + 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) + print(compute_overlap_time(large, short)) +""" \ No newline at end of file