forked from UCL-RITS/rse-classwork-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start moving over to loading tests from yaml file. Answers UCL-RITS#92
- Loading branch information
Showing
5 changed files
with
95 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pytest | ||
pytest-cov | ||
datetime | ||
codecov | ||
codecov | ||
pyyaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
- generic: | ||
time_range_1: | ||
start: 2010-01-12 10:00:00 | ||
end: 2010-01-12 12:00:00 | ||
time_range_2: | ||
start: 2010-01-12 10:30:00 | ||
end: 2010-01-12 10:45:00 | ||
num_intervals: 2 | ||
time_between_intervals: 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: | ||
start: 2010-01-12 10:00:00 | ||
end: 2010-01-12 12:00:00 | ||
time_range_2: | ||
start: 2010-01-12 12:30:00 | ||
end: 2010-01-12 12:45:00 | ||
expected: [] | ||
- several_overlaps: | ||
time_range_1: | ||
start: 2010-01-12 10:00:00 | ||
end: 2010-01-12 12:00:00 | ||
num_intervals: 2 | ||
time_between_intervals: 0 | ||
time_range_2: | ||
start: 2010-01-12 10:30:00 | ||
end: 2010-01-12 11:30:00 | ||
num_intervals: 2 | ||
time_between_intervals: 0 | ||
expected: | ||
- ["2010-01-12 10:30:00", "2010-01-12 11:00:00"] | ||
- ["2010-01-12 11:00:00", "2010-01-12 11:30:00"] | ||
- one_finish_another_start: | ||
time_range_1: | ||
start: 2010-01-12 10:00:00 | ||
end: 2010-01-12 12:00:00 | ||
time_range_2: | ||
start: 2010-01-12 12:00:00 | ||
end: 2010-01-12 12:30:00 | ||
expected: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
import times | ||
import pytest | ||
import yaml | ||
|
||
class TestTimeOverlap: | ||
"""TestTimeOverlap class contains all functions involved in | ||
testing the overlap of two times based on example UCL RITS code""" | ||
|
||
@pytest.mark.parametrize("time_range_1, time_range_2, expected", [ | ||
# Test hardcoded input from example problem | ||
(times.large, times.short, [('2010-01-12 10:30:00', '2010-01-12 10:37:00'), ('2010-01-12 10:38:00', '2010-01-12 10:45:00')]), | ||
# Tests two time ranges that do not overlap | ||
(times.time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00"), times.time_range("2010-01-12 12:30:00", "2010-01-12 12:45:00"), []), | ||
# Tests intervals where each has several intervals and two sub-intervals overlap | ||
(times.time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00", 2, 0), times.time_range("2010-01-12 10:30:00", "2010-01-12 11:30:00", 2, 0), [('2010-01-12 10:30:00', '2010-01-12 11:00:00'),('2010-01-12 11:00:00', '2010-01-12 11:30:00')]), | ||
# Tests two intervals where one finishes at exactly the same time another started | ||
(times.time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00"), times.time_range("2010-01-12 12:00:00", "2010-01-12 12:30:00"), []) | ||
]) | ||
def test_given_input(self, time_range_1, time_range_2, expected): | ||
"""Tests hardcoded input given as part of the example problem""" | ||
result = times.compute_overlap_time(time_range_1, time_range_2) | ||
assert result == expected | ||
def get_test_data(file_path): | ||
test_array = [] | ||
# Load the fixtures for testing | ||
with open(file_path) as file: | ||
loaded_tests = yaml.load(file, Loader=yaml.SafeLoader) | ||
for test in loaded_tests: | ||
# Get test parameters | ||
test_parameters = list(test.values())[0] | ||
# Convert expected arrays into tuples | ||
expected_values = [] | ||
for expected in test_parameters['expected']: | ||
expected_values.append((expected[0], expected[1])) | ||
# Make the current test tuple | ||
current_test = (times.time_range( | ||
test_parameters['time_range_1']['start'], | ||
test_parameters['time_range_1']['end'], | ||
test_parameters['time_range_1'].get('num_intervals', 1), | ||
test_parameters['time_range_1'].get('time_between_intervals', 0)), | ||
times.time_range( | ||
test_parameters['time_range_2']['start'], | ||
test_parameters['time_range_2']['end'], | ||
test_parameters['time_range_2'].get('num_intervals', 1), | ||
test_parameters['time_range_2'].get('time_between_intervals', 0)), | ||
expected_values) | ||
# Add it to the array | ||
test_array.append(current_test) | ||
return test_array | ||
|
||
def test_negative_time_interval(self): | ||
"""Tests a time range which goes backwards""" | ||
t1 = times.time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") | ||
t2 = times.time_range("2010-01-12 11:00:00", "2010-01-12 10:30:00") | ||
with pytest.raises(ValueError): | ||
times.compute_overlap_time(t1, t2) | ||
@pytest.mark.parametrize("time_range_1, time_range_2, expected", get_test_data("./week05-testing/fixture.yaml")) | ||
def test_fixture_input(time_range_1, time_range_2, expected): | ||
result = times.compute_overlap_time(time_range_1, time_range_2) | ||
assert result == expected | ||
|
||
def test_negative_time_interval(): | ||
"""Tests a time range which goes backwards""" | ||
with pytest.raises(ValueError): | ||
times.time_range("2010-01-12 15:00:00", "2010-01-12 12:00:00") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters