Skip to content

Commit 160bc72

Browse files
committed
Updated tests
1 parent d92dbf6 commit 160bc72

File tree

16 files changed

+271
-197
lines changed

16 files changed

+271
-197
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ htmlcov
99
.coverage
1010
.test
1111
.tox
12+
.cache
1213

1314
docs/build
1415
dist

neo4j/v1/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ def _connect(self, access_mode=None):
275275
def _disconnect(self, sync):
276276
if self._connection:
277277
if sync:
278-
self._connection.sync()
278+
try:
279+
self._connection.sync()
280+
except ServiceUnavailable:
281+
pass
279282
if self._connection:
280283
self._connection.in_use = False
281284
self._connection = None

test/__main__.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/examples/__main__.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/examples/result_retain_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def add_employee_to_company(tx, person, company_name):
4545
"MERGE (com:Company {name: $company_name}) "
4646
"MERGE (emp)-[:WORKS_FOR]->(com)",
4747
person_name=person["name"], company_name=company_name)
48+
return 1
4849

4950
@staticmethod
5051
def match_person_nodes(tx):

test/examples/service_unavailable_example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from test.examples.base_application import BaseApplication
2222

2323
# tag::service-unavailable-import[]
24-
from neo4j.v1 import ServiceUnavailable
24+
from neo4j.exceptions import ServiceUnavailable
2525
# end::service-unavailable-import[]
2626

2727

@@ -31,10 +31,10 @@ def __init__(self, uri, user, password):
3131

3232
# tag::service-unavailable[]
3333
def add_item(self):
34-
with self._driver.session() as session:
35-
try:
34+
try:
35+
with self._driver.session() as session:
3636
session.write_transaction(lambda tx: tx.run("CREATE (a:Item)"))
37-
return True
38-
except ServiceUnavailable:
39-
return False
37+
return True
38+
except ServiceUnavailable:
39+
return False
4040
# end::service-unavailable[]

test/examples/test_examples.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21+
22+
from contextlib import contextmanager
23+
from io import StringIO
24+
import sys
25+
from unittest import TestCase
26+
2127
from test.integration.tools import IntegrationTestCase
2228

2329

24-
## Python2 doesn't have contextlib.redirect_stdout()
25-
# http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
26-
import sys
27-
from contextlib import contextmanager
30+
# Python2 doesn't have contextlib.redirect_stdout()
31+
# http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
2832
@contextmanager
2933
def stdout_redirector(stream):
3034
old_stdout = sys.stdout
@@ -34,12 +38,6 @@ def stdout_redirector(stream):
3438
finally:
3539
sys.stdout = old_stdout
3640

37-
def get_string_io():
38-
if sys.version_info[0] < 3:
39-
from StringIO import StringIO
40-
else:
41-
from io import StringIO
42-
return StringIO()
4341

4442
class ExamplesTest(IntegrationTestCase):
4543

@@ -78,7 +76,7 @@ def test_config_unencrypted_example(self):
7876
def test_cypher_error_example(self):
7977
from test.examples.cypher_error_example import CypherErrorExample
8078

81-
f = get_string_io()
79+
f = StringIO()
8280
with stdout_redirector(f):
8381
example = CypherErrorExample(self.bolt_uri, self.user, self.password)
8482
try:
@@ -102,7 +100,7 @@ def test_driver_lifecycle_example(self):
102100
def test_hello_world_example(self):
103101
from test.examples.hello_world_example import HelloWorldExample
104102

105-
f = get_string_io()
103+
f = StringIO()
106104
with stdout_redirector(f):
107105
example = HelloWorldExample(self.bolt_uri, self.user, self.password)
108106
example.print_greeting("hello, world")
@@ -185,6 +183,6 @@ def test_service_unavailable_example(self):
185183
from test.examples.service_unavailable_example import ServiceUnavailableExample
186184

187185
example = ServiceUnavailableExample(self.bolt_uri, self.user, self.password)
188-
self.__class__._stop_server()
186+
self._stop_server()
189187

190-
self.assertFalse(example.addItem())
188+
self.assertFalse(example.add_item())

test/integration/__main__.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/performance/test_results.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2017 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
from itertools import product
23+
24+
from pytest import mark
25+
26+
from neo4j.v1 import GraphDatabase
27+
from .tools import GraphDatabaseServer
28+
29+
30+
class ReadWorkload(object):
31+
32+
server = None
33+
driver = None
34+
35+
@classmethod
36+
def setup_class(cls):
37+
cls.server = server = GraphDatabaseServer()
38+
server.start()
39+
cls.driver = GraphDatabase.driver(server.bolt_uri, auth=server.auth_token)
40+
41+
@classmethod
42+
def teardown_class(cls):
43+
cls.driver.close()
44+
cls.server.stop()
45+
46+
def work(self, *units_of_work):
47+
def runner():
48+
with self.driver.session() as session:
49+
for unit_of_work in units_of_work:
50+
session.read_transaction(unit_of_work)
51+
return runner
52+
53+
54+
class TestReadWorkload(ReadWorkload):
55+
56+
@staticmethod
57+
def test_cypher(width):
58+
return "UNWIND range(1, $count) AS _ RETURN {}".format(", ".join("$x AS x{}".format(i) for i in range(width)))
59+
60+
@staticmethod
61+
def uow(record_count, record_width, value):
62+
63+
def _(tx):
64+
s = "UNWIND range(1, $record_count) AS _ RETURN {}".format(
65+
", ".join("$x AS x{}".format(i) for i in range(record_width)))
66+
p = {"record_count": record_count, "x": value}
67+
for record in tx.run(s, p):
68+
assert all(x == value for x in record.values())
69+
70+
return _
71+
72+
@mark.parametrize("record_count,record_width,value", product(
73+
[1, 1000], # record count
74+
[1, 10], # record width
75+
[1, u'hello, world'], # value
76+
))
77+
def test_1x1(self, benchmark, record_count, record_width, value):
78+
benchmark(self.work(self.uow(record_count, record_width, value)))

0 commit comments

Comments
 (0)