Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Pytest plugin (#29)
Browse files Browse the repository at this point in the history
Pytest plugin
  • Loading branch information
ovv committed Jan 17, 2018
1 parent 00593e1 commit ef43ed8
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 57 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ Changelog
dev
```

0.3.4
`````

* Refactor tests
* Create pytest plugin with ``slack_event``, ``slack_command`` and ``slack_action`` fixtures.
* Create pytest plugin with useful fixtures.


0.3.3
Expand Down
36 changes: 28 additions & 8 deletions docs/testing.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
=========================================
:mod:`slack.tests.plugin` - Pytest plugin
=========================================
===========================================
:mod:`slack.tests.plugin` - Pytest fixtures
===========================================

Slack-sansio provide a pytest plugin with some fixtures to facilitate testing of the slack API.


Installation
============

To load the plugin add the snippet below to your ``conftest.py``.

.. code-block:: python
pytest_plugins = "slack.tests.plugin",
The ``slack_actions``, ``slack_commands`` and ``slack_events`` fixtures return sample of incoming actions, commands or
events coming from slack.
Available fixtures
==================

.. code-block:: python
.. automodule:: slack.tests.plugin
:members:


Available data
==============

.. autoclass:: slack.tests.data.Events
:members:

.. autoclass:: slack.tests.data.Messages
:members:

.. autoclass:: slack.tests.data.Commands
:members:

async def test_event(self, slack_event):
event = slack.events.Event.from_http(slack_event)
.. autoclass:: slack.tests.data.Actions
:members:

.. autoclass:: slack.tests.data.Methods
:members:
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}

requires['dev'].update(*requires.values())
requires['doc'].update(requires['tests'])
requires['full'].update(requires['requests'], requires['aiohttp'], requires['curio'], requires['trio'],
requires['treq'])

Expand Down
2 changes: 1 addition & 1 deletion slack/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Version information

__version__ = '0.3.3'
__version__ = '0.3.4'
43 changes: 21 additions & 22 deletions slack/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def client(request, io_client):
elif isinstance(request.param['_request'], list):
for index, item in enumerate(request.param['_request']):
request.param['_request'][index] = _default_response(item)
else:
raise ValueError('Invalid `_request` parameters: %s', request.param['_request'])

if 'token' not in request.param:
request.param['token'] = TOKEN
Expand Down Expand Up @@ -103,29 +105,32 @@ def _default_response(response):
if 'content-type' not in response['headers']:
response['headers']['content-type'] = default_response['headers']['content-type']
if isinstance(response['body'], str):
response['body'] = copy.deepcopy(data.methods.payloads[response['body']])
response['body'] = copy.deepcopy(data.Methods[response['body']].value)
return response


@pytest.fixture(params={**data.events.events, **data.events.message})
@pytest.fixture(params={**data.Events.__members__, **data.Messages.__members__})
def raw_event(request):
if isinstance(request.param, str):
if request.param in data.events.events:
return copy.deepcopy(data.events.events[request.param])
elif request.param in data.events.message:
return copy.deepcopy(data.events.message[request.param])
else:
raise ValueError(f'Event "{request.param}" not found')
try:
return copy.deepcopy(data.Events[request.param].value)
except KeyError:
pass
try:
return copy.deepcopy(data.Messages[request.param].value)
except KeyError:
pass
raise KeyError(f'Event "{request.param}" not found')
else:
return copy.deepcopy(request.param)


@pytest.fixture(params={**data.events.events, **data.events.message})
@pytest.fixture(params={**data.Events.__members__, **data.Messages.__members__})
def event(request):
return Event.from_http(raw_event(request))


@pytest.fixture(params={**data.events.message})
@pytest.fixture(params={**data.Messages.__members__})
def message(request):
return Event.from_http(raw_event(request))

Expand All @@ -150,18 +155,15 @@ def message_router():
return MessageRouter()


@pytest.fixture(params={**data.actions.actions})
@pytest.fixture(params={**data.Actions.__members__})
def action(request):
return Action.from_http(raw_action(request))


@pytest.fixture(params={**data.actions.actions})
@pytest.fixture(params={**data.Actions.__members__})
def raw_action(request):
if isinstance(request.param, str):
if request.param in data.actions.actions:
return copy.deepcopy(data.actions.actions[request.param])
else:
raise ValueError(f'Raw action "{request.param}" not found')
return copy.deepcopy(data.Actions[request.param].value)
else:
return copy.deepcopy(request.param)

Expand All @@ -171,18 +173,15 @@ def action_router():
return ActionRouter()


@pytest.fixture(params={**data.commands.commands})
@pytest.fixture(params={**data.Commands.__members__})
def raw_command(request):
if isinstance(request.param, str):
if request.param in data.commands.commands:
return copy.deepcopy(data.commands.commands[request.param])
else:
raise ValueError(f'Command "{request.param}" not found')
return copy.deepcopy(data.Commands[request.param].value)
else:
return copy.deepcopy(request.param)


