|
1 | | -#!/usr/bin/env python |
2 | | -# -*- coding: utf-8 -*- |
3 | | - |
4 | | -"""Tests for `graphql_ws` package.""" |
| 1 | +from collections import OrderedDict |
| 2 | +try: |
| 3 | + from unittest import mock |
| 4 | +except ImportError: |
| 5 | + import mock |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
| 9 | +from graphql_ws import base, constants |
| 10 | + |
8 | 11 |
|
9 | | -from graphql_ws import base |
| 12 | +@pytest.fixture |
| 13 | +def cc(): |
| 14 | + cc = base.BaseConnectionContext(ws=None) |
| 15 | + cc.operations = {"yes": "1"} |
| 16 | + return cc |
10 | 17 |
|
11 | 18 |
|
12 | 19 | @pytest.fixture |
13 | | -def response(): |
14 | | - """Sample pytest fixture. |
| 20 | +def ss(): |
| 21 | + return base.BaseSubscriptionServer(schema=None) |
| 22 | + |
| 23 | + |
| 24 | +class TestConnectionContextOperation: |
| 25 | + def test_no_operations_initially(self): |
| 26 | + cc = base.BaseConnectionContext(ws=None) |
| 27 | + assert not cc.operations |
| 28 | + |
| 29 | + def test_has_operation(self, cc): |
| 30 | + assert cc.has_operation("yes") |
| 31 | + |
| 32 | + def test_has_operation_missing(self, cc): |
| 33 | + assert not cc.has_operation("no") |
| 34 | + |
| 35 | + def test_register_operation(self, cc): |
| 36 | + cc.register_operation("new", "2") |
| 37 | + assert "new" in cc.operations |
| 38 | + |
| 39 | + def test_get_operation(self, cc): |
| 40 | + assert cc.get_operation("yes") == "1" |
| 41 | + |
| 42 | + def test_remove_operation(self, cc): |
| 43 | + cc.remove_operation("yes") |
| 44 | + assert not cc.operations |
| 45 | + |
| 46 | + |
| 47 | +class TestConnectionContextNotImplentedMethods: |
| 48 | + def test_receive(self): |
| 49 | + with pytest.raises(NotImplementedError): |
| 50 | + base.BaseConnectionContext(ws=None).receive() |
| 51 | + |
| 52 | + def test_send(self): |
| 53 | + with pytest.raises(NotImplementedError): |
| 54 | + base.BaseConnectionContext(ws=None).send("TEST") |
| 55 | + |
| 56 | + def test_closed(self): |
| 57 | + with pytest.raises(NotImplementedError): |
| 58 | + base.BaseConnectionContext(ws=None).closed |
| 59 | + |
| 60 | + def test_close(self): |
| 61 | + with pytest.raises(NotImplementedError): |
| 62 | + base.BaseConnectionContext(ws=None).close(123) |
| 63 | + |
| 64 | + |
| 65 | +class TestProcessMessage: |
| 66 | + def test_init(self, ss, cc): |
| 67 | + ss.on_connection_init = mock.Mock() |
| 68 | + ss.process_message( |
| 69 | + cc, {"id": "1", "type": constants.GQL_CONNECTION_INIT, "payload": "payload"} |
| 70 | + ) |
| 71 | + ss.on_connection_init.assert_called_with(cc, "1", "payload") |
| 72 | + |
| 73 | + def test_terminate(self, ss, cc): |
| 74 | + ss.on_connection_terminate = mock.Mock() |
| 75 | + ss.process_message(cc, {"id": "1", "type": constants.GQL_CONNECTION_TERMINATE}) |
| 76 | + ss.on_connection_terminate.assert_called_with(cc, "1") |
| 77 | + |
| 78 | + def test_start(self, ss, cc): |
| 79 | + ss.get_graphql_params = mock.Mock() |
| 80 | + ss.get_graphql_params.return_value = {"params": True} |
| 81 | + cc.has_operation = mock.Mock() |
| 82 | + cc.has_operation.return_value = False |
| 83 | + ss.unsubscribe = mock.Mock() |
| 84 | + ss.on_start = mock.Mock() |
| 85 | + ss.process_message( |
| 86 | + cc, {"id": "1", "type": constants.GQL_START, "payload": {"a": "b"}} |
| 87 | + ) |
| 88 | + assert not ss.unsubscribe.called |
| 89 | + ss.on_start.assert_called_with(cc, "1", {"params": True}) |
| 90 | + |
| 91 | + def test_start_existing_op(self, ss, cc): |
| 92 | + ss.get_graphql_params = mock.Mock() |
| 93 | + ss.get_graphql_params.return_value = {"params": True} |
| 94 | + cc.has_operation = mock.Mock() |
| 95 | + cc.has_operation.return_value = True |
| 96 | + ss.unsubscribe = mock.Mock() |
| 97 | + ss.on_start = mock.Mock() |
| 98 | + ss.process_message( |
| 99 | + cc, {"id": "1", "type": constants.GQL_START, "payload": {"a": "b"}} |
| 100 | + ) |
| 101 | + assert ss.unsubscribe.called |
| 102 | + ss.on_start.assert_called_with(cc, "1", {"params": True}) |
| 103 | + |
| 104 | + def test_start_bad_graphql_params(self, ss, cc): |
| 105 | + ss.get_graphql_params = mock.Mock() |
| 106 | + ss.get_graphql_params.return_value = None |
| 107 | + cc.has_operation = mock.Mock() |
| 108 | + cc.has_operation.return_value = False |
| 109 | + ss.send_error = mock.Mock() |
| 110 | + ss.unsubscribe = mock.Mock() |
| 111 | + ss.on_start = mock.Mock() |
| 112 | + ss.process_message( |
| 113 | + cc, {"id": "1", "type": constants.GQL_START, "payload": {"a": "b"}} |
| 114 | + ) |
| 115 | + assert ss.send_error.called |
| 116 | + assert ss.send_error.call_args[0][:2] == (cc, "1") |
| 117 | + assert isinstance(ss.send_error.call_args[0][2], Exception) |
| 118 | + assert not ss.on_start.called |
| 119 | + |
| 120 | + def test_stop(self, ss, cc): |
| 121 | + ss.on_stop = mock.Mock() |
| 122 | + ss.process_message(cc, {"id": "1", "type": constants.GQL_STOP}) |
| 123 | + ss.on_stop.assert_called_with(cc, "1") |
| 124 | + |
| 125 | + def test_invalid(self, ss, cc): |
| 126 | + ss.send_error = mock.Mock() |
| 127 | + ss.process_message(cc, {"id": "1", "type": "unknown"}) |
| 128 | + assert ss.send_error.called |
| 129 | + assert ss.send_error.call_args[0][:2] == (cc, "1") |
| 130 | + assert isinstance(ss.send_error.call_args[0][2], Exception) |
| 131 | + |
| 132 | + |
| 133 | +def test_get_graphql_params(ss, cc): |
| 134 | + payload = { |
| 135 | + "query": "req", |
| 136 | + "variables": "vars", |
| 137 | + "operationName": "query", |
| 138 | + "context": "ctx", |
| 139 | + } |
| 140 | + assert ss.get_graphql_params(cc, payload) == { |
| 141 | + "request_string": "req", |
| 142 | + "variable_values": "vars", |
| 143 | + "operation_name": "query", |
| 144 | + "context_value": "ctx", |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | +def test_build_message(ss): |
| 149 | + assert ss.build_message("1", "query", "PAYLOAD") == { |
| 150 | + "id": "1", |
| 151 | + "type": "query", |
| 152 | + "payload": "PAYLOAD", |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | +def test_build_message_partial(ss): |
| 157 | + assert ss.build_message(id="1", op_type=None, payload=None) == {"id": "1"} |
| 158 | + assert ss.build_message(id=None, op_type="query", payload=None) == {"type": "query"} |
| 159 | + assert ss.build_message(id=None, op_type=None, payload="PAYLOAD") == { |
| 160 | + "payload": "PAYLOAD" |
| 161 | + } |
| 162 | + assert ss.build_message(id=None, op_type=None, payload=None) == {} |
| 163 | + |
| 164 | + |
| 165 | +def test_send_execution_result(ss): |
| 166 | + ss.execution_result_to_dict = mock.Mock() |
| 167 | + ss.execution_result_to_dict.return_value = {"res": "ult"} |
| 168 | + ss.send_message = mock.Mock() |
| 169 | + ss.send_message.return_value = "returned" |
| 170 | + assert "returned" == ss.send_execution_result(cc, "1", "result") |
| 171 | + ss.send_message.assert_called_with(cc, "1", constants.GQL_DATA, {"res": "ult"}) |
| 172 | + |
| 173 | + |
| 174 | +def test_execution_result_to_dict(ss): |
| 175 | + result = mock.Mock() |
| 176 | + result.data = "DATA" |
| 177 | + result.errors = "ER" |
| 178 | + result_dict = ss.execution_result_to_dict(result) |
| 179 | + assert isinstance(result_dict, OrderedDict) |
| 180 | + assert result_dict == { |
| 181 | + "data": "DATA", |
| 182 | + "errors": [{"message": "E"}, {"message": "R"}], |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | +def test_send_message(ss, cc): |
| 187 | + ss.build_message = mock.Mock() |
| 188 | + ss.build_message.return_value = {"mess": "age"} |
| 189 | + cc.send = mock.Mock() |
| 190 | + cc.send.return_value = "returned" |
| 191 | + assert "returned" == ss.send_message(cc) |
| 192 | + cc.send.assert_called_with('{"mess": "age"}') |
| 193 | + |
| 194 | + |
| 195 | +class TestSSNotImplemented: |
| 196 | + def test_handle(self, ss): |
| 197 | + with pytest.raises(NotImplementedError): |
| 198 | + ss.handle(ws=None, request_context=None) |
| 199 | + |
| 200 | + def test_on_open(self, ss): |
| 201 | + with pytest.raises(NotImplementedError): |
| 202 | + ss.on_open(connection_context=None) |
| 203 | + |
| 204 | + def test_on_connect(self, ss): |
| 205 | + with pytest.raises(NotImplementedError): |
| 206 | + ss.on_connect(connection_context=None, payload=None) |
| 207 | + |
| 208 | + def test_on_close(self, ss): |
| 209 | + with pytest.raises(NotImplementedError): |
| 210 | + ss.on_close(connection_context=None) |
15 | 211 |
|
16 | | - See more at: http://doc.pytest.org/en/latest/fixture.html |
17 | | - """ |
18 | | - # import requests |
19 | | - # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') |
| 212 | + def test_on_connection_init(self, ss): |
| 213 | + with pytest.raises(NotImplementedError): |
| 214 | + ss.on_connection_init(connection_context=None, op_id=None, payload=None) |
20 | 215 |
|
| 216 | + def test_on_stop(self, ss): |
| 217 | + with pytest.raises(NotImplementedError): |
| 218 | + ss.on_stop(connection_context=None, op_id=None) |
21 | 219 |
|
22 | | -def test_content(response): |
23 | | - """Sample pytest test function with the pytest fixture as an argument.""" |
24 | | - # from bs4 import BeautifulSoup |
25 | | - # assert 'GitHub' in BeautifulSoup(response.content).title.string |
| 220 | + def test_on_start(self, ss): |
| 221 | + with pytest.raises(NotImplementedError): |
| 222 | + ss.on_start(connection_context=None, op_id=None, params=None) |
0 commit comments