def test_from_events_invalid(self): """Tests from_events() with invalid situations.""" # No start. events = [Event(0, False)] expected = [TimePeriod(None, 0)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # No end. events = [Event(0, True)] expected = [TimePeriod(0, None)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple start. events = [Event(0, True), Event(1, True), Event(2, False)] expected = [TimePeriod(0, 2)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple end. events = [Event(0, True), Event(1, False), Event(2, False)] expected = [TimePeriod(0, 1)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func))
def test_one_intersecting_period(self): """Only one period matches logs.""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(self.year2001, self.year2002), TimePeriod(self.year2003, self.year2004), ]) self.assertSequenceEqual([[], self.year2003_logs], self.to_lists(results))
def test_both_periods(self): """Both sets of logs, accesses individually.""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2003, self.year2004), ]) self.assertSequenceEqual([self.year2000_logs, self.year2003_logs], self.to_lists(results))
def test_flight_period_multiple_flights(self): """Multiple flight reported together.""" self.create_event(self.year2000, True) self.create_event(self.year2000 + self.ten_minutes, False) self.create_event(self.year2001, True) self.create_event(self.year2001 + self.ten_minutes, False) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + self.ten_minutes), TimePeriod(self.year2001, self.year2001 + self.ten_minutes), ])
def test_flight_period_multiple_flights_order(self): """Multiple flights listed in chronological order.""" self.create_event(self.year2001, True) self.create_event(self.year2001 + self.ten_minutes, False) self.create_event(self.year2000, True) self.create_event(self.year2000 + self.ten_minutes, False) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + self.ten_minutes), TimePeriod(self.year2001, self.year2001 + self.ten_minutes), ])
def test_from_events_invalid(self): """Tests from_events() with invalid situations.""" # No start. events = [Event(0, False)] expected = [TimePeriod(None, 0)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # No end. events = [Event(0, True)] expected = [TimePeriod(0, None)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple start. events = [Event(0, True), Event(1, True), Event(2, False)] expected = [TimePeriod(0, 2)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple end. events = [Event(0, True), Event(1, False), Event(2, False)] expected = [TimePeriod(0, 1)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func))
def test_within_no_end(self): """Tests the within method with defined start and no end.""" t = TimePeriod(start=datetime.datetime(2000, 1, 1)) # Greater than start self.assertTrue(t.within(datetime.datetime(2000, 6, 1))) # Inclusive start self.assertTrue(t.within(datetime.datetime(2000, 1, 1))) # Not within below self.assertFalse(t.within(datetime.datetime(1999, 1, 1))) # Way above self.assertTrue(t.within(datetime.datetime(2002, 1, 1)))
def test_flight_period_multiple_takeoff(self): """Double takeoff ignored.""" self.create_event(self.year2000, True) # Take off again? Ignored self.create_event(self.year2000 + self.ten_minutes, True) # Land self.create_event(self.year2000 + 2 * self.ten_minutes, False) self.create_event(self.year2001, True) self.create_event(self.year2001 + self.ten_minutes, False) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + 2 * self.ten_minutes), TimePeriod(self.year2001, self.year2001 + self.ten_minutes), ])
def test_flight_period_multiple_landing(self): """Double landing ignored""" self.create_event(self.year2000, True) self.create_event(self.year2000 + self.ten_minutes, False) # Land again? Ignored self.create_event(self.year2000 + 2 * self.ten_minutes, False) self.create_event(self.year2001, True) self.create_event(self.year2001 + self.ten_minutes, False) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + self.ten_minutes), TimePeriod(self.year2001, self.year2001 + self.ten_minutes), ])
def test_within_no_start(self): """Tests the within method with defined end and no start.""" t = TimePeriod(end=datetime.datetime(2001, 1, 1)) # Less than end self.assertTrue(t.within(datetime.datetime(2000, 6, 1))) # Inclusive end self.assertTrue(t.within(datetime.datetime(2001, 1, 1))) # Way below self.assertTrue(t.within(datetime.datetime(1999, 1, 1))) # Not within above self.assertFalse(t.within(datetime.datetime(2002, 1, 1)))
def test_full_range(self): """All logs from (-inf, inf).""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(None, None) ]) self.assertSequenceEqual([self.logs], self.to_lists(results))
def test_single_period(self): """Single set of logs accessible.""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(self.year2000, self.year2001) ]) self.assertSequenceEqual([self.year2000_logs], self.to_lists(results))
def test_open_start(self): """Logs (-inf, 2001)""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(None, self.year2001), ]) self.assertSequenceEqual([self.year2000_logs], self.to_lists(results))
def test_non_intersecting_period(self): """No logs matched.""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(self.year2001, self.year2002), ]) self.assertSequenceEqual([[]], self.to_lists(results))
def test_open_end(self): """Logs (2003, inf)""" results = UasTelemetry.by_time_period(self.user1, [ TimePeriod(self.year2003, None), ]) self.assertSequenceEqual([self.year2003_logs], self.to_lists(results))
def test_flight_period_missing_landing(self): """Missing landing reported as None.""" self.create_event(self.year2000, True) self.evaluate_periods([ TimePeriod(self.year2000, None), ]) # yapf: disable
def test_flight_period_missing_takeoff(self): """Missing takeoff reported as None.""" self.create_event(self.year2000, False) self.evaluate_periods([ TimePeriod(None, self.year2000), ]) # yapf: disable
def test_no_logs(self): """Test behavior when no logs are present.""" delta = datetime.timedelta(seconds=10) self.assertSequenceEqual( (10, 10), UasTelemetry.rates( self.user1, [TimePeriod(self.year2000, self.year2000 + delta)]))
def test_infinite_period(self): """Can't calculate a rate for an infinite (unbounded) period.""" delta = datetime.timedelta(seconds=1) logs = self.create_logs(self.user1, delta=delta) period = TimePeriod(None, None) self.assertSequenceEqual((None, None), UasTelemetry.rates(self.user1, [period]))
def test_basic_flight_period(self): """Single flight reported as period.""" self.create_event(self.year2000, True) self.create_event(self.year2000 + self.ten_minutes, False) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + self.ten_minutes), ])
def test_flight_period_missing_multiple(self): """Multiple flight can be missing details.""" # Forgot takeoff self.create_event(self.year2000, False) # Normal self.create_event(self.year2001, True) self.create_event(self.year2001 + self.ten_minutes, False) # Forgot landing self.create_event(self.year2002, True) self.evaluate_periods([ TimePeriod(None, self.year2000), TimePeriod(self.year2001, self.year2001 + self.ten_minutes), TimePeriod(self.year2002, None), ])
def test_ignore_start_end(self): """When start and end are None, only times between logs are compared.""" delta = datetime.timedelta(seconds=1) logs = self.create_logs(self.user1, delta=delta) period = TimePeriod(None, None) rates = UasTelemetry.rates(self.user1, [period]) self.assertSequenceEqual((1, 1), rates)
def flights(cls, user): """Gets the time periods for which the given user was in flight. Args: user: The user for which to get flight periods for. Returns: A list of TimePeriod objects corresponding to individual flights. """ return TimePeriod.from_events(TakeoffOrLandingEvent.by_user(user), is_start_func=lambda x: x.uas_in_air, is_end_func=lambda x: not x.uas_in_air)
def test_flight_period_specific_mission(self): """Tests that it only includes flights for specified mission.""" self.create_event(self.year2000, True) self.create_event(self.year2000 + self.ten_minutes, False) self.create_event(self.year2001, True, self.mission2) self.create_event(self.year2001 + self.ten_minutes, False, self.mission2) self.evaluate_periods([ TimePeriod(self.year2000, self.year2000 + self.ten_minutes), ])
def test_from_events(self): """Tests from_events().""" # No periods. events = [] expected = [] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Standard period. events = [Event(0, True), Event(1, False)] expected = [TimePeriod(0, 1)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple periods. events = [Event(0, True), Event(1, False), Event(2, True), Event(3, False)] expected = [TimePeriod(0, 1), TimePeriod(2, 3)] self.assertEqual(expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func))
def test_from_events(self): """Tests from_events().""" # No periods. events = [] expected = [] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Standard period. events = [Event(0, True), Event(1, False)] expected = [TimePeriod(0, 1)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func)) # Multiple periods. events = [ Event(0, True), Event(1, False), Event(2, True), Event(3, False) ] expected = [TimePeriod(0, 1), TimePeriod(2, 3)] self.assertEqual( expected, TimePeriod.from_events(events, self.is_start_func, self.is_end_func))
def test_missions(self): """Tets the missions().""" on_clock = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) on_clock.save() on_clock.timestamp = self.year2000 on_clock.save() on_timeout = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=True) on_timeout.save() on_timeout.timestamp = self.year2001 on_timeout.save() off_timeout = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) off_timeout.save() off_timeout.timestamp = self.year2002 off_timeout.save() off_clock = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) off_clock.save() off_clock.timestamp = self.year2003 off_clock.save() random_event = MissionClockEvent(user=self.user2, team_on_clock=True, team_on_timeout=False) random_event.save() missions = MissionClockEvent.missions(self.user1) self.assertEqual([ TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2002, self.year2003) ], missions)
def test_kml(self): """ Tests the generation of kml data The correct number of elements are generated The meta-data tag is present """ for cur_obst in self.obstacles: kml = Kml() kml_mission = kml.newfolder(name='SubFolder') cur_obst.kml([ TimePeriod(timezone.now(), timezone.now() + datetime.timedelta(seconds=10)) ], kml_mission, kml.document) result_kml = kml.kml() self.assertEqual(101, result_kml.count('<gx:coord>'))
def test_non_constant_rate(self): """Rates computed correctly when non-constant.""" delta = datetime.timedelta(seconds=1) start = timezone.now() self.create_logs(self.user1, num=10, start=start, delta=delta) self.create_logs(self.user1, num=10, start=start + 10 * delta, delta=2 * delta) period = TimePeriod(start - delta, start + 10 * delta + 10 * (2 * delta)) rates = UasTelemetry.rates(self.user1, [period]) self.assertSequenceEqual((2, (1.0 * 11 + 2.0 * 10) / (11 + 10)), rates)
def test_no_data(self): self.assertEqual(None, UasTelemetry.last_for_user(self.user1)) self.assertEqual(0, len(UasTelemetry.by_user(self.user1))) # Empty time priod. self.assertSequenceEqual([], UasTelemetry.by_time_period(self.user1, [])) self.assertTupleEqual((None, None), UasTelemetry.rates(self.user1, [])) start = timezone.now() delta = datetime.timedelta(seconds=10) # Time periods which calculate a rate. time_period_sets = [ [TimePeriod(start, start + delta)], [ TimePeriod(start, start + delta), TimePeriod(start + delta, start + delta * 2) ], ] for time_periods in time_period_sets: for logs in UasTelemetry.by_time_period(self.user1, time_periods): self.assertEqual(0, len(logs)) self.assertTupleEqual( (delta.total_seconds(), delta.total_seconds()), UasTelemetry.rates(self.user1, time_periods)) # Open time periods which can't calculate a rate. time_period_sets = [ [], [TimePeriod(None, start)], [TimePeriod(start + delta, None)], [ TimePeriod(None, start), TimePeriod(start, start + delta), TimePeriod(start + delta, None) ], ] for time_periods in time_period_sets: for logs in UasTelemetry.by_time_period(self.user1, time_periods): self.assertEqual(0, len(logs)) self.assertTupleEqual((None, None), UasTelemetry.rates(self.user1, time_periods))
def test_within_standard(self): """Tests the within method with defined start and end.""" t = TimePeriod(start=datetime.datetime(2000, 1, 1), end=datetime.datetime(2001, 1, 1)) # Clearly within self.assertTrue(t.within(datetime.datetime(2000, 6, 1))) # Inclusive start self.assertTrue(t.within(datetime.datetime(2000, 1, 1))) # Inclusive end self.assertTrue(t.within(datetime.datetime(2001, 1, 1))) # Not within below self.assertFalse(t.within(datetime.datetime(1999, 1, 1))) # Not within above self.assertFalse(t.within(datetime.datetime(2002, 1, 1)))
def test_eq(self): """Tests TimePeriod equality.""" # Self equality a = TimePeriod() self.assertEqual(a, a) # Two None objects a = TimePeriod() b = TimePeriod() self.assertEqual(a, b) # Same start (and end) a = TimePeriod(start=timezone.now()) b = TimePeriod(start=a.start) self.assertEqual(a, b) # Same start, different end a = TimePeriod(start=timezone.now()) b = TimePeriod(start=a.start, end=timezone.now()) self.assertNotEqual(a, b) # Different start, same end a = TimePeriod(start=timezone.now(), end=timezone.now()) b = TimePeriod(start=timezone.now(), end=a.end) self.assertNotEqual(a, b) # Different start, different end a = TimePeriod(start=timezone.now(), end=timezone.now()) b = TimePeriod(start=timezone.now(), end=timezone.now()) self.assertNotEqual(a, b)
def test_duration_infinite(self): """Tests the duration with infinite value (no endpoint).""" t = TimePeriod(start=datetime.datetime(2000, 1, 1)) self.assertIsNone(t.duration()) t = TimePeriod(end=datetime.datetime(2000, 1, 1)) self.assertIsNone(t.duration())
def test_duration_finite(self): """Tests the duration with endpoints and finite time.""" t = TimePeriod(start=datetime.datetime(2000, 1, 1), end=datetime.datetime(2000, 1, 2)) self.assertEqual(datetime.timedelta(days=1), t.duration())