-
Notifications
You must be signed in to change notification settings - Fork 4
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
376 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace EF.Extensions.PgCopy.Tests.DbContext | ||
{ | ||
[Table("blog", Schema = "public")] | ||
public class Blog | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
[Column("id")] | ||
public int Id { get; set; } | ||
|
||
[Column("url")] public string Url { get; set; } | ||
|
||
[Column("creation_datetime", TypeName = "timestamptz")] | ||
public DateTime CreationDateTime { get; set; } = DateTime.Now; | ||
|
||
public List<Post> Posts { 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,29 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EF.Extensions.PgCopy.Tests.DbContext | ||
{ | ||
public class BloggingContext : Microsoft.EntityFrameworkCore.DbContext | ||
{ | ||
private const string ConnectionString = "Host=localhost;Port=54322;Database=dbtest;Username=db_user;Password=dtpass"; | ||
|
||
public DbSet<Blog> Blogs { get; set; } | ||
public DbSet<Post> Posts { get; set; } | ||
|
||
public BloggingContext(DbContextOptions<BloggingContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public BloggingContext() | ||
{ | ||
|
||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseNpgsql(ConnectionString, opt => | ||
opt.CommandTimeout((int) TimeSpan.FromMinutes(1).TotalSeconds)); | ||
} | ||
} | ||
} |
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; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace EF.Extensions.PgCopy.Tests.DbContext | ||
{ | ||
[Table("post", Schema = "public")] | ||
public class Post | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
[Column("id")] | ||
public int Id { get; set; } | ||
|
||
[Column("title")] public string Title { get; set; } | ||
[Column("content")] public string Content { get; set; } | ||
[Column("blog_id")] public int? BlogId { get; set; } | ||
[Column("post_date")] public DateTime PostDate { get; set; } | ||
[Column("online"), Required] public bool Online { get; set; } | ||
|
||
[Column("creation_datetime", TypeName = "timestamptz")] | ||
public DateTime CreationDateTime { get; set; } = DateTime.Now; | ||
|
||
public Blog Blog { get; set; } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
EF.Extensions.PgCopy.Tests/EF.Extensions.PgCopy.Tests.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\EF.Extensions.PgCopy\EF.Extensions.PgCopy.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using EF.Extensions.PgCopy.Tests.DbContext; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit; | ||
|
||
namespace EF.Extensions.PgCopy.Tests | ||
{ | ||
public class EfCopyTest : IDisposable | ||
{ | ||
public static readonly string ConnectionString = | ||
"Host=localhost;Port=54322;Database=dbtest;Username=db_user;Password=dtpass"; | ||
|
||
private BloggingContext _dbContext; | ||
|
||
public EfCopyTest() | ||
{ | ||
var options = new DbContextOptionsBuilder<BloggingContext>() | ||
.UseNpgsql(ConnectionString, opt => opt.CommandTimeout((int) TimeSpan.FromMinutes(1).TotalSeconds)) | ||
.Options; | ||
|
||
_dbContext = new BloggingContext(options); | ||
} | ||
|
||
[Fact] | ||
public void SaveCopyTest() | ||
{ | ||
var posts = GeneratePosts(); | ||
var blogs = GenerateBlogs(); | ||
_dbContext.Posts.AddRange(posts); | ||
_dbContext.Blogs.AddRange(blogs); | ||
|
||
_dbContext.SaveByCopyChanges(); | ||
|
||
var blogs_count = _dbContext.Blogs.Count(); | ||
var posts_count = _dbContext.Posts.Count(); | ||
|
||
Assert.Equal(blogs.Count(), blogs_count); | ||
Assert.Equal(posts.Count(), posts_count); | ||
} | ||
|
||
[Fact] | ||
public void SaveCopyAsyncTest() | ||
{ | ||
var posts = GeneratePosts(); | ||
var blogs = GenerateBlogs(); | ||
_dbContext.Posts.AddRange(posts); | ||
_dbContext.Blogs.AddRange(blogs); | ||
|
||
_dbContext.SaveByCopyChanges(); | ||
|
||
var blogs_count = _dbContext.Blogs.Count(); | ||
var posts_count = _dbContext.Posts.Count(); | ||
|
||
Assert.Equal(blogs.Count(), blogs_count); | ||
Assert.Equal(posts.Count(), posts_count); | ||
} | ||
|
||
[Fact] | ||
public async Task SaveCopyGraphAsyncTest() | ||
{ | ||
var post = new Post | ||
{ | ||
Online = true, | ||
Content = @"Some Content for unit test", | ||
Title = "Post title", | ||
Blog = new Blog | ||
{ | ||
Url = "http://blog.com" | ||
} | ||
}; | ||
|
||
await _dbContext.Posts.AddAsync(post); | ||
|
||
await _dbContext.SaveByCopyChangesAsync(); | ||
|
||
var blogsCount = await _dbContext.Blogs.CountAsync(); | ||
var postsCount = await _dbContext.Posts.CountAsync(); | ||
|
||
Assert.Equal(1, blogsCount); | ||
Assert.Equal(1, postsCount); | ||
|
||
var expected = await _dbContext.Blogs | ||
.Where(x => Microsoft.EntityFrameworkCore.EF.Functions.Like(x.Url, "%blog.com%")) | ||
.CountAsync(); | ||
|
||
Assert.Equal(1, expected); | ||
} | ||
|
||
private static IEnumerable<Post> GeneratePosts(string title = "default title", long count = 100) | ||
{ | ||
for (var i = 0; i < count; i++) | ||
{ | ||
yield return new Post | ||
{ | ||
Online = i % 2 == 0, | ||
Content = $"Post some content {Guid.NewGuid().ToString()} into {title}-{i}", | ||
PostDate = DateTime.Now, | ||
Title = $"{title}-{i}", | ||
CreationDateTime = DateTime.Now | ||
}; | ||
} | ||
} | ||
|
||
private static IEnumerable<Blog> GenerateBlogs(string url = "default url", long count = 100) | ||
{ | ||
for (var i = 0; i < count; i++) | ||
{ | ||
yield return new Blog | ||
{ | ||
Url = $"https://{url}/{i}", | ||
CreationDateTime = DateTime.Now | ||
}; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_dbContext.Database.ExecuteSqlRaw(@"TRUNCATE TABLE post RESTART IDENTITY CASCADE; | ||
TRUNCATE TABLE blog RESTART IDENTITY CASCADE;"); | ||
_dbContext.Dispose(); | ||
} | ||
} | ||
} |
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.