SqlSugar/Src/Asp.Net/SqlServerTest/OldTest/UnitTest/Query/SingleQuery.cs

152 lines
7.8 KiB
C#
Raw Normal View History

2017-03-04 01:22:06 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.Linq.Expressions;
using OrmTest.Models;
2017-03-04 15:07:58 +08:00
namespace OrmTest.UnitTest
2017-03-04 01:22:06 +08:00
{
2017-05-16 13:55:57 +08:00
public class SingleQuery : UnitTestBase
2017-03-04 01:22:06 +08:00
{
2017-03-04 15:07:58 +08:00
private SingleQuery() { }
public SingleQuery(int eachCount)
2017-03-04 01:22:06 +08:00
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
Q2();
}
base.End("Method Test");
}
public void Q2()
{
using (var db = GetInstance())
{
2017-05-28 19:41:42 +08:00
var t1 = db.Queryable<Student>().ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]", null, t1.Key, null, "single t1 Error");
2017-04-29 20:27:55 +08:00
2017-05-28 19:41:42 +08:00
var t2 = db.Queryable<Student>().With(SqlWith.NoLock).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WITH(NOLOCK)", null, t2.Key, null, "single t2 Error");
2017-04-29 20:27:55 +08:00
2017-05-28 19:41:42 +08:00
var t3 = db.Queryable<Student>().OrderBy(it=>it.Id).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] ORDER BY [ID] ASC", null, t3.Key, null, "single t3 Error");
2017-04-29 20:27:55 +08:00
2017-05-28 19:41:42 +08:00
var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
2017-07-08 01:41:59 +08:00
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");
2017-04-29 20:27:55 +08:00
2017-05-28 19:41:42 +08:00
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
2017-07-08 01:41:59 +08:00
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");
2017-04-29 20:27:55 +08:00
int pageIndex = 2;
int pageSize = 10;
2017-06-08 12:34:22 +08:00
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
2017-07-08 01:41:59 +08:00
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");
2017-04-29 20:27:55 +08:00
2017-05-19 11:20:07 +08:00
int studentCount=db.Ado.GetInt("select count(1) from Student");
2017-04-29 20:27:55 +08:00
var countIsSuccess=db.Queryable<Student>().Count()== studentCount;
if (!countIsSuccess) {
2017-05-26 14:11:04 +08:00
throw new Exception(" single countIsSuccess Error");
2017-04-29 20:27:55 +08:00
}
2017-06-08 12:34:22 +08:00
var t7 = db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToPageList(pageIndex,pageSize,ref studentCount);
2017-04-29 20:27:55 +08:00
countIsSuccess = studentCount == db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize * pageIndex).Count();
if (!countIsSuccess)
{
2017-05-28 19:41:42 +08:00
throw new Exception("single t7 Error");
2017-04-29 20:27:55 +08:00
}
2017-05-19 11:20:07 +08:00
int studentMin = db.Ado.GetInt("select min(id) from Student");
2017-04-29 20:27:55 +08:00
var minIsSuccess = db.Queryable<Student>().Min(it=>it.Id) == studentMin;
if (!minIsSuccess)
{
2017-05-26 14:11:04 +08:00
throw new Exception("single minIsSuccess Error");
2017-04-29 20:27:55 +08:00
}
2017-05-19 11:20:07 +08:00
int studentMax = db.Ado.GetInt("select max(id) from Student");
2017-04-29 20:27:55 +08:00
var maxIsSuccess = db.Queryable<Student>().Max(it => it.Id) == studentMax;
if (!maxIsSuccess)
{
2017-05-26 14:11:04 +08:00
throw new Exception("single maxIsSuccess Error");
2017-04-29 20:27:55 +08:00
}
2017-05-19 11:20:07 +08:00
int studentAvg = db.Ado.GetInt("select avg(id) from Student");
2017-04-29 20:27:55 +08:00
var avgIsSuccess = db.Queryable<Student>().Avg(it => it.Id) == studentAvg;
if (!maxIsSuccess)
{
2017-05-26 14:11:04 +08:00
throw new Exception(" single avgIsSuccess Error");
2017-04-29 20:27:55 +08:00
}
2017-05-19 11:20:07 +08:00
int studentSum = db.Ado.GetInt("select sum(id) from Student");
2017-04-29 20:27:55 +08:00
var sumIsSuccess = db.Queryable<Student>().Sum(it => it.Id) == studentSum;
if (!sumIsSuccess)
{
2017-05-26 14:11:04 +08:00
throw new Exception("single sumIsSuccess Error");
2017-04-29 20:27:55 +08:00
}
2017-04-29 20:37:37 +08:00
2017-05-28 19:41:42 +08:00
var t8 = db.Queryable<Student>()
2017-04-29 20:37:37 +08:00
.Where(it=>it.Id==1)
2017-05-31 15:42:21 +08:00
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
2017-06-08 12:34:22 +08:00
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
2017-07-08 01:41:59 +08:00
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>() {
2017-04-29 20:37:37 +08:00
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
2017-05-28 19:41:42 +08:00
}, t8.Key, t8.Value,"single t8 Error");
2017-06-05 11:11:50 +08:00
var t9 = db.Queryable<Student>()
.In(1)
.Select(it => new { it.Id, it.Name,x=it.Id }).ToSql();
base.Check("SELECT [ID] AS [Id] , [Name] AS [Name] , [ID] AS [x] FROM [STudent] WHERE [Id] IN (@InPara0) ", new List<SugarParameter>() {
new SugarParameter("@InPara0",1) },t9.Key,t9.Value, "single t9 error");
2017-07-22 12:58:59 +08:00
var t10 = db.Queryable<Student>().Select(it => new StudentEnum() { Id = SqlFunc.GetSelfAndAutoFill(it.Id) }).ToSql();
base.Check("SELECT * FROM [STudent] ", null, t10.Key, t10.Value, "single t10 error");
2017-07-28 10:18:34 +08:00
var t11= db.Queryable<Student>().GroupBy("id").OrderBy("id").Select("id").ToSql();
base.Check("SELECT id FROM [STudent] GROUP BY id ORDER BY id ", null, t11.Key, t11.Value, "single t11 error");
2017-08-13 18:19:31 +08:00
var t12 = db.Queryable<Student>().Where(it=>it.Id!=null).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [ID] IS NOT NULL )", null, t12.Key, t12.Value, "single t12 error");
2017-09-19 13:23:35 +08:00
var id = 1;
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();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] it WHERE ((SELECT MAX([Id]) FROM [School] WHERE (( [Id] = [it].[ID] ) AND ( [Id] = @Id0 ))) = @Const1 )",
new List<SugarParameter>() {
new SugarParameter("@Id0",1),
new SugarParameter("@Const1",1)
}, t13.Key, t13.Value, "single t13 error ");
2017-11-18 01:44:17 +08:00
var t14 = db.Queryable<Student>()
.Where(it => it.Name == "a" && SqlFunc.HasValue(it.Name)).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE (( [Name] = @Name0 ) AND ( [Name]<>'' AND [Name] IS NOT NULL ))",
new List<SugarParameter>() {
new SugarParameter("@Name0","a")
}, t14.Key, t14.Value, "single t14 error ");
2017-12-19 16:20:35 +08:00
var t15 = db.Queryable<CapitalEntity>()
.Select(x => new
{
TGYArea = SqlFunc.AggregateSum(SqlFunc.IIF(x.FlatProp == "1", x.Areas, 0))
}).ToSql();
base.Check("SELECT SUM(( CASE WHEN ( [FlatProp] = @FlatProp0 ) THEN [Areas] ELSE @MethodConst1 END )) AS [TGYArea] FROM [RENT_CAPITAL] ", new List<SugarParameter>()
{
new SugarParameter("@FlatProp0","1"),
new SugarParameter("@MethodConst1",0)
}, t15.Key, t15.Value, "single t15 error");
2017-03-04 01:22:06 +08:00
}
}
}
}