Skip to content

Commit

Permalink
Avoid code repetition in the tests UCL-RITS#89
Browse files Browse the repository at this point in the history
  • Loading branch information
Aixi committed Nov 5, 2020
1 parent db8893b commit f2df2e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Binary file modified week05-testing/__pycache__/test_times.cpython-38-pytest-5.4.3.pyc
Binary file not shown.
Binary file modified week05-testing/__pycache__/times.cpython-38.pyc
Binary file not shown.
47 changes: 28 additions & 19 deletions week05-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
from times import time_range, compute_overlap_time
from pytest import raises
import pytest

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)
@pytest.mark.parametrize("large, short, expected",
[(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 11:00:00", "2010-01-12 12:00:00"), time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00"), [])
, (time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0), time_range("2010-01-12 11:00:00", "2010-01-12 12:00:00", 2, 0), [('2010-01-12 11:00:00', '2010-01-12 11:30:00'), ('2010-01-12 11:30:00', '2010-01-12 12:00:00')])
,(time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0), time_range("2010-01-12 13:00:00", "2010-01-12 14:00:00", 2, 0), [])
,(time_range("2010-01-12 13:00:00", "2010-01-12 12:00:00", 4, 60), time_range("2010-01-12 14:30:00", "2010-01-12 12:00:00", 2, 60), [])
])

def test_given_input(large, short,expected):
# 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')]
# 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_no_overlap():
large = time_range("2010-01-12 11:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00")
def test_no_overlap(large, short,expected):
# large = time_range("2010-01-12 11:00:00", "2010-01-12 12:00:00")
# short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00")
result = compute_overlap_time(large, short)
expected = []
# expected = []
assert result == expected

def test_contain_intervals():
large = time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0)
short = time_range("2010-01-12 11:00:00", "2010-01-12 12:00:00", 2, 0)
def test_contain_intervals(large, short,expected):
# large = time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0)
# short = time_range("2010-01-12 11:00:00", "2010-01-12 12:00:00", 2, 0)
result = compute_overlap_time(large, short)
expected = [('2010-01-12 11:00:00', '2010-01-12 11:30:00'), ('2010-01-12 11:30:00', '2010-01-12 12:00:00')]
# expected = [('2010-01-12 11:00:00', '2010-01-12 11:30:00'), ('2010-01-12 11:30:00', '2010-01-12 12:00:00')]
assert result == expected

def test_touching_times():
large = time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0)
short = time_range("2010-01-12 13:00:00", "2010-01-12 14:00:00", 2, 0)
def test_touching_times(large, short,expected):
# large = time_range("2010-01-12 10:00:00", "2010-01-12 13:00:00", 2, 0)
# short = time_range("2010-01-12 13:00:00", "2010-01-12 14:00:00", 2, 0)
result = compute_overlap_time(large, short)
expected = []
# expected = []
assert result == expected

def test_backfoward_date():
large = time_range("2010-01-12 13:00:00", "2010-01-12 12:00:00", 4, 60)
short = time_range("2010-01-12 14:30:00", "2010-01-12 12:00:00", 2, 60)
def test_backfoward_date(large,short,expected):
# large = time_range("2010-01-12 13:00:00", "2010-01-12 12:00:00", 4, 60)
# short = time_range("2010-01-12 14:30:00", "2010-01-12 12:00:00", 2, 60)
with raises(ValueError):
compute_overlap_time(large, short)
2 changes: 2 additions & 0 deletions week05-testing/times.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
from pytest import raises


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")
Expand Down

0 comments on commit f2df2e9

Please sign in to comment.