Ejemplo n.º 1
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()
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
 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")
Ejemplo n.º 6
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()
Ejemplo n.º 7
0
class TestDataTypes(unittest.TestCase):
    def setUp(self):
        self._backend = new_backend()
        self._host, self._port = get_neo4j_host_and_port()
        self._scheme = "bolt://%s:%d" % (self._host, self._port)
        self._session = None
        self._driver = None

    def tearDown(self):
        if self._session:
            self._session.close()
        if self._driver:
            self._driver.close()
        self._backend.close()

    def createDriverAndSession(self):
        auth_token = AuthorizationToken(
            scheme="basic",
            principal=os.environ.get(env_neo4j_user, "neo4j"),
            credentials=os.environ.get(env_neo4j_pass, "pass"))
        self._driver = Driver(self._backend, self._scheme, auth_token)
        self._session = self._driver.session("w")

    def verifyCanEcho(self, val):
        result = self._session.run("RETURN $x as y", params={"x": val})
        record = result.next()
        self.assertEqual(record, types.Record(values=[val]))

    def testShouldEchoBack(self):
        if get_driver_name() in ['java']:
            self.skipTest("Not implemented in backend")

        vals = [
            types.CypherBool(True),
            types.CypherBool(False),
            types.CypherNull(),
            types.CypherInt(1),
            types.CypherInt(-7),
            types.CypherInt(-129),
            types.CypherInt(129),
            types.CypherInt(2147483647),
            types.CypherInt(-2147483647),
            #types.CypherFloat(9223372036854775807),       # TODO: Investigate
            #types.CypherFloat(-9223372036854775807),
            #types.CypherFloat(1.7976931348623157E+308),
            #types.CypherFloat(2.2250738585072014e-308),
            #types.CypherFloat(4.9E-324),
            #types.CypherFloat(0.0),  # Js can not determine if it should be 0 or 0.0
            types.CypherFloat(1.1),
            types.CypherString("1"),
            types.CypherString("-17∂ßå®"),
            types.CypherString("String"),
            types.CypherString(""),
        ]

        self.createDriverAndSession()
        for val in vals:
            self.verifyCanEcho(val)

    def testShouldEchoVeryLongList(self):
        if get_driver_name() in ['java']:
            self.skipTest("Not implemented in backend")

        vals = [
            types.CypherNull(),
            types.CypherInt(1),
            types.CypherFloat(1.1),
            types.CypherString("hello"),
            types.CypherBool(True),
        ]

        self.createDriverAndSession()

        for val in vals:
            long_list = []
            for i in range(1000):
                long_list.append(val)
            self.verifyCanEcho(types.CypherList(long_list))

    def testShouldEchoVeryLongString(self):
        if get_driver_name() in ['java']:
            self.skipTest("Not implemented in backend")

        self.createDriverAndSession()
        long_string = "*" * 10000
        self.verifyCanEcho(types.CypherString(long_string))

    def testShouldEchoNestedLists(self):
        if get_driver_name() in ['java']:
            self.skipTest("Not implemented in backend")

        test_lists = [
            types.CypherList([
                types.CypherInt(1),
                types.CypherInt(2),
                types.CypherInt(3),
                types.CypherInt(4)
            ]),
            types.CypherList([
                types.CypherString("a"),
                types.CypherString("b"),
                types.CypherString("c"),
                types.CypherString("˚C")
            ]),
            types.CypherList([types.CypherBool(True),
                              types.CypherBool(False)]),
            types.CypherList([
                types.CypherFloat(1.1),
                types.CypherFloat(2.2),
                types.CypherFloat(3.3),
                types.CypherFloat(4.4)
            ]),
            types.CypherList([types.CypherNull(None),
                              types.CypherNull(None)]),
            types.CypherList([
                types.CypherNull(None),
                types.CypherBool(True),
                types.CypherString("Hello world"),
                types.CypherInt(-1234567890),
                types.CypherFloat(123.456)
            ])
        ]

        self.createDriverAndSession()
        self.verifyCanEcho(types.CypherList(test_lists))

    def testShouldEchoNode(self):
        self.createDriverAndSession()

        result = self._session.run(
            "CREATE (n:TestLabel {num: 1, txt: 'abc'}) RETURN n")
        record = result.next()
        self.assertNotIsInstance(record, types.NullRecord)

        node = record.values[0]
        self.assertIsInstance(node, types.CypherNode)
        self.assertEqual(node.labels,
                         types.CypherList([types.CypherString('TestLabel')]))
        self.assertEqual(
            node.props,
            types.CypherMap({
                "num": types.CypherInt(1),
                "txt": types.CypherString('abc')
            }))

    # Work in progress
    def testShouldEchoVeryLongMap(self):
        if get_driver_name() not in ['dotnet', 'go']:
            self.skipTest("Not implemented in backend")

        test_list = [
            types.CypherNull(None),
            types.CypherInt(1),
            types.CypherFloat(1.1),
            types.CypherString("Hello World"),
            types.CypherBool(True)
        ]

        self.createDriverAndSession()

        long_map = {}
        for cypherType in test_list:
            long_map.clear()
            for i in range(1000):
                long_map[str(i)] = cypherType
            self.verifyCanEcho(types.CypherMap(long_map))

    def testShouldEchoNestedMap(self):
        if get_driver_name() not in ['dotnet', 'go']:
            self.skipTest("Not implemented in backend")

        test_maps = {
            "a":
            types.CypherMap({
                "a": types.CypherInt(1),
                "b": types.CypherInt(2),
                "c": types.CypherInt(3),
                "d": types.CypherInt(4)
            }),
            "b":
            types.CypherMap({
                "a": types.CypherBool(True),
                "b": types.CypherBool(False)
            }),
            "c":
            types.CypherMap({
                "a": types.CypherFloat(1.1),
                "b": types.CypherFloat(2.2),
                "c": types.CypherFloat(3.3)
            }),
            "d":
            types.CypherMap({
                "a": types.CypherString("a"),
                "b": types.CypherString("b"),
                "c": types.CypherString("c"),
                "temp": types.CypherString("˚C")
            }),
            "e":
            types.CypherMap({"a": types.CypherNull(None)}),
            "f":
            types.CypherMap({
                "a": types.CypherInt(1),
                "b": types.CypherBool(True),
                "c": types.CypherFloat(3.3),
                "d": types.CypherString("Hello World"),
                "e": types.CypherNull(None)
            }),
        }

        self.createDriverAndSession()
        self.verifyCanEcho(types.CypherMap(test_maps))
Ejemplo n.º 8
0
def get_driver(backend):
    """ Returns default driver for tests that do not test this aspect
    """
    host, port = get_neo4j_host_and_port()
    scheme = "bolt://%s:%d" % (host, port)
    return Driver(backend, scheme, get_authorization())