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.

152 lines
7.9 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SqlSugar;
  7. using System.Linq.Expressions;
  8. using OrmTest.Models;
  9. namespace OrmTest.UnitTest
  10. {
  11. public class SingleQuery : UnitTestBase
  12. {
  13. private SingleQuery() { }
  14. public SingleQuery(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. Q2();
  24. }
  25. base.End("Method Test");
  26. }
  27. public void Q2()
  28. {
  29. using (var db = GetInstance())
  30. {
  31. var t1 = db.Queryable<Student>().ToSql();
  32. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]", null, t1.Key, null, "single t1 Error");
  33. var t2 = db.Queryable<Student>().With(SqlWith.NoLock).ToSql();
  34. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WITH(NOLOCK)", null, t2.Key, null, "single t2 Error");
  35. var t3 = db.Queryable<Student>().OrderBy(it => it.Id).ToSql();
  36. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] ORDER BY [ID] ASC", null, t3.Key, null, "single t3 Error");
  37. var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
  38. base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC ) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 1 AND 3", null, t4.Key, null, "single t4 Error");
  39. var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
  40. base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC ) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 4 AND 9223372036854775807", null, t5.Key, null, "single t5 Error");
  41. int pageIndex = 2;
  42. int pageSize = 10;
  43. var t6 = db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToSql();
  44. base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC ) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 11 AND 20", null, t6.Key, null, "single t6 Error");
  45. int studentCount = db.Ado.GetInt("select count(1) from Student");
  46. var countIsSuccess = db.Queryable<Student>().Count() == studentCount;
  47. if (!countIsSuccess)
  48. {
  49. throw new Exception(" single countIsSuccess Error");
  50. }
  51. var t7 = db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToPageList(pageIndex, pageSize, ref studentCount);
  52. countIsSuccess = studentCount == db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize * pageIndex).Count();
  53. if (!countIsSuccess)
  54. {
  55. throw new Exception("single t7 Error");
  56. }
  57. int studentMin = db.Ado.GetInt("select min(id) from Student");
  58. var minIsSuccess = db.Queryable<Student>().Min(it => it.Id) == studentMin;
  59. if (!minIsSuccess)
  60. {
  61. throw new Exception("single minIsSuccess Error");
  62. }
  63. int studentMax = db.Ado.GetInt("select max(id) from Student");
  64. var maxIsSuccess = db.Queryable<Student>().Max(it => it.Id) == studentMax;
  65. if (!maxIsSuccess)
  66. {
  67. throw new Exception("single maxIsSuccess Error");
  68. }
  69. int studentAvg = db.Ado.GetInt("select avg(id) from Student");
  70. var avgIsSuccess = db.Queryable<Student>().Avg(it => it.Id) == studentAvg;
  71. if (!maxIsSuccess)
  72. {
  73. throw new Exception(" single avgIsSuccess Error");
  74. }
  75. int studentSum = db.Ado.GetInt("select sum(id) from Student");
  76. var sumIsSuccess = db.Queryable<Student>().Sum(it => it.Id) == studentSum;
  77. if (!sumIsSuccess)
  78. {
  79. throw new Exception("single sumIsSuccess Error");
  80. }
  81. var t8 = db.Queryable<Student>()
  82. .Where(it => it.Id == 1)
  83. .WhereIF(true, it => SqlFunc.Contains(it.Name, "a"))
  84. .OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).With(SqlWith.NoLock).ToSql();
  85. base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC ) AS RowIndex FROM [STudent] WITH(NOLOCK) WHERE ( [ID] = @Id0 ) AND ([Name] like '%'+@MethodConst1+'%') ) T WHERE RowIndex BETWEEN 11 AND 20", new List<SugarParameter>() {
  86. new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
  87. }, t8.Key, t8.Value, "single t8 Error");
  88. var t9 = db.Queryable<Student>()
  89. .In(1)
  90. .Select(it => new { it.Id, it.Name, x = it.Id }).ToSql();
  91. base.Check("SELECT [ID] AS [Id] , [Name] AS [Name] , [ID] AS [x] FROM [STudent] WHERE [Id] IN (@InPara0) ", new List<SugarParameter>() {
  92. new SugarParameter("@InPara0",1) }, t9.Key, t9.Value, "single t9 error");
  93. var t10 = db.Queryable<Student>().Select(it => new StudentEnum() { Id = SqlFunc.GetSelfAndAutoFill(it.Id) }).ToSql();
  94. base.Check("SELECT * FROM [STudent] ", null, t10.Key, t10.Value, "single t10 error");
  95. var t11 = db.Queryable<Student>().GroupBy("id").OrderBy("id").Select("id").ToSql();
  96. base.Check("SELECT id FROM [STudent] GROUP BY id ORDER BY id ", null, t11.Key, t11.Value, "single t11 error");
  97. var t12 = db.Queryable<Student>().Where(it => it.Id != -1).ToSql();
  98. //base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [ID] IS NOT NULL )", null, t12.Key, t12.Value, "single t12 error");
  99. var id = 1;
  100. var t13 = db.Queryable<Student>().Where(it => SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id && s.Id == id).Max(s => s.Id) == 1).ToSql();
  101. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] it WHERE ((SELECT MAX([Id]) FROM [School] WHERE (( [Id] = [it].[ID] ) AND ( [Id] = @Id0 ))) = @Const1 )",
  102. new List<SugarParameter>() {
  103. new SugarParameter("@Id0",1),
  104. new SugarParameter("@Const1",1)
  105. }, t13.Key, t13.Value, "single t13 error ");
  106. var t14 = db.Queryable<Student>()
  107. .Where(it => it.Name == "a" && SqlFunc.HasValue(it.Name)).ToSql();
  108. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE (( [Name] = @Name0 ) AND ( [Name]<>'' AND [Name] IS NOT NULL ))",
  109. new List<SugarParameter>() {
  110. new SugarParameter("@Name0","a")
  111. }, t14.Key, t14.Value, "single t14 error ");
  112. var t15 = db.Queryable<CapitalEntity>()
  113. .Select(x => new
  114. {
  115. TGYArea = SqlFunc.AggregateSum(SqlFunc.IIF(x.FlatProp == "1", x.Areas, 0))
  116. }).ToSql();
  117. base.Check("SELECT SUM(( CASE WHEN ( [FlatProp] = @FlatProp0 ) THEN [Areas] ELSE @MethodConst1 END )) AS [TGYArea] FROM [RENT_CAPITAL] ", new List<SugarParameter>()
  118. {
  119. new SugarParameter("@FlatProp0","1"),
  120. new SugarParameter("@MethodConst1",0)
  121. }, t15.Key, t15.Value, "single t15 error");
  122. }
  123. }
  124. }
  125. }