Beispiel #1
0
    def test_forgets_address_on_database_unavailable_error(self):
        with StubCluster("v3/router.script", "v3/database_unavailable.script"):
            uri = "bolt+routing://127.0.0.1:9001"
            with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
                with driver.session(
                        default_access_mode=READ_ACCESS) as session:

                    pool = driver._pool
                    table = pool.routing_table
                    table.readers.remove(('127.0.0.1', 9005))

                    with self.assertRaises(TransientError) as raised:
                        _ = session.run("RETURN 1")
                    assert raised.exception.title == "DatabaseUnavailable"

                    pool = driver._pool
                    table = pool.routing_table

                    # address should not have connections in the pool, it has failed
                    assert ('127.0.0.1', 9004) not in pool.connections
                    assert table.routers == {('127.0.0.1', 9001),
                                             ('127.0.0.1', 9002),
                                             ('127.0.0.1', 9003)}
                    # reader 127.0.0.1:9004 should've been forgotten because of an raised
                    assert not table.readers
                    assert table.writers == {('127.0.0.1', 9006)}
Beispiel #2
0
    def test_two_sessions_can_share_a_connection(self):
        with StubCluster("v3/router.script", "v3/return_1_four_times.script"):
            uri = "bolt+routing://127.0.0.1:9001"
            with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
                session_1 = driver.session(default_access_mode=READ_ACCESS)
                session_2 = driver.session(default_access_mode=READ_ACCESS)

                result_1a = session_1.run("RETURN $x", {"x": 1})
                c = session_1._connection
                result_1a.consume()

                result_2a = session_2.run("RETURN $x", {"x": 1})
                assert session_2._connection is c
                result_2a.consume()

                result_1b = session_1.run("RETURN $x", {"x": 1})
                assert session_1._connection is c
                result_1b.consume()

                result_2b = session_2.run("RETURN $x", {"x": 1})
                assert session_2._connection is c
                result_2b.consume()

                session_2.close()
                session_1.close()
Beispiel #3
0
    def test_forgets_address_on_service_unavailable_error(self):
        with StubCluster("v3/router.script", "v3/rude_reader.script"):
            uri = "bolt+routing://127.0.0.1:9001"
            with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
                with driver.session(
                        default_access_mode=READ_ACCESS) as session:

                    pool = driver._pool
                    table = pool.routing_table
                    table.readers.remove(('127.0.0.1', 9005))

                    with self.assertRaises(SessionExpired):
                        _ = session.run("RETURN 1")

                    # address should have connections in the pool but be inactive, it has failed
                    assert ('127.0.0.1', 9004) in pool.connections
                    conns = pool.connections[('127.0.0.1', 9004)]
                    conn = conns[0]
                    assert conn._closed is True
                    assert conn.in_use is True
                    assert table.routers == {('127.0.0.1', 9001),
                                             ('127.0.0.1', 9002),
                                             ('127.0.0.1', 9003)}
                    # reader 127.0.0.1:9004 should've been forgotten because of an error
                    assert not table.readers
                    assert table.writers == {('127.0.0.1', 9006)}

                assert conn.in_use == False
def test_bolt_uri_constructs_bolt_driver(driver_info, test_script):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_uri_constructs_bolt_driver
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"]) as driver:
            assert isinstance(driver, BoltDriver)