@pytest.fixture(params={**data.commands.commands})
@pytest.fixture(params={**data.Commands.__members__})
def command(request):
return Command(raw_command(request))

Expand Down
5 changes: 4 additions & 1 deletion slack/tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from . import events, methods, actions, commands # noQa
from .actions import Actions # noQa F401
from .commands import Commands # noQa F401
from .events import Events, Messages # noQa F401
from .methods import Methods # noQa F401
17 changes: 13 additions & 4 deletions slack/tests/data/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

from enum import Enum

button_ok = {
'type': 'interactive_message',
'actions': [
Expand Down Expand Up @@ -65,7 +67,14 @@
raw_button_ok = {'payload': json.dumps(button_ok)}
raw_button_cancel = {'payload': json.dumps(button_cancel)}

actions = {
'button_ok': raw_button_ok,
'button_cancel': raw_button_cancel
}

class Actions(Enum):
"""
List of available action for testing
- button_ok
- button_cancel
"""
button_ok = raw_button_ok
button_cancel = raw_button_cancel
16 changes: 12 additions & 4 deletions slack/tests/data/commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum

no_text = {
'token': 'supersecuretoken',
Expand Down Expand Up @@ -27,7 +28,14 @@
'trigger_id': '000000000.0000000000.e1bb750705a2f472e4476c4228cf4784'
}

commands = {
'text': text,
'no_text': no_text,
}

class Commands(Enum):
"""
List of available command for testing
- text
- no_text
"""
text = text
no_text = no_text
66 changes: 65 additions & 1 deletion slack/tests/data/events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

from enum import Enum

CHANNEL_DELETED = {
'token': 'supersecuretoken',
'team_id': 'T000AAA0A',
Expand Down Expand Up @@ -87,6 +89,25 @@
'event_time': 123456789
}


MESSAGE_MENTION = {
'token': 'supersecuretoken',
'team_id': 'T000AAA0A',
'api_app_id': 'A0AAAAAAA',
'event': {
'type': 'message',
'user': 'U000AA000',
'text': '<@U0AAA0A00> hello world',
'ts': '123456789.000001',
'channel': 'C00000A00',
'event_ts': '123456789.000001'
},
'type': 'event_callback',
'authed_teams': ['T000AAA0A'],
'event_id': 'AAAAAAA',
'event_time': 123456789
}

MESSAGE_SNIPPET = {
'token': 'supersecuretoken',
'team_id': 'T000AAA0A',
Expand Down Expand Up @@ -340,7 +361,8 @@
'attachment': MESSAGE_ATTACHMENTS,
'edit': MESSAGE_EDIT,
'edit_threaded': MESSAGE_EDIT_THREADED,
'bot_edit': MESSAGE_BOT_EDIT
'bot_edit': MESSAGE_BOT_EDIT,
'mention': MESSAGE_MENTION
}

rtm_events = {
Expand All @@ -350,3 +372,45 @@
'bot': json.dumps(MESSAGE_BOT['event']),
'reconnect_url': json.dumps(RECONNECT_URL)
}


class Events(Enum):
"""
List of available event for testing
- channel_deleted
- pin_added
- reaction_added
"""
channel_deleted = CHANNEL_DELETED
pin_added = PIN_ADDED
reaction_added = REACTION_ADDED


class Messages(Enum):
"""
List of available message for testing
- simple
- snippet
- shared
- threaded
- bot
- bot_edit
- attachment
- edit
- edit_threaded
- mention
"""
simple = MESSAGE_SIMPLE
snippet = MESSAGE_SNIPPET
shared = MESSAGE_SHARED
threaded = MESSAGE_THREADED
bot = MESSAGE_BOT
bot_edit = MESSAGE_BOT_EDIT
attachment = MESSAGE_ATTACHMENTS
edit = MESSAGE_EDIT
edit_threaded = MESSAGE_EDIT_THREADED
mention = MESSAGE_MENTION
26 changes: 19 additions & 7 deletions slack/tests/data/methods.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

CHANNELS = {
"ok": True,
"channels": [
Expand Down Expand Up @@ -116,10 +118,20 @@
"url": "wss:\/\/testteam.slack.com/012345678910"
}

payloads = {
'channels_iter': CHANNELS_ITER,
'channels': CHANNELS,
'users.info': USERS_INFO,
'auth.test': AUTH_TEST,
'rtm.connect': RTM_CONNECT
}

class Methods(Enum):
"""
List of available methods for testing
- channels
- channels_iter (channels with a cursor)
- users_info
- auth_test
- rtm_connect
"""
channels_iter = CHANNELS_ITER
channels = CHANNELS
users_info = USERS_INFO
auth_test = AUTH_TEST
rtm_connect = RTM_CONNECT

0 comments on commit ef43ed8

Please sign in to comment.