|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import json |
| 5 | +import unittest |
| 6 | + |
| 7 | +import azure.functions.timer as timer |
| 8 | +from azure.functions.meta import Datum |
| 9 | + |
| 10 | + |
| 11 | +class TestTimer(unittest.TestCase): |
| 12 | + def test_timer_decode(self): |
| 13 | + # given |
| 14 | + data = '''{"Schedule":{"AdjustForDST":true}, |
| 15 | + "ScheduleStatus":{ |
| 16 | + "Last":"2022-03-28T15:40:00.0105419-05:00", |
| 17 | + "Next":"2022-03-28T15:45:00-05:00", |
| 18 | + "LastUpdated":"2022-03-28T15:40:00.0105419-05:00"}, |
| 19 | + "IsPastDue":false}''' |
| 20 | + datum: Datum = Datum(value=data, type='json') |
| 21 | + data_dict = json.loads(data) |
| 22 | + |
| 23 | + # when |
| 24 | + timer_request: timer.TimerRequest = \ |
| 25 | + timer.TimerRequestConverter.decode(datum, trigger_metadata={}) |
| 26 | + |
| 27 | + # then |
| 28 | + self.assertEqual(timer_request.schedule, data_dict["Schedule"]) |
| 29 | + self.assertEqual(timer_request.schedule_status, |
| 30 | + data_dict["ScheduleStatus"]) |
| 31 | + self.assertEqual(timer_request.past_due, data_dict["IsPastDue"]) |
| 32 | + |
| 33 | + def test_timer_initialize_without_args(self): |
| 34 | + # given |
| 35 | + past_due = False |
| 36 | + schedule_status = {} |
| 37 | + schedule = {} |
| 38 | + |
| 39 | + # when |
| 40 | + test_timer = timer.TimerRequest() |
| 41 | + |
| 42 | + # then |
| 43 | + self.assertEqual(past_due, test_timer.past_due) |
| 44 | + self.assertEqual(schedule_status, test_timer.schedule_status) |
| 45 | + self.assertEqual(schedule, test_timer.schedule) |
| 46 | + |
| 47 | + def test_timer_no_implementation_exception(self): |
| 48 | + # given |
| 49 | + datum: Datum = Datum(value="test", type='string') |
| 50 | + is_exception_raised = False |
| 51 | + |
| 52 | + # when |
| 53 | + try: |
| 54 | + timer.TimerRequestConverter.decode(datum, trigger_metadata={}) |
| 55 | + except NotImplementedError: |
| 56 | + is_exception_raised = True |
| 57 | + |
| 58 | + # then |
| 59 | + self.assertTrue(is_exception_raised) |
| 60 | + |
| 61 | + def test_timer_input_type(self): |
| 62 | + check_input_type = ( |
| 63 | + timer.TimerRequestConverter.check_input_type_annotation |
| 64 | + ) |
| 65 | + self.assertTrue(check_input_type(timer.TimerRequest)) |
| 66 | + self.assertFalse(check_input_type(str)) |
0 commit comments