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_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)
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
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
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 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