如何模拟gremlin服务器或创建内存图以进行单元测试? [英] How to mock a gremlin server or create in-memory graph for unit testing?

查看:157
本文介绍了如何模拟gremlin服务器或创建内存图以进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gremlin.Net,并且想为查询数据库的功能编写单元测试.我希望查询能够在模拟数据上运行,以查看结果是否已正确转换为所需的格式,尤其是对于具有dynamic类型的Traverser.Object.

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.

有什么办法可以做到这一点?也许可以用代码运行服务器,或者拥有一个内存中的graph实例.

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

这是一个玩具示例:

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;

在上面的示例中,leafInforootInfo具有dynamic类型,因此针对测试图运行此功能可以断言这些变量已正确使用,例如leafInfo["some_property"][0]可分配给MyFormat.LeafProperty

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

推荐答案

我不确定在.NET应用程序中模拟Gremlin Server的好方法.我想您可以尝试编写IRemoteConnection的某种实现并将其提供为:

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.

我会说,.NET(和其他非JVM语言)开发人员经常将Gremlin Server与Docker结合使用.在单元测试开始之前启动它(使用TinkerGraph进行快速测试,但如果可能的话,可能不是完美的测试),并在测试结束时将其关闭.我想这不是经典意义上的真正的单元测试",但是它可以运行并且可以很快.我们在TinkerPop本身用于.NET以及其他GLV的单元测试采用了这种方法.我们与Maven集成并配置Gremlin Server以启动/停止其标准生命周期.该配置的主要部分可以找到这里.

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.

如果我们对.NET这样的GLV有更好的测试支持,那就太好了,但是当我回答完这个问题时,我想知道IRemoteConnection是否无法使事情变得整洁.也许,如果有更好的测试支持,它将以某种方式从该接口中出来.

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.

这篇关于如何模拟gremlin服务器或创建内存图以进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