| [ Web Proxy ] |
| Viewing: https://memgraph.com/docs/custom-query-modules/python/mock-python-api | [Back] [Original] |
The mock Python query module API enables you to develop and test query modules for Memgraph without having to run a Memgraph instance by simulating its behavior. As the mock API is compatible with the Python API, you can add modules developed with it to Memgraph as-is, without modifying the code.
It is implemented in mgp_mock.py, which contains definitions of all
classes and functions provided for developing query module procedures and
functions. The source file is located in the Memgraph installation directory,
inside /usr/include/memgraph.
Because the mock APIs classes and functions are compatible with the corresponding Python API classes and functions, the Python API reference applies, with the following exceptions:
Record class) are printable.UnableToAllocateError, InsufficientBufferError,
OutOfRangeError, KeyAlreadyExistsError, SerializationError and
AuthorizationError).must_abort and check_must_abort).
These methods are used to check whether Memgraph has notified the query
module to abort its execution.ProcCtx and FuncCtx classes take a NetworkX
MultiDiGraph
because thats the data structure the mock API uses for internal graph
representations.The mock Python API uses a graph representation based on the NetworkX MultiDiGraph, which is a directed graph that supports parallel edges (relationships) and custom node/relationship attributes.
All elements of a Memgraph graph are supported by the mock API, with the following rules about representing node labels and relationship types:
"labels" as a
":"-separated string, e.g. the node (n:Actor:Director) has
{"labels": "Actor:Director"}."type".Before importing the mock API, you need to make it visible to the query module,
e.g. by adding the path of mgp_mock.py to PYTHONPATH or copying mgp_mock.py
to the directory containing the module.
The following code block contains an example query procedure and a runner for query procedures:
import mgp_mock as mgp
import networkx as nx
@mgp.read_proc
def example_procedure(context: mgp.ProcCtx) -> mgp.Record(status=str):
return mgp.Record(status="Hello, world!")
graph = nx.MultiDiGraph() # Empty graph
context = mgp.ProcCtx(graph) # Create a context instance
result = example_procedure(context) # Run the procedure
print(result) # Hello, world!As the mock Python API is compatible with the Python query module API, adding a module developed with the mock API to Memgraph is a simple task.
mgp_mock import with import mgp
mgp_mock (or alias) to mgp.| Web Proxy Viewer | New URL | Original Page |