【发布时间】:2020-07-05 12:09:59
【问题描述】:
我正在使用 Gremlin.Net,我想为查询数据库的函数编写单元测试。我希望查询在模拟数据上运行,以查看结果是否正确转换为我想要的格式,尤其是 Traverser.Object 具有 dynamic 类型。
有什么方法可以实现吗?可能在代码中运行服务器或在内存中拥有图形实例。
这是一个玩具示例:
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;
在上面的示例中,leafInfo 和 rootInfo 具有 dynamic 类型,因此针对测试图运行此函数可以断言这些变量已正确使用,例如leafInfo["some_property"][0] 可分配给 MyFormat.LeafProperty
【问题讨论】:
-
这个问题不应该被关闭。熟悉 Gremlin 和 Apache TinkerPop 的人很清楚。事实上,有趣的是,我认为我以前从未见过公开提出过这个问题。