How to mock a gremlin server or create in-memory graph for unit testing?

丶灬走出姿态 提交于 2020-07-19 04:29:23

问题


I'm using Gremlin.Net and I want to write unit tests for the functions that query the database. I want the queries to run on a mock data, to see if the result is being converted correctly to my desired format, especially with Traverser.Object that has a dynamic type.

Is there any way I can achieve this? Maybe run a server in code or have an in-memory instance of graph.

Here's a toy example:

var query = graphTraversalSource.V(leafIds).As("leaf")
                .Emit(__.HasLabel("root"))
                .As("root")
                .Repeat(
                    __.InE("related_to").OtherV()
                    .SimplePath())
                .Dedup()
                .Select<Vertex>("leaf", "root")
                .By(__.ValueMap<string, string>(true));

var res = new List<MyFormat>();
foreach (var t in query.Traversers)
{
    var leafInfo = t.Object["leaf"];
    var rootInfo = t.Object["root"];

    var tmp = new MyFormat
    {
        LeafId = leafInfo[T.Id],
        LeafLabel = leafInfo[T.Label],
        LeafProperty = leafInfo["some_property"][0],
        RootId = rootInfo[T.Id],
        RootProperty = rootInfo["some_other_propert"][0]
    };

    res.Add(tmp);
}

return res;

In the example above, leafInfo and rootInfo have dynamic types, so having this function run against a test graph could assert that those variables are used correctly, e.g. leafInfo["some_property"][0] is assignable to MyFormat.LeafProperty


回答1:


I'm not sure there is a good way to mock Gremlin Server within a .NET application. I suppose you could try to write some kind of implementation of the IRemoteConnection and provide it as:

var g = Traversal().WithRemote(new MyRemoteConnection());

but it might not be trivial to accomplish depending on what you hope to test and how you hope to do it.

I'd say that more often than not, .NET (and other non-JVM language) developers use Gremlin Server with Docker. Start it before your unit tests begin (use TinkerGraph for fast, but perhaps not perfect tests if you can) and shut it down when they end. I suppose that's not a real "unit test" in the classic sense, but it works and can be fast. The unit tests we use in TinkerPop itself for .NET as well as other GLVs takes this very approach. We integrate with Maven and configure Gremlin Server to start/stop through its standard lifecycle. The main part of that configuration can be found here.

It would be nice if we had better testing support for GLVs like .NET, but as I get to the end of this answer I wonder if you can't make something neat happen with IRemoteConnection. Perhaps if there were better testing support it would come out of that interface somehow.



来源:https://stackoverflow.com/questions/60840508/how-to-mock-a-gremlin-server-or-create-in-memory-graph-for-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!