Skip to content
Permalink
Browse files

Fixed dependencies.

Use factories for tests.
  • Loading branch information...
javipalanca committed Nov 4, 2019
1 parent 71987d4 commit 9a1bb6c9e918a035a144bed888efb988974bf624
Showing with 235 additions and 248 deletions.
  1. +2 −0 pytest.ini
  2. +3 −1 requirements.txt
  3. +2 −1 requirements_dev.txt
  4. +58 −0 tests/conftest.py
  5. +60 −0 tests/factories.py
  6. +15 −15 tests/test_agent.py
  7. +41 −55 tests/test_behaviour.py
  8. +7 −11 tests/test_container.py
  9. +0 −26 tests/test_message.py
  10. +39 −47 tests/test_presence.py
  11. +8 −10 tests/test_web.py
  12. +0 −82 tests/utils.py
@@ -0,0 +1,2 @@
[pytest]
testpaths = tests/
@@ -1,4 +1,6 @@
multidict==4.4.0
multidict==4.5.2
pyasn1-modules==0.2.7
pyasn1==0.4.7
aioxmpp>=0.10
aiohttp==3.5.4
aiohttp_jinja2>=0.14.0
@@ -7,7 +7,7 @@ pyflakes==1.6.0
tox>2.3.1
coverage==4.0.3
Sphinx==1.4.8
cryptography==2.2.2
#cryptography==2.2.2
pyyaml>=4.2b1
pytest==3.6.1
pytest-runner==2.11.1
@@ -23,3 +23,4 @@ sphinx_rtd_theme==0.4.0
testfixtures==6.3.0
pytest-aiohttp==0.3.0
docutils==0.12
factory-boy==2.12.0
@@ -0,0 +1,58 @@
import time

import pytest
from aioxmpp import JID

from spade import quit_spade
from spade.container import Container
from spade.message import Message


@pytest.fixture
def jid():
return JID.fromstr("friend@localhost/home")


@pytest.fixture
def message():
return Message(
to="to@localhost",
sender="sender@localhost",
body="message body",
thread="thread-id",
metadata={
"metadata1": "value1",
"metadata2": "value2"
}
)


@pytest.fixture
def message2():
return Message(
to="to2@localhost",
sender="sender2@localhost",
body="message body",
thread="thread-id",
)


@pytest.fixture(autouse=True)
def run_around_tests():
# Code that will run before your test, for example:
# A test function will be run at this point
container = Container()
if not container.is_running:
container.__init__()
yield
# Code that will run after your test, for example:
quit_spade()


def wait_for_behaviour_is_killed(behaviour, tries=500, sleep=0.01):
counter = 0
while not behaviour.is_killed() and counter < tries:
time.sleep(sleep)
counter += 1
if not behaviour.is_killed():
raise Exception("Behaviour not finished")
@@ -0,0 +1,60 @@
import factory
from aioxmpp import PresenceShow, PresenceState
from asynctest import CoroutineMock, Mock

from spade.agent import Agent


