Exemple #1
0
class Tx(unittest.TestCase):
    def setUp(self):
        self._backend = new_backend()
        self._server = StubServer(9001)
        uri = "bolt://%s" % self._server.address
        self._driver = Driver(self._backend, uri,
                              AuthorizationToken(scheme="basic"))

    def tearDown(self):
        self._backend.close()
        # If test raised an exception this will make sure that the stub server
        # is killed and it's output is dumped for analys.
        self._server.reset()

    # Tests that a committed transaction can return the last bookmark
    def test_last_bookmark(self):
        if get_driver_name() not in ["go", "dotnet"]:
            self.skipTest("session.lastBookmark not implemented in backend")

        self._server.start(script=script_commit)
        session = self._driver.session("w")
        tx = session.beginTransaction()
        tx.run("RETURN 1 as n")
        tx.commit()
        bookmarks = session.lastBookmarks()
        session.close()
        self._driver.close()
        self._server.done()

        self.assertEqual(bookmarks, ["bm"])
Exemple #2
0
    def test_read(self):
        if self._driver in ["javascript"]:
            self.skipTest(
                "Transactional functions not implemented in testkit backend")

        script = "retry_read.script"
        if self._driver in ["go"]:
            # Until Go is updated to use PULL with n
            script = "retry_read_v3.script"
        self._server.start(os.path.join(scripts_path, script))

        num_retries = 0

        def retry_once(tx):
            nonlocal num_retries
            num_retries = num_retries + 1
            result = tx.run("RETURN 1")
            record = result.next()
            return record.values[0]

        auth = AuthorizationToken(scheme="basic",
                                  principal="neo4j",
                                  credentials="pass")
        driver = Driver(self._backend, "bolt://%s" % self._server.address,
                        auth)
        session = driver.session("r")
        x = session.readTransaction(retry_once)
        self.assertIsInstance(x, types.CypherInt)
        self.assertEqual(x.value, 1)
        self.assertEqual(num_retries, 1)

        session.close()
        driver.close()
        self._server.done()
Exemple #3
0
    def test_read_twice(self):
        self._server.start(
            os.path.join(scripts_path, "retry_read_twice.script"))

        num_retries = 0

        def retry_twice(tx):
            nonlocal num_retries
            num_retries = num_retries + 1
            result = tx.run("RETURN 1")
            record = result.next()
            return record.values[0]

        auth = AuthorizationToken(scheme="basic",
                                  principal="neo4j",
                                  credentials="pass")
        driver = Driver(self._backend, "bolt://%s" % self._server.address,
                        auth)
        session = driver.session("r")
        x = session.readTransaction(retry_twice)
        self.assertIsInstance(x, types.CypherInt)
        self.assertEqual(x.value, 1)
        self.assertEqual(num_retries, 2)

        session.close()
        driver.close()
        self._server.done()
Exemple #4
0
    def test_disconnect_on_commit(self):
        # Should NOT retry when connection is lost on unconfirmed commit.
        # The rule could be relaxed on read transactions therefore we test on writeTransaction.
        # An error should be raised to indicate the failure
        if not self._driverName in ["go"]:
            self.skipTest(
                "Backend missing support for SessionWriteTransaction")
        self._server.start(script=script_commit_disconnect)
        num_retries = 0

        def once(tx):
            nonlocal num_retries
            num_retries = num_retries + 1
            result = tx.run("RETURN 1")
            record = result.next()

        auth = AuthorizationToken(scheme="basic")
        driver = Driver(self._backend, "bolt://%s" % self._server.address,
                        auth)
        session = driver.session("w")

        with self.assertRaises(types.DriverError) as e:  # Check further...
            session.writeTransaction(once)

        self.assertEqual(num_retries, 1)
        session.close()
        driver.close()
        self._server.done()
Exemple #5
0
 def _run(self, n, script, end, expectedSequence, expectedError=False):
     uri = "bolt://%s" % self._server.address
     driver = Driver(self._backend, uri, AuthorizationToken(scheme="basic"))
     self._server.start(script=script,
                        vars={
                            "#END#": end,
                            "#VERSION#": "4"
                        })
     session = driver.session("w", fetchSize=n)
     result = session.run("RETURN 1 AS n")
     gotError = False
     sequence = []
     while True:
         try:
             next = result.next()
         except Exception as e:
             gotError = True
             break
         if isinstance(next, types.NullRecord):
             break
         sequence.append(next.values[0].value)
     driver.close()
     self._server.done()
     self.assertEqual(expectedSequence, sequence)
     self.assertEqual(expectedError, gotError)
