Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
new commit testing week5
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinafni committed Nov 4, 2020
1 parent 6043a70 commit 5a28022
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions week05-testing/test_times.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from times import compute_overlap_time, time_range
import pytest

def test_given_input():

# Assert 1 - test should pass
range1 = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
range2 = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(range1, range2)
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

# Assert 2 - test should work
range5 = time_range("2001-01-12 10:00:00", "2010-01-12 12:00:00")
range6 = time_range("2001-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(range5, range6)
expected = [('2001-01-12 10:30:00', '2005-07-13 22:37:00'), ('2005-07-13 22:38:00', '2010-01-12 10:45:00')]
assert result == expected

# Assert 2 - test should fail (value error in range time / instead of -)
range3 = time_range("2010-01-12 10:00:00", "2010-01/12 12:00:00")
range4 = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(range3, range4)
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

8 changes: 6 additions & 2 deletions week05-testing/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
# d is the time difference
# sec_range gives the difference interval in seconds
# number of intervals [t2,t3]
def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): # what's intervals?
def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): # gaps in seconds - possible in the interval(s)
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects
d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1)
print('d')
print(d)
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)]
print(sec_range)
return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range]
# from object datetime to string - output: 7200sec

Expand All @@ -25,9 +28,10 @@ def compute_overlap_time(range1, range2):
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") # range 1 [t1,t2]
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) # range 2 [t3,t4]
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) # range 2 [t3,t6] -> [t3,t4][t5,t6]
print(large)
print(short)
print(compute_overlap_time(large, short))

0 comments on commit 5a28022

Please sign in to comment.