class TestPattern(ResponsePattern): response = [ OptimizedSpeech.build('1234', '210'), OptimizedSpeech.build('1234', '123') ] @Ev.ON_MESSAGE('122') def pattern122(self, request: OptimizedSpeech): return self.response
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)
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)
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)
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
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)
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)
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)
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)
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)
class TestPattern(ResponsePattern): response = OptimizedSpeech.build('1234', '400') @Ev.ON_UNREGISTERED def unregistered(self, request: OptimizedSpeech): return self.response
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)
def test_build_valid(self): speech = OptimizedSpeech.build('1234', '050') expect = OptimizedSpeech('1234', '050', 'Statement', None, []) self.assertEqual(speech, expect)
def pattern122(self, request: OptimizedSpeech): err = 1 / 0 return OptimizedSpeech.build('1234', '123')
def pattern050(self, request: OptimizedSpeech, now: str): if 'error' in request.user_defined_messages: raise ValueError() return OptimizedSpeech.build('1234', '050', now)
def unregistered(self, request: OptimizedSpeech, now: str): return OptimizedSpeech.build('1234', '400', now)
class TestPattern(ResponsePattern): response = OptimizedSpeech.build('1234', '400') @Ev.ON_INVALID def invalid(self, request: str): return self.response
def error(self, error: BaseException, now: str): return OptimizedSpeech.build('1234', '109', now)
def test_build_invalid__status_code(self): speech = OptimizedSpeech.build('1234', '999') self.assertIsNone(speech)
def _build_message(self, status_code: str, *message: str): return OptimizedSpeech.build(self.drone_id, status_code, *message)