示例#1
0
def test_create(driver: Driver):
    node = TModel(uid=10, name='test', value=123)
    with driver.session() as session:
        session.write_transaction(create_nodes, node)
        found_node = session.read_transaction(find_nodes, TModel, node.uid)

    assert isinstance(node, TModel)
    assert [node] == found_node
    def test_custom_resolver(self):
        def my_resolver(socket_address):
            self.assertEqual(socket_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 Driver("bolt://*", auth=self.auth_token,
                    resolver=my_resolver) as driver:
            with driver.session() as session:
                summary = session.run("RETURN 1").summary()
                self.assertEqual(summary.server.address, ("127.0.0.1", 7687))
示例#3
0
def test_custom_resolver(service):
    _, port = service.addresses[0]

    def my_resolver(socket_address):
        assert socket_address == ("*", 7687)
        yield "99.99.99.99", port     # should be rejected as unable to connect
        yield "127.0.0.1", port       # should succeed

    with Driver("bolt://*", auth=service.auth, resolver=my_resolver) as driver:
        with driver.session() as session:
            summary = session.run("RETURN 1").summary()
            assert summary.server.address == ("127.0.0.1", 7687)
示例#4
0
def test_create_nodes(driver: Driver, id_started, name_fmt):
    nodes = []
    for i in range(10):
        nodes.append(
            TModel(uid=i + id_started, name=name_fmt.format(i), value=123 * i))

    with driver.session() as session:
        session.write_transaction(create_nodes, *nodes)
        found_nodes = session.read_transaction(find_nodes, TModel,
                                               nodes[0].uid)

    assert len(found_nodes) == 1

    assert isinstance(found_nodes[0], TModel)
    assert nodes[:1] == found_nodes

    with driver.session() as session:
        found_nodes = session.read_transaction(find_nodes, TModel)

    assert len(found_nodes)

    for item in found_nodes:
        assert isinstance(item, TModel)
示例#5
0
def set_infected(driver: Driver, chat_id: int) -> str:
    '''Create a patient zero in the DB if doesn't exist.'''

    session: Session
    with driver.session() as session:
        result: str
        try:
            result = session.write_transaction(transactions.create_node,
                                               chat_id)
        except ConstraintError:
            # The user already exist, just return its referrer
            result = session.read_transaction(transactions.get_referrer,
                                              chat_id)

        return result
示例#6
0
def read_eco_transitions(driver: Driver,
                         model_id: str) -> Iterable[TransitionRule]:
    """Executes database transaction to read ecological transition rules.

    Args:
        driver: Database driver.
        model_id: Name of the specific model version for which to extract the
            ecological transitions from the database.

    Returns:
        Iterable over the complete set of transition rules for the model.
    """
    with driver.session() as session:
        trans_rules = session.read_transaction(eco_transitions_query, model_id)
    return trans_rules
示例#7
0
 def run_query(cls, driver: Driver, **kwargs):
     """
     :param driver: database driver
         (false interface that requires session() with a read_transtion()
     :param kwargs:
     :return:
     """
     with driver.session() as session:
         validated_kwargs = field_types.validate_inputs(
             cls.fields, **kwargs)
         query_str, binding = cls.get_query(**validated_kwargs)
         log.debug(f"running query :: {query_str}")
         return cls.transform_output(
             session.read_transaction(cls.transaction, query_str,
                                      **binding))
def main():
    from neo4j import Driver
    # from neo4j.bolt.diagnostics import watch
    # watch("neobolt")
    with Driver("bolt://", auth=("neo4j", "password")) as dx:
        p = dx.pipeline(flush_every=1024)
        pusher = Pusher(p)
        puller = Puller(p)
        try:
            pusher.start()
            puller.start()
            while True:
                print("sent %d, received %d, backlog %d" % (pusher.count, puller.count, pusher.count - puller.count))
                sleep(1)
        except KeyboardInterrupt:
            pusher.running = False
            pusher.join()
            puller.running = False
            puller.join()
示例#9
0
def set_infected_from(driver: Driver, chat_id: int, referrer: str) -> str:
    '''Create an infected in the DB if doesn't exist.'''

    session: Session
    with driver.session() as session:
        result: str
        try:
            result_st: BoltStatementResult = session.write_transaction(
                transactions.create_node_from, chat_id, referrer)

            if result_st.peek() is None:
                # If the referrer is not valid, just be the patient zero
                result = session.write_transaction(transactions.create_node,
                                                   chat_id)
            else:
                result = result_st.single().value()

        except ConstraintError:
            # The user already exist, just return its referrer
            result = session.read_transaction(transactions.get_referrer,
                                              chat_id)

        return result
示例#10
0
                    #print('pulled', self.count)


    def flatten_once(gen):
        for batch in gen:
            #print('flattening', batch)
            if batch is None:
                print(f"None batch!?")
            else:
                for i in batch:
                    #print(i)
                    yield i

    # from neobolt.diagnostics import watch
    # watch("neobolt")
    with Driver(fromDriver[0], auth=fromDriver[1]) as fm:
        with Driver(toDriver[0], auth=toDriver[1], encrypted=True) as to:
            def get_gen(self):
                while self.running:
                    yield self.queue.get()


            fm_p = fm.pipeline(flush_every=0)
            fm_pusher = Pusher(fm_p, ((x, None) for x in [
                'CALL apoc.export.cypher.all(null, {format:"plain",streamStatements:true,batchSize:5000}) YIELD cypherStatements RETURN cypherStatements']))
            to_p = to.pipeline(flush_every=0)
            to_p.push("MATCH (n) DETACH DELETE n")
            for x in to_p.pull():
                print("delete", x)

            to_p._flush_every= 10000
示例#11
0
def driver(uri, auth):
    driver = Driver(uri, auth=auth)
    try:
        yield driver
    finally:
        driver.close()
示例#12
0
def test_should_fail_on_incorrect_password(uri):
    with raises(AuthError):
        with Driver(uri, auth=("neo4j", "wrong-password")) as driver:
            with driver.session() as session:
                _ = session.run("RETURN 1")
示例#13
0
def test_custom_ca_not_implemented(uri, auth):
    with raises(NotImplementedError):
        _ = Driver(uri, auth=auth, encrypted=True,
                   trust=TRUST_CUSTOM_CA_SIGNED_CERTIFICATES)
示例#14
0
def test_fail_nicely_when_using_http_port(service):
    address = service.addresses[0]
    uri = "bolt://{}:7474".format(address[0])
    with raises(ServiceUnavailable):
        _ = Driver(uri, auth=service.auth, encrypted=False)
示例#15
0
def test_invalid_url_scheme(service):
    address = service.addresses[0]
    uri = "x://{}:{}".format(address[0], address[1])
    with raises(ValueError):
        _ = Driver(uri, auth=service.auth)