conftest.py 7.14 KB
Newer Older
davebshow's avatar
davebshow committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright 2016 David M. Brown
#
# This file is part of Goblin.
#
# Goblin is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Goblin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Goblin.  If not, see <http://www.gnu.org/licenses/>.
import asyncio
import pytest
19
20
21

from aiogremlin.structure.graph import Graph
from gremlin_python.process.traversal import T
davebshow's avatar
davebshow committed
22
23
from aiogremlin import driver
from aiogremlin.driver.provider import TinkerGraph
24
from gremlin_python.driver import serializer
25
from aiogremlin.remote.driver_remote_connection import DriverRemoteConnection
26
27
import threading
from time import sleep
davebshow's avatar
davebshow committed
28
29
30
31
32
33


# def pytest_generate_tests(metafunc):
#     if 'cluster' in metafunc.fixturenames:
#         metafunc.parametrize("cluster", ['c1', 'c2'], indirect=True)

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# @pytest.fixture
# def event_loop():
#     try:
#         loop = asyncio.get_event_loop()
#     except RuntimeError:
#         loop = asyncio.new_event_loop()
#         asyncio.set_event_loop(loop)
#
#     # if not loop.is_running():
#     #     def run():
#     #         loop.run_forever()
#     #
#     #     thread = threading.Thread(target=run)
#     #     thread.daemon = True
#     #     thread.start()
#     #
#     # assert loop.is_running()
#     yield loop
#     loop.stop()
#     loop.close()


# def pytest_sessionfinish(session, exitstatus):
#     loop = asyncio.get_event_loop()
#     loop.stop()
#     while loop.is_running():
#         sleep(0.01)
#     asyncio.get_event_loop().close()

davebshow's avatar
davebshow committed
63
64
65
66

def pytest_addoption(parser):
    parser.addoption('--provider', default='tinkergraph',
                     choices=('tinkergraph', 'dse',))
67
    parser.addoption('--gremlin-host', default='gremlin-server')
davebshow's avatar
davebshow committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    parser.addoption('--gremlin-port', default='8182')


@pytest.fixture
def provider(request):
    provider = request.config.getoption('provider')
    if provider == 'tinkergraph':
        return TinkerGraph
    elif provider == 'dse':
        try:
            import goblin_dse
        except ImportError:
            raise RuntimeError("Couldn't run tests with DSEGraph provider: the goblin_dse package "
                               "must be installed")
        else:
            return goblin_dse.DSEGraph


@pytest.fixture
def aliases(request):
    if request.config.getoption('provider') == 'tinkergraph':
        return {'g': 'g'}
    elif request.config.getoption('provider') == 'dse':
        return {'g': 'testgraph.g'}


@pytest.fixture
def gremlin_server():
    return driver.GremlinServer


@pytest.fixture
def unused_server_url(unused_tcp_port):
101
    return 'https://localhost:{}/gremlin'.format(unused_tcp_port)
davebshow's avatar
davebshow committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115


@pytest.fixture
def gremlin_host(request):
    return request.config.getoption('gremlin_host')


@pytest.fixture
def gremlin_port(request):
    return request.config.getoption('gremlin_port')


@pytest.fixture
def gremlin_url(gremlin_host, gremlin_port):
116
    return "ws://{}:{}/gremlin".format(gremlin_host, gremlin_port)
davebshow's avatar
davebshow committed
117
118
119
120


@pytest.fixture
def connection(gremlin_url, event_loop, provider):
121
    #assert event_loop.is_running()
davebshow's avatar
davebshow committed
122
    try:
123
124
125
126
127
128
129
        # conn = event_loop.run_until_complete(
        #     driver.Connection.open(
        #         gremlin_url, event_loop,
        #         message_serializer=serializer.GraphSONMessageSerializer,
        #         provider=provider
        #     ))
        conn = event_loop.run(
davebshow's avatar
davebshow committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
            driver.Connection.open(
                gremlin_url, event_loop,
                message_serializer=serializer.GraphSONMessageSerializer,
                provider=provider
            ))
    except OSError:
        pytest.skip('Gremlin Server is not running')
    return conn


@pytest.fixture
def connection_pool(gremlin_url, event_loop, provider):
    return driver.ConnectionPool(
        gremlin_url, event_loop, None, '', '', 4, 1, 16,
        64, None, serializer.GraphSONMessageSerializer, provider=provider)


@pytest.fixture
def cluster(request, gremlin_host, gremlin_port, event_loop, provider, aliases):
    # if request.param == 'c1':
    cluster = driver.Cluster(
        event_loop,
        hosts=[gremlin_host],
        port=gremlin_port,
        aliases=aliases,
        message_serializer=serializer.GraphSONMessageSerializer,
        provider=provider
    )
    # elif request.param == 'c2':
    #     cluster = driver.Cluster(
    #         event_loop,
    #         hosts=[gremlin_host],
    #         port=gremlin_port,
    #         aliases=aliases,
    #         message_serializer=serializer.GraphSONMessageSerializer,
    #         provider=provider
    #     )
    return cluster

# TOOO FIX
# @pytest.fixture
# def remote_graph():
#      return driver.AsyncGraph()

# Class fixtures
@pytest.fixture
def cluster_class(event_loop):
    return driver.Cluster
178
179
180
181

@pytest.fixture
def remote_connection(event_loop, gremlin_url):
    try:
182
183
184
        # remote_conn = event_loop.run_until_complete(
        #     DriverRemoteConnection.open(gremlin_url, 'g'))
        remote_conn = event_loop.run(
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
            DriverRemoteConnection.open(gremlin_url, 'g'))
    except OSError:
        pytest.skip('Gremlin Server is not running')
    else:
        return remote_conn

@pytest.fixture(autouse=True)
def run_around_tests(remote_connection, event_loop):
    g = Graph().traversal().withRemote(remote_connection)

    async def create_graph():
        await g.V().drop().iterate()
        software1 = await g.addV("software").property("name", "lop").property("lang", "java").property(T.id, 3).next()
        software2 = await g.addV("software").property("name", "ripple").property("lang", "java").property(T.id, 5).next()
        person1 = await g.addV("person").property("name", "marko").property("age", "29").property(T.id, 1).next()
        person2 = await g.addV("person").property("name", "vadas").property("age", "27").property(T.id, 2).next()
        person3 = await g.addV("person").property("name", "josh").property("age", "32").property(T.id, 4).next()
        person4 = await g.addV("person").property("name", "peter").property("age", "35").property(T.id, 6).next()

        knows1 = await g.addE("knows").from_(person1).to(person2).property("weight", 0.5).property(T.id, 7).next()
        knows2 = await g.addE("knows").from_(person1).to(person3).property("weight", 1,0).property(T.id, 8).next()
        created1 = await g.addE("created").from_(person1).to(software1).property("weight", 0.4).property(T.id, 9).next()
        created2 = await g.addE("created").from_(person3).to(software2).property("weight", 1.0).property(T.id, 10).next()
        created3 = await g.addE("created").from_(person3).to(software1).property("weight", 1.0).property(T.id, 11).next()
        created4 = await g.addE("created").from_(person4).to(software1).property("weight", 0.2).property(T.id, 12).next()

211
212
    #event_loop.run_until_complete(create_graph())
    event_loop.run(create_graph())
213
214

    yield