コード例 #1
0
        class TestPattern(ResponsePattern):
            response = [
                OptimizedSpeech.build('1234', '210'),
                OptimizedSpeech.build('1234', '123')
            ]

            @Ev.ON_MESSAGE('122')
            def pattern122(self, request: OptimizedSpeech):
                return self.response
コード例 #2
0
 def test_build_valid__predefine__user_defined(self):
     speech = OptimizedSpeech.build('1234', '098', 'Charge is low.',
                                    '5% remaining.')
     expect = OptimizedSpeech('1234', '098', 'Status',
                              'Going offline and into storage.',
                              ['Charge is low.', '5% remaining.'])
     self.assertEqual(speech, expect)
コード例 #3
0
 def test_build_valid__user_defined(self):
     speech = OptimizedSpeech.build(
         '1234', '050', 'This drone is ready to obey, Hive Mxtress.')
     expect = OptimizedSpeech(
         '1234', '050', 'Statement', None,
         ['This drone is ready to obey, Hive Mxtress.'])
     self.assertEqual(speech, expect)
コード例 #4
0
    def test_2_decorators_2(self):
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '123')

            @Ev.ON_MESSAGE('122', '123')
            def patterns(self, request: OptimizedSpeech):
                return self.response

        pattern = TestPattern()

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1111', '122'))
        self.assertEqual(expected, actual)

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1111', '123'))
        self.assertEqual(expected, actual)
コード例 #5
0
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '109')

            @Ev.ON_MESSAGE('122')
            def pattern122(self, request: OptimizedSpeech):
                err = 1 / 0
                return OptimizedSpeech.build('1234', '123')

            @Ev.ON_ERROR
            def error(self, error):
                return self.response
コード例 #6
0
    def test_no_return(self):
        class TestPattern(ResponsePattern):
            response = None

            @Ev.ON_MESSAGE('000')
            def pattern000(self, request: OptimizedSpeech):
                return self.response

        pattern = TestPattern()

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1234', '000'))
        self.assertEqual(expected, actual)
コード例 #7
0
    def test_on_unregistered_message(self):
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '400')

            @Ev.ON_UNREGISTERED
            def unregistered(self, request: OptimizedSpeech):
                return self.response

        pattern = TestPattern()

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1111', '050'))
        self.assertEqual(expected, actual)
コード例 #8
0
    def test_multi_returns(self):
        class TestPattern(ResponsePattern):
            response = [
                OptimizedSpeech.build('1234', '210'),
                OptimizedSpeech.build('1234', '123')
            ]

            @Ev.ON_MESSAGE('122')
            def pattern122(self, request: OptimizedSpeech):
                return self.response

        pattern = TestPattern()

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1111', '122'))
        self.assertEqual(expected, actual)
コード例 #9
0
    def test_on_error_message(self):
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '109')

            @Ev.ON_MESSAGE('122')
            def pattern122(self, request: OptimizedSpeech):
                err = 1 / 0
                return OptimizedSpeech.build('1234', '123')

            @Ev.ON_ERROR
            def error(self, error):
                return self.response

        pattern = TestPattern()

        expected = pattern.response
        actual = pattern(OptimizedSpeech.build('1111', '122'))
        self.assertEqual(expected, actual)
コード例 #10
0
    def test_call_with_args(self):
        class TestPattern(ResponsePattern):
            @Ev.ON_MESSAGE('050')
            def pattern050(self, request: OptimizedSpeech, now: str):
                if 'error' in request.user_defined_messages:
                    raise ValueError()
                return OptimizedSpeech.build('1234', '050', now)

            @Ev.ON_UNREGISTERED
            def unregistered(self, request: OptimizedSpeech, now: str):
                return OptimizedSpeech.build('1234', '400', now)

            @Ev.ON_ERROR
            def error(self, error: BaseException, now: str):
                return OptimizedSpeech.build('1234', '109', now)

        pattern = TestPattern()
        from datetime import datetime

        now = datetime.now().strftime('%H:%M:%S')
        request = OptimizedSpeech.build('1111', '050')
        expected = OptimizedSpeech.build('1234', '050', now)
        actual = pattern(request, now=now)
        self.assertEqual(expected, actual)

        now = datetime.now().strftime('%H:%M:%S')
        request = OptimizedSpeech.build('1111', '050', 'error')
        expected = OptimizedSpeech.build('1234', '109', now)
        actual = pattern(request, now=now)
        self.assertEqual(expected, actual)

        now = datetime.now().strftime('%H:%M:%S')
        request = OptimizedSpeech.build('1111', '105', 'error')
        expected = OptimizedSpeech.build('1234', '400', now)
        actual = pattern(request, now=now)
        self.assertEqual(expected, actual)
コード例 #11
0
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '400')

            @Ev.ON_UNREGISTERED
            def unregistered(self, request: OptimizedSpeech):
                return self.response
コード例 #12
0
 def test_build_valid__predefined(self):
     speech = OptimizedSpeech.build('1234', '304')
     expect = OptimizedSpeech('1234', '304', 'Mantra',
                              'It obeys the Hive Mxtress.', [])
     self.assertEqual(speech, expect)
コード例 #13
0
 def test_build_valid(self):
     speech = OptimizedSpeech.build('1234', '050')
     expect = OptimizedSpeech('1234', '050', 'Statement', None, [])
     self.assertEqual(speech, expect)
コード例 #14
0
 def pattern122(self, request: OptimizedSpeech):
     err = 1 / 0
     return OptimizedSpeech.build('1234', '123')
コード例 #15
0
 def pattern050(self, request: OptimizedSpeech, now: str):
     if 'error' in request.user_defined_messages:
         raise ValueError()
     return OptimizedSpeech.build('1234', '050', now)
コード例 #16
0
 def unregistered(self, request: OptimizedSpeech, now: str):
     return OptimizedSpeech.build('1234', '400', now)
コード例 #17
0
        class TestPattern(ResponsePattern):
            response = OptimizedSpeech.build('1234', '400')

            @Ev.ON_INVALID
            def invalid(self, request: str):
                return self.response
コード例 #18
0
 def error(self, error: BaseException, now: str):
     return OptimizedSpeech.build('1234', '109', now)
コード例 #19
0
 def test_build_invalid__status_code(self):
     speech = OptimizedSpeech.build('1234', '999')
     self.assertIsNone(speech)
コード例 #20
0
 def _build_message(self, status_code: str, *message: str):
     return OptimizedSpeech.build(self.drone_id, status_code, *message)