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