Beispiel #1
0
 def test_should_be_able_to_write_as_default(self):
     with StubCluster("v3/router.script", "v3/create_a.script"):
         uri = "bolt+routing://127.0.0.1:9001"
         with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
             with driver.session() as session:
                 result = session.run("CREATE (a $x)",
                                      {"x": {
                                          "name": "Alice"
                                      }})
                 assert not list(result)
                 assert result.summary().server.address == ('127.0.0.1',
                                                            9006)
Beispiel #2
0
 def test_should_call_get_routing_table_with_context(self):
     with StubCluster("v3/get_routing_table_with_context.script",
                      "v3/return_1_on_9002.script"):
         uri = "bolt+routing://127.0.0.1:9001/?name=molly&age=1"
         with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
             with driver.session(
                     default_access_mode=READ_ACCESS) as session:
                 result = session.run("RETURN $x", {"x": 1})
                 for record in result:
                     assert record["x"] == 1
                 assert result.summary().server.address == ('127.0.0.1',
                                                            9002)
Beispiel #3
0
 def test_should_serve_read_when_missing_writer(self):
     with StubCluster("v3/router_no_writers.script",
                      "v3/return_1_on_9005.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:
                 result = session.run("RETURN $x", {"x": 1})
                 for record in result:
                     assert record["x"] == 1
                 assert result.summary().server.address == ('127.0.0.1',
                                                            9005)
Beispiel #4
0
def test_read_transaction_with_error(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_read_transaction_with_error
    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(tx):
                    tx.run("X")

                with pytest.raises(Neo4jError):
                    _ = session.read_transaction(unit_of_work)
Beispiel #5
0
def test_no_retry_write_on_user_canceled_tx(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_no_retry_write_on_user_canceled_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(tx):
                    tx.run("RETURN 1")

                with pytest.raises(TransientError):
                    _ = session.write_transaction(unit_of_work)
def test_bolt_driver_on_exit_result_consume(driver_info, test_script,
                                            database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_on_exit_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:
            with driver.session(database=database,
                                fetch_size=2,
                                default_access_mode=READ_ACCESS) as session:
                result = session.run("UNWIND [1,2,3,4] AS x RETURN x")
Beispiel #7
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 #8
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:
            assert isinstance(driver, Neo4jDriver)
            driver.update_routing_table()
            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)}
Beispiel #9
0
def test_neo4j_driver_verify_connectivity_server_down(driver_info,
                                                      test_script):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_neo4j_driver_verify_connectivity_server_down
    with StubCluster(test_script):
        driver = GraphDatabase.driver(driver_info["uri_neo4j"],
                                      auth=driver_info["auth_token"],
                                      user_agent="test")
        assert isinstance(driver, Neo4jDriver)

        with pytest.raises(ServiceUnavailable):
            driver.verify_connectivity()

        driver.close()
Beispiel #10
0
def test_write_transaction_with_error(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_write_transaction_with_error
    with StubCluster(*test_scripts):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"]) as driver:
            with driver.session() as session:

                def unit_of_work(tx):
                    tx.run("X")

                with pytest.raises(Neo4jError):
                    _ = session.write_transaction(unit_of_work)
Beispiel #11
0
def test_no_retry_read_on_user_canceled_tx(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_no_retry_read_on_user_canceled_tx
    with StubCluster(*test_scripts):
        uri = "neo4j://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"]) as driver:
            with driver.session() as session:

                def unit_of_work(tx):
                    tx.run("RETURN 1")

                with pytest.raises(TransientError):
                    _ = session.read_transaction(unit_of_work)
Beispiel #12
0
def test_should_reconnect_for_new_query(driver_info, test_scripts, test_run_args):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_reconnect_for_new_query
    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_1 = session.run(*test_run_args)
                assert session._connection is not None
                result_1.consume()
                assert session._connection is None
                result_2 = session.run(*test_run_args)
                assert session._connection is not None
                result_2.consume()
                assert session._connection is None
 def test_should_automatically_chain_bookmarks(self):
     with StubCluster("v3/router.script", "v3/bookmark_chain.script"):
         uri = "bolt+routing://localhost:9001"
         with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
             with driver.session(default_access_mode=READ_ACCESS,
                                 bookmarks=["bookmark:0",
                                            "bookmark:1"]) as session:
                 with session.begin_transaction():
                     pass
                 assert session.last_bookmark() == "bookmark:2"
                 with session.begin_transaction():
                     pass
                 assert session.last_bookmark() == "bookmark:3"
 def test_should_retain_connection_if_fetching_multiple_results(self):
     with StubCluster("v3/router.script", "v3/return_1_twice.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:
                 result_1 = session.run("RETURN $x", {"x": 1})
                 result_2 = session.run("RETURN $x", {"x": 1})
                 assert session._connection is not None
                 result_1.consume()
                 assert session._connection is not None
                 result_2.consume()
                 assert session._connection is None
Beispiel #15
0
def test_direct_can_handle_noop(driver_info, test_script):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_direct_can_handle_noop
    with StubCluster(test_script):
        uri = "bolt://localhost:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  **driver_config) as driver:
            assert isinstance(driver, BoltDriver)
            with driver.session(fetch_size=2,
                                default_access_mode=READ_ACCESS) as session:
                result = session.run("RETURN 1 AS x")
                value = result.single().value()
                assert value == 1
Beispiel #16
0
def test_should_call_get_routing_table_with_context(driver_info, test_scripts,
                                                    test_run_args):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_call_get_routing_table_with_context
    with StubCluster(*test_scripts):
        uri = "neo4j://localhost:9001/?name=molly&age=1"
        with GraphDatabase.driver(uri,
                                  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', 9002)
Beispiel #17
0
 def test_should_discover_servers_on_driver_construction(self):
     with StubCluster({9001: "v3/router.script"}):
         uri = "bolt+routing://127.0.0.1:9001"
         with GraphDatabase.driver(uri,
                                   auth=self.auth_token,
                                   encrypted=False) as driver:
             table = driver._pool.routing_table
             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)}
Beispiel #18
0
def test_should_automatically_chain_bookmarks(driver_info, test_scripts):
    # python -m pytest tests/stub/test_bookmarking.py -s -v -k test_should_automatically_chain_bookmarks
    with StubCluster(*test_scripts):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"]) as driver:
            with driver.session(default_access_mode=READ_ACCESS,
                                bookmarks=["bookmark:0", "bookmark:1"]) as session:
                with session.begin_transaction():
                    pass
                assert session.last_bookmark() == "bookmark:2"
                with session.begin_transaction():
                    pass
                assert session.last_bookmark() == "bookmark:3"
Beispiel #19
0
def test_dbms_cluster_routing_get_routing_table(driver_info, test_script, test_database):
    # python -m pytest tests/stub/test_multi_database.py -s -v -k test_dbms_cluster_routing_get_routing_table

    test_config = {
        "user_agent": "test",
        "database": test_database,
    }

    with StubCluster(test_script):
        uri = "neo4j://localhost:9001"
        driver = GraphDatabase.driver(uri, auth=driver_info["auth_token"], **test_config)
        assert isinstance(driver, Neo4jDriver)
        driver.close()
Beispiel #20
0
def test_connection_error_on_explicit_commit(driver_info, test_script):
    # python -m pytest tests/stub/test_transactions.py -s -v -k test_connection_error_on_explicit_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:
                tx = session.begin_transaction()
                result = tx.run("CREATE (n {name:'Bob'})")
                _ = list(result)
                with pytest.raises(ServiceUnavailable):
                    tx.commit()
Beispiel #21
0
 def test_autocommit_transaction_included_in_chain(self):
     with StubCluster({9001: "v3/router.script", 9004: "v3/bookmark_chain_with_autocommit.script"}):
         uri = "bolt+routing://localhost:9001"
         with GraphDatabase.driver(uri, auth=self.auth_token, encrypted=False) as driver:
             with driver.session(access_mode=READ_ACCESS, bookmark="bookmark:1") as session:
                 with session.begin_transaction():
                     pass
                 assert session.last_bookmark() == "bookmark:2"
                 session.run("RETURN 1").consume()
                 assert session.last_bookmark() == "bookmark:3"
                 with session.begin_transaction():
                     pass
                 assert session.last_bookmark() == "bookmark:4"
Beispiel #22
0
 def test_should_disconnect_after_fetching_autocommit_result(self):
     with StubCluster({
             9001: "v3/router.script",
             9004: "v3/return_1.script"
     }):
         uri = "bolt+routing://127.0.0.1:9001"
         with GraphDatabase.driver(uri,
                                   auth=self.auth_token,
                                   encrypted=False) as driver:
             with driver.session(access_mode=READ_ACCESS) as session:
                 result = session.run("RETURN $x", {"x": 1})
                 assert session._connection is not None
                 result.consume()
                 assert session._connection is None
Beispiel #23
0
def test_bolt_driver_fetch_size_config_normal_case(driver_info, test_script, database):
    # python -m pytest tests/stub/test_directdriver.py -s -v -k test_bolt_driver_fetch_size_config_normal_case
    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")
                assert isinstance(result, Result)
                for record in result:
                    expected.append(record["x"])

        assert expected == [1, 2, 3, 4]
Beispiel #24
0
    def test_write_transaction(self):
        with StubCluster("v3/router.script", "v3/return_1_in_write_tx.script"):
            uri = "bolt+routing://localhost:9001"
            with GraphDatabase.driver(uri, auth=self.auth_token) as driver:
                with driver.session() as session:

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

                    value = session.write_transaction(unit_of_work)
                    assert value == 1
def test_write_transaction(driver_info, test_scripts):
    # python -m pytest tests/stub/test_accesslevel.py -s -v -k test_write_transaction
    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(tx):
                    total = 0
                    for record in tx.run("RETURN 1"):
                        total += record[0]
                    return total

                value = session.write_transaction(unit_of_work)
                assert value == 1
def test_should_retain_connection_if_fetching_multiple_results(
        driver_info, test_scripts, test_run_args):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_should_retain_connection_if_fetching_multiple_results
    with StubCluster(*test_scripts):
        uri = "neo4j://127.0.0.1:9001"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"]) as driver:
            with driver.session(default_access_mode=READ_ACCESS) as session:
                result_1 = session.run(*test_run_args)
                result_2 = session.run(*test_run_args)
                assert session._connection is not None
                result_1.consume()
                assert session._connection is not None
                result_2.consume()
                assert session._connection is None
Beispiel #27
0
 def test_should_be_able_to_read(self):
     with StubCluster({
             9001: "v3/router.script",
             9004: "v3/return_1.script"
     }):
         uri = "bolt+routing://127.0.0.1:9001"
         with GraphDatabase.driver(uri,
                                   auth=self.auth_token,
                                   encrypted=False) as driver:
             with driver.session(access_mode=READ_ACCESS) as session:
                 result = session.run("RETURN $x", {"x": 1})
                 for record in result:
                     assert record["x"] == 1
                 assert result.summary().server.address == ('127.0.0.1',
                                                            9004)
Beispiel #28
0
def test_default_access_mode_defined_at_session_level(driver_info, test_scripts, test_run_args):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_default_access_mode_defined_at_session_level
    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:
                with session.begin_transaction() as tx:
                    result = tx.run(*test_run_args)
                    assert session._connection is not None
                    result.consume()
                    assert session._connection is not None
                    result = tx.run(*test_run_args)
                    assert session._connection is not None
                    result.consume()
                    assert session._connection is not None
                assert session._connection is None
Beispiel #29
0
def test_hello_routing(driver_info, test_scripts):
    # python -m pytest tests/stub/test_routingdriver.py -s -v -k test_hello_routing
    with StubCluster(*test_scripts):
        uri = "neo4j://localhost:9001/?region=china&policy=my_policy"
        with GraphDatabase.driver(uri,
                                  auth=driver_info["auth_token"],
                                  user_agent="test") as driver:
            with driver.session(default_access_mode=READ_ACCESS,
                                fetch_size=-1) as session:
                result = session.run("RETURN 1 AS x")
                for record in result:
                    assert record["x"] == 1
                address = result.consume().server.address
                assert address.host == "127.0.0.1"
                assert address.port == 9002
Beispiel #30
0
def test_autocommit_transaction_included_in_chain(driver_info, test_scripts):
    # python -m pytest tests/stub/test_bookmarking.py -s -v -k test_autocommit_transaction_included_in_chain
    with StubCluster(*test_scripts):
        uri = "neo4j://localhost:9001"
        with GraphDatabase.driver(uri, auth=driver_info["auth_token"]) as driver:
            with driver.session(default_access_mode=READ_ACCESS,
                                bookmarks=["bookmark:1"]) as session:
                with session.begin_transaction():
                    pass
                assert session.last_bookmark() == "bookmark:2"
                session.run("RETURN 1").consume()
                assert session.last_bookmark() == "bookmark:3"
                with session.begin_transaction():
                    pass
                assert session.last_bookmark() == "bookmark:4"