Skip to content

Commit

Permalink
Improve the parametrised testing by adding a yaml file. Answers UCL-R…
Browse files Browse the repository at this point in the history
…ITS/rsd-classwork-2020#92
  • Loading branch information
Nikolaos Schoinas committed Nov 11, 2020
1 parent d788409 commit a65d1c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
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: []
56 changes: 18 additions & 38 deletions week05-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -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
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")
14 changes: 9 additions & 5 deletions week05-testing/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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))

1 comment on commit a65d1c6

@nikoSchoinas
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answers UCL-RITS#92

Please sign in to comment.