def test_walking_suggestion_service_error(self, decide): WalkingSuggestionDecisionService.process_decision(self.decision) decision = WalkingSuggestionDecision.objects.get() self.assertFalse(decision.treated) self.assertFalse(decision.available) self.assertTrue(decision.unavailable_service_error)
def test_single_walking_suggestion_per_period(self): current_time = timezone.now().replace( hour = 14, minute = 0 ) suggestion_time = current_time + timedelta(minutes=10) SuggestionTime.objects.create( user = self.user, category = SuggestionTime.MIDAFTERNOON, hour = suggestion_time.hour, minute = suggestion_time.minute ) # Make walking suggestion decision 5 minutes before current_time # so as second walking suggestion decision can't be made at durring # the MIDAFTERNOON timeperiod. WalkingSuggestionDecisionService.make_decision( datetime = current_time - timedelta(minutes=5), user=self.user ) try: WalkingSuggestionDecisionService.make_decision( user = self.user, datetime = current_time ) self.fail('Should not have made second walking suggestion decision') except WalkingSuggestionDecisionService.RandomizationUnavailable: pass self.assertEqual(WalkingSuggestionDecision.objects.count(), 1)
def test_step_count_was_over_limit(self): self.create_step_count(10, 5) self.create_step_count(100, 20) decision = WalkingSuggestionDecision.objects.create( user=self.user, time=timezone.now()) service = WalkingSuggestionDecisionService(decision) service.update_availability() decision = WalkingSuggestionDecision.objects.get() self.assertTrue(decision.available)
def test_available_with_no_step_counts(self): decision = WalkingSuggestionDecision.objects.create( user=self.user, time=timezone.now()) service = WalkingSuggestionDecisionService(decision) available = service.update_availability() self.assertFalse(available) decision = WalkingSuggestionDecision.objects.get() self.assertTrue(decision.available) self.assertFalse(decision.unavailable_no_step_count_data)
def test_step_count_over_limit(self): self.create_step_count(10, 5) self.create_step_count(100, 10) decision = WalkingSuggestionDecision.objects.create( user=self.user, time=timezone.now()) service = WalkingSuggestionDecisionService(decision) available = service.update_availability() self.assertFalse(available) decision = WalkingSuggestionDecision.objects.get() self.assertFalse(decision.available) self.assertTrue(decision.unavailable_not_sedentary)
def test_configuration_not_enabled(self): self.configuration.enabled = False self.configuration.save() decision = WalkingSuggestionDecision.objects.create( user=self.user, time=timezone.now()) service = WalkingSuggestionDecisionService(decision) available = service.update_availability() self.assertFalse(available) decision = WalkingSuggestionDecision.objects.get() self.assertFalse(decision.available) self.assertTrue(decision.unavailable_disabled)
def test_step_count_before_walking_suggestion_time(self): current_time = timezone.now().replace(hour=14, minute=0) suggestion_time = current_time + timedelta(minutes=10) # Destroy default suggestion times, so test logic will work SuggestionTime.objects.all().delete() SuggestionTime.objects.create(user=self.user, category=SuggestionTime.MIDAFTERNOON, hour=suggestion_time.hour, minute=suggestion_time.minute) WalkingSuggestionDecisionService.make_decision(datetime=current_time, user=self.user) decision = WalkingSuggestionDecision.objects.get() self.assertEqual(decision.category, SuggestionTime.MIDAFTERNOON)
def testCreateDecision(self): datetime_now = datetime(2019, 4, 30, 20, 0).astimezone(pytz.UTC) self.now.return_value = datetime_now SuggestionTime.objects.create( user = self.user, category = SuggestionTime.EVENING, hour = 20, minute = 0 ) WalkingSuggestionDecisionService.make_decision_now(username='******') decision = WalkingSuggestionDecision.objects.get() self.assertEqual(decision.time, datetime_now) self.assertIn(SuggestionTime.EVENING, decision.get_context()) self.process_decision.assert_called()
def test_handles_timezones_correctly(self, get_timezone_at): tz = pytz.timezone('America/Los_Angeles') get_timezone_at.return_value = tz datetime_now = tz.localize(datetime(2019, 4, 30, 20, 4)) self.now.return_value = datetime_now SuggestionTime.objects.create( user = self.user, category = SuggestionTime.EVENING, hour = 20, minute = 0 ) WalkingSuggestionDecisionService.make_decision_now(username='******') decision = WalkingSuggestionDecision.objects.get() self.assertEqual(decision.time, datetime_now) self.assertIn(SuggestionTime.EVENING, decision.get_context()) self.process_decision.assert_called()
def send_walking_suggestion(modeladmin, request, queryset): for configuration in queryset: category = random.choice(SuggestionTime.TIMES) decision_service = WalkingSuggestionDecisionService.create_decision( user = configuration.user, category = category, test = True ) decision_service.update_context() decision_service.decide() decision_service.send_message()
def testDoesNotCreateMultipleDecisionsOfSameType(self): SuggestionTime.objects.create( user =self.user, category = SuggestionTime.LUNCH, hour = 11, minute = 0 ) SuggestionTime.objects.create( user = self.user, category = SuggestionTime.MIDAFTERNOON, hour = 15, minute = 30 ) self.now.return_value = datetime(2019, 4, 30, 11).astimezone(pytz.UTC) WalkingSuggestionDecisionService.make_decision_now(username='******') self.assertEqual(WalkingSuggestionDecision.objects.count(), 1) #Should not make new decision self.now.return_value = datetime(2019, 4, 30, 11, 3).astimezone(pytz.UTC) try: WalkingSuggestionDecisionService.make_decision_now(username='******') self.fail('Should have thrown exception') except WalkingSuggestionDecisionService.RandomizationUnavailable: pass self.assertEqual(WalkingSuggestionDecision.objects.count(), 1) #New decision for midafternoon self.now.return_value = datetime(2019, 4, 30, 15, 30).astimezone(pytz.UTC) WalkingSuggestionDecisionService.make_decision_now(username='******') self.assertEqual(WalkingSuggestionDecision.objects.count(), 2)
def testDoesNotCreateDecisionIfNotCorrectTime(self): SuggestionTime.objects.create( user =self.user, category = SuggestionTime.LUNCH, hour = 11, minute = 0 ) # Before decision window self.now.return_value = datetime(2019, 4, 30, 10, 54).astimezone(pytz.UTC) try: WalkingSuggestionDecisionService.make_decision_now(username='******') self.fail('Should have thrown exception') except WalkingSuggestionDecisionService.RandomizationUnavailable: pass # After decision window self.now.return_value = datetime(2019, 4, 30, 11, 6).astimezone(pytz.UTC) try: WalkingSuggestionDecisionService.make_decision_now(username='******') self.fail('Should have thrown exception') except WalkingSuggestionDecisionService.RandomizationUnavailable: pass self.assertEqual(WalkingSuggestionDecision.objects.count(), 0) self.process_decision.assert_not_called() # In decision window self.now.return_value = datetime(2019, 4, 30, 11, 3).astimezone(pytz.UTC) WalkingSuggestionDecisionService.make_decision_now(username='******') self.assertEqual(WalkingSuggestionDecision.objects.count(), 1) self.process_decision.assert_called()
def test_catch_walking_suggestion_randomization_unavialable(self): self.walking_suggestion_make_decision.side_effect = RuntimeError( 'test') try: step_count_message_randomization(username='******') self.fail('Should have failed') except RuntimeError: pass self.anti_sedentary_make_decision.assert_not_called() self.walking_suggestion_make_decision.side_effect = WalkingSuggestionDecisionService.RandomizationUnavailable( 'test') step_count_message_randomization(username='******') self.anti_sedentary_make_decision.assert_called_with(username='******')
def test_decision_not_to_treat(self, decide): WalkingSuggestionDecisionService.process_decision(self.decision) self.update_context.assert_called() decide.assert_called() self.send_message.assert_not_called()