Skip to content

Commit

Permalink
Adding some unit tests for the core.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsargiot committed Feb 18, 2013
1 parent f0bc8ee commit d5227fe
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
Empty file.
13 changes: 13 additions & 0 deletions debugger_plugin/core/test/helpers.py
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions debugger_plugin/core/test/test_process.py
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 28 additions & 0 deletions debugger_plugin/core/test/test_rpc_adapter.py
Original file line number Diff line number Diff line change
@@ -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()
67 changes: 67 additions & 0 deletions debugger_plugin/core/test/test_serialize.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d5227fe

Please sign in to comment.