Skip to content

Commit

Permalink
Add read level support for queries and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthrp committed Nov 5, 2024
1 parent c1c71d2 commit f9ddcbd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
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,12 +33,11 @@ public async Task<string> Ping(CancellationToken cancellationToken = default)
}

/// <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 });
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;
}
}

0 comments on commit f9ddcbd

Please sign in to comment.