def test_basic_access(self): start = timezone.now() - datetime.timedelta(seconds=10) logs = self.create_logs(self.user1, start=start) log = AccessLog.last_for_user(self.user1) self.assertEqual(logs[-1], log) results = AccessLog.by_user(self.user1) self.assertSequenceEqual(logs, results)
def test_no_data(self): logs = AccessLog.by_user(self.user1) self.assertEqual(len(logs), 0) logs = AccessLog.by_time_period(self.user1, []) self.assertEqual(len(logs), 0) log_rates = AccessLog.rates(self.user1, []) self.assertTupleEqual(log_rates, (None, None))
def test_no_data(self): log = AccessLog.last_for_user(self.user1) self.assertEqual(None, log) logs = AccessLog.by_user(self.user1) self.assertEqual(len(logs), 0) logs = AccessLog.by_time_period(self.user1, []) self.assertEqual(len(logs), 0) log_rates = AccessLog.rates(self.user1, []) self.assertTupleEqual(log_rates, (None, None))
def test_multi_user(self): # Intersperse logs from two users logs = [] for _ in xrange(10): logs += self.create_logs(self.user1, num=1) self.create_logs(self.user2, num=1) log = AccessLog.last_for_user(self.user1) self.assertEqual(logs[-1], log) results = AccessLog.by_user(self.user1) self.assertSequenceEqual(logs, results)
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))
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))
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))
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))
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))
def test_last_for_user_before_time(self): start = timezone.now() delta = datetime.timedelta(seconds=1) logs = self.create_logs(self.user1, num=10, start=start, delta=delta) before_time = start + delta * 3 log = AccessLog.last_for_user(self.user1, before_time=before_time) self.assertEqual(logs[2], log)
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))
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))
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))
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))
def test_user_active(self): delta = datetime.timedelta(seconds=1) self.create_logs(self.user1, start=self.year2000, num=10, delta=delta) latest_time = self.year2000 + 10 * delta # Active for user with recent logs self.assertTrue(AccessLog.user_active(self.user1, base=latest_time)) # Not active for user with no logs self.assertFalse(AccessLog.user_active(self.user2, base=latest_time)) # Not active for user with no recent logs self.assertFalse(AccessLog.user_active(self.user1, base=self.year2001)) # Active now self.create_logs(self.user1, num=10, delta=delta) self.assertTrue(AccessLog.user_active(self.user1))
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)
def test_constant_rate(self): """Rates computed correctly.""" delta = datetime.timedelta(seconds=1) logs = self.create_logs(self.user1, delta=delta) period = self.consistent_period(logs, delta) rates = AccessLog.rates(self.user1, [period]) self.assertSequenceEqual((1, 1), rates)
def test_provided_logs(self): """Rates computed with provided logs.""" delta = datetime.timedelta(seconds=1) used_logs = self.create_logs(self.user1, delta=delta) unused_logs = self.create_logs(self.user1, delta=delta) period = self.consistent_period(used_logs, delta) rates = AccessLog.rates(self.user1, [period], time_period_logs=[used_logs]) self.assertSequenceEqual((1, 1), rates)
def test_multiple_periods(self): """Multiple periods are combined without introducing errors.""" delta = datetime.timedelta(seconds=1) logs = [ self.create_logs(self.user1, start=self.year2000, delta=delta), self.create_logs(self.user1, start=self.year2001, delta=delta), ] periods = [self.consistent_period(l, delta) for l in logs] rates = AccessLog.rates(self.user1, periods) self.assertSequenceEqual((1, 1), rates)
def create_logs(self, user, num=10, start=None, delta=None): if start is None: start = timezone.now() if delta is None: delta = datetime.timedelta(seconds=1) logs = [] for i in xrange(num): log = AccessLog(user=user) log.save() log.timestamp = start + i * delta log.save() logs.append(log) return logs
def test_different_deltas(self): """Sets of logs are combined for overall rates.""" delta = datetime.timedelta(seconds=1) logs = [ self.create_logs(self.user1, num=1000, start=self.year2000, delta=delta), self.create_logs(self.user1, num=1000, start=self.year2001, delta=delta / 2), ] periods = [self.consistent_period(l, delta) for l in logs] rates = AccessLog.rates(self.user1, periods) self.assertAlmostEqual(1.0, rates[0]) # max self.assertAlmostEqual(0.75, rates[1], delta=0.001) # avg
def test_basic_access(self): logs = self.create_logs(self.user1) results = AccessLog.by_user(self.user1) self.assertSequenceEqual(logs, results)
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))
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))
def test_unicode(self): """Tests the unicode method executes.""" log = AccessLog(timestamp=timezone.now(), user=self.user1) log.save() log.__unicode__()