forked from surrealdb/surrealdb.net
-
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.
✨ add method to create multiple records at once (surrealdb#117)
- Loading branch information
Showing
8 changed files
with
258 additions
and
9 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,220 @@ | ||
using System.Text; | ||
|
||
namespace SurrealDb.Net.Tests; | ||
|
||
public class InsertTests | ||
{ | ||
[Theory] | ||
[InlineData("Endpoint=mem://")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=CBOR")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=CBOR")] | ||
public async Task ShouldInsertZeroRecord(string connectionString) | ||
{ | ||
IEnumerable<Empty>? list = null; | ||
IEnumerable<Empty>? 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 = File.ReadAllText(filePath, Encoding.UTF8); | ||
string query = fileContent; | ||
using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
await client.RawQuery(query); | ||
result = await client.Insert("empty", Enumerable.Empty<Empty>()); | ||
list = await client.Select<Empty>("empty"); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
list.Should().NotBeNull().And.HaveCount(0); | ||
|
||
result.Should().NotBeNull().And.HaveCount(0); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=mem://")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=CBOR")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=CBOR")] | ||
public async Task ShouldInsertPostsWithAutogeneratedId(string connectionString) | ||
{ | ||
IEnumerable<Post>? list = null; | ||
IEnumerable<Post>? 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 = File.ReadAllText(filePath, Encoding.UTF8); | ||
string query = fileContent; | ||
using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
await client.RawQuery(query); | ||
var post = new Post | ||
{ | ||
Title = "A new article", | ||
Content = "This is a new article created using the .NET SDK" | ||
}; | ||
result = await client.Insert("post", new[] { post }); | ||
list = await client.Select<Post>("post"); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
list.Should().NotBeNull().And.HaveCount(3); | ||
|
||
result.Should().NotBeNull().And.HaveCount(1); | ||
|
||
var post = result!.First(); | ||
post!.Title.Should().Be("A new article"); | ||
post!.Content.Should().Be("This is a new article created using the .NET SDK"); | ||
post!.CreatedAt.Should().NotBeNull(); | ||
post!.Status.Should().Be("DRAFT"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=mem://")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=CBOR")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=CBOR")] | ||
public async Task ShouldInsertPostsWithPredefinedId(string connectionString) | ||
{ | ||
IEnumerable<Post>? list = null; | ||
IEnumerable<Post>? 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 = File.ReadAllText(filePath, Encoding.UTF8); | ||
string query = fileContent; | ||
using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
await client.RawQuery(query); | ||
var post = new Post | ||
{ | ||
Id = new Thing("post", "another"), | ||
Title = "A new article", | ||
Content = "This is a new article created using the .NET SDK" | ||
}; | ||
result = await client.Insert("post", new[] { post }); | ||
list = await client.Select<Post>("post"); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
list.Should().NotBeNull().And.HaveCount(3); | ||
|
||
result.Should().NotBeNull().And.HaveCount(1); | ||
|
||
var post = result!.First(); | ||
post!.Title.Should().Be("A new article"); | ||
post!.Content.Should().Be("This is a new article created using the .NET SDK"); | ||
post!.CreatedAt.Should().NotBeNull(); | ||
post!.Status.Should().Be("DRAFT"); | ||
|
||
var anotherPost = list!.First(r => r.Id!.Id == "another"); | ||
|
||
anotherPost.Should().NotBeNull(); | ||
anotherPost!.Title.Should().Be("A new article"); | ||
anotherPost!.Content.Should().Be("This is a new article created using the .NET SDK"); | ||
anotherPost!.CreatedAt.Should().NotBeNull(); | ||
anotherPost!.Status.Should().Be("DRAFT"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Endpoint=mem://")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=http://127.0.0.1:8000;User=root;Pass=root;Serialization=CBOR")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=JSON")] | ||
[InlineData("Endpoint=ws://127.0.0.1:8000/rpc;User=root;Pass=root;Serialization=CBOR")] | ||
public async Task ShouldInsertMultiplePosts(string connectionString) | ||
{ | ||
IEnumerable<Post>? result = null; | ||
IEnumerable<Post>? list = 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 = File.ReadAllText(filePath, Encoding.UTF8); | ||
string query = fileContent; | ||
using var client = surrealDbClientGenerator.Create(connectionString); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
await client.RawQuery(query); | ||
var posts = new List<Post> | ||
{ | ||
new Post | ||
{ | ||
Id = new Thing("post", "A"), | ||
Title = "An article", | ||
Content = "This is a new article" | ||
}, | ||
new Post | ||
{ | ||
Id = new Thing("post", "B"), | ||
Title = "An article", | ||
Content = "This is a new article" | ||
}, | ||
new Post | ||
{ | ||
Id = new Thing("post", "C"), | ||
Title = "An article", | ||
Content = "This is a new article" | ||
} | ||
}; | ||
result = await client.Insert("post", posts); | ||
list = await client.Select<Post>("post"); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
result.Should().NotBeNull().And.HaveCount(3); | ||
list.Should().NotBeNull().And.HaveCount(5); | ||
} | ||
} |
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