def test_run_with_timeout(self): try: with connect(self.bolt_address, auth=self.auth_token) as cx1: cx1.run("CREATE (a:Node)") cx1.discard_all() cx1.sync() with connect(self.bolt_address, auth=self.auth_token) as cx2: cx1.begin() cx1.run("MATCH (a:Node) SET a.property = 1") cx1.sync() cx2.run("MATCH (a:Node) SET a.property = 2", timeout=0.25) with self.assertRaises(TransientError): cx2.sync() finally: self.delete_all()
def test_run_with_timeout(neo4j, delete_all_afterwards): with connect(neo4j.addresses[0], auth=neo4j.auth) as cx1: cx1.run("CREATE (a:Node)") cx1.discard_all() cx1.send_all() cx1.fetch_all() with connect(neo4j.addresses[0], auth=neo4j.auth) as cx2: cx1.begin() cx1.run("MATCH (a:Node) SET a.property = 1") cx1.send_all() cx1.fetch_all() cx2.run("MATCH (a:Node) SET a.property = 2", timeout=0.25) with raises(TransientError): cx2.send_all() cx2.fetch_all()
def test_non_string_user_and_password(self): v = self.server_version_info() if (3, 5, 0) <= v.version_tuple <= (3, 5, 3): raise SkipTest( "Non-string passwords are broken in server version %r" % v) with self.assertRaises(AuthError): _ = connect(self.bolt_address, auth=(1, 1))
def test_disconnect_after_init(self): with StubCluster({9001: "v1/disconnect_after_init.script"}): address = ("127.0.0.1", 9001) with connect(address, auth=self.auth_token, encrypted=False) as cx: with self.assertRaises(ServiceUnavailable): metadata = {} cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.sync()
def test_return_1(self): with connect(self.bolt_address, auth=self.auth_token) as cx: metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.sync() self.assertEqual([[1]], records)
def test_connection_simple_run(self): records = [] with connect(self.bolt_address, auth=self.auth_token) as cx: metadata = {} cx.run("RETURN 1", {}, on_success=metadata.update) cx.pull_all(on_records=records.extend, on_success=metadata.update) cx.sync() self.assertEqual(records, [[1]])
def test_disconnect_after_init(): with StubCluster({9001: "v3/disconnect_after_init.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: with raises(ServiceUnavailable): metadata = {} cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.send_all() cx.fetch_all()
def main(): records = [] with connect(("localhost", 7687), auth=("neo4j", "password")) as cx: for _ in range(10000): metadata = {} cx.run("RETURN 1", {}, on_success=metadata.update) cx.pull_all(on_records=records.extend, on_success=metadata.update) cx.send_all() cx.fetch_all()
def update_password(user, password, new_password): """ Test utility for setting the initial password. :param user: user name :param password: current password :param new_password: new password """ class AuthToken(object): def __init__(self): self.scheme = "basic" self.principal = user self.credentials = password auth = AuthToken() setattr( auth, "new-credentials", new_password) # TODO: hopefully switch hyphen to underscore on server connect(("localhost", 7687), auth=auth).close()
def _test(self, statement, parameters, assertion): data = [] metadata = {} with connect(self.bolt_address, auth=self.auth_token) as cx: cx.begin() cx.run(statement, parameters, on_success=metadata.update) cx.pull_all(on_records=data.extend, on_success=metadata.update) cx.rollback() cx.sync() assertion(data, metadata)
def test_run_with_metadata(self): with connect(self.bolt_address, auth=self.auth_token) as cx: metadata = {} records = [] cx.run("CALL dbms.getTXMetaData", metadata={"foo": "bar"}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.sync() self.assertEqual([[{"foo": "bar"}]], records)
def test_disconnect_on_pull_all(self): with StubCluster({9001: "v1/disconnect_on_pull_all.script"}): address = ("127.0.0.1", 9001) with connect(address, auth=self.auth_token, encrypted=False) as cx: with self.assertRaises(ServiceUnavailable): metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.sync()
def test_multiple_chunk_response(self): b = bytearray(16365) records = [] with connect(self.bolt_address, auth=self.auth_token) as cx: metadata = {} cx.run("CREATE (a) SET a.foo = $x RETURN a", {"x": b}, on_success=metadata.update) cx.pull_all(on_records=records.extend, on_success=metadata.update) cx.sync() foo = records[0][0][2]["foo"] self.assertEqual(b, foo)
def test_custom_resolver(self): address = ("*", DEFAULT_PORT) def my_resolver(unresolved_address): self.assertEqual(unresolved_address, ("*", DEFAULT_PORT)) yield "99.99.99.99", self.bolt_port # this should be rejected as unable to connect yield "127.0.0.1", self.bolt_port # this should succeed with connect(address, auth=self.auth_token, resolver=my_resolver) as cx: self.assertEqual(cx.server.address, ("127.0.0.1", 7687))
def test_return_1(self): with StubCluster({9001: "v3/return_1.script"}): address = ("127.0.0.1", 9001) with connect(address, auth=self.auth_token, encrypted=False) as cx: metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.sync() self.assertEqual([[1]], records)
def test_return_1_as_read(): with StubCluster({9001: "v3/return_1_as_read.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, mode="r", on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.send_all() cx.fetch_all() assert records == [[1]]
def test_custom_resolver(neo4j): address = ("*", DEFAULT_PORT) bolt_port = neo4j.addresses[0][1] def my_resolver(unresolved_address): assert unresolved_address == ("*", DEFAULT_PORT) yield "99.99.99.99", bolt_port # should be rejected as can't connect yield "127.0.0.1", bolt_port # should succeed with connect(address, auth=neo4j.auth, resolver=my_resolver) as cx: assert cx.server.address == ("127.0.0.1", 7687)
def test_address_deactivation_on_database_unavailable_error(): with StubCluster({9001: "v3/database_unavailable.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: cx.pool = FakeConnectionPool() with raises(DatabaseUnavailableError): metadata = {} cx.run("RETURN 1", {}, on_success=metadata.update) cx.pull_all() cx.send_all() cx.fetch_all() assert ("127.0.0.1", 9001) in cx.pool.deactivated_addresses
def test_disconnect_on_pull_all(): with StubCluster({9001: "v3/disconnect_on_pull_all.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: with raises(ServiceUnavailable): metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.send_all() cx.fetch_all()
def main(): parser = ArgumentParser(description="Execute one or more Cypher statements using Bolt.") parser.add_argument("statement", nargs="+") parser.add_argument("-a", "--address", default="localhost:7687", metavar="ADDRESS") parser.add_argument("-k", "--keys", action="store_true") parser.add_argument("-P", "--password") parser.add_argument("-p", "--parameter", action="append", metavar="NAME=VALUE") parser.add_argument("-q", "--quiet", action="store_true") parser.add_argument("-s", "--secure", action="store_true") parser.add_argument("-U", "--user", default="neo4j") parser.add_argument("-v", "--verbose", action="count") parser.add_argument("-x", "--times", type=int, default=1) parser.add_argument("-z", "--summary", action="store_true") args = parser.parse_args() if args.verbose: Watcher("neobolt").watch(logging.DEBUG, stderr) parameters = {} for parameter in args.parameter or []: name, _, value = parameter.partition("=") if value == "" and name in parameters: del parameters[name] else: try: parameters[name] = json_loads(value) except ValueError: parameters[name] = value cx = connect(SocketAddress.parse(args.address, DEFAULT_PORT), auth=(args.user, args.password), encrypted=args.secure) try: for _ in range(args.times): for statement in args.statement: metadata = {} records = [] try: cx.run(statement, parameters, on_success=metadata.update) cx.pull_all(on_records=records.extend, on_success=metadata.update) cx.sync() except CypherError as error: stderr.write("%s: %s\r\n" % (error.code, error.message)) else: if not args.quiet: if args.keys: stdout.write("%s\r\n" % "\t".join(metadata.get("fields", ()))) for i, record in enumerate(records): stdout.write("%s\r\n" % "\t".join(map(repr, record))) if args.summary: for key, value in sorted(metadata): stdout.write("{}: {}\r\n".format(key, value)) stdout.write("\r\n") finally: cx.close()
def test_run_with_timeout(): with StubCluster({9001: "v3/run_with_timeout.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.run("RETURN $x", {"x": 1}, timeout=12.34, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.send_all() cx.fetch_all() assert records == [[1]]
def test_begin_with_timeout(): with StubCluster({9001: "v3/begin_with_timeout.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.begin(timeout=12.34, on_success=metadata.update) cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.commit(on_success=metadata.update) cx.send_all() cx.fetch_all() assert records == [[1]] assert {"fields": ["x"], "bookmark": "bookmark:1"} == metadata
def test_return_1_in_tx_as_read(): with StubCluster({9001: "v3/return_1_in_tx_as_read.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.begin(on_success=metadata.update) cx.run("RETURN $x", {"x": 1}, mode="r", on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.commit(on_success=metadata.update) cx.send_all() cx.fetch_all() assert records == [[1]] assert {"fields": ["x"], "bookmark": "bookmark:1"} == metadata
def test_return_1_in_tx(self): with connect(self.bolt_address, auth=self.auth_token) as cx: metadata = {} records = [] cx.begin(on_success=metadata.update) cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.commit(on_success=metadata.update) cx.sync() self.assertEqual([[1]], records) self.assertTrue(metadata["bookmark"].startswith("neo4j:bookmark:")) self.assertEqual(metadata["fields"], ["$x"]) self.assertIsInstance(metadata["t_first"], int) self.assertIsInstance(metadata["t_last"], int) self.assertEqual(metadata["type"], "r")
def test_fail_on_commit(): with StubCluster({9001: "v3/fail_on_commit.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.begin(on_success=metadata.update) cx.run("CREATE (a) RETURN id(a)", on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.send_all() cx.fetch_all() cx.commit(on_success=metadata.update) with raises(ServiceUnavailable): cx.send_all() cx.fetch_all()
def test_connection_error_on_commit(): with StubCluster({9001: "v3/connection_error_on_commit.script"}): address = ("127.0.0.1", 9001) with connect(address) as cx: metadata = {} records = [] cx.begin(on_success=metadata.update) cx.run("CREATE (a) RETURN id(a)", on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.send_all() cx.fetch_all() cx.commit(on_success=metadata.update) with raises(IncompleteCommitError): cx.send_all() cx.fetch_all()
def test_return_1_in_tx(self): with StubCluster({9001: "v3/return_1_in_tx.script"}): address = ("127.0.0.1", 9001) with connect(address, auth=self.auth_token, encrypted=False) as cx: metadata = {} records = [] cx.begin(on_success=metadata.update) cx.run("RETURN $x", {"x": 1}, on_success=metadata.update) cx.pull_all(on_success=metadata.update, on_records=records.extend) cx.commit(on_success=metadata.update) cx.sync() self.assertEqual([[1]], records) self.assertEqual({ "fields": ["x"], "bookmark": "bookmark:1" }, metadata)
def setUpClass(cls): if is_listening(cls.bolt_address): print("Using existing server listening on port {}\n".format( cls.bolt_port)) try: cx = connect(cls.bolt_address, auth=cls.auth_token) except AuthError as error: raise RuntimeError("Failed to authenticate (%s)" % error) else: cx.close() elif cls.server_package is not None: print("Using server from package {}\n".format(cls.server_package)) package = copy_dist(cls.server_package, cls.local_server_package) home = cls._unpack(package) cls._start_server(home) elif cls.neoctrl_args is not None: print("Using boltkit to install server 'neoctrl-install {}'\n". format(cls.neoctrl_args)) edition = "enterprise" if "-e" in cls.neoctrl_args else "community" version = cls.neoctrl_args.split()[-1] home = _install(edition, version, cls.run_path) cls._start_server(home) else: raise SkipTest("No Neo4j server available for %s" % cls.__name__)
def connector(address, **kwargs): return connect(address, **dict(config, **kwargs))
def connector(address_, **kwargs): return connect(address_, auth=cx_data["auth"], **kwargs)