mirror of
git://git.psyced.org/git/pypsyc
synced 2024-08-15 03:20:04 +00:00
added new pypsyc as 'mjacob2'
This commit is contained in:
parent
0abc7a0888
commit
9f7c4147c7
46 changed files with 7243 additions and 0 deletions
97
mjacob2/tests/test_core/test_mmp.py
Normal file
97
mjacob2/tests/test_core/test_mmp.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
from nose.tools import assert_raises
|
||||
from twisted.protocols.loopback import loopbackAsync as loopback_async
|
||||
|
||||
from pypsyc.core.mmp import Uni, Header, Circuit
|
||||
|
||||
|
||||
UNI = 'psyc://server/foo/bar'
|
||||
|
||||
class TestUni(object):
|
||||
def test_uni(self):
|
||||
uni = Uni(UNI)
|
||||
assert uni == UNI
|
||||
assert uni.prefix == 'psyc'
|
||||
|
||||
def test_parts(self):
|
||||
uni = Uni(UNI)
|
||||
assert uni.into_parts() == ['server', 'foo', 'bar']
|
||||
assert Uni.from_parts(uni.into_parts()) == uni
|
||||
|
||||
def test_descendant_ancestor(self):
|
||||
uni = Uni(UNI)
|
||||
assert uni.is_descendant_of('psyc://server/foo')
|
||||
assert not uni.is_descendant_of('psyc://server/spam')
|
||||
assert uni.is_ancestor_of('psyc://server/foo/bar/child')
|
||||
assert not uni.is_ancestor_of('psyc://server/spam/eggs/child')
|
||||
|
||||
assert uni.is_descendant_of('psyc://server/')
|
||||
assert Uni('psyc://server/').is_ancestor_of('psyc://server/foo/bar')
|
||||
|
||||
def test_chain(self):
|
||||
assert Uni(UNI).chain('foobar') == 'psyc://server/foo/bar/foobar'
|
||||
assert Uni('psyc://server/').chain('foo') == 'psyc://server/foo'
|
||||
|
||||
|
||||
class TestHeader(object):
|
||||
def test_empty(self):
|
||||
header = Header()
|
||||
assert header == {}
|
||||
header._init()
|
||||
assert header.source is None
|
||||
assert header.target is None
|
||||
assert header.context is None
|
||||
|
||||
def test_header(self):
|
||||
header_dict = {
|
||||
'_source': 'psyc://server/uni1',
|
||||
'_target': 'psyc://server/uni2',
|
||||
'_context': 'psyc://server/@place'
|
||||
}
|
||||
header = Header(header_dict)
|
||||
assert header == header_dict
|
||||
header._init()
|
||||
assert header.source == Uni('psyc://server/uni1')
|
||||
assert header.target == Uni('psyc://server/uni2')
|
||||
assert header.context == Uni('psyc://server/@place')
|
||||
|
||||
|
||||
class TestCircuit(object):
|
||||
def setup(self):
|
||||
self.success = False
|
||||
|
||||
def test_render_empty(self):
|
||||
self._test({}, [])
|
||||
|
||||
def test_onlyvars(self):
|
||||
self._test({'_target': 'psyc://example.org/'}, [])
|
||||
|
||||
def test_onlycontent(self):
|
||||
self._test({}, ['content'])
|
||||
|
||||
def test_packet(self):
|
||||
self._test({'_target': 'psyc://example.org/'}, ['content'])
|
||||
|
||||
def _test(self, header, content):
|
||||
def received(header_, content_):
|
||||
assert header_ == header
|
||||
assert content_ == content
|
||||
self.success = True
|
||||
|
||||
circuit1 = Circuit()
|
||||
circuit1.packet_received = received
|
||||
|
||||
circuit2 = Circuit()
|
||||
circuit2.initiator = True
|
||||
circuit2.inited = lambda: circuit2.send(header, iter(content))
|
||||
|
||||
loopback_async(circuit1, circuit2)
|
||||
assert self.success
|
||||
|
||||
def test_subclass(self):
|
||||
circuit = Circuit()
|
||||
circuit.lineReceived('|')
|
||||
assert_raises(NotImplementedError, circuit.lineReceived, '|')
|
151
mjacob2/tests/test_core/test_psyc.py
Normal file
151
mjacob2/tests/test_core/test_psyc.py
Normal file
|
@ -0,0 +1,151 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
from mock import Mock
|
||||
from nose.tools import assert_raises
|
||||
from tests.constants import USER1_UNI, USER2_UNI
|
||||
from tests.helpers import check_success, rendered
|
||||
|
||||
from pypsyc.core.psyc import PSYCPacket, PSYCObject
|
||||
from pypsyc.util import schedule
|
||||
|
||||
|
||||
class TestPSYCPacket(object):
|
||||
def test_vars(self):
|
||||
packet = PSYCPacket(
|
||||
modifiers={
|
||||
':': {'_foo': 'bar'},
|
||||
'=': {'_spam': 'eggs'}
|
||||
}
|
||||
)
|
||||
self._test(packet, [':_foo\tbar', '=_spam\teggs'])
|
||||
|
||||
def test_method(self):
|
||||
packet = PSYCPacket(mc='_notice_foo')
|
||||
self._test(packet, ['_notice_foo'])
|
||||
|
||||
def test_packet(self):
|
||||
packet = PSYCPacket(
|
||||
modifiers={
|
||||
':': {'_foo': 'bar'}
|
||||
},
|
||||
mc='_message_foo',
|
||||
data='this is\ndata'
|
||||
)
|
||||
self._test(packet, [':_foo\tbar', '_message_foo', 'this is\ndata'])
|
||||
|
||||
def test_binary1(self):
|
||||
packet = PSYCPacket({':': {'_foo': 'foo\nbar'}})
|
||||
self._test(packet, [':_foo 7\tfoo\nbar'])
|
||||
assert PSYCPacket.parse([':_foo 7\tfoo', 'bar']) == packet
|
||||
|
||||
def test_binary2(self):
|
||||
packet = PSYCPacket({':': {'_foo': 'foo\n\nbar'}})
|
||||
self._test(packet, [':_foo 8\tfoo\n\nbar'])
|
||||
assert PSYCPacket.parse([':_foo 8\tfoo', '', 'bar']) == packet
|
||||
|
||||
def _test(self, packet, rendered_):
|
||||
assert PSYCPacket.parse(packet.render()) == packet
|
||||
assert rendered(packet) == rendered_
|
||||
|
||||
def test_nomethod(self):
|
||||
packet = PSYCPacket(data='foobar')
|
||||
assert_raises(AssertionError, list, packet.render())
|
||||
|
||||
def test_repr(self):
|
||||
s = '\n'.join((
|
||||
r"PSYCPacket(",
|
||||
r" modifiers={",
|
||||
r" ':': {'_foo': 'bar'},",
|
||||
r" '=': {'_foo': 'bar'}",
|
||||
r" },",
|
||||
r" mc='_message_foo',",
|
||||
r" data='this is\ndata'",
|
||||
r")"
|
||||
))
|
||||
assert repr(eval(s)) == s
|
||||
|
||||
def test_equal(self):
|
||||
assert PSYCPacket() == PSYCPacket()
|
||||
assert not PSYCPacket() != PSYCPacket()
|
||||
|
||||
def test_empty(self):
|
||||
packet = PSYCPacket()
|
||||
self._test(packet, [])
|
||||
assert packet.modifiers == {':': {}}
|
||||
assert packet.mc is None
|
||||
assert packet.data == ''
|
||||
|
||||
def test_from_kwds(self):
|
||||
packet1 = PSYCPacket.from_kwds(mc='_message_public',
|
||||
data="hello", _somevar='foo')
|
||||
packet2 = PSYCPacket(mc='_message_public', data="hello",
|
||||
modifiers={':': {'_somevar': 'foo'}})
|
||||
assert packet1 == packet2
|
||||
|
||||
|
||||
class TestPSYCObject(object):
|
||||
def test_handle_packet(self):
|
||||
psyc = PSYCObject(None)
|
||||
psyc.handlers['_message'] = message = Mock()
|
||||
psyc.handlers['_message_public'] = message_public = Mock()
|
||||
|
||||
p1 = PSYCPacket() # psyc_packet should return
|
||||
p2 = PSYCPacket(mc='_message_public')
|
||||
p3 = PSYCPacket(mc='_message_private')
|
||||
|
||||
assert psyc.handle_packet({}, p1.render()) == p1
|
||||
assert psyc.handle_packet({}, p2.render()) == p2
|
||||
assert psyc.handle_packet({}, p3.render()) == p3
|
||||
assert message_public.call_args_list == [((p2,),)]
|
||||
assert message.call_args_list == [((p3,),)]
|
||||
|
||||
def test_handle_packet_internal_error(self):
|
||||
psyc = PSYCObject(None)
|
||||
psyc.handlers['_message_public'] = Mock(side_effect=Exception)
|
||||
|
||||
packet = PSYCPacket(mc='_message_public')
|
||||
assert psyc.handle_packet({}, packet.render()) == packet
|
||||
|
||||
def test_sendmsg_packet(self):
|
||||
packet = PSYCPacket()
|
||||
sendfunc = Mock()
|
||||
psyc = PSYCObject(sendfunc, USER1_UNI)
|
||||
|
||||
psyc.sendmsg(USER2_UNI, packet)
|
||||
header = {'_source': USER1_UNI, '_target': USER2_UNI}
|
||||
assert sendfunc.call_args_list == [((header, rendered(packet)),)]
|
||||
|
||||
def test_sendmsg_kwds(self):
|
||||
packet = PSYCPacket({':': {'_somevar': 'foo'}}, mc='_message_private')
|
||||
sendfunc = Mock()
|
||||
psyc = PSYCObject(sendfunc, USER1_UNI)
|
||||
|
||||
psyc.sendmsg(USER2_UNI, mc='_message_private', _somevar='foo')
|
||||
header = {'_source': USER1_UNI, '_target': USER2_UNI}
|
||||
assert sendfunc.call_args_list == [((header, rendered(packet)),)]
|
||||
|
||||
@check_success
|
||||
def test_async(self):
|
||||
MC = '_request_foo'
|
||||
ECHO = PSYCPacket(mc='_echo_foo')
|
||||
o1 = PSYCObject(lambda *args: o2.handle_packet(*args), USER1_UNI)
|
||||
o2 = PSYCObject(lambda *args: o1.handle_packet(*args), USER2_UNI)
|
||||
o2.handlers[MC] = lambda _: ECHO
|
||||
|
||||
def _test():
|
||||
ret = o1.sendmsg(USER2_UNI, mc=MC)
|
||||
assert ret == ECHO
|
||||
self.success = True
|
||||
schedule(_test)
|
||||
|
||||
def test_async_internal_error(self):
|
||||
MC = '_request_foo'
|
||||
ERROR = PSYCPacket(mc='_error_internal')
|
||||
o1 = PSYCObject(lambda *args: o2.handle_packet(*args), USER1_UNI)
|
||||
o2 = PSYCObject(lambda *args: o1.handle_packet(*args), USER2_UNI)
|
||||
o2.handlers[MC] = Mock(side_effect=Exception)
|
||||
|
||||
ret = o1.sendmsg(USER2_UNI, mc=MC)
|
||||
assert ret == ERROR
|
Loading…
Add table
Add a link
Reference in a new issue