Skip to content

Commit b2a0269

Browse files
authored
Added RabbitMQ integration tests (#257)
1 parent fed63d1 commit b2a0269

File tree

4 files changed

+142
-31
lines changed

4 files changed

+142
-31
lines changed

.travis.yml

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
language: python
2-
sudo: false
2+
sudo: required
3+
dist: xenial
34
cache: pip
5+
python:
6+
- '2.7'
7+
- '3.4'
8+
- '3.5'
9+
- '3.6'
10+
- '3.7'
11+
- 'pypy2.7-6.0'
12+
- 'pypy3.5-6.0'
13+
14+
services:
15+
- rabbitmq
416

517
env:
618
global:
719
PYTHONUNBUFFERED=yes
20+
matrix:
21+
- MATRIX_TOXENV=unit
22+
- MATRIX_TOXENV=integration-rabbitmq
823

924
stages:
1025
- lint
@@ -13,30 +28,9 @@ stages:
1328
matrix:
1429
fast_finish: true
1530
include:
16-
- python: 2.7
17-
env: TOXENV=2.7
18-
- python: pypy2.7-6.0
19-
env: TOXENV=pypy
20-
dist: xenial
21-
- python: pypy3.5-6.0
22-
env: TOXENV=pypy3
23-
dist: xenial
24-
- python: 3.4
25-
env: TOXENV=3.4
26-
- python: 3.5
27-
env: TOXENV=3.5
28-
- python: 3.6
29-
env: TOXENV=3.6
30-
- python: 3.7
31-
env: TOXENV=3.7
32-
sudo: true
33-
dist: xenial
3431
- python: 2.7
3532
env: TOXENV=flake8
3633
stage: lint
37-
- python: 3.6
38-
env: TOXENV=flake8
39-
stage: lint
4034
- python: 2.7
4135
env: TOXENV=flakeplus
4236
stage: lint
@@ -47,6 +41,20 @@ matrix:
4741
env: TOXENV=apicheck
4842
stage: lint
4943

44+
before_install:
45+
# - sudo apt install libcurl4-openssl-dev libssl-dev gnutls-dev
46+
- if [[ -v MATRIX_TOXENV ]]; then export TOXENV=${TRAVIS_PYTHON_VERSION}-${MATRIX_TOXENV}; fi; env
47+
- |
48+
if [[ "$TOXENV" == *integration* ]]; then
49+
sudo echo 'deb https://dl.bintray.com/rabbitmq-erlang/debian xenial main' > /etc/apt/sources.list.d/rabbitmq-bintray.list
50+
sudo apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys "0x6B73A36E6026DFCA"
51+
wget -O - "https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc" | sudo apt-key add -
52+
sudo apt update
53+
sudo apt install rabbitmq-server -y
54+
sudo systemctl enable rabbitmq-server
55+
sudo systemctl start rabbitmq-server
56+
fi
57+
5058
install:
5159
- pip install -U pip setuptools wheel | cat
5260
- pip install -U tox | cat

conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
def pytest_addoption(parser):
3+
parser.addoption("-E", action="store", metavar="NAME",
4+
help="only run tests matching the environment NAME.")
5+
6+
def pytest_configure(config):
7+
# register an additional marker
8+
config.addinivalue_line("markers",
9+
"env(name): mark test to run only on named environment")
10+
11+
def pytest_runtest_setup(item):
12+
envnames = [mark.args[0] for mark in item.iter_markers(name='env')]
13+
if envnames:
14+
if item.config.getoption("-E") not in envnames:
15+
pytest.skip("test requires env in %r" % envnames)

t/integration/test_rmq.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
from case import Mock, ANY
4+
import pytest
5+
import amqp
6+
7+
8+
@pytest.mark.env('rabbitmq')
9+
def test_connect():
10+
connection = amqp.Connection()
11+
connection.connect()
12+
connection.close()
13+
14+
15+
@pytest.mark.env('rabbitmq')
16+
class test_rabbitmq_operations():
17+
18+
@pytest.fixture(autouse=True)
19+
def setup_conn(self):
20+
self.connection = amqp.Connection()
21+
self.connection.connect()
22+
self.channel = self.connection.channel()
23+
yield
24+
self.connection.close()
25+
26+
@pytest.mark.parametrize(
27+
"publish_method", ('basic_publish', 'basic_publish_confirm')
28+
)
29+
def test_publish_consume(self, publish_method):
30+
callback = Mock()
31+
self.channel.queue_declare(
32+
queue='py-amqp-unittest', durable=False, exclusive=True
33+
)
34+
getattr(self.channel, publish_method)(
35+
amqp.Message('Unittest'), routing_key='py-amqp-unittest'
36+
)
37+
self.channel.basic_consume(
38+
queue='py-amqp-unittest',
39+
callback=callback,
40+
consumer_tag='amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg'
41+
)
42+
self.connection.drain_events()
43+
callback.assert_called_once_with(ANY)
44+
msg = callback.call_args[0][0]
45+
assert isinstance(msg, amqp.Message)
46+
assert msg.body_size == len('Unittest')
47+
assert msg.body == 'Unittest'
48+
assert msg.frame_method == amqp.spec.Basic.Deliver
49+
assert msg.delivery_tag == 1
50+
assert msg.ready is True
51+
assert msg.delivery_info == {
52+
'consumer_tag': 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg',
53+
'delivery_tag': 1,
54+
'redelivered': False,
55+
'exchange': '',
56+
'routing_key': 'py-amqp-unittest'
57+
}
58+
assert msg.properties == {'content_encoding': 'utf-8'}
59+
60+
self.channel.basic_ack(msg.delivery_tag)
61+
62+
def test_publish_get(self):
63+
self.channel.queue_declare(
64+
queue='py-amqp-unittest', durable=False, exclusive=True
65+
)
66+
self.channel.basic_publish(
67+
amqp.Message('Unittest'), routing_key='py-amqp-unittest'
68+
)
69+
msg = self.channel.basic_get(
70+
queue='py-amqp-unittest',
71+
)
72+
assert msg.body_size == 8
73+
assert msg.body == 'Unittest'
74+
assert msg.frame_method == amqp.spec.Basic.GetOk
75+
assert msg.delivery_tag == 1
76+
assert msg.ready is True
77+
assert msg.delivery_info == {
78+
'delivery_tag': 1, 'redelivered': False,
79+
'exchange': '',
80+
'routing_key': 'py-amqp-unittest', 'message_count': 0
81+
}
82+
assert msg.properties == {
83+
'content_encoding': 'utf-8'
84+
}
85+
86+
self.channel.basic_ack(msg.delivery_tag)
87+
88+
msg = self.channel.basic_get(
89+
queue='py-amqp-unittest',
90+
)
91+
assert msg is None

tox.ini

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
[tox]
22
envlist =
3-
2.7
4-
pypy
5-
pypy3
6-
3.4
7-
3.5
8-
3.6
9-
3.7
3+
{2.7,pypy2.7-6.0,pypy3.5-6.0,3.4,3.5,3.6,3.7}-unit
4+
{2.7,pypy2.7-6.0,pypy3.5-6.0,3.4,3.5,3.6,3.7}-integration-rabbitmq
105
flake8
116
flakeplus
127
apicheck
@@ -22,11 +17,13 @@ deps=
2217
flake8,flakeplus,pydocstyle: -r{toxinidir}/requirements/pkgutils.txt
2318
sitepackages = False
2419
recreate = False
25-
commands = py.test -xv --cov=amqp --cov-report=xml --no-cov-on-fail
20+
commands =
21+
unit: py.test -xv --cov=amqp --cov-report=xml --no-cov-on-fail t/unit
22+
integration: py.test -xv -E rabbitmq t/integration
2623

2724
basepython =
2825
2.7,flakeplus,flake8,apicheck,linkcheck,pydocstyle: python2.7
29-
pypy,pypy3: pypy
26+
pypy2.7-6.0,pypy3.5-6.0: pypy
3027
3.4: python3.4
3128
3.5: python3.5
3229
3.6: python3.6

0 commit comments

Comments
 (0)