From 1555f232153b5904b67e924275e5d39ff506adf2 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 21 Jan 2019 15:25:46 +0000 Subject: [PATCH] tests: add some more helper function tests. --- tests/call_error_test.py | 35 +++++++++++++++++++++++ tests/error_test.py | 40 ++++++++++++++++++++++++++ tests/policy_function_test.py | 40 ++++++++++++++++++++++++++ tests/types_test.py | 54 +++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 tests/error_test.py create mode 100644 tests/policy_function_test.py diff --git a/tests/call_error_test.py b/tests/call_error_test.py index 1480a743..72d3c46e 100644 --- a/tests/call_error_test.py +++ b/tests/call_error_test.py @@ -16,15 +16,18 @@ class ConstructorTest(testlib.TestCase): def test_string_noargs(self): e = self.klass('%s%s') self.assertEquals(e.args[0], '%s%s') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) def test_string_args(self): e = self.klass('%s%s', 1, 1) self.assertEquals(e.args[0], '11') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) def test_from_exc(self): ve = plain_old_module.MyError('eek') e = self.klass(ve) self.assertEquals(e.args[0], 'plain_old_module.MyError: eek') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) def test_form_base_exc(self): ve = SystemExit('eek') @@ -32,6 +35,7 @@ class ConstructorTest(testlib.TestCase): self.assertEquals(e.args[0], # varies across 2/3. '%s.%s: eek' % (type(ve).__module__, type(ve).__name__)) + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) def test_from_exc_tb(self): try: @@ -41,8 +45,39 @@ class ConstructorTest(testlib.TestCase): e = self.klass(ve) self.assertTrue(e.args[0].startswith('plain_old_module.MyError: eek')) + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) self.assertTrue('test_from_exc_tb' in e.args[0]) + def test_bytestring_conversion(self): + e = self.klass(mitogen.core.b('bytes')) + self.assertEquals(u'bytes', e.args[0]) + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) + + def test_reduce(self): + e = self.klass('eek') + func, (arg,) = e.__reduce__() + self.assertTrue(func is mitogen.core._unpickle_call_error) + self.assertEquals(arg, e.args[0]) + + +class UnpickleCallErrorTest(testlib.TestCase): + func = staticmethod(mitogen.core._unpickle_call_error) + + def test_not_unicode(self): + self.assertRaises(TypeError, + lambda: self.func(mitogen.core.b('bad'))) + + def test_oversized(self): + self.assertRaises(TypeError, + lambda: self.func(mitogen.core.b('b'*10001))) + + def test_reify(self): + e = self.func(u'some error') + self.assertEquals(mitogen.core.CallError, type(e)) + self.assertEquals(1, len(e.args)) + self.assertEquals(mitogen.core.UnicodeType, type(e.args[0])) + self.assertEquals(u'some error', e.args[0]) + class PickleTest(testlib.TestCase): klass = mitogen.core.CallError diff --git a/tests/error_test.py b/tests/error_test.py new file mode 100644 index 00000000..7f890875 --- /dev/null +++ b/tests/error_test.py @@ -0,0 +1,40 @@ + +import logging +import subprocess +import time +import zlib + +import unittest2 + +import testlib +import mitogen.master +import mitogen.parent +import mitogen.utils + + +class ConstructorTest(testlib.TestCase): + klass = mitogen.core.Error + + def test_literal_no_format(self): + e = self.klass('error') + self.assertEquals(e.args[0], 'error') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) + + def test_literal_format_chars_present(self): + e = self.klass('error%s') + self.assertEquals(e.args[0], 'error%s') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) + + def test_format(self): + e = self.klass('error%s', 123) + self.assertEquals(e.args[0], 'error123') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) + + def test_bytes_to_unicode(self): + e = self.klass(mitogen.core.b('error')) + self.assertEquals(e.args[0], 'error') + self.assertTrue(isinstance(e.args[0], mitogen.core.UnicodeType)) + + +if __name__ == '__main__': + unittest2.main() diff --git a/tests/policy_function_test.py b/tests/policy_function_test.py new file mode 100644 index 00000000..56e33b89 --- /dev/null +++ b/tests/policy_function_test.py @@ -0,0 +1,40 @@ + +import mock +import unittest2 + +import mitogen.core +import mitogen.parent + +import testlib + + +class HasParentAuthorityTest(testlib.TestCase): + func = staticmethod(mitogen.core.has_parent_authority) + + def call(self, auth_id): + msg = mitogen.core.Message(auth_id=auth_id) + return self.func(msg) + + @mock.patch('mitogen.context_id', 5555) + @mock.patch('mitogen.parent_ids', [111, 222]) + def test_okay(self): + self.assertFalse(self.call(0)) + self.assertTrue(self.call(5555)) + self.assertTrue(self.call(111)) + + +class IsImmediateChildTest(testlib.TestCase): + func = staticmethod(mitogen.core.has_parent_authority) + + def call(self, auth_id, remote_id): + msg = mitogen.core.Message(auth_id=auth_id) + stream = mock.Mock(remote_id=remote_id) + return self.func(msg, stream) + + def test_okay(self): + self.assertFalse(0, 1) + self.assertTrue(1, 1) + + +if __name__ == '__main__': + unittest2.main() diff --git a/tests/types_test.py b/tests/types_test.py index f929c098..2f0b1306 100644 --- a/tests/types_test.py +++ b/tests/types_test.py @@ -76,5 +76,59 @@ class SecretTest(testlib.TestCase): mitogen.core.b(secret2)) +class KwargsTest(testlib.TestCase): + klass = mitogen.core.Kwargs + + def test_empty(self): + kw = self.klass({}) + self.assertEquals({}, kw) + self.assertEquals('Kwargs({})', repr(kw)) + klass, (dct,) = kw.__reduce__() + self.assertTrue(klass is self.klass) + self.assertTrue(type(dct) is dict) + self.assertEquals({}, dct) + + @unittest2.skipIf(condition=lambda: not mitogen.core.PY3, + reason='py3 only') + def test_unicode_conversion(self): + kw = self.klass({mitogen.core.b('key'): 123}) + self.assertEquals({mitogen.core.b('key'): 123}, kw) + self.assertEquals("Kwargs({'key': 123})", repr(kw)) + klass, (dct,) = kw.__reduce__() + self.assertTrue(klass is self.klass) + self.assertTrue(type(dct) is dict) + self.assertEquals({u'key': 123}, dct) + key, = dct + self.assertTrue(type(key) is mitogen.core.UnicodeType) + + +class AdornedUnicode(mitogen.core.UnicodeType): + pass + + +class ToTextTest(testlib.TestCase): + func = staticmethod(mitogen.core.to_text) + + def test_bytes(self): + s = self.func(mitogen.core.b('bytes')) + self.assertEquals(mitogen.core.UnicodeType, type(s)) + self.assertEquals(s, u'bytes') + + def test_unicode(self): + s = self.func(u'text') + self.assertEquals(mitogen.core.UnicodeType, type(s)) + self.assertEquals(s, u'text') + + def test_adorned_unicode(self): + s = self.func(AdornedUnicode(u'text')) + self.assertEquals(mitogen.core.UnicodeType, type(s)) + self.assertEquals(s, u'text') + + def test_integer(self): + s = self.func(123) + self.assertEquals(mitogen.core.UnicodeType, type(s)) + self.assertEquals(s, u'123') + + if __name__ == '__main__': unittest2.main()