diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml new file mode 100644 index 0000000..a29462c --- /dev/null +++ b/.github/workflows/python-tests.yml @@ -0,0 +1,28 @@ +name: Python package + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + # This case we are only updating pip, but you could add other dependencies needed. + run: | + python -m pip install --upgrade pip + python -m pip install pytest + python -m pip install pytest-cov + - name: Test with pytest + run: | + pytest + pytest --cov="." \ No newline at end of file diff --git a/test_times.py b/test_times.py new file mode 100644 index 0000000..d035c01 --- /dev/null +++ b/test_times.py @@ -0,0 +1,44 @@ +from times import * +import pytest + +def test_given_input(): + """Standard results""" + 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 + +def test_no_overlap(): + """Two time ranges that do not overlap""" + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") + short = time_range("2010-01-12 12:30:00", "2010-01-12 12:45:00") + result = compute_overlap_time(large, short) + expected = [] + assert result == expected + +def test_several_intervals(): + """Two time ranges that both contain several intervals each""" + large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00", 2, 60) #10-15 16-31 + short = time_range("2010-01-12 10:15:00", "2010-01-12 10:46:00", 2, 60) #15-30 31-46 + result = compute_overlap_time(large, short) + expected = [('2010-01-12 10:15:00', '2010-01-12 10:15:00'), ('2010-01-12 10:16:00', '2010-01-12 10:30:00'), ('2010-01-12 10:31:00', '2010-01-12 10:31:00')] + assert result == expected + +def test_end_to_end(): + """Two time ranges that end exactly at the same time when the other starts""" + large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00") + short = time_range("2010-01-12 10:31:00", "2010-01-12 10:46:00") + result = compute_overlap_time(large, short) + expected = [('2010-01-12 10:31:00', '2010-01-12 10:31:00')] + assert result == expected + +def test_time_range_backwards(): + with pytest.raises(ValueError, match=r"End time: .* is before start time: .*\."): + time_range("2010-01-12 10:45:00", "2010-01-12 10:31:00") + +""" +For test coverage, run in terminal: +pytest --cov="." --cov-report html +python -m http.server -d htmlcov +""" \ No newline at end of file diff --git a/times.py b/times.py index d57f401..e53766d 100644 --- a/times.py +++ b/times.py @@ -1,9 +1,11 @@ -import datetime +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(f"End time: {end_time} is before start time: {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), start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s)) @@ -17,7 +19,8 @@ def compute_overlap_time(range1, range2): for start2, end2 in range2: low = max(start1, start2) high = min(end1, end2) - overlap_time.append((low, high)) + if (low <= high): + overlap_time.append((low, high)) return overlap_time if __name__ == "__main__":