Issues (13)

tests/conftest.py (1 issue)

1
# Copyright 2016 David M. Brown
2
#
3
# This file is part of Goblin.
4
#
5
# Goblin is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Affero General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Goblin is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with Goblin.  If not, see <http://www.gnu.org/licenses/>.
17
import asyncio
18
import pytest
19
20
from aiogremlin.structure.graph import Graph
21
from gremlin_python.process.traversal import T
22
from aiogremlin import driver
23
from aiogremlin.driver.provider import TinkerGraph
24
from gremlin_python.driver import serializer
25
from aiogremlin.remote.driver_remote_connection import DriverRemoteConnection
26
27
28
# def pytest_generate_tests(metafunc):
29
#     if 'cluster' in metafunc.fixturenames:
30
#         metafunc.parametrize("cluster", ['c1', 'c2'], indirect=True)
31
32
33
def pytest_addoption(parser):
34
    parser.addoption('--provider', default='tinkergraph',
35
                     choices=('tinkergraph', 'dse',))
36
    parser.addoption('--gremlin-host', default='gremlin-server')
37
    parser.addoption('--gremlin-port', default='8182')
38
39
40
@pytest.fixture
41
def provider(request):
42
    provider = request.config.getoption('provider')
43
    if provider == 'tinkergraph':
44
        return TinkerGraph
45
    elif provider == 'dse':
46
        try:
47
            import goblin_dse
48
        except ImportError:
49
            raise RuntimeError("Couldn't run tests with DSEGraph provider: the goblin_dse package "
50
                               "must be installed")
51
        else:
52
            return goblin_dse.DSEGraph
53
54
55
@pytest.fixture
56
def aliases(request):
57
    if request.config.getoption('provider') == 'tinkergraph':
58
        return {'g': 'g'}
59
    elif request.config.getoption('provider') == 'dse':
60
        return {'g': 'testgraph.g'}
61
62
63
@pytest.fixture
64
def gremlin_server():
65
    return driver.GremlinServer
66
67
68
@pytest.fixture
69
def unused_server_url(unused_tcp_port):
70
    return 'https://localhost:{}/gremlin'.format(unused_tcp_port)
71
72
73
@pytest.fixture
74
def gremlin_host(request):
75
    return request.config.getoption('gremlin_host')
76
77
78
@pytest.fixture
79
def gremlin_port(request):
80
    return request.config.getoption('gremlin_port')
81
82
83
@pytest.fixture
84
def gremlin_url(gremlin_host, gremlin_port):
85
    return "ws://{}:{}/gremlin".format(gremlin_host, gremlin_port)
86
87
88
@pytest.fixture
89
def connection(gremlin_url, event_loop, provider):
90
    try:
91
        conn = event_loop.run_until_complete(
92
            driver.Connection.open(
93
                gremlin_url, event_loop,
94
                message_serializer=serializer.GraphSONMessageSerializer,
95
                provider=provider
96
            ))
97
    except OSError:
98
        pytest.skip('Gremlin Server is not running')
99
    return conn
100
101
102
@pytest.fixture
103
def connection_pool(gremlin_url, event_loop, provider):
104
    return driver.ConnectionPool(
105
        gremlin_url, event_loop, None, '', '', 4, 1, 16,
106
        64, None, serializer.GraphSONMessageSerializer, provider=provider)
107
108
109
@pytest.fixture
110
def cluster(request, gremlin_host, gremlin_port, event_loop, provider, aliases):
111
    # if request.param == 'c1':
112
    cluster = driver.Cluster(
113
        event_loop,
114
        hosts=[gremlin_host],
115
        port=gremlin_port,
116
        aliases=aliases,
117
        message_serializer=serializer.GraphSONMessageSerializer,
118
        provider=provider
119
    )
120
    # elif request.param == 'c2':
121
    #     cluster = driver.Cluster(
122
    #         event_loop,
123
    #         hosts=[gremlin_host],
124
    #         port=gremlin_port,
125
    #         aliases=aliases,
126
    #         message_serializer=serializer.GraphSONMessageSerializer,
127
    #         provider=provider
128
    #     )
129
    return cluster
130
131
# TOOO FIX
132
# @pytest.fixture
133
# def remote_graph():
134
#      return driver.AsyncGraph()
135
136
# Class fixtures
137
@pytest.fixture
138
def cluster_class(event_loop):
139
    return driver.Cluster
140
141
@pytest.fixture
142
def remote_connection(event_loop, gremlin_url):
143
    try:
144
        remote_conn = event_loop.run_until_complete(
145
            DriverRemoteConnection.open(gremlin_url, 'g'))
146
    except OSError:
147
        pytest.skip('Gremlin Server is not running')
148
    else:
149
        return remote_conn
150
151 View Code Duplication
@pytest.fixture(autouse=True)
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
152
def run_around_tests(remote_connection, event_loop):
153
    g = Graph().traversal().withRemote(remote_connection)
154
155
    async def create_graph():
156
        await g.V().drop().iterate()
157
        software1 = await g.addV("software").property("name", "lop").property("lang", "java").property(T.id, 3).next()
158
        software2 = await g.addV("software").property("name", "ripple").property("lang", "java").property(T.id, 5).next()
159
        person1 = await g.addV("person").property("name", "marko").property("age", "29").property(T.id, 1).next()
160
        person2 = await g.addV("person").property("name", "vadas").property("age", "27").property(T.id, 2).next()
161
        person3 = await g.addV("person").property("name", "josh").property("age", "32").property(T.id, 4).next()
162
        person4 = await g.addV("person").property("name", "peter").property("age", "35").property(T.id, 6).next()
163
164
        knows1 = await g.addE("knows").from_(person1).to(person2).property("weight", 0.5).property(T.id, 7).next()
165
        knows2 = await g.addE("knows").from_(person1).to(person3).property("weight", 1,0).property(T.id, 8).next()
166
        created1 = await g.addE("created").from_(person1).to(software1).property("weight", 0.4).property(T.id, 9).next()
167
        created2 = await g.addE("created").from_(person3).to(software2).property("weight", 1.0).property(T.id, 10).next()
168
        created3 = await g.addE("created").from_(person3).to(software1).property("weight", 1.0).property(T.id, 11).next()
169
        created4 = await g.addE("created").from_(person4).to(software1).property("weight", 0.2).property(T.id, 12).next()
170
171
    event_loop.run_until_complete(create_graph())
172
173
    yield
174