Esempio n. 1
0
    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()
Esempio n. 2
0
    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()
Esempio n. 3
0
    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)
Esempio n. 4
0
    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()