Exemple #6
0
    def test_nested(self):
        if get_driver_name() not in ['go', 'dotnet']:
            self.skipTest(
                "Need support for specifying session fetch size in testkit backend"
            )
        uri = "bolt://%s" % self._server.address
        driver = Driver(self._backend, uri, AuthorizationToken(scheme="basic"))
        self._server.start(script=TxRun.script_nested_n,
                           vars={"#VERSION#": "4"})
        session = driver.session("w", fetchSize=1)
        tx = session.beginTransaction()
        res1 = tx.run("CYPHER")
        seq = []
        seqs = []
        while True:
            rec1 = res1.next()
            if isinstance(rec1, types.NullRecord):
                break
            seq.append(rec1.values[0].value)
            seq2 = []
            res2 = tx.run("CYPHER")
            while True:
                rec2 = res2.next()
                if isinstance(rec2, types.NullRecord):
                    break
                seq2.append(rec2.values[0].value)
            seqs.append(seq2)

        tx.commit()
        driver.close()
        self._server.done()
        self.assertEqual(["1_1", "1_2"], seq)
        self.assertEqual([["2_1", "2_2"], ["3_1"]], seqs)
Exemple #7
0
class Transport(unittest.TestCase):
    def setUp(self):
        self._backend = new_backend()
        self._server = StubServer(9001)
        self._driverName = get_driver_name()
        auth = AuthorizationToken(scheme="basic", principal="neo4j", credentials="pass")
        uri = "bolt://%s" % self._server.address
        self._driver = Driver(self._backend, uri, auth)
        self._session = self._driver.session("w")

    def test_noop(self):
        # Verifies that no op messages sent on bolt chunking layer are ignored. The no op messages
        # are sent from server as a way to notify that the connection is still up.
        # Bolt 4.1 >
        bolt_version = "4.1"
        self._server.start(script=script, vars = {"$bolt_version": bolt_version})
        result = self._session.run("RETURN 1 as n")
        record = result.next()
        nilrec = result.next()
        self._driver.close()
        self._server.done()

        # Verify the result
        self.assertEqual(record.values[0].value, 1) # Indirectly verifies that we got a record
        self.assertIsInstance(nilrec, types.NullRecord)
Exemple #8
0
    def test_session_run_read(self):
        if get_driver_name() not in ['go', 'dotnet']:
            self.skipTest(
                "Session with named database not implemented in backend")

        driver = Driver(self._backend, self._uri, self._auth, self._userAgent)
        self._routingServer.start(script=self.router_script(),
                                  vars=self.get_vars())
        self._readServer.start(script=self.read_script(), vars=self.get_vars())
        session = driver.session('r', database=self.get_db())
        session.run("RETURN 1 as n")
        session.close()
        driver.close()
        self._routingServer.done()
        self._readServer.done()
Exemple #9
0
def try_connect(backend, server, scheme, host):
    url = "%s://%s:%d" % (scheme, host, 6666)
    # Doesn't really matter
    auth = AuthorizationToken(scheme="basic",
                              principal="neo4j",
                              credentials="pass")
    driver = Driver(backend, url, auth)
    session = driver.session("r")
    try:
        result = session.run("RETURN 1 as n")
    except:
        pass
    session.close()
    driver.close()
    return server.connected()
Exemple #10
0
    def test_tx_run_write(self):
        if get_driver_name() not in ['go', 'dotnet']:
            self.skipTest(
                "Session with named database not implemented in backend")

        driver = Driver(self._backend, self._uri, self._auth, self._userAgent)
        self._routingServer.start(script=self.router_script(),
                                  vars=self.get_vars())
        self._writeServer.start(script=self.write_tx_script(),
                                vars=self.get_vars())
        session = driver.session('w', database=self.get_db())
        tx = session.beginTransaction()
        tx.run("RETURN 1 as n")
        tx.commit()
        session.close()
        driver.close()
        self._routingServer.done()
        self._writeServer.done()
Exemple #11
0
    def test_session_run_read(self):
        if get_driver_name() not in ['go', 'dotnet']:
            self.skipTest(
                "Session with named database not implemented in backend")

        # Driver is configured to talk to "routing" stub server
        uri = "bolt://%s" % self._server.address
        self._server.start(script=self.script(), vars=self.get_vars())
        driver = Driver(self._backend,
                        uri,
                        AuthorizationToken(scheme="basic",
                                           principal="p",
                                           credentials="c"),
                        userAgent="007")
        session = driver.session('r', database="adb")
        session.run("RETURN 1 as n")
        session.close()
        driver.close()
        self._server.done()
Exemple #12
0
    def test_read_twice(self):
        # We could probably use AUTO RESET in the script but this makes the diffs more
        # obvious.
        vars = {
            "$extra_reset_1": "",
            "$extra_reset_2": "",
        }
        if self._driverName not in ["go"]:
            vars["$extra_reset_2"] = "C: RESET\nS: SUCCESS {}"
        if self._driverName in ["java", "javascript"]:
            vars["$extra_reset_1"] = "C: RESET\nS: SUCCESS {}"

        self._server.start(script=script_retry, vars=vars)
        num_retries = 0

        def twice(tx):
            nonlocal num_retries
            num_retries = num_retries + 1
            result = tx.run("RETURN 1")
            record = result.next()
            return record.values[0]

        auth = AuthorizationToken(scheme="basic",
                                  principal="neo4j",
                                  credentials="pass")
        driver = Driver(self._backend, "bolt://%s" % self._server.address,
                        auth)
        session = driver.session("r")
        x = session.readTransaction(twice)
        self.assertIsInstance(x, types.CypherInt)
        self.assertEqual(x.value, 1)
        self.assertEqual(num_retries, 2)

        session.close()
        driver.close()
        self._server.done()
