1
|
|
|
# import asyncio |
2
|
|
|
# import uuid |
3
|
|
|
# from unittest import mock |
4
|
|
|
# |
5
|
|
|
# import json |
6
|
|
|
# import pytest |
7
|
|
|
# |
8
|
|
|
# import aiohttp |
9
|
|
|
# from aiohttp import client_ws |
10
|
|
|
# |
11
|
|
|
# from aiogremlin.gremlin_python.driver import request |
12
|
|
|
# |
13
|
|
|
# import goblin |
14
|
|
|
# from goblin import driver |
15
|
|
|
# from goblin import provider |
16
|
|
|
# |
17
|
|
|
# request_id = uuid.UUID(int=215449331521667564889692237976543325869, |
18
|
|
|
# version=4) |
19
|
|
|
# |
20
|
|
|
# |
21
|
|
|
# # based on this handy tip on SO: |
22
|
|
|
# # http://stackoverflow.com/a/29905620/6691423 |
23
|
|
|
# def get_mock_coro(return_value): |
24
|
|
|
# async def mock_coro(*args, **kwargs): |
25
|
|
|
# return return_value |
26
|
|
|
# |
27
|
|
|
# return mock.Mock(wraps=mock_coro) |
28
|
|
|
# |
29
|
|
|
# |
30
|
|
|
# async def mock_receive(): |
31
|
|
|
# message = mock.Mock() |
32
|
|
|
# message.tp = aiohttp.MsgType.close |
33
|
|
|
# return message |
34
|
|
|
# |
35
|
|
|
# |
36
|
|
|
# async def mock_ws_connect(*args, **kwargs): |
37
|
|
|
# mock_client = mock.Mock(spec=client_ws.ClientWebSocketResponse) |
38
|
|
|
# mock_client.closed = False |
39
|
|
|
# mock_client.receive = mock.Mock(wraps=mock_receive) |
40
|
|
|
# mock_client.close = get_mock_coro(None) |
41
|
|
|
# return mock_client |
42
|
|
|
# |
43
|
|
|
# |
44
|
|
|
# class TestProvider(provider.Provider): |
45
|
|
|
# DEFAULT_OP_ARGS = { |
46
|
|
|
# 'standard': { |
47
|
|
|
# 'eval': { |
48
|
|
|
# 'fictional_argument': 'fictional_value' |
49
|
|
|
# }, |
50
|
|
|
# }, |
51
|
|
|
# 'session': { |
52
|
|
|
# 'eval': { |
53
|
|
|
# 'manageTransaction': True |
54
|
|
|
# }, |
55
|
|
|
# } |
56
|
|
|
# } |
57
|
|
|
# |
58
|
|
|
# @staticmethod |
59
|
|
|
# def get_hashable_id(val): |
60
|
|
|
# return val |
61
|
|
|
# |
62
|
|
|
# |
63
|
|
|
# def deserialize_json_request(request): |
64
|
|
|
# header_len = request[0] + 1 |
65
|
|
|
# payload = request[header_len:] |
66
|
|
|
# return json.loads(payload.decode()) |
67
|
|
|
# |
68
|
|
|
# |
69
|
|
|
# @pytest.fixture(params=(driver.GraphSONMessageSerializer, |
70
|
|
|
# driver.GraphSONMessageSerializer)) |
71
|
|
|
# def message_serializer(request): |
72
|
|
|
# return request.param |
73
|
|
|
# |
74
|
|
|
# |
75
|
|
|
# @pytest.mark.parametrize( |
76
|
|
|
# 'processor_name,key,value', |
77
|
|
|
# (('standard', 'fictional_argument', 'fictional_value'), |
78
|
|
|
# ('session', 'manageTransaction', True))) |
79
|
|
|
# def test_get_processor_provider_default_args(processor_name, key, value): |
80
|
|
|
# processor = driver.GraphSONMessageSerializer.get_processor( |
81
|
|
|
# TestProvider, processor_name) |
82
|
|
|
# assert processor._default_args == TestProvider.DEFAULT_OP_ARGS[ |
83
|
|
|
# processor_name] |
84
|
|
|
# eval_args = processor.get_op_args('eval', {'gremlin': 'g.V()'}) |
85
|
|
|
# assert eval_args['gremlin'] == 'g.V()' |
86
|
|
|
# assert eval_args[key] == value |
87
|
|
|
# |
88
|
|
|
# |
89
|
|
|
# @pytest.mark.parametrize('processor,key,value', |
90
|
|
|
# (('', 'fictional_argument', 'fictional_value'), |
91
|
|
|
# ('session', 'manageTransaction', True))) |
92
|
|
|
# def test_serializer_default_op_args(message_serializer, processor, key, |
93
|
|
|
# value): |
94
|
|
|
# g = driver.AsyncGraph().traversal() |
95
|
|
|
# traversal = g.V().hasLabel('stuff').has('foo', 'bar') |
96
|
|
|
# serialized_message = message_serializer.serialize_message( |
97
|
|
|
# TestProvider, |
98
|
|
|
# str(uuid.uuid4()), |
99
|
|
|
# processor=processor, |
100
|
|
|
# op='eval', |
101
|
|
|
# gremlin=traversal.bytecode) |
102
|
|
|
# message = deserialize_json_request(serialized_message) |
103
|
|
|
# assert message['args'][key] == value |
104
|
|
|
# |
105
|
|
|
# |
106
|
|
|
# @pytest.mark.parametrize('processor,key,value', |
107
|
|
|
# (('', 'fictional_argument', 'fictional_value'), |
108
|
|
|
# ('session', 'manageTransaction', True))) |
109
|
|
|
# @pytest.mark.asyncio |
110
|
|
|
# async def test_conn_default_op_args(event_loop, monkeypatch, processor, |
111
|
|
|
# key, value): |
112
|
|
|
# mock_client_session = mock.Mock(spec=aiohttp.ClientSession) |
113
|
|
|
# mock_client_session_instance = mock.Mock(spec=aiohttp.ClientSession) |
114
|
|
|
# mock_client_session.return_value = mock_client_session_instance |
115
|
|
|
# mock_client_session_instance.ws_connect = mock.Mock( |
116
|
|
|
# wraps=mock_ws_connect) |
117
|
|
|
# mock_client_session_instance.close = get_mock_coro( |
118
|
|
|
# None) # otherwise awaiting ws.close is an error |
119
|
|
|
# |
120
|
|
|
# monkeypatch.setattr(aiohttp, 'ClientSession', mock_client_session) |
121
|
|
|
# monkeypatch.setattr(uuid, 'uuid4', mock.Mock(return_value=request_id)) |
122
|
|
|
# |
123
|
|
|
# conn = await driver.Connection.open( |
124
|
|
|
# 'some_url', |
125
|
|
|
# event_loop, |
126
|
|
|
# message_serializer=driver.GraphSONMessageSerializer, |
127
|
|
|
# provider=TestProvider) |
128
|
|
|
# |
129
|
|
|
# resp = await conn.submit( |
130
|
|
|
# gremlin='g.V().hasLabel("foo").count()', |
131
|
|
|
# processor=processor, |
132
|
|
|
# op='eval') |
133
|
|
|
# |
134
|
|
|
# submitted_bytes = conn._ws.send_bytes.call_args[0][0] |
135
|
|
|
# submitted_json = submitted_bytes[17:].decode() |
136
|
|
|
# submitted_dict = json.loads(submitted_json) |
137
|
|
|
# |
138
|
|
|
# assert submitted_dict['args'][key] == value |
139
|
|
|
# |
140
|
|
|
# await conn.close() |
141
|
|
|
# resp.close() |
142
|
|
|
# |
143
|
|
|
# |
144
|
|
|
# @pytest.mark.asyncio |
145
|
|
|
# async def test_cluster_conn_provider(event_loop, gremlin_host, |
146
|
|
|
# gremlin_port): |
147
|
|
|
# cluster = await driver.Cluster.open( |
148
|
|
|
# event_loop, |
149
|
|
|
# provider=TestProvider, |
150
|
|
|
# hosts=[gremlin_host], |
151
|
|
|
# port=gremlin_port) |
152
|
|
|
# assert cluster.config['provider'] == TestProvider |
153
|
|
|
# |
154
|
|
|
# pooled_conn = await cluster.get_connection() |
155
|
|
|
# assert pooled_conn._conn._provider == TestProvider |
156
|
|
|
# |
157
|
|
|
# await cluster.close() |
158
|
|
|
# |
159
|
|
|
# |
160
|
|
|
# @pytest.mark.asyncio |
161
|
|
|
# async def test_app_cluster_provider(event_loop): |
162
|
|
|
# app = await goblin.Goblin.open(event_loop, provider=TestProvider) |
163
|
|
|
# assert app._provider is TestProvider |
164
|
|
|
# assert app._cluster.config['provider'] is TestProvider |
165
|
|
|
# |
166
|
|
|
# await app.close() |
167
|
|
|
# |
168
|
|
|
# |
169
|
|
|
# @pytest.mark.asyncio |
170
|
|
|
# async def test_app_provider_hashable_id(event_loop): |
171
|
|
|
# app = await goblin.Goblin.open(event_loop, provider=TestProvider) |
172
|
|
|
# assert app._get_hashable_id is TestProvider.get_hashable_id |
173
|
|
|
# |
174
|
|
|
# await app.close() |
175
|
|
|
|