Skip to content

1.0 tck tests - Error handling #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion neo4j/v1/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from socket import create_connection, SHUT_RDWR, error as SocketError
from struct import pack as struct_pack, unpack as struct_unpack, unpack_from as struct_unpack_from

import errno

from .constants import DEFAULT_PORT, DEFAULT_USER_AGENT, KNOWN_HOSTS, MAGIC_PREAMBLE, \
TRUST_DEFAULT, TRUST_ON_FIRST_USE
from .compat import hex2
Expand Down Expand Up @@ -374,12 +376,14 @@ def connect(host, port=None, ssl_context=None, **config):
"""

# Establish a connection to the host and port specified
# Catches refused connections see:
# https://docs.python.org/2/library/errno.html
port = port or DEFAULT_PORT
if __debug__: log_info("~~ [CONNECT] %s %d", host, port)
try:
s = create_connection((host, port))
except SocketError as error:
if error.errno == 111:
if error.errno == 111 or error.errno == 61:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment might be useful here to highlight what errors these are.

raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % (host, port))
else:
raise
Expand Down
31 changes: 20 additions & 11 deletions neo4j/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class which can be used to obtain `Driver` instances that are used for
from .compat import integer, string, urlparse
from .connection import connect, Response, RUN, PULL_ALL
from .constants import ENCRYPTED_DEFAULT, TRUST_DEFAULT, TRUST_SIGNED_CERTIFICATES
from .exceptions import CypherError, ResultError
from .exceptions import CypherError, ProtocolError, ResultError
from .ssl_compat import SSL_AVAILABLE, SSLContext, PROTOCOL_SSLv23, OP_NO_SSLv2, CERT_REQUIRED
from .types import hydrated

Expand Down Expand Up @@ -107,7 +107,8 @@ def __init__(self, url, **config):
self.host = parsed.hostname
self.port = parsed.port
else:
raise ValueError("Unsupported URL scheme: %s" % parsed.scheme)
raise ProtocolError("Unsupported URI scheme: '%s' in url: '%s'. Currently only supported 'bolt'." %
(parsed.scheme, url))
self.config = config
self.max_pool_size = config.get("max_pool_size", DEFAULT_MAX_POOL_SIZE)
self.session_pool = deque()
Expand Down Expand Up @@ -239,7 +240,7 @@ def keys(self):
# Fetch messages until we have the header or a failure
while self._keys is None and not self._consumed:
self.connection.fetch()
return self._keys
return tuple(self._keys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this previously coming through as a non-tuple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it comes as a list

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then 👍


def buffer(self):
if self.connection and not self.connection.closed:
Expand All @@ -262,9 +263,9 @@ def single(self):
records = list(self)
num_records = len(records)
if num_records == 0:
raise ResultError("No records found in stream")
raise ResultError("Cannot retrieve a single record, because this result is empty.")
elif num_records != 1:
raise ResultError("Multiple records found in stream")
raise ResultError("Expected a result with a single record, but this result contains at least one more.")
else:
return records[0]

Expand Down Expand Up @@ -396,7 +397,6 @@ def contains_updates(self):
#: a list of sub-plans
Plan = namedtuple("Plan", ("operator_type", "identifiers", "arguments", "children"))


#: A profiled plan describes how the database executed your statement.
#:
#: db_hits:
Expand Down Expand Up @@ -484,7 +484,12 @@ def run(self, statement, parameters=None):
:return: Cypher result
:rtype: :class:`.StatementResult`
"""
if self.transaction:
raise ProtocolError("Statements cannot be run directly on a session with an open transaction;"
" either run from within the transaction or use a different session.")
return self._run(statement, parameters)

def _run(self, statement, parameters=None):
# Ensure the statement is a Unicode value
if isinstance(statement, bytes):
statement = statement.decode("UTF-8")
Expand Down Expand Up @@ -517,14 +522,18 @@ def close(self):
"""
if self.last_result:
self.last_result.buffer()
if self.transaction:
self.transaction.close()
self.driver.recycle(self)

def begin_transaction(self):
""" Create a new :class:`.Transaction` within this session.

:return: new :class:`.Transaction` instance.
"""
assert not self.transaction
if self.transaction:
raise ProtocolError("You cannot begin a transaction on a session with an open transaction;"
" either run from within the transaction or use a different session.")
self.transaction = Transaction(self)
return self.transaction

Expand Down Expand Up @@ -552,7 +561,7 @@ class Transaction(object):

def __init__(self, session):
self.session = session
self.session.run("BEGIN")
self.session._run("BEGIN")

def __enter__(self):
return self
Expand All @@ -570,7 +579,7 @@ def run(self, statement, parameters=None):
:return:
"""
assert not self.closed
return self.session.run(statement, parameters)
return self.session._run(statement, parameters)

def commit(self):
""" Mark this transaction as successful and close in order to
Expand All @@ -591,9 +600,9 @@ def close(self):
"""
assert not self.closed
if self.success:
self.session.run("COMMIT")
self.session._run("COMMIT")
else:
self.session.run("ROLLBACK")
self.session._run("ROLLBACK")
self.closed = True
self.session.transaction = None

