Set up Mock Gremlin repository with Moq for unit testing #333
Unanswered
yongkansage
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Not sure if you solved this or not but, what I resorted to doing was setting up integration tests and testing against either a local graph or an instance in the cloud. |
Beta Was this translation helpful? Give feedback.
3 replies
-
We use a common base as our source, using what I saw in the repo for testing with public abstract class TestGraphSource : GraphSource, IGremlinQueryExecutor
{
protected override IGremlinQuerySource ConfigureSource(IConfigurableGremlinQuerySource source)
=> source.ConfigureEnvironment(env => env.UseExecutor(this));
public virtual IAsyncEnumerable<object> Execute(object serializedQuery, IGremlinQueryEnvironment environment)
=> Array.Empty<object>().ToAsyncEnumerable();
} Couple public static class BytecodeExtensions
{
public static List<Instruction> FindAddV(this object query, string label)
{
var instructions = query.As<Bytecode>().StepInstructions;
var index = instructions.FindIndex(step => step.IsAddV(label));
if (index < 0)
{
return new List<Instruction>();
}
return instructions.Skip(index).ToList();
}
public static bool HasAddETo(this IEnumerable<Instruction> instructions, string edge, string to)
=> instructions.Any(step => step.IsAddE(edge))
&& instructions.Any(step => step.IsTo(to));
}
public static class InstructionExtensions
{
public static bool IsAddV(this Instruction instruction, string label)
=> instruction.OperatorName == "addV" && instruction.Arguments[0] == label;
public static bool IsAddE(this Instruction instruction, string label)
=> instruction.OperatorName == "addE" && instruction.Arguments[0] == label;
public static bool IsTo(this Instruction instruction, string label)
=> instruction.OperatorName == "to" && instruction.Arguments[0].StepInstructions[1].Arguments[0] == label;
} Example usage: public class GetProfileHandlerTests
{
private readonly GetProfileHandler _sut;
private readonly TestGraphSource _graph = Substitute.ForPartsOf<TestGraphSource>();
private readonly Fixture _fixture = new();
private readonly GetProfile _request;
public GetProfileHandlerTests()
{
_sut = new GetProfileHandler(_graph);
_request = _fixture.Create<GetProfile>();
}
[Fact]
public async Task Handle_ShouldReturnNull_WhenVertexDoesNotExist()
{
var result = await _sut.Handle(_request, default);
result.Should().BeNull();
}
[Fact]
public async Task Handle_ShouldReturnProfile_WhenVertexExist()
{
var profile = new[] { _fixture.Create<Profile>() };
_graph.Execute(Arg.Any<object>(), Arg.Any<IGremlinQueryEnvironment>())
.Returns(profile.ToAsyncEnumerable());
var result = await _sut.Handle(_request, default);
result.Should().NotBeNull();
}
} Example of verifying instructions: [Fact]
public async Task Handle_ShouldAddProfileToGraph_WhenSucceeded()
{
await _sut.Handle(_request, default);
_graph.Received(1).Execute(
Arg.Is<object>(query =>
query.As<Bytecode>().StepInstructions.Any(step => step.IsAddV("Profile"))),
Arg.Any<IGremlinQueryEnvironment>());
}
[Fact]
public async Task Handle_ShouldAddEdgeToGraph_WhenVertexIsAdded()
{
await _sut.Handle(_request, default);
_graph.Received(1).Execute(
Arg.Is<object>(query => query.FindAddV("Profile").HasAddETo("ManagedBy", "Profile")),
Arg.Any<IGremlinQueryEnvironment>());
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Any suggestions or examples are welcome
Beta Was this translation helpful? Give feedback.
All reactions