Exemplo n.º 1
0
 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))
Exemplo n.º 2
0
    def test_one_intersecting_period(self):
        """Only one period matches logs."""
        results = AccessLog.by_time_period(self.user1, [
            TimePeriod(self.year2001, self.year2002),
            TimePeriod(self.year2003, self.year2004),
        ])

        self.assertSequenceEqual([[], self.year2003_logs],
                                 self.to_lists(results))
Exemplo n.º 3
0
    def test_both_periods(self):
        """Both sets of logs, accesses individually."""
        results = AccessLog.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))
Exemplo n.º 4
0
    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),
        ])
Exemplo n.º 5
0
    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),
        ])
Exemplo n.º 6
0
 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))
Exemplo n.º 7
0
    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),
        ])
Exemplo n.º 8
0
    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),
        ])
Exemplo n.º 9
0
    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)))
Exemplo n.º 10
0
    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)))
Exemplo n.º 11
0
    def test_open_end(self):
        """Logs (2003, inf)"""
        results = AccessLog.by_time_period(self.user1, [
            TimePeriod(self.year2003, None),
        ])

        self.assertSequenceEqual([self.year2003_logs], self.to_lists(results))
Exemplo n.º 12
0
    def test_open_start(self):
        """Logs (-inf, 2001)"""
        results = AccessLog.by_time_period(self.user1, [
            TimePeriod(None, self.year2001),
        ])

        self.assertSequenceEqual([self.year2000_logs], self.to_lists(results))
Exemplo n.º 13
0
    def test_non_intersecting_period(self):
        """No logs matched."""
        results = AccessLog.by_time_period(self.user1, [
            TimePeriod(self.year2001, self.year2002),
        ])

        self.assertSequenceEqual([[]], self.to_lists(results))
Exemplo n.º 14
0
    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
Exemplo n.º 15
0
    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
Exemplo n.º 16
0
    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),
        ])
Exemplo n.º 17
0
    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),
        ])
Exemplo n.º 18
0
    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 = AccessLog.rates(self.user1, [period])

        self.assertSequenceEqual((1, 1), rates)
Exemplo n.º 19
0
 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))
Exemplo n.º 20
0
 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))
Exemplo n.º 21
0
    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)
Exemplo n.º 22
0
    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)))
Exemplo n.º 23
0
    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)))
Exemplo n.º 24
0
    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)))
Exemplo n.º 25
0
    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)))
Exemplo n.º 26
0
 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())
Exemplo n.º 27
0
 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())
Exemplo n.º 28
0
 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())
Exemplo n.º 29
0
    def test_single_period(self):
        """Single set of logs accessible."""
        results = AccessLog.by_time_period(
            self.user1, [TimePeriod(self.year2000, self.year2001)])

        self.assertSequenceEqual([self.year2000_logs], self.to_lists(results))
Exemplo n.º 30
0
    def test_full_range(self):
        """All logs from (-inf, inf)."""
        results = AccessLog.by_time_period(self.user1,
                                           [TimePeriod(None, None)])

        self.assertSequenceEqual([self.logs], self.to_lists(results))
Exemplo n.º 31
0
    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)
Exemplo n.º 32
0
 def consistent_period(self, logs, delta):
     # rates() uses time between beginning/end of the period
     # and the first/last log to compute rates, so to get constant rates,
     # the period must begin and end delta seconds before/after the logs.
     return TimePeriod(logs[0].timestamp - delta,
                       logs[-1].timestamp + delta)
Exemplo n.º 33
0
 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())