From d5227fee4a0a8c5825539fa62b7cd9badf158465 Mon Sep 17 00:00:00 2001 From: jsargiot Date: Sun, 17 Feb 2013 21:02:03 -0300 Subject: [PATCH] Adding some unit tests for the core. --- debugger_plugin/core/test/__init__.py | 0 debugger_plugin/core/test/helpers.py | 13 ++++ debugger_plugin/core/test/test_process.py | 52 ++++++++++++++ debugger_plugin/core/test/test_rpc_adapter.py | 28 ++++++++ debugger_plugin/core/test/test_serialize.py | 67 +++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 debugger_plugin/core/test/__init__.py create mode 100644 debugger_plugin/core/test/helpers.py create mode 100644 debugger_plugin/core/test/test_process.py create mode 100644 debugger_plugin/core/test/test_rpc_adapter.py create mode 100644 debugger_plugin/core/test/test_serialize.py diff --git a/debugger_plugin/core/test/__init__.py b/debugger_plugin/core/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/debugger_plugin/core/test/helpers.py b/debugger_plugin/core/test/helpers.py new file mode 100644 index 0000000..6b1a408 --- /dev/null +++ b/debugger_plugin/core/test/helpers.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 *-* + +import ndb + +class MockDebugger(ndb.Debugger): + + def __init__(self): + ndb.Debugger.__init__(self, 'mock.py') + + def run(self): + # bla bla debugging + self._state = ndb.STATE_TERMINATED diff --git a/debugger_plugin/core/test/test_process.py b/debugger_plugin/core/test/test_process.py new file mode 100644 index 0000000..e60d425 --- /dev/null +++ b/debugger_plugin/core/test/test_process.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 *-* +import tempfile +import os +import unittest + +import process + + +class TestCodeExecutor(unittest.TestCase): + + code_a_eq_3 = '#/usr/env/python\na = 3' + + def test_load_file(self): + # Write a small temporary script + osfd, tp = tempfile.mkstemp() + try: + with os.fdopen(osfd, 'w') as fd: + fd.write(self.code_a_eq_3) + + ce = process.CodeExecutor(filename=tp) + self.assertEquals(ce._code.rstrip(), self.code_a_eq_3) + finally: + os.remove(tp) + + def test_run_with_file(self): + # Write a small temporary script + osfd, tp = tempfile.mkstemp() + try: + with os.fdopen(osfd, 'w') as fd: + fd.write(self.code_a_eq_3) + + global_run = {'a': 2} + ce = process.CodeExecutor(filename=tp) + ce.run(global_run) + self.assertEquals(global_run['a'], 3) + finally: + os.remove(tp) + + def test_load_string(self): + ce = process.CodeExecutor(string=self.code_a_eq_3) + self.assertEquals(ce._code.rstrip(), self.code_a_eq_3) + + def test_run_with_string(self): + global_run = {'a': 2} + ce = process.CodeExecutor(string=self.code_a_eq_3) + ce.run(global_run) + self.assertEquals(global_run['a'], 3) + + +if __name__ == '__main__': + unittest.main() diff --git a/debugger_plugin/core/test/test_rpc_adapter.py b/debugger_plugin/core/test/test_rpc_adapter.py new file mode 100644 index 0000000..0533482 --- /dev/null +++ b/debugger_plugin/core/test/test_rpc_adapter.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 *-* +""" +This module provides RPC interaction with the debugger. +""" + +import unittest +import xmlrpclib +import test.helpers + +import rpc_adapter +import ndb + + +class TestRPCDebuggerAdapter(unittest.TestCase): + + def test_start(self): + tst_dbg = test.helpers.MockDebugger() + + self.assertEquals(tst_dbg._state, ndb.STATE_PAUSED) + server = rpc_adapter.RPCDebuggerAdapter(tst_dbg) + server.export_start() + self.assertEquals(tst_dbg._state, ndb.STATE_RUNNING) + server.export_stop() + self.assertEquals(tst_dbg._state, ndb.STATE_TERMINATED) + +if __name__ == '__main__': + unittest.main() diff --git a/debugger_plugin/core/test/test_serialize.py b/debugger_plugin/core/test/test_serialize.py new file mode 100644 index 0000000..76c5320 --- /dev/null +++ b/debugger_plugin/core/test/test_serialize.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# -*- coding: utf-8 *-* +import unittest + +import serialize + + +class TestSerialization(unittest.TestCase): + + def test_serialize_basic(self): + res = serialize.serialize('i', 'i', 1) + self.assertEquals(res['name'], 'i') + self.assertFalse(res['has_childs']) + + def test_serialize_flat_dict(self): + f_dict = {'a': 'First','b': 'Second','c': 'Third'} + res = serialize.serialize('f', 'f', f_dict) + + self.assertEquals(res['name'], 'f') + self.assertTrue(res['has_childs']) + self.assertEquals(res['type'], 'dict') + self.assertEquals(len(res['childs']), 3) + + # Check the first child + f_child = res['childs'][0] + self.assertEquals(f_child['type'], 'str') + + def test_serialize_deep_dict(self): + d_dict = { 'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]} + res = serialize.serialize('d', 'd', d_dict, 2) + + self.assertEquals(res['name'], 'd') + self.assertTrue(res['has_childs']) + self.assertEquals(len(res['childs']), 3) + + # Check the first child + f_child = res['childs'][0] + self.assertTrue(f_child['has_childs']) + self.assertEquals(f_child['type'], 'list') + self.assertEquals(len(f_child['childs']), 3) + + def test_serialize_object(self): + class _TestObject(object): + """Class object to test serialization of object types.""" + def __init__(self): + """Testing method.""" + self._attr = "a" + def un_metodo(self, unarg = None): + """Testing method.""" + return "Hola" + self._attr + repr(unarg) + def otro_metodo(self): + """Testing method.""" + return "Chau" + self._attr + + c_obj = _TestObject() + res = serialize.serialize('c', 'c', c_obj) + + self.assertEquals(res['name'], 'c') + self.assertTrue(res['has_childs']) + self.assertEquals(res['type'], '_TestObject') + # Check the first child + f_child = res['childs'][0] + self.assertEquals(f_child['type'], 'instancemethod') + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file