Skip to content

Commit

Permalink
Fixture UCL-RITS#92
Browse files Browse the repository at this point in the history
  • Loading branch information
umitozmen committed Nov 8, 2020
1 parent adbcce4 commit 2b702c4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\umitozm\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe"
}
Binary file modified week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc
Binary file not shown.
Binary file added week05-testing/__pycache__/times.cpython-37.pyc
Binary file not shown.
Binary file modified week05-testing/__pycache__/times.cpython-38.pyc
Binary file not shown.
20 changes: 20 additions & 0 deletions week05-testing/fixture.yaml
Original file line number Diff line number Diff line change
@@ -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: []
46 changes: 22 additions & 24 deletions week05-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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")
"""
38 changes: 20 additions & 18 deletions week05-testing/times.py
Original file line number Diff line number Diff line change
@@ -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)
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))
"""

0 comments on commit 2b702c4

Please sign in to comment.