class MockedConnectedAgent(Agent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._async_connect = CoroutineMock()
self._async_register = CoroutineMock()
self.conn_coro = Mock()
self.conn_coro.__aexit__ = CoroutineMock()
self.stream = Mock()


class MockedAgentFactory(factory.Factory):
class Meta:
model = MockedConnectedAgent

jid = "fake@jid"
password = "fake_password"


class MockedPresenceConnectedAgent(Agent):
def __init__(self,
available=None,
show=None,
status=None,
priority=0,
*args, **kwargs):
super().__init__(*args, **kwargs)
if status is None:
status = {}
self._async_connect = CoroutineMock()
self.conn_coro = Mock()
self.conn_coro.__aexit__ = CoroutineMock()

self.available = available
self.show = show
self.status = status
self.priority = priority

def mock_presence(self):
show = self.show if self.show is not None else PresenceShow.NONE
available = self.available if self.available is not None else False
state = PresenceState(available, show)
self.presence.presenceserver.set_presence(state, self.status, self.priority)


class MockedPresenceAgentFactory(factory.Factory):
class Meta:
model = MockedPresenceConnectedAgent
jid = "fake@jid"
password = "fake_password"
available = None
show = None
status = {}
priority = 0
@@ -9,7 +9,7 @@
from spade.behaviour import OneShotBehaviour
from spade.message import Message
from spade.template import Template
from tests.utils import make_connected_agent, run_around_tests
from .factories import MockedAgentFactory


def test_create_agent(mocker):
@@ -37,7 +37,7 @@ def test_create_agent(mocker):


def test_connected_agent():
agent = make_connected_agent()
agent = MockedAgentFactory()
assert agent.is_alive() is False

future = agent.start(auto_register=False)
@@ -50,17 +50,17 @@ def test_connected_agent():


def test_name():
agent = make_connected_agent(jid="john@fake_server")
agent = MockedAgentFactory(jid="john@fake_server")
assert agent.name == "john"


def test_avatar():
agent = make_connected_agent(jid="test_avatar@fake_server")
agent = MockedAgentFactory(jid="test_avatar@fake_server")
assert agent.avatar == "http://www.gravatar.com/avatar/44bdc5585ef57844edb11c5b9711d2e6?d=monsterid"


def test_setup():
agent = make_connected_agent()
agent = MockedAgentFactory()
agent.setup = CoroutineMock()
future = agent.start(auto_register=False)
assert future.result() is None
@@ -70,18 +70,18 @@ def test_setup():


def test_set_get():
agent = make_connected_agent()
agent = MockedAgentFactory()
agent.set("KB_name", "KB_value")
assert agent.get("KB_name") == "KB_value"


def test_get_none():
agent = make_connected_agent()
agent = MockedAgentFactory()
assert agent.get("KB_name_unknown") is None


def test_client():
agent = make_connected_agent()
agent = MockedAgentFactory()
assert agent.client is None

future = agent.start()
@@ -90,7 +90,7 @@ def test_client():


def test_register():
agent = make_connected_agent()
agent = MockedAgentFactory()
agent.register = Mock()

future = agent.start(auto_register=True)
@@ -102,7 +102,7 @@ def test_register():


def test_receive_without_behaviours():
agent = make_connected_agent()
agent = MockedAgentFactory()
aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT)
msg = Message.from_node(aiomsg)

@@ -128,13 +128,13 @@ class DummyBehav(OneShotBehaviour):

class CreateBehav(OneShotBehaviour):
async def run(self):
self.agent.agent2 = make_connected_agent()
self.agent.agent2 = MockedAgentFactory()
self.agent.agent2._done = False
self.agent.agent2.add_behaviour(DummyBehav())
await self.agent.agent2.start(auto_register=False)
self.kill()

agent1 = make_connected_agent()
agent1 = MockedAgentFactory()
agent1.agent2 = None
agent1.add_behaviour(CreateBehav())
future = agent1.start(auto_register=False)
@@ -159,7 +159,7 @@ class DummyBehav(OneShotBehaviour):

class SetupAgent(Agent):
async def setup(self):
self.agent2 = make_connected_agent()
self.agent2 = MockedAgentFactory()
self.agent2._done = False
self.agent2.add_behaviour(DummyBehav())
await self.agent2.start(auto_register=False)
@@ -187,7 +187,7 @@ class SetupAgent(Agent):


def test_submit_send():
agent = make_connected_agent()
agent = MockedAgentFactory()

class DummyBehav(OneShotBehaviour):
async def run(self):
@@ -213,7 +213,7 @@ class DummyBehav(OneShotBehaviour):


def test_stop_agent_with_blocking_await():
agent1 = make_connected_agent()
agent1 = MockedAgentFactory()
agent1.value = 1000

class StopBehav(OneShotBehaviour):

0 comments on commit 9a1bb6c

Please sign in to comment.
You can’t perform that action at this time.