Skip to content

Commit

Permalink
- 修复 Enum -> MapType(string) + GroupBy 解析问题;#1727
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jan 26, 2024
1 parent d32ba64 commit 2598451
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 49 deletions.
102 changes: 56 additions & 46 deletions FreeSql.Tests/FreeSql.Tests/SqlServer/MapType/EnumTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using FreeSql.Tests.DataContext.SqlServer;
using System;
using System.Linq;
using System.Numerics;
using Xunit;

Expand Down Expand Up @@ -31,19 +32,19 @@ class EnumTestMap
[Column(MapType = typeof(int?))]
public ToStringMapEnum? enumnullable_to_int { get; set; }
}
public enum ToStringMapEnum { 中国人, abc, 香港 }
public enum ToStringMapEnum { 中国人, abc, 香港 }
[Fact]
public void EnumToString()
{
//insert
var orm = g.sqlserver;
var item = new EnumTestMap { };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).First();
var find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_string, find.enum_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_string);

item = new EnumTestMap { enum_to_string = ToStringMapEnum.abc };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
Expand All @@ -54,38 +55,47 @@ public void EnumToString()
Assert.Equal(ToStringMapEnum.abc, find.enum_to_string);

//update all
item.enum_to_string = ToStringMapEnum.香港;
item.enum_to_string = ToStringMapEnum.香港;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_string, find.enum_to_string);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_string);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_string);

item.enum_to_string = ToStringMapEnum.中国人;
item.enum_to_string = ToStringMapEnum.中国人;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_string, find.enum_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_string);

//update set
Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_string, ToStringMapEnum.香港).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).First();
Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_string, ToStringMapEnum.香港).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_string);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_string);

Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_string, ToStringMapEnum.abc).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.abc).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(ToStringMapEnum.abc, find.enum_to_string);

var sql = orm.Select<EnumTestMap>().GroupBy(a => a.id).ToSql(g => new {
id = g.Key,
sum1 = g.Sum(g.Value.enum_to_string == ToStringMapEnum.香港 || g.Value.enum_to_string == ToStringMapEnum.abc ? 1 : 0),
sum2 = g.Sum(g.Value.enum_to_string == ToStringMapEnum.中国人 || g.Value.enum_to_string == ToStringMapEnum.abc ? 1 : 0),
});
Assert.Equal(@"SELECT a.[id] as1, sum(case when (a.[enum_to_string] = N'香港' OR a.[enum_to_string] = N'abc') then 1 else 0 end) as2, sum(case when (a.[enum_to_string] = N'中国人' OR a.[enum_to_string] = N'abc') then 1 else 0 end) as3
FROM [EnumTestMap] a
GROUP BY a.[id]", sql);

//delete
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(1, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_string == ToStringMapEnum.abc).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id).First());
}
Expand All @@ -102,26 +112,26 @@ public void EnumNullableToString()
Assert.Equal(item.enumnullable_to_string, find.enumnullable_to_string);
Assert.Null(find.enumnullable_to_string);

item = new EnumTestMap { enumnullable_to_string = ToStringMapEnum.中国人 };
item = new EnumTestMap { enumnullable_to_string = ToStringMapEnum.中国人 };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.中国人).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enumnullable_to_string, find.enumnullable_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enumnullable_to_string);
Assert.Equal(ToStringMapEnum.中国人, find.enumnullable_to_string);

//update all
item.enumnullable_to_string = ToStringMapEnum.香港;
item.enumnullable_to_string = ToStringMapEnum.香港;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enumnullable_to_string, find.enumnullable_to_string);
Assert.Equal(ToStringMapEnum.香港, find.enumnullable_to_string);
Assert.Equal(ToStringMapEnum.香港, find.enumnullable_to_string);

item.enumnullable_to_string = null;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).First());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).First());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == null).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Expand All @@ -144,8 +154,8 @@ public void EnumNullableToString()
Assert.Null(find.enumnullable_to_string);

//delete
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(1, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_string == null).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id).First());
}
Expand All @@ -157,11 +167,11 @@ public void EnumToInt()
var orm = g.sqlserver;
var item = new EnumTestMap { };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).First();
var find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_int, find.enum_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_int);

item = new EnumTestMap { enum_to_int = ToStringMapEnum.abc };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
Expand All @@ -172,28 +182,28 @@ public void EnumToInt()
Assert.Equal(ToStringMapEnum.abc, find.enum_to_int);

//update all
item.enum_to_int = ToStringMapEnum.香港;
item.enum_to_int = ToStringMapEnum.香港;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_int, find.enum_to_int);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_int);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_int);

item.enum_to_int = ToStringMapEnum.中国人;
item.enum_to_int = ToStringMapEnum.中国人;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enum_to_int, find.enum_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enum_to_int);

//update set
Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_int, ToStringMapEnum.香港).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).First();
Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_int, ToStringMapEnum.香港).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_int);
Assert.Equal(ToStringMapEnum.香港, find.enum_to_int);

Assert.Equal(1, orm.Update<EnumTestMap>().Where(a => a.id == item.id).Set(a => a.enum_to_int, ToStringMapEnum.abc).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.abc).First();
Expand All @@ -202,8 +212,8 @@ public void EnumToInt()
Assert.Equal(ToStringMapEnum.abc, find.enum_to_int);

//delete
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(1, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enum_to_int == ToStringMapEnum.abc).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id).First());
}
Expand All @@ -220,26 +230,26 @@ public void EnumNullableToInt()
Assert.Equal(item.enumnullable_to_int, find.enumnullable_to_int);
Assert.Null(find.enumnullable_to_int);

item = new EnumTestMap { enumnullable_to_int = ToStringMapEnum.中国人 };
item = new EnumTestMap { enumnullable_to_int = ToStringMapEnum.中国人 };
Assert.Equal(1, orm.Insert<EnumTestMap>().AppendData(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.中国人).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.中国人).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enumnullable_to_int, find.enumnullable_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enumnullable_to_int);
Assert.Equal(ToStringMapEnum.中国人, find.enumnullable_to_int);

//update all
item.enumnullable_to_int = ToStringMapEnum.香港;
item.enumnullable_to_int = ToStringMapEnum.香港;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).First();
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.enumnullable_to_int, find.enumnullable_to_int);
Assert.Equal(ToStringMapEnum.香港, find.enumnullable_to_int);
Assert.Equal(ToStringMapEnum.香港, find.enumnullable_to_int);

item.enumnullable_to_int = null;
Assert.Equal(1, orm.Update<EnumTestMap>().SetSource(item).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).First());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).First());
find = orm.Select<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == null).First();
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Expand All @@ -262,8 +272,8 @@ public void EnumNullableToInt()
Assert.Null(find.enumnullable_to_int);

//delete
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.中国人).ExecuteAffrows());
Assert.Equal(0, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == ToStringMapEnum.香港).ExecuteAffrows());
Assert.Equal(1, orm.Delete<EnumTestMap>().Where(a => a.id == item.id && a.enumnullable_to_int == null).ExecuteAffrows());
Assert.Null(orm.Select<EnumTestMap>().Where(a => a.id == item.id).First());
}
Expand Down
Loading

0 comments on commit 2598451

Please sign in to comment.