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

Add read level support for queries and unit tests #15

Merged
merged 1 commit into from
Nov 5, 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
28 changes: 28 additions & 0 deletions RqliteDotnet.Test/UrlBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;

namespace RqliteDotnet.Test;

public class UrlBuilderTests
{
[Test]
public void UrlBuilder_BuildsCorrectUrl()
{
var query = "select * from foo";
var baseUrl = "/db/query?timings";
var url = UrlBuilder.Build(baseUrl, query, ReadLevel.Default);

Assert.That(url.StartsWith(baseUrl));
Assert.That(url, Is.EqualTo("/db/query?timings&q=select%20%2A%20from%20foo"));
}

[Test]
public void UrlBuilder_BuildsCorrectUrlWithReadLevel()
{
var query = "select * from foo";
var baseUrl = "/db/query?timings";
var url = UrlBuilder.Build(baseUrl, query, ReadLevel.Strong);

Assert.That(url.StartsWith(baseUrl));
Assert.That(url, Is.EqualTo("/db/query?timings&q=select%20%2A%20from%20foo&level=strong"));
}
}
3 changes: 2 additions & 1 deletion RqliteDotnet/IRqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public interface IRqliteClient
/// Query DB and return result
/// </summary>
/// <param name="query">Query to run</param>
/// <param name="level"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<QueryResults> Query(string query, CancellationToken cancellationToken);
Task<QueryResults> Query(string query, ReadLevel level, CancellationToken cancellationToken);

/// <summary>
/// Execute command and return result
Expand Down
11 changes: 11 additions & 0 deletions RqliteDotnet/ReadLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace RqliteDotnet;

public enum ReadLevel
{
Default = 1,
Weak,
Linearizable,
Strong,
None,
Auto
}
7 changes: 3 additions & 4 deletions RqliteDotnet/RqliteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
}

/// <inheritdoc />
public async Task<QueryResults> Query(string query, CancellationToken cancellationToken = default)
public async Task<QueryResults> Query(string query, ReadLevel level = ReadLevel.Default, CancellationToken cancellationToken = default)
{
var data = "&q=" + Uri.EscapeDataString(query);
var baseUrl = "/db/query?timings";
var url = UrlBuilder.Build("/db/query?timings", query, level);

var r = await _httpClient.GetAsync($"{baseUrl}&{data}", cancellationToken);
var r = await _httpClient.GetAsync(url, cancellationToken);
var str = await r.Content.ReadAsStringAsync(cancellationToken);

var result = JsonSerializer.Deserialize<QueryResults>(str, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
return result;

Check warning on line 44 in RqliteDotnet/RqliteClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 44 in RqliteDotnet/RqliteClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

/// <inheritdoc />
Expand Down
26 changes: 26 additions & 0 deletions RqliteDotnet/UrlBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace RqliteDotnet;

public static class UrlBuilder
{
public static string Build(string baseUrl, string query, ReadLevel level)
{
var data = "&q=" + Uri.EscapeDataString(query);
var readLevelParam = GetReadLevel(level);

return $"{baseUrl}{data}{readLevelParam}";
}

private static string GetReadLevel(ReadLevel level)
{
var result = level switch

Check warning on line 15 in RqliteDotnet/UrlBuilder.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(RqliteDotnet.ReadLevel)0' is not covered.

Check warning on line 15 in RqliteDotnet/UrlBuilder.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(RqliteDotnet.ReadLevel)0' is not covered.
{
ReadLevel.Default => "",
ReadLevel.Weak => "&level=weak",
ReadLevel.Linearizable => "&level=linearizable",
ReadLevel.Strong => "&level=strong",
ReadLevel.None => "&level=none",
ReadLevel.Auto => "&level=auto"
};
return result;
}
}
Loading