You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
10 KiB

2 years ago
  1. using OrmTest.Models;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OrmTest.UnitTest
  10. {
  11. public class Select : UnitTestBase
  12. {
  13. private Select() { }
  14. public Select(int eachCount)
  15. {
  16. this.Count = eachCount;
  17. }
  18. internal void Init()
  19. {
  20. base.Begin();
  21. for (int i = 0; i < base.Count; i++)
  22. {
  23. single();
  24. single2();
  25. single3();
  26. single4();
  27. single5();
  28. single6();
  29. single7();
  30. single8();
  31. Multiple();
  32. Multiple2();
  33. singleDynamic();
  34. MultipleDynamic();
  35. }
  36. base.End("Select Test");
  37. }
  38. private void single7()
  39. {
  40. Expression<Func<DataTestInfo2, DataTestInfo2>> exp =it => new DataTestInfo2() { Bool1=it.Bool1 , Bool2=it.Bool2 };
  41. ExpressionContext expContext = new ExpressionContext();
  42. expContext.IsSingle = false;
  43. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  44. var selectorValue = expContext.Result.GetString();
  45. var pars = expContext.Parameters;
  46. base.Check(
  47. selectorValue,
  48. pars,
  49. @"[Bool1] AS [Bool1] , [Bool2] AS [Bool2] ",
  50. new List<SugarParameter>()
  51. {
  52. },
  53. "Select.single7 Error");
  54. }
  55. private void single8()
  56. {
  57. Expression<Func<DataTestInfo2, object>> exp = it => new { Bool1 = it.Bool1, Bool2 = it.Bool2 };
  58. ExpressionContext expContext = new ExpressionContext();
  59. expContext.IsSingle = false;
  60. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  61. var selectorValue = expContext.Result.GetString();
  62. var pars = expContext.Parameters;
  63. base.Check(
  64. selectorValue,
  65. pars,
  66. @"[Bool1] AS [Bool1] , [Bool2] AS [Bool2] ",
  67. new List<SugarParameter>()
  68. {
  69. },
  70. "Select.single8 Error");
  71. }
  72. private void Multiple()
  73. {
  74. Expression<Func<Student, School, object>> exp = (it, school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id, TestId = it.Id + 1 };
  75. ExpressionContext expContext = new ExpressionContext();
  76. expContext.IsSingle = false;
  77. expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
  78. var selectorValue = expContext.Result.GetString();
  79. var pars = expContext.Parameters;
  80. base.Check(
  81. selectorValue,
  82. pars,
  83. @" @constant0 AS [Name] , [it].[Id] AS [Id] , [school].[Id] AS [SchoolId] , ( [it].[Id] + @Id1 ) AS [TestId] ",
  84. new List<SugarParameter>(){
  85. new SugarParameter("@constant0","a"),
  86. new SugarParameter("@Id1",1)
  87. },
  88. "Select.Multiple Error");
  89. }
  90. private void Multiple2()
  91. {
  92. Expression<Func<Student, School, object>> exp = (it, school) => new ViewModelStudent3() { SchoolName=school.Name,Id=SqlFunc.GetSelfAndAutoFill(it.Id) };
  93. ExpressionContext expContext = new ExpressionContext();
  94. expContext.IsSingle = false;
  95. expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
  96. var selectorValue = expContext.Result.GetString();
  97. var pars = expContext.Parameters;
  98. base.Check(
  99. selectorValue,
  100. pars,
  101. @" [school].[Name] AS [SchoolName] ,it.*",
  102. new List<SugarParameter>(){
  103. },
  104. "Select.Multiple Error");
  105. }
  106. private void MultipleDynamic()
  107. {
  108. Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
  109. ExpressionContext expContext = new ExpressionContext();
  110. expContext.IsSingle = false;
  111. expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
  112. var selectorValue = expContext.Result.GetString();
  113. var pars = expContext.Parameters;
  114. base.Check(
  115. selectorValue,
  116. pars,
  117. @" @constant0 AS [Name] , ( [it].[Id] / @Id1 ) AS [Id] , [school].[Id] AS [SchoolId] ",
  118. new List<SugarParameter>(){
  119. new SugarParameter("@constant0","a"),
  120. new SugarParameter("@Id1", 2)},
  121. "Select.MultipleDynamic Error");
  122. }
  123. private void single()
  124. {
  125. int p = 1;
  126. Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p,TestId=it.Id+11 };
  127. ExpressionContext expContext = new ExpressionContext();
  128. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  129. var selectorValue = expContext.Result.GetString();
  130. var pars = expContext.Parameters;
  131. base.Check(
  132. selectorValue,
  133. pars,
  134. @" @constant0 AS [Name] , [Id] AS [Id] , @constant1 AS [SchoolId] , ( [Id] + @Id2 ) AS [TestId] ",
  135. new List<SugarParameter>(){
  136. new SugarParameter("@constant0","a"),
  137. new SugarParameter("@constant1",1),
  138. new SugarParameter("@Id2",11 ) },
  139. "Select.single Error");
  140. }
  141. private void single2(int p=1)
  142. {
  143. Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p, TestId = it.Id + 11 };
  144. ExpressionContext expContext = new ExpressionContext();
  145. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  146. var selectorValue = expContext.Result.GetString();
  147. var pars = expContext.Parameters;
  148. base.Check(
  149. selectorValue,
  150. pars,
  151. @" @constant0 AS [Name] , [Id] AS [Id] , @constant1 AS [SchoolId] , ( [Id] + @Id2 ) AS [TestId] ",
  152. new List<SugarParameter>(){
  153. new SugarParameter("@constant0","a"),
  154. new SugarParameter("@constant1",1),
  155. new SugarParameter("@Id2",11 ) },
  156. "Select.single Error");
  157. }
  158. private void single3(int p = 1)
  159. {
  160. Expression<Func<Student, object>> exp = it => new DataTestInfo() { Datetime1=DateTime.Now, String=it.Name};
  161. ExpressionContext expContext = new ExpressionContext();
  162. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  163. var selectorValue = expContext.Result.GetString();
  164. var pars = expContext.Parameters;
  165. base.Check(
  166. @" @constant0 AS [Datetime1] , [Name] AS [String] ", null,selectorValue,null,
  167. "Select.single3 Error");
  168. }
  169. private void single4(int p = 1)
  170. {
  171. Expression<Func<Student, object>> exp = it => it.CreateTime.HasValue;
  172. SqlServerExpressionContext expContext = new SqlServerExpressionContext();
  173. expContext.IsSingle = false;
  174. expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
  175. var selectorValue = expContext.Result.GetString();
  176. var pars = expContext.Parameters;
  177. base.Check(
  178. @"( [it].[CreateTime]<>'' AND [it].[CreateTime] IS NOT NULL )", null, selectorValue, null,
  179. "Select.single4 Error");
  180. }
  181. private void single5()
  182. {
  183. var p =(DateTime?) DateTime.Now;
  184. Expression<Func<Student, object>> exp = it => p.HasValue;
  185. SqlServerExpressionContext expContext = new SqlServerExpressionContext();
  186. expContext.IsSingle = false;
  187. expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
  188. var selectorValue = expContext.Result.GetString();
  189. var pars = expContext.Parameters;
  190. base.Check(
  191. @"( @constant0<>'' AND @constant0 IS NOT NULL )", new List<SugarParameter>() {
  192. new SugarParameter("@constant0",p)
  193. }, selectorValue, pars,
  194. "Select.single5 Error");
  195. }
  196. private void single6()
  197. {
  198. var p = (DateTime?)DateTime.Now;
  199. Expression<Func<Student, object>> exp = it => p.Value;
  200. SqlServerExpressionContext expContext = new SqlServerExpressionContext();
  201. expContext.IsSingle = false;
  202. expContext.Resolve(exp, ResolveExpressType.FieldSingle);
  203. var selectorValue = expContext.Result.GetString();
  204. var pars = expContext.Parameters;
  205. base.Check(
  206. @" @Const0 ", new List<SugarParameter>() {
  207. new SugarParameter("@Const0",p)
  208. }, selectorValue, pars,
  209. "Select.single6 Error");
  210. }
  211. private void singleDynamic()
  212. {
  213. string a = "a";
  214. Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a,p=it.Id*2 };
  215. ExpressionContext expContext = new ExpressionContext();
  216. expContext.Resolve(exp, ResolveExpressType.SelectSingle);
  217. var selectorValue = expContext.Result.GetString();
  218. var pars = expContext.Parameters;
  219. base.Check(
  220. selectorValue,
  221. pars,
  222. @" [Id] AS [x] , @constant0 AS [shoolid] , @constant1 AS [name] , ( [Id] * @Id2 ) AS [p] ",
  223. new List<SugarParameter>(){
  224. new SugarParameter("@constant0",1),
  225. new SugarParameter("@constant1","a"),
  226. new SugarParameter("@Id2",2)},
  227. "Select.singleDynamic Error");
  228. }
  229. }
  230. }