Beispiel #5
0
def test_read_tx_then_write_tx(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_read_tx_then_write_tx
    with StubCluster(*test_scripts):
        with GraphDatabase.driver(driver_info["uri_neo4j"],
                                  auth=driver_info["auth_token"]) as driver:
            with driver.session(fetch_size=-1) as session:

                def unit_of_work_1(tx):
                    total = 0
                    for record in tx.run("RETURN 1"):
                        total += record[0]
                    return total

                def unit_of_work_2(tx):
                    total = 0
                    for record in tx.run("RETURN 2"):
                        total += record[0]
                    return total

                value = session.read_transaction(unit_of_work_1)
                assert session.last_bookmark() == "bookmark:1"
                assert value == 1
                value = session.write_transaction(unit_of_work_2)
                assert session.last_bookmark() == "bookmark:2"
                assert value == 2
def test_bolt_driver_read_transaction_fetch_size_config_normal_case(
        driver_info, test_script, database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_read_transaction_fetch_size_config_normal_case
    @unit_of_work(timeout=3, metadata={"foo": "bar"})
    def unwind(transaction):
        assert isinstance(transaction, Transaction)
        values = []
        result = transaction.run("UNWIND [1,2,3,4] AS x RETURN x")
        assert isinstance(result, Result)
        for record in result:
            values.append(record["x"])
        return values

    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  user_agent="test") as driver:
            assert isinstance(driver, BoltDriver)
            with driver.session(database=database,
                                fetch_size=2,
                                default_access_mode=READ_ACCESS) as session:
                expected = session.read_transaction(unwind)

        assert expected == [1, 2, 3, 4]
def test_bolt_driver_fetch_size_config_run_run(driver_info, test_script,
                                               database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_fetch_size_config_run_run
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  user_agent="test") as driver:
            assert isinstance(driver, BoltDriver)
            with driver.session(database=database,
                                fetch_size=2,
                                default_access_mode=READ_ACCESS) as session:
                expected = []
                result1 = session.run(
                    "UNWIND [1,2,3,4] AS x RETURN x"
                )  # The session should discard all if in streaming mode and a new run is invoked.
                result2 = session.run("UNWIND [5,6,7,8] AS x RETURN x")

                for record in result2:
                    expected.append(record["x"])

                result_summary = result2.consume()
                assert result_summary.server.address == ('127.0.0.1', 9001)

        assert expected == [5, 6, 7, 8]
        assert result_summary.server.address == ('127.0.0.1', 9001)
def test_bolt_driver_fetch_size_config_case_result_consume(
        driver_info, test_script, database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_fetch_size_config_case_result_consume
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  user_agent="test") as driver:
            assert isinstance(driver, BoltDriver)
            with driver.session(database=database,
                                fetch_size=2,
                                default_access_mode=READ_ACCESS) as session:
                expected = []
                result = session.run("UNWIND [1,2,3,4] AS x RETURN x")
                for record in result:
                    expected.append(record["x"])

                result_summary = result.consume()
                assert result_summary.server.address == ('127.0.0.1', 9001)

                result_summary = result.consume(
                )  # It will just return the result summary

        assert expected == [1, 2, 3, 4]
        assert result_summary.server.address == ('127.0.0.1', 9001)
Beispiel #9
0
def test_should_be_able_to_set_multiple_bookmarks(driver_info, test_script):
    # python -m pytest tests/stub/test_bookmarking.py -s -v -k test_should_be_able_to_set_multiple_bookmarks
    with StubCluster(test_script):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"]) as driver:
            with driver.session(bookmarks=[":1", ":2"]) as session:
                assert session.next_bookmarks() == (":1", ":2")
Beispiel #10
0
def test_should_be_no_bookmark_in_new_session(driver_info, test_script):
    # python -m pytest tests/stub/test_bookmarking.py -s -v -k test_should_be_no_bookmark_in_new_session
    with StubCluster(test_script):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"]) as driver:
            with driver.session() as session:
                assert session.last_bookmark() is None
 def test_direct_session_close_after_server_close(self):
     with StubCluster("v3/disconnect_after_init.script"):
         uri = "bolt://127.0.0.1:9001"
         with GraphDatabase.driver(uri, auth=self.auth_token, max_retry_time=0,
                                   acquire_timeout=3, user_agent="test") as driver:
             with driver.session() as session:
                 with self.assertRaises(ServiceUnavailable):
                     session.write_transaction(lambda tx: tx.run("CREATE (a:Item)"))
Beispiel #12
0
def test_bolt_driver_on_close_result_consume(driver_info, test_script, database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_on_close_result_consume
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"], **driver_config) as driver:
            session = driver.session(database=database, fetch_size=2, default_access_mode=READ_ACCESS)
            result = session.run("UNWIND [1,2,3,4] AS x RETURN x")
            session.close()
def test_connection_error_on_commit(driver_info, test_script):
    # python -m pytest tests/stub/test_transactions.py -s -v -k test_connection_error_on_commit
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"], **driver_config) as driver:
            with driver.session(**session_config) as session:
                with pytest.raises(ServiceUnavailable):
                    session.write_transaction(create_bob)
Beispiel #14
0
def test_should_be_able_to_write_as_default(driver_info, test_scripts):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_be_able_to_write_as_default
    with StubCluster(*test_scripts):
        with GraphDatabase.driver(driver_info["uri_neo4j"], auth=driver_info["auth_token"]) as driver:
            with driver.session(fetch_size=-1) as session:
                result = session.run("CREATE (a $x)", {"x": {"name": "Alice"}})
                assert not list(result)
                assert result.consume().server.address == ('127.0.0.1', 9006)
Beispiel #15
0
def test_direct_disconnect_on_pull_all(driver_info, test_script):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_direct_disconnect_on_pull_all
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"], **driver_config) as driver:
            with pytest.raises(ServiceUnavailable):
                with driver.session(**session_config) as session:
                    session.run("RETURN $x", {"x": 1}).consume()
Beispiel #16
0
 def test_cannot_discover_servers_on_silent_router(self):
     with StubCluster({9001: "v3/silent_router.script"}):
         uri = "bolt+routing://127.0.0.1:9001"
         with self.assertRaises(RoutingProtocolError):
             with GraphDatabase.driver(uri,
                                       auth=self.auth_token,
                                       encrypted=False):
                 pass
Beispiel #17
0
def test_should_be_able_to_set_bookmark(driver_info, test_script):
    # python -m pytest tests/stub/test_bookmarking.py -s -v -k test_should_be_able_to_set_bookmark
    with StubCluster(test_script):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"]) as driver:
            with driver.session(bookmarks=["X"], fetch_size=-1) as session:
                assert session._bookmarks == ("X", )
Beispiel #18
0
 def test_cannot_discover_servers_on_non_router(self):
     with StubCluster({9001: "v3/non_router.script"}):
         uri = "bolt+routing://127.0.0.1:9001"
         with self.assertRaises(ServiceUnavailable):
             with GraphDatabase.driver(uri,
                                       auth=self.auth_token,
                                       encrypted=False):
                 pass
Beispiel #19
0
def test_should_disconnect_after_fetching_autocommit_result(driver_info, test_scripts):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_disconnect_after_fetching_autocommit_result
    with StubCluster(*test_scripts):
        with GraphDatabase.driver(driver_info["uri_neo4j"], auth=driver_info["auth_token"]) as driver:
            with driver.session(default_access_mode=READ_ACCESS, fetch_size=-1) as session:
                result = session.run("RETURN $x", {"x": 1})
                assert session._connection is not None
                result.consume()
                assert session._connection is None
Beispiel #20
0
def test_should_discover_servers_on_driver_construction(driver_info, test_script):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_discover_servers_on_driver_construction
    with StubCluster(test_script):
        with GraphDatabase.driver(driver_info["uri_neo4j"], auth=driver_info["auth_token"]) as driver:
            table = driver._pool.routing_tables[DEFAULT_DATABASE]
            assert table.routers == {('127.0.0.1', 9001), ('127.0.0.1', 9002),
                                     ('127.0.0.1', 9003)}
            assert table.readers == {('127.0.0.1', 9004), ('127.0.0.1', 9005)}
            assert table.writers == {('127.0.0.1', 9006)}
 def test_connection_error_on_commit(self):
     with StubCluster("v3/connection_error_on_commit.script"):
         uri = "bolt://127.0.0.1:9001"
         with GraphDatabase.driver(uri,
                                   auth=self.auth_token,
                                   max_retry_time=0) as driver:
             with driver.session() as session:
                 with self.assertRaises(ServiceUnavailable):
                     session.write_transaction(self.create_bob)
def test_direct_verify_connectivity(driver_info, test_script, test_expected):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_direct_verify_connectivity
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  **driver_config) as driver:
            assert isinstance(driver, BoltDriver)
            assert driver.verify_connectivity() == test_expected
Beispiel #23
0
def test_direct_driver_handshake_negotiation(driver_info, test_script):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_direct_driver_handshake_negotiation
    with StubCluster(test_script):
        uri = "bolt://localhost:9001"
        driver = GraphDatabase.driver(uri,
                                      auth=driver_info["auth_token"],
                                      **driver_config)
        assert isinstance(driver, BoltDriver)
        driver.close()
Beispiel #24
0
def test_should_error_when_missing_reader(driver_info, test_script):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_error_when_missing_reader
    with StubCluster(test_script):
        with pytest.raises(BoltRoutingError):
            with GraphDatabase.driver(
                    driver_info["uri_neo4j"],
                    auth=driver_info["auth_token"]) as driver:
                assert isinstance(driver, Neo4jDriver)
                driver.update_routing_table()
Beispiel #25
0
def test_cannot_discover_servers_on_silent_router(driver_info, test_script):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_cannot_discover_servers_on_silent_router
    with StubCluster(test_script):
        with pytest.raises(BoltRoutingError):
            with GraphDatabase.driver(
                    driver_info["uri_neo4j"],
                    auth=driver_info["auth_token"]) as driver:
                assert isinstance(driver, Neo4jDriver)
                driver.update_routing_table()
Beispiel #26
0
 def test_direct_disconnect_on_pull_all(self):
     with StubCluster({9001: "v3/disconnect_on_pull_all.script"}):
         uri = "bolt://127.0.0.1:9001"
         with GraphDatabase.driver(uri,
                                   auth=self.auth_token,
                                   encrypted=False) as driver:
             with self.assertRaises(ServiceUnavailable):
                 with driver.session() as session:
                     session.run("RETURN $x", {"x": 1}).consume()
Beispiel #27
0
def test_direct_session_close_after_server_close(driver_info, test_script):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_direct_session_close_after_server_close
    with StubCluster(test_script):
        uri = "bolt://127.0.0.1:9001"

        with GraphDatabase.driver(uri, auth=driver_info["auth_token"], **driver_config) as driver:
            with driver.session(**session_config) as session:
                with pytest.raises(ServiceUnavailable):
                    session.write_transaction(lambda tx: tx.run(Query("CREATE (a:Item)", timeout=1)))
Beispiel #28
0
def test_should_serve_read_when_missing_writer(driver_info, test_scripts, test_run_args):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_serve_read_when_missing_writer
    with StubCluster(*test_scripts):
        with GraphDatabase.driver(driver_info["uri_neo4j"], auth=driver_info["auth_token"]) as driver:
            with driver.session(default_access_mode=READ_ACCESS, fetch_size=-1) as session:
                result = session.run(*test_run_args)
                for record in result:
                    assert record["x"] == 1
                assert result.consume().server.address == ('127.0.0.1', 9005)
Beispiel #29
0
def test_routing_disconnect_on_pull_all(driver_info, test_scripts):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_routing_disconnect_on_pull_all
    with StubCluster(*test_scripts):
        with GraphDatabase.driver(driver_info["uri_neo4j"],
                                  auth=driver_info["auth_token"]) as driver:
            with pytest.raises(SessionExpired):
                with driver.session(default_access_mode=READ_ACCESS,
                                    fetch_size=-1) as session:
                    session.run("RETURN $x", {"x": 1}).consume()
 def test_routing_disconnect_on_pull_all(self):
     with StubCluster("v3/router.script",
                      "v3/disconnect_on_pull_all_9004.script"):
         uri = "bolt+routing://127.0.0.1:9001"
         with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
             with self.assertRaises(SessionExpired):
                 with driver.session(
                         default_access_mode=READ_ACCESS) as session:
                     session.run("RETURN $x", {"x": 1}).consume()