forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlQueryPagerTest.cs
More file actions
58 lines (48 loc) · 2.22 KB
/
SqlQueryPagerTest.cs
File metadata and controls
58 lines (48 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Simple.Data.SqlServer;
namespace Simple.Data.SqlTest
{
[TestFixture]
public class SqlQueryPagerTest
{
static readonly Regex Normalize = new Regex(@"\s+", RegexOptions.Multiline);
[Test]
public void ShouldApplyPagingUsingOrderBy()
{
const string sql = "select a,b,c from d where a = 1 order by c";
const string expected =
"with __data as (select a,b,c, row_number() over(order by c) as [_#_] from d where a = 1)"
+ " select a,b,c from __data where [_#_] between @skip + 1 and @skip + @take";
var modified = new SqlQueryPager().ApplyPaging(sql, "@skip", "@take");
modified = Normalize.Replace(modified, " ").ToLowerInvariant();
Assert.AreEqual(expected, modified);
}
[Test]
public void ShouldApplyPagingUsingOrderByFirstColumnIfNotAlreadyOrdered()
{
const string sql = "select a,b,c from d where a = 1";
const string expected =
"with __data as (select a,b,c, row_number() over(order by a) as [_#_] from d where a = 1)"
+ " select a,b,c from __data where [_#_] between @skip + 1 and @skip + @take";
var modified = new SqlQueryPager().ApplyPaging(sql, "@skip", "@take");
modified = Normalize.Replace(modified, " ").ToLowerInvariant();
Assert.AreEqual(expected, modified);
}
[Test]
public void ShouldCopeWithAliasedColumns()
{
const string sql = "select [a],[b] as [foo],[c] from [d] where [a] = 1";
const string expected =
"with __data as (select [a],[b] as [foo],[c], row_number() over(order by [a]) as [_#_] from [d] where [a] = 1)"
+ " select [a],[foo],[c] from __data where [_#_] between @skip + 1 and @skip + @take";
var modified = new SqlQueryPager().ApplyPaging(sql, "@skip", "@take");
modified = Normalize.Replace(modified, " ").ToLowerInvariant();
Assert.AreEqual(expected, modified);
}
}
}