Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement integration test for Execute #22

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions RqliteDotnet.IntegrationTest/RqliteClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
using System.Net.Http.Json;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Text.RegularExpressions;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using NUnit.Framework;

namespace RqliteDotnet.IntegrationTest;

public class RqliteClientTests
{
private const int Port = 4001;

[Test]
public async Task RqliteClientPing_Works()
private IContainer _container;
private HttpClient _httpClient;

[SetUp]
public async Task Setup()
{
var container = new ContainerBuilder()
.WithImage("rqlite/rqlite:8.32.7")
_container = new ContainerBuilder()
.WithImage("rqlite/rqlite:8.36.1")
.WithPortBinding(Port, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(Port)))
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(Port))
.UntilMessageIsLogged("is now Leader",o => o.WithTimeout(TimeSpan.FromSeconds(20))))
.Build();

await container.StartAsync().ConfigureAwait(false);

var url = $"http://{container.Hostname}:{container.GetMappedPublicPort(Port)}";
await _container.StartAsync().ConfigureAwait(false);
}

[Test]
public async Task RqliteClientPing_Works()
{
var url = $"http://{_container.Hostname}:{_container.GetMappedPublicPort(Port)}";
var client = new RqliteClient(url);

var version = await client.Ping();

Assert.That(version, Is.Not.Empty);
Assert.That(Regex.IsMatch(version, @"v\d+.\d+\.*")); //v8.10.1
}

[Test]
public async Task RqliteClient_CanGetInsertData()
{
var url = $"http://{_container.Hostname}:{_container.GetMappedPublicPort(Port)}";
_httpClient = new HttpClient() { BaseAddress = new Uri(url) };
var content =
new StringContent("[ \"CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT, age INTEGER)\" ]", Encoding.UTF8, "application/json");
var r = await _httpClient.PostAsync("/db/execute?timings", content);

Assert.That(r.IsSuccessStatusCode);

var client = new RqliteClient(url);


var result = await client.Execute("insert into foo(id, name, age) VALUES(1,\\\"john\\\", 42)");
Assert.That(result!.Results!.Count, Is.EqualTo(1));
Assert.That(result!.Results[0].RowsAffected, Is.EqualTo(1));
}
}
7 changes: 6 additions & 1 deletion RqliteDotnet/RqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RqliteDotnet;

public class RqliteClient : IRqliteClient
public class RqliteClient : IRqliteClient, IDisposable
{
private readonly HttpClient _httpClient;

Expand Down Expand Up @@ -147,4 +147,9 @@ private string GetParameters(DbFlag? flags)

return x;
}

public void Dispose()
{
_httpClient.Dispose();
}
}
Loading