Skip to content

Commit

Permalink
add PgArrayToMany ObservableCollection test #1145
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jun 6, 2022
1 parent e092823 commit e900f83
Show file tree
Hide file tree
Showing 8 changed files with 1,010 additions and 51 deletions.
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,75 +1,115 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Xunit;

namespace FreeSql.Tests.PostgreSQL
namespace FreeSql.Tests.PostgreSQL.PgArrayToMany
{
public class PostgreSQLPgArrayToManyTest
public class LazyLoadingTest
{

[Table(Name = "pgarray_tomany_user_lazyloading")]
public class UserLazyLoading
public class User
{
[Column(IsPrimary = true)]
public int UserId { get; set; }
public int[] RoleIds { get; set; }
public string UserName { get; set; }

[Navigate(nameof(RoleIds))]
public virtual List<RoleLazyLoading> Roles { get; set; }
public virtual List<Role> Roles { get; set; }
}

[Table(Name = "pgarray_tomany_role_lazyloading")]
public class RoleLazyLoading
public class Role
{
[Column(IsPrimary = true)]
public int RoleId { get; set; }
public string RoleName { get; set; }

[Navigate(nameof(UserLazyLoading.RoleIds))]
public virtual List<UserLazyLoading> Users { get; set; }
[Navigate(nameof(User.RoleIds))]
public virtual List<User> Users { get; set; }
}

[Fact]
public void LazyLoading()
{
var fsql = g.pgsql;
fsql.Delete<UserLazyLoading>().Where("1=1").ExecuteAffrows();
fsql.Delete<RoleLazyLoading>().Where("1=1").ExecuteAffrows();
fsql.Delete<User>().Where("1=1").ExecuteAffrows();
fsql.Delete<Role>().Where("1=1").ExecuteAffrows();

var roles = new[]
{
new RoleLazyLoading { RoleId = 1, RoleName = "role1" },
new RoleLazyLoading { RoleId = 2, RoleName = "role2" },
new RoleLazyLoading { RoleId = 3, RoleName = "role3" }
new Role { RoleId = 1, RoleName = "role1" },
new Role { RoleId = 2, RoleName = "role2" },
new Role { RoleId = 3, RoleName = "role3" }
};
Assert.Equal(3, fsql.Insert(roles).ExecuteAffrows());
var users = new[]
{
new UserLazyLoading { UserId = 11, RoleIds = new [] { 1,2 }, UserName = "user1" },
new UserLazyLoading { UserId = 12, RoleIds = new [] { 1,2,3 }, UserName = "user2" },
new UserLazyLoading { UserId = 13, RoleIds = new [] { 1,3 }, UserName = "user3" },
new UserLazyLoading { UserId = 14, RoleIds = new [] { 3,2,1 }, UserName = "user4" },
new UserLazyLoading { UserId = 15, RoleIds = null, UserName = "user5" },
new UserLazyLoading { UserId = 16, RoleIds = new int[0], UserName = "user6" },
new User { UserId = 11, RoleIds = new [] { 1,2 }, UserName = "user1" },
new User { UserId = 12, RoleIds = new [] { 1,2,3 }, UserName = "user2" },
new User { UserId = 13, RoleIds = new [] { 1,3 }, UserName = "user3" },
new User { UserId = 14, RoleIds = new [] { 3,2,1 }, UserName = "user4" },
new User { UserId = 15, RoleIds = null, UserName = "user5" },
new User { UserId = 16, RoleIds = new int[0], UserName = "user6" },
};
Assert.Equal(6, fsql.Insert(users).ExecuteAffrows());

var users4 = fsql.Select<UserLazyLoading>().IncludeMany(a => a.Roles).ToList(a => new
var users5Select = fsql.Select<User>().Where(a => a.Roles.Any(b => b.RoleName == "role1"));
Assert.Equal(@"SELECT a.""userid"", a.""roleids"", a.""username""
FROM ""pgarray_tomany_user_lazyloading"" a
WHERE (exists(SELECT 1
FROM ""pgarray_tomany_role_lazyloading"" b
WHERE (a.""roleids"" @> ARRAY[b.""roleid""]::int4[]) AND (b.""rolename"" = 'role1')
limit 1))", users5Select.ToSql());
var users5 = users5Select.ToList();
Assert.Equal(4, users5.Count);
users5Select = fsql.Select<User>().Where(a => a.Roles.AsSelect().Any(b => b.RoleName == "role1"));
Assert.Equal(@"SELECT a.""userid"", a.""roleids"", a.""username""
FROM ""pgarray_tomany_user_lazyloading"" a
WHERE (exists(SELECT 1
FROM ""pgarray_tomany_role_lazyloading"" b
WHERE (b.""rolename"" = 'role1') AND (a.""roleids"" @> ARRAY[b.""roleid""]::int4[])
limit 1))", users5Select.ToSql());
users5 = users5Select.ToList();
Assert.Equal(4, users5.Count);

var roles5Select = fsql.Select<Role>().Where(a => a.Users.Any(b => b.UserName == "user1"));
Assert.Equal(@"SELECT a.""roleid"", a.""rolename""
FROM ""pgarray_tomany_role_lazyloading"" a
WHERE (exists(SELECT 1
FROM ""pgarray_tomany_user_lazyloading"" b
WHERE (b.""roleids"" @> ARRAY[a.""roleid""]::int4[]) AND (b.""username"" = 'user1')
limit 1))", roles5Select.ToSql());
var roles5 = roles5Select.ToList();
Assert.Equal(2, roles5.Count);
roles5Select = fsql.Select<Role>().Where(a => a.Users.AsSelect().Any(b => b.UserName == "user1"));
Assert.Equal(@"SELECT a.""roleid"", a.""rolename""
FROM ""pgarray_tomany_role_lazyloading"" a
WHERE (exists(SELECT 1
FROM ""pgarray_tomany_user_lazyloading"" b
WHERE (b.""username"" = 'user1') AND (b.""roleids"" @> ARRAY[a.""roleid""]::int4[])
limit 1))", roles5Select.ToSql());
roles5 = roles5Select.ToList();
Assert.Equal(2, roles5.Count);


var users4 = fsql.Select<User>().IncludeMany(a => a.Roles).ToList(a => new
{
user = a, roles = a.Roles
});
var roles4 = fsql.Select<RoleLazyLoading>().IncludeMany(a => a.Users).ToList(a => new
var roles4 = fsql.Select<Role>().IncludeMany(a => a.Users).ToList(a => new
{
role = a,
users = a.Users
});


var users3 = fsql.Select<UserLazyLoading>().IncludeMany(a => a.Roles).ToList();
var users3 = fsql.Select<User>().IncludeMany(a => a.Roles).ToList();
Assert.Equal(6, users3.Count);
var users2 = users3;
Assert.Equal(11, users2[0].UserId);
Expand Down Expand Up @@ -122,7 +162,7 @@ public void LazyLoading()
Assert.Null(users3[4].Roles);
Assert.Empty(users3[5].Roles);

var roles3 = fsql.Select<RoleLazyLoading>().IncludeMany(a => a.Users).ToList();
var roles3 = fsql.Select<Role>().IncludeMany(a => a.Users).ToList();
Assert.Equal(3, roles3.Count);
roles2 = roles3;
Assert.Equal(1, roles2[0].RoleId);
Expand Down Expand Up @@ -171,8 +211,8 @@ public void LazyLoading()
Assert.Equal("user4", users2[2].UserName);
Assert.Equal("3,2,1", string.Join(",", users2[2].RoleIds));

var role = fsql.Select<RoleLazyLoading>().Where(a => a.RoleId == 1).First();
Assert.IsNotType<RoleLazyLoading>(role);
var role = fsql.Select<Role>().Where(a => a.RoleId == 1).First();
Assert.IsNotType<Role>(role);

users2 = role.Users;
Assert.Equal(4, users2.Count);
Expand Down Expand Up @@ -221,8 +261,8 @@ public void LazyLoading()
Assert.Equal(1, roles2[2].RoleId);
Assert.Equal("role1", roles2[2].RoleName);

var user = fsql.Select<UserLazyLoading>().Where(a => a.UserId == 11).First();
Assert.IsNotType<UserLazyLoading>(user);
var user = fsql.Select<User>().Where(a => a.UserId == 11).First();
Assert.IsNotType<User>(user);

roles2 = user.Roles;
Assert.Equal(2, roles2.Count);
Expand Down
Loading

0 comments on commit e900f83

Please sign in to comment.