|
1 | | -from graphql_subscriptions import subscription_manager |
| 1 | +import pytest |
| 2 | +import redis |
| 3 | +from graphql_subscriptions import RedisPubsub |
2 | 4 |
|
3 | 5 |
|
4 | | -def test_some_test(): |
5 | | - assert True |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def mock_redis(monkeypatch): |
| 8 | + import fakeredis |
| 9 | + monkeypatch.setattr(redis, 'StrictRedis', fakeredis.FakeStrictRedis) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize('test_input, expected', [ |
| 13 | + ('test', 'test'), |
| 14 | + ({1: 'test'}, {1: 'test'}), |
| 15 | + (None, None) |
| 16 | +]) |
| 17 | +def test_pubsub_subscribe_and_publish(test_input, expected): |
| 18 | + ps = RedisPubsub() |
| 19 | + |
| 20 | + def message_handler(message): |
| 21 | + assert message == expected |
| 22 | + |
| 23 | + def publish_callback(sub_id): |
| 24 | + assert ps.publish('a', test_input) |
| 25 | + |
| 26 | + ps.subscribe('a', message_handler, {}).then(publish_callback) |
| 27 | + |
| 28 | + while True: |
| 29 | + message = ps.pubsub.get_message(ignore_subscribe_messages=True) |
| 30 | + if message: |
| 31 | + ps.handle_message(message) |
| 32 | + break |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize('test_input, expected', [ |
| 36 | + ('test', 'test'), |
| 37 | + ({1: 'test'}, {1: 'test'}), |
| 38 | + (None, None) |
| 39 | +]) |
| 40 | +def test_pubsub_subscribe_and_unsubscribe(test_input, expected): |
| 41 | + ps = RedisPubsub() |
| 42 | + |
| 43 | + def message_handler(message): |
| 44 | + assert 0 |
| 45 | + |
| 46 | + def unsubscribe_publish_callback(sub_id): |
| 47 | + ps.unsubscribe(sub_id) |
| 48 | + assert ps.publish('a', test_input) |
| 49 | + |
| 50 | + ps.subscribe('a', message_handler, {}).then(unsubscribe_publish_callback) |
0 commit comments