-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
390 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace SurrealDb.Net.Tests; | ||
|
||
public class ExportTests | ||
{ | ||
private readonly VerifySettings _verifySettings = new(); | ||
|
||
public ExportTests() | ||
{ | ||
_verifySettings.DisableRequireUniquePrefix(); | ||
_verifySettings.UseDirectory("Snapshots"); | ||
|
||
// 💡 "ScrubInlineDateTimes" won't work as DateTime cannot be parsed with more than 7 seconds fraction units | ||
_verifySettings.AddScrubber( | ||
(sb, counter) => | ||
{ | ||
const string pattern = @"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}Z"; | ||
var matches = Regex.Matches(sb.ToString(), pattern); | ||
foreach (Match match in matches) | ||
{ | ||
string value = match.Value; | ||
var date = DateTime.Parse(value[..^4] + "Z"); | ||
int id = counter.Next(date); | ||
string name = $"DateTime_{id}"; | ||
sb.Replace(value, name); | ||
} | ||
} | ||
); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=mem://")] | ||
[InlineData("Endpoint=rocksdb://")] | ||
[InlineData("Endpoint=surrealkv://")] | ||
public async Task NotSupportedForEmbeddedMode(string connectionString) | ||
{ | ||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
await using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
await client.Export(); | ||
}; | ||
|
||
await func.Should().ThrowAsync<NotImplementedException>(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root")] | ||
public async Task ShouldExportEmptyDatabaseWithDefaultOptions(string connectionString) | ||
{ | ||
string? result = null; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
await using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
result = await client.Export(); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
await Verify(result, _verifySettings); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root")] | ||
public async Task ShouldExportFullDatabaseWithDefaultOptions(string connectionString) | ||
{ | ||
string? result = null; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
string filePath = Path.Combine( | ||
AppDomain.CurrentDomain.BaseDirectory, | ||
"Schemas/post.surql" | ||
); | ||
string fileContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8); | ||
string query = fileContent; | ||
await using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
(await client.RawQuery(query)).EnsureAllOks(); | ||
await client.Delete("post"); | ||
var post = new Post | ||
{ | ||
Id = ("post", "dotnet-123456"), | ||
Title = "A new article", | ||
Content = "This is a new article created using the .NET SDK" | ||
}; | ||
await client.Create(post); | ||
result = await client.Export(); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
await Verify(result, _verifySettings); //.AddScrubber(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
....Net.Tests/Snapshots/ExportTests.ShouldExportEmptyDatabaseWithDefaultOptions.verified.txt
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,6 @@ | ||
-- ------------------------------ | ||
-- OPTION | ||
-- ------------------------------ | ||
|
||
OPTION IMPORT; | ||
|
23 changes: 23 additions & 0 deletions
23
...b.Net.Tests/Snapshots/ExportTests.ShouldExportFullDatabaseWithDefaultOptions.verified.txt
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,23 @@ | ||
-- ------------------------------ | ||
-- OPTION | ||
-- ------------------------------ | ||
|
||
OPTION IMPORT; | ||
|
||
-- ------------------------------ | ||
-- TABLE: post | ||
-- ------------------------------ | ||
|
||
DEFINE TABLE post TYPE NORMAL SCHEMAFULL PERMISSIONS FOR select, create, update, delete WHERE $auth.id != NONE; | ||
|
||
DEFINE FIELD content ON post TYPE string PERMISSIONS FULL; | ||
DEFINE FIELD created_at ON post TYPE datetime DEFAULT time::now() PERMISSIONS FULL; | ||
DEFINE FIELD status ON post TYPE string DEFAULT 'DRAFT' ASSERT $value INSIDE ['DRAFT', 'PUBLISHED'] PERMISSIONS FULL; | ||
DEFINE FIELD title ON post TYPE string PERMISSIONS FULL; | ||
|
||
-- ------------------------------ | ||
-- TABLE DATA: post | ||
-- ------------------------------ | ||
|
||
INSERT [ { content: 'This is a new article created using the .NET SDK', created_at: d'DateTime_1', id: post:⟨dotnet-123456⟩, status: 'DRAFT', title: 'A new article' } ]; | ||
|
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
Oops, something went wrong.