-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query): Support querying cache entries using patterns with * and…
… ? as wildcards BREAKING CHANGE: MongoDB no longer supports regex queries for Flush that does not contian a wildcard pattern (? or *)
- Loading branch information
Showing
8 changed files
with
257 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Text.Json; | ||
|
||
namespace PersistedCache.Helpers; | ||
|
||
public class JsonHelper | ||
{ | ||
public static bool TryDeserialize<T>(string json, out T? result, JsonSerializerOptions? options = null) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrEmpty(json)) | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
|
||
result = JsonSerializer.Deserialize<T?>(json, options); | ||
return result != null; | ||
} | ||
catch | ||
{ | ||
result = default; | ||
return false; | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
src/PersistedCache/Validators.cs → src/PersistedCache/Helpers/Validators.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/PersistedCache.Tests/Helpers/CompletelyDifferentObject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace PersistedCache.Tests.Helpers; | ||
|
||
public class CompletelyDifferentObject | ||
{ | ||
public List<string> TestValue { get; set; } = new List<string>(); | ||
public int TestNumber { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System.Threading.Tasks; | ||
using AutoFixture; | ||
using FluentAssertions; | ||
using PersistedCache.Tests.Common; | ||
using PersistedCache.Tests.Fixtures; | ||
using PersistedCache.Tests.Helpers; | ||
using Xunit; | ||
|
||
namespace PersistedCache.Tests; | ||
|
||
public abstract class QueryTests : BaseTest | ||
{ | ||
private readonly IPersistedCache _cache; | ||
private readonly Fixture _fixture = new Fixture(); | ||
|
||
public QueryTests(IPersistedCache cache) : base(cache) | ||
{ | ||
_cache = cache; | ||
} | ||
|
||
[Theory] | ||
[InlineData("key*")] | ||
[InlineData("key?")] | ||
public void Query_WithValidPattern_ReturnsExpectedValues(string pattern) | ||
{ | ||
// Arrange | ||
_cache.Set("key1", _fixture.Create<RandomObject>(), Expire.Never); | ||
_cache.Set("key2", _fixture.Create<RandomObject>(), Expire.Never); | ||
_cache.Set("key3", _fixture.Create<RandomObject>(), Expire.Never); | ||
|
||
// Act | ||
var result = _cache.Query<RandomObject>(pattern); | ||
|
||
// Assert | ||
result.Should().HaveCount(3); | ||
} | ||
|
||
[Fact] | ||
public void Query_WithNoMatchingPattern_ReturnsEmptyCollection() | ||
{ | ||
// Arrange | ||
_cache.Set("key1", _fixture.Create<RandomObject>(), Expire.Never); | ||
_cache.Set("key2", _fixture.Create<RandomObject>(), Expire.Never); | ||
|
||
// Act | ||
var result = _cache.Query<RandomObject>("keyyyy*"); | ||
|
||
// Assert | ||
result.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void Query_WithAstrixPattern_ReturnsAllValues() | ||
{ | ||
// Arrange | ||
_cache.Set("key1", _fixture.Create<RandomObject>(), Expire.Never); | ||
_cache.Set("key2", _fixture.Create<RandomObject>(), Expire.Never); | ||
_cache.Set("key3", _fixture.Create<RandomObject>(), Expire.Never); | ||
|
||
// Act | ||
var result = _cache.Query<RandomObject>("*"); | ||
|
||
// Assert | ||
result.Should().HaveCount(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Query_WhenExpiredValues_ReturnsOnlyNonExpiredValues() | ||
{ | ||
// Arrange | ||
await _cache.SetAsync("key1", _fixture.Create<RandomObject>(), Expire.InMilliseconds(1)); | ||
await _cache.SetAsync("key2", _fixture.Create<RandomObject>(), Expire.Never); | ||
await _cache.SetAsync("key3", _fixture.Create<RandomObject>(), Expire.InMilliseconds(1)); | ||
|
||
// Act | ||
await Task.Delay(2); | ||
var result = _cache.QueryAsync<RandomObject>("*"); | ||
|
||
// Assert | ||
(await result).Should().HaveCount(1); | ||
} | ||
} | ||
|
||
[Collection(nameof(MySqlFixture))] | ||
public class MySqlQueryTestsExecutor : QueryTests | ||
{ | ||
public MySqlQueryTestsExecutor(MySqlFixture fixture) : base(fixture.PersistedCache) | ||
{ | ||
} | ||
} | ||
|
||
[Collection(nameof(PostgreSqlFixture))] | ||
public class PostgreSqlQueryTestsExecutor : QueryTests | ||
{ | ||
public PostgreSqlQueryTestsExecutor(PostgreSqlFixture fixture) : base(fixture.PersistedCache) | ||
{ | ||
} | ||
} | ||
|
||
// [Collection(nameof(SqlServerFixture))] | ||
// public class SqlServerQueryTestsExecutor : QueryTests | ||
// { | ||
// public SqlServerQueryTestsExecutor(SqlServerFixture fixture) : base(fixture.PersistedCache) | ||
// { | ||
// } | ||
// } | ||
|
||
[Collection(nameof(FileSystemFixture))] | ||
public class FileSystemQueryTestsExecutor : QueryTests | ||
{ | ||
public FileSystemQueryTestsExecutor(FileSystemFixture fixture) : base(fixture.PersistedCache) | ||
{ | ||
} | ||
} | ||
|
||
[Collection(nameof(SqliteFixture))] | ||
public class SqliteQueryTestsExecutor : QueryTests | ||
{ | ||
public SqliteQueryTestsExecutor(SqliteFixture fixture) : base(fixture.PersistedCache) | ||
{ | ||
} | ||
} | ||
|
||
[Collection(nameof(MongoDbFixture))] | ||
public class MongoDbQueryTestsExecutor : QueryTests | ||
{ | ||
public MongoDbQueryTestsExecutor(MongoDbFixture fixture) : base(fixture.PersistedCache) | ||
{ | ||
} | ||
} |