Expand Down
4 changes: 2 additions & 2 deletions runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ echo ""

TEST_RUNNER="coverage run -m ${UNITTEST} discover -vfs ${TEST}"
EXAMPLES_RUNNER="coverage run -m ${UNITTEST} discover -vfs examples"
BEHAVE_RUNNER="behave --tags=-db --tags=-in_dev test/tck"
BEHAVE_RUNNER="behave --tags=-db --tags=-tls --tags=-fixed_session_pool test/tck"

if [ ${RUNNING} -eq 1 ]
then
Expand Down Expand Up @@ -112,4 +112,4 @@ else

fi

fi
fi
1 change: 0 additions & 1 deletion test/tck/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def after_all(context):


def after_scenario(context, scenario):
pass
for runner in tck_util.runners:
runner.close()

2 changes: 1 addition & 1 deletion test/tck/resultparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import json
import re
from neo4j.v1 import Node, Relationship, Path
from tck_util import TestValue
from test_value import TestValue


def parse_values_to_comparable(row):
Expand Down
50 changes: 4 additions & 46 deletions test/tck/steps/cypher_compability_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from behave import *

from test.tck import tck_util
from test.tck.tck_util import TestValue
from test.tck.resultparser import parse_values, parse_values_to_comparable
from test.tck.resultparser import parse_values

use_step_matcher("re")

Expand Down Expand Up @@ -54,51 +53,10 @@ def step_impl(context, statement):

@then("result")
def step_impl(context):
expected = table_to_comparable_result(context.table)
expected = tck_util.table_to_comparable_result(context.table)
assert(len(context.results) > 0)
for result in context.results:
records = list(result)
given = driver_result_to_comparable_result(records)
if not unordered_equal(given, expected):
given = tck_util.driver_result_to_comparable_result(records)
if not tck_util.unordered_equal(given, expected):
raise Exception("Does not match given: \n%s expected: \n%s" % (given, expected))


def _driver_value_to_comparable(val):
if isinstance(val, list):
l = [_driver_value_to_comparable(v) for v in val]
return l
else:
return TestValue(val)


def table_to_comparable_result(table):
result = []
keys = table.headings
for row in table:
result.append(
{keys[i]: parse_values_to_comparable(row[i]) for i in range(len(row))})
return result


def driver_result_to_comparable_result(result):
records = []
for record in result:
records.append({key: _driver_value_to_comparable(record[key]) for key in record})
return records


def unordered_equal(given, expected):
l1 = given[:]
l2 = expected[:]
assert isinstance(l1, list)
assert isinstance(l2, list)
assert len(l1) == len(l2)
for d1 in l1:
size = len(l2)
for d2 in l2:
if d1 == d2:
l2.remove(d2)
break
if size == len(l2):
return False
return True
2 changes: 0 additions & 2 deletions test/tck/steps/driver_result_api_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def step_impl(context, expected):
def step_impl(context):
for summary in context.summaries:
for row in context.table:
print(row[0].replace(" ","_"))
print(getattr(summary.counters, row[0].replace(" ","_")))
assert getattr(summary.counters, row[0].replace(" ","_")) == parse_values(row[1])


Expand Down
96 changes: 96 additions & 0 deletions test/tck/steps/error_reporting_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

# Copyright (c) 2002-2016 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from behave import *

from neo4j.v1.exceptions import ProtocolError, CypherError
from test.tck import tck_util

from neo4j.v1 import GraphDatabase

use_step_matcher("re")


@given("I have a driver")
def step_impl(context):
context.driver = tck_util.driver


@step("I start a `Transaction` through a session")
def step_impl(context):
context.session = context.driver.session()
context.session.begin_transaction()


@step("`run` a query with that same session without closing the transaction first")
def step_impl(context):
try:
context.session.run("CREATE (:n)")
except Exception as e:
context.exception = e
finally:
context.session.close()


@step("I start a new `Transaction` with the same session before closing the previous")
def step_impl(context):
try:
context.session.begin_transaction()
except Exception as e:
context.exception = e
finally:
context.session.close()


@step("I run a non valid cypher statement")
def step_impl(context):
try:
s = context.driver.session()
print(s.transaction)
s.run("NOT VALID").consume()
except Exception as e:
context.exception = e


@step("I set up a driver to an incorrect port")
def step_impl(context):
try:
context.driver = GraphDatabase.driver("bolt://localhost:7777")
context.driver.session()
except Exception as e:
context.exception = e


@step("I set up a driver with wrong scheme")
def step_impl(context):
try:
context.driver = GraphDatabase.driver("wrong://localhost")
context.driver.session()
except Exception as e:
context.exception = e


@step("it throws a `ClientException`")
def step_impl(context):
print(context.exception)
assert context.exception is not None
assert type(context.exception) == ProtocolError or type(context.exception) == CypherError
assert isinstance(context.exception, ProtocolError) or isinstance(context.exception, CypherError)
assert str(context.exception).startswith(context.table.rows[0][0])
Loading