Exemple #13
0
class SessionRunDisconnected(unittest.TestCase):
    def setUp(self):
        self._backend = new_backend()
        self._server = StubServer(9001)
        self._driverName = get_driver_name()
        auth = AuthorizationToken(scheme="basic",
                                  principal="neo4j",
                                  credentials="pass")
        uri = "bolt://%s" % self._server.address
        self._driver = Driver(self._backend,
                              uri,
                              auth,
                              userAgent=customUserAgent)
        self._session = self._driver.session("w")

    def tearDown(self):
        self._backend.close()
        # If test raised an exception this will make sure that the stub server
        # is killed and it's output is dumped for analys.
        self._server.reset()

    # Helper function that runs the sequence and returns the name of the step at which the
    # error happened.
    def _run(self):
        try:
            result = self._session.run("RETURN 1 as n")
        except types.DriverError:
            return "after run"

        try:
            record = result.next()
        except types.DriverError:
            return "after first next"

        try:
            nullRecord = result.next()
        except types.DriverError:
            return "after last next"

        return "success"

    def test_disconnect_on_hello(self):
        # Verifies how the driver handles when server disconnects right after driver sent bolt
        # hello message.
        if not self._driverName in ["go"]:
            self.skipTest(
                "No support for custom user-agent in testkit backend")
        self._server.start(script=script_on_hello)
        step = self._run()
        self._session.close()
        self._driver.close()
        self._server.done()

        expectedStep = "after first next"
        if self._driverName in ["go"]:
            # Go reports this error earlier
            expectedStep = "after run"
        self.assertEqual(step, expectedStep)

    def test_disconnect_on_run(self):
        # Verifies how the driver handles when server disconnects right after driver sent bolt
        # run message.
        self._server.start(script=script_on_run)
        step = self._run()
        self._session.close()
        self._driver.close()
        self._server.done()

        expectedStep = "after first next"
        if self._driverName in ["go"]:
            # Go reports this error earlier
            expectedStep = "after run"
        self.assertEqual(step, expectedStep)

    def test_disconnect_on_pull(self):
        # Verifies how the driver handles when server disconnects right after driver sent bolt
        # pull message.
        self._server.start(script=script_on_pull)
        step = self._run()
        self._session.close()
        self._driver.close()
        self._server.done()

        expectedStep = "after first next"
        if self._driverName in ["go"]:
            # Go reports this error earlier
            expectedStep = "after run"
        self.assertEqual(step, expectedStep)
Exemple #14
0
class SessionRunParameters(unittest.TestCase):
    def setUp(self):
        self._backend = new_backend()
        self._server = StubServer(9001)
        self._driverName = get_driver_name()
        auth = AuthorizationToken()
        uri = "bolt://%s" % self._server.address
        self._driver = Driver(self._backend, uri,
                              AuthorizationToken(scheme="basic"))

    def tearDown(self):
        self._backend.close()
        # If test raised an exception this will make sure that the stub server
        # is killed and it's output is dumped for analys.
        self._server.reset()

    def _run(self,
             accessMode,
             params=None,
             bookmarks=None,
             txMeta=None,
             timeout=None):
        session = self._driver.session(accessMode, bookmarks=bookmarks)
        try:
            result = session.run("RETURN 1 as n",
                                 params=params,
                                 txMeta=txMeta,
                                 timeout=timeout)
            result.next()
        finally:
            session.close()

    def test_accessmode_read(self):
        if self._driverName not in ["go", "java", "dotnet"]:
            self.skipTest("Session accessmode not implemented in backend")

        self._server.start(script=script_accessmode_read)
        self._run("r")
        self._driver.close()
        self._server.done()

    def test_accessmode_write(self):
        self._server.start(script=script_accessmode_write)
        self._run("w")
        self._driver.close()
        self._server.done()

    def test_bookmarks(self):
        if self._driverName not in ["go", "dotnet"]:
            self.skipTest("Session bookmarks not implemented in backend")
        self._server.start(script=script_bookmarks)
        self._run("w", bookmarks=["b1", "b2"])
        self._driver.close()
        self._server.done()

    def test_txmeta(self):
        if self._driverName not in ["go", "dotnet"]:
            self.skipTest("Session txmeta not implemented in backend")
        self._server.start(script=script_txmeta)
        self._run("w", txMeta={"akey": "aval"})
        self._driver.close()
        self._server.done()

    def test_timeout(self):
        if self._driverName not in ["go", "dotnet"]:
            self.skipTest("Session timeout not implemented in backend")
        self._server.start(script=script_timeout)
        self._run("w", timeout=17)
        self._driver.close()
        self._server.done()

    def test_combined(self):
        if self._driverName not in ["go", "dotnet"]:
            self.skipTest("Session parameters not implemented in backend")
        self._server.start(script=script_combined)
        self._run("r",
                  params={"p": types.CypherInt(1)},
                  bookmarks=["b0"],
                  txMeta={"k": "v"},
                  timeout=11)
        self._driver.close()
        self._server.done()