Skip to content

Commit

Permalink
test: fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Oct 20, 2024
1 parent fab9076 commit 4a82ef2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
51 changes: 51 additions & 0 deletions test/Codibre.GrpcSqlProxy.Test/GrpcSqlProxyClientBatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,55 @@ FROM TB_PEDIDO
})
));
}

[Fact]
public async Task Should_Run_PrepareBatchQuery_WithAsyncCallback()
{
// Arrange
var server = await TestServer.Get();
var client = new GrpcSqlProxyClient(
new SqlProxyClientOptions(
server.Url,
server.Config.GetConnectionString("SqlConnection") ?? throw new Exception("No connection string")
)
{
Compress = false
}
);

// Act
using var channel = client.CreateChannel();
List<(int, TB_PEDIDO)> list = [];
var pars = GetList().ToArray();
await channel.Batch.RunInTransaction(async () =>
{
channel.Batch.AddNoResultScript($"DELETE FROM TB_PEDIDO");
foreach (var i in pars)
{
await channel.Batch.AddTransactionScript($"INSERT INTO TB_PEDIDO VALUES ({i})");
}
await channel.Batch.FlushTransaction();
await pars.PrepareQueryBatch(channel.Batch, (i, b) =>
{
return new ValueTask<IResultHook<TB_PEDIDO>>(b.QueryFirstHook<TB_PEDIDO>(@$"SELECT *
FROM TB_PEDIDO
WHERE CD_PEDIDO = {i}"));
})
.ForEachAwaitAsync(x =>
{
list.Add((x.Key, x.Value.Result));
return Task.CompletedTask;
});
channel.Batch.CancelTransaction();
});

// Assert
list.Count.Should().Be(pars.Length);
list.Should().BeEquivalentTo(pars.Select(
x => (x, new TB_PEDIDO
{
CD_PEDIDO = x
})
));
}
}
7 changes: 6 additions & 1 deletion test/Codibre.GrpcSqlProxy.Test/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TB_PESSOA
public int CD_PESSOA { get; set; }
}

public class TestServer
public class TestServer : IDisposable
{
private readonly WebApplication _app;
private static Task _run;

Check warning on line 27 in test/Codibre.GrpcSqlProxy.Test/TestServer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_run' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 27 in test/Codibre.GrpcSqlProxy.Test/TestServer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_run' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 27 in test/Codibre.GrpcSqlProxy.Test/TestServer.cs

View workflow job for this annotation

GitHub Actions / semantic

Non-nullable field '_run' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 27 in test/Codibre.GrpcSqlProxy.Test/TestServer.cs

View workflow job for this annotation

GitHub Actions / semantic

Non-nullable field '_run' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
Expand Down Expand Up @@ -52,4 +52,9 @@ public static async Task<TestServer> Get()
await _run;
return _instance;
}

public void Dispose()
{
_app.StopAsync().GetAwaiter().GetResult();
}
}

0 comments on commit 4a82ef2

Please sign in to comment.