def test_database_empty_spans(self): a = datetime.fromtimestamp(0) b = datetime.fromtimestamp(1) self.assertEqual(list(self.database.get_desk_spans(a, b)), [Span(a, b, Down())]) self.assertEqual(list(self.database.get_session_spans(a, b)), [Span(a, b, Inactive())])
def test_compute_active_time_active(self): desk_span = Span(datetime.fromtimestamp(0), datetime.fromtimestamp(1000), Down()) session_span = Span(datetime.fromtimestamp(0), datetime.fromtimestamp(1000), Active()) self.assertEqual( stats.compute_active_time([session_span], [desk_span]), timedelta(seconds=1000))
def test_database_insert_session(self): a = datetime(2017, 1, 1) b = datetime(2017, 1, 2) c = datetime(2017, 1, 3) event = Event(b, Active()) self.database.insert_session_event(event) events = self.database.get_session_events() self.assertEqual(events, [event]) spans = list(self.database.get_session_spans(a, c)) self.assertEqual(spans, [Span(a, b, Inactive()), Span(b, c, Active())])
def test_database_insert_desk(self): a = datetime(2017, 1, 1) b = datetime(2017, 1, 2) c = datetime(2017, 1, 3) event = Event(b, Up()) self.database.insert_desk_event(event) events = self.database.get_desk_events() self.assertEqual(events, [event]) spans = list(self.database.get_desk_spans(a, c)) self.assertEqual(spans, [Span(a, b, Down()), Span(b, c, Up())])
def test_webserver_get_session(self): self.database.return_value.get_session_spans.return_value = [ Span(0, 1, model.Inactive()) ] rv = self.app.get('/api/session') self.assertEqual(b'0', rv.data) self.database.return_value.get_session_spans.return_value = [ Span(0, 1, model.Active()) ] rv = self.app.get('/api/session') self.assertEqual(b'1', rv.data)
def test_webserver_get_desk(self): self.database.return_value.get_desk_spans.return_value = [ Span(0, 1, model.Down()) ] rv = self.app.get('/api/desk') self.assertEqual(b'0', rv.data) self.database.return_value.get_desk_spans.return_value = [ Span(0, 1, model.Up()) ] rv = self.app.get('/api/desk') self.assertEqual(b'1', rv.data)
def test_set_desk_down(self, compute_active_time): compute_active_time.return_value = timedelta(0) self.database.get_session_spans.return_value = [ Span(self.beginning, self.now, model.Active()) ] self.database.get_desk_spans.return_value = [ Span(self.beginning, self.now, model.Down()) ] event = Event(datetime(2017, 2, 13, 12, 0, 0), model.Down()) self.controller.set_desk(event.index, event.data) self.database.insert_desk_event.assert_called_with(event) self.hardware.go.assert_called_with(event.data) self.timer.stop.assert_not_called() self.timer.schedule.assert_called_with( timedelta(minutes=50), model.Up())
def test_init_active_and_down(self): self.database.get_session_spans.return_value = [ Span(self.beginning, self.now, model.Active()) ] self.controller.init(self.now) self.timer.schedule.assert_called_once_with(timedelta(0), model.Up()) self.hardware.light.assert_called_once_with(model.Active()) self.hardware.go.assert_not_called()
def test_update_timer_active_duration(self, compute_active_time): compute_active_time.return_value = timedelta(minutes=10) self.database.get_session_spans.return_value = [ Span(self.beginning, self.now, model.Active()) ] self.controller.update_timer(self.now) self.timer.schedule.assert_called_with( timedelta(minutes=40), model.Up())
def test_group_into_days_identity(self): spans = [ Span(datetime(2017, 4, 12, 10, 0, 0), datetime(2017, 4, 12, 10, 30, 0), Active()) ] flatten = lambda l: [item for sublist in l for item in sublist] daily_active_time = stats.compute_daily_active_time(spans) self.assertEqual(flatten(stats.group_into_days(daily_active_time)), daily_active_time)
def setUp(self): self.database_patcher = patch( 'autodesk.model.Database', autospec=True) self.timer_patcher = patch( 'autodesk.timer.Timer', autospec=True) self.hardware_patcher = patch( 'autodesk.hardware.Hardware', autospec=True) # Starting the patch will import hardware and thus try to import the # RPi modules which fails on non raspberry computers. import sys sys.modules['RPi'] = MagicMock() self.addCleanup(sys.modules.pop, 'RPi') sys.modules['RPi.GPIO'] = MagicMock() self.addCleanup(sys.modules.pop, 'RPi.GPIO') self.database = self.database_patcher.start() self.addCleanup(self.database_patcher.stop) self.timer = self.timer_patcher.start() self.addCleanup(self.timer_patcher.stop) self.hardware = self.hardware_patcher.start() self.addCleanup(self.hardware_patcher.stop) self.beginning = datetime.fromtimestamp(0) self.now = datetime(2017, 4, 14, 10, 0, 0) self.database.get_desk_spans.return_value = [ Span(self.beginning, self.now, model.Down()) ] self.database.get_session_spans.return_value = [ Span(self.beginning, self.now, model.Inactive()) ] limits = (timedelta(minutes=50), timedelta(minutes=10)) self.controller = Controller( self.hardware, limits, self.timer, self.database) self.addCleanup(self.controller.close)
def test_compute_daily_active_time_sum(self): spans = [ Span(datetime(2017, 4, 12, 10, 0, 0), datetime(2017, 4, 12, 10, 30, 0), Active()) ] self.assertEqual(sum(stats.compute_daily_active_time(spans)), 30)