-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
272745a
commit 5550aad
Showing
15 changed files
with
358 additions
and
14 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
net/DevExtreme.AspNet.Data.Tests.Common/PaginateViaPrimaryKeyTestHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests { | ||
|
||
public class PaginateViaPrimaryKeyTestHelper { | ||
|
||
public interface IDataItem { | ||
int K1 { get; set; } | ||
long K2 { get; set; } | ||
} | ||
|
||
public static IEnumerable<T> CreateTestData<T>() where T : IDataItem, new() { | ||
for(var i = 1; i <= 3; i++) | ||
yield return new T { K1 = i, K2 = i }; | ||
} | ||
|
||
public static void Run<T>(IQueryable<T> source) { | ||
Run(source, new[] { "K1" }); | ||
Run(source, new[] { "K1", "K2" }); | ||
} | ||
|
||
static void Run<T>(IQueryable<T> source, string[] pk) { | ||
var loadOptions = new SampleLoadOptions { | ||
PrimaryKey = pk, | ||
PaginateViaPrimaryKey = true, | ||
Filter = new[] { "K1", ">", "1" }, | ||
Select = new[] { "K1", "K2" }, | ||
Sort = new[] { | ||
new SortingInfo { Selector = "K1" } | ||
}, | ||
Skip = 1, | ||
Take = 1, | ||
RequireTotalCount = true | ||
}; | ||
|
||
var loadResult = DataSourceLoader.Load(source, loadOptions); | ||
var data = loadResult.data.Cast<IDictionary<string, object>>().ToArray(); | ||
|
||
Assert.Single(data); | ||
Assert.Equal(3, data[0]["K1"]); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
net/DevExtreme.AspNet.Data.Tests.EF6/PaginateViaPrimaryKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests.EF6 { | ||
|
||
public class PaginateViaPrimaryKey_DataItem : PaginateViaPrimaryKeyTestHelper.IDataItem { | ||
public int K1 { get; set; } | ||
public long K2 { get; set; } | ||
} | ||
|
||
public class PaginateViaPrimaryKey { | ||
|
||
[Fact] | ||
public void Scenario() { | ||
TestDbContext.Exec(context => { | ||
var set = context.Set<PaginateViaPrimaryKey_DataItem>(); | ||
foreach(var i in PaginateViaPrimaryKeyTestHelper.CreateTestData<PaginateViaPrimaryKey_DataItem>()) | ||
set.Add(i); | ||
context.SaveChanges(); | ||
PaginateViaPrimaryKeyTestHelper.Run(set); | ||
PaginateViaPrimaryKeyTestHelper.Run(set.Select(i => new { i.K1, i.K2 })); | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
net/DevExtreme.AspNet.Data.Tests.EFCore2/PaginateViaPrimaryKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests.EFCore2 { | ||
|
||
public class PaginateViaPrimaryKey { | ||
|
||
[Table(nameof(PaginateViaPrimaryKey) + "_" + nameof(DataItem))] | ||
public class DataItem : PaginateViaPrimaryKeyTestHelper.IDataItem { | ||
public int K1 { get; set; } | ||
public long K2 { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Scenario() { | ||
TestDbContext.Exec(context => { | ||
var set = context.Set<DataItem>(); | ||
set.AddRange(PaginateViaPrimaryKeyTestHelper.CreateTestData<DataItem>()); | ||
context.SaveChanges(); | ||
PaginateViaPrimaryKeyTestHelper.Run(set); | ||
PaginateViaPrimaryKeyTestHelper.Run(set.Select(i => new { i.K1, i.K2 })); | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
net/DevExtreme.AspNet.Data.Tests.NH/PaginateViaPrimaryKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using FluentNHibernate.Mapping; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests.NH { | ||
|
||
public class PaginateViaPrimaryKey { | ||
|
||
public class DataItem : PaginateViaPrimaryKeyTestHelper.IDataItem { | ||
public virtual int K1 { get; set; } | ||
public virtual long K2 { get; set; } | ||
} | ||
|
||
public class DataItemMap : ClassMap<DataItem> { | ||
public DataItemMap() { | ||
Table(nameof(PaginateViaPrimaryKey) + "_" + nameof(DataItem)); | ||
Id(p => p.K1); | ||
Map(p => p.K2); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Scenario() { | ||
SessionFactoryHelper.Exec(session => { | ||
foreach(var i in PaginateViaPrimaryKeyTestHelper.CreateTestData<DataItem>()) | ||
session.Save(i); | ||
var query = session.Query<DataItem>(); | ||
PaginateViaPrimaryKeyTestHelper.Run(query); | ||
PaginateViaPrimaryKeyTestHelper.Run(query.Select(i => new { i.K1, i.K2 })); | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
net/DevExtreme.AspNet.Data.Tests.Xpo/PaginateViaPrimaryKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using DevExpress.Xpo; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests.Xpo { | ||
|
||
public class PaginateViaPrimaryKey { | ||
|
||
[Persistent(nameof(PaginateViaPrimaryKey) + "_" + nameof(DataItem))] | ||
public class DataItem : PaginateViaPrimaryKeyTestHelper.IDataItem { | ||
[Key] | ||
public int K1 { get; set; } | ||
public long K2 { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Scenario() { | ||
UnitOfWorkHelper.Exec(uow => { | ||
foreach(var i in PaginateViaPrimaryKeyTestHelper.CreateTestData<DataItem>()) | ||
uow.Save(i); | ||
uow.CommitChanges(); | ||
var query = uow.Query<DataItem>(); | ||
PaginateViaPrimaryKeyTestHelper.Run(query); | ||
PaginateViaPrimaryKeyTestHelper.Run(query.Select(i => new { i.K1, i.K2 })); | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
net/DevExtreme.AspNet.Data.Tests/PaginateViaPrimaryKeyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace DevExtreme.AspNet.Data.Tests { | ||
|
||
public class PaginateViaPrimaryKeyTests { | ||
|
||
[Fact] | ||
public void SingleKey() { | ||
var data = Enumerable.Range(0, 5) | ||
.Select(i => new { ID = i }) | ||
.ToArray(); | ||
|
||
var loadOptions = new SampleLoadOptions { | ||
SuppressGuardNulls = true, | ||
|
||
PrimaryKey = new[] { "ID" }, | ||
PaginateViaPrimaryKey = true, | ||
|
||
Filter = new[] { "ID", ">", "0" }, | ||
|
||
Skip = 2, | ||
Take = 2 | ||
}; | ||
|
||
var loadResult = DataSourceLoader.Load(data, loadOptions); | ||
|
||
Assert.Equal( | ||
"[{ID:3},{ID:4}]", | ||
DataToString(loadResult.data) | ||
); | ||
|
||
var log = loadOptions.ExpressionLog; | ||
|
||
Assert.EndsWith( | ||
".Where(obj => (obj.ID > 0))" + | ||
".OrderBy(obj => obj.ID)" + | ||
".Select(obj => new AnonType`1(I0 = obj.ID))" + | ||
".Skip(2).Take(2)", | ||
log[0] | ||
); | ||
|
||
Assert.EndsWith( | ||
".Where(obj => ((obj.ID == 3) OrElse (obj.ID == 4)))" + | ||
".OrderBy(obj => obj.ID)", | ||
log[1] | ||
); | ||
} | ||
|
||
[Fact] | ||
public void MultiKey() { | ||
var data = Enumerable.Range(0, 5) | ||
.Select(i => new { K1 = i, K2 = 5 - i }) | ||
.ToArray(); | ||
|
||
var loadOptions = new SampleLoadOptions { | ||
SuppressGuardNulls = true, | ||
|
||
PrimaryKey = new[] { "K1", "K2" }, | ||
PaginateViaPrimaryKey = true, | ||
|
||
Skip = 3, | ||
Take = 2 | ||
}; | ||
|
||
DataSourceLoader.Load(data, loadOptions); | ||
|
||
Assert.Contains( | ||
".Where(obj => (((obj.K1 == 3) AndAlso (obj.K2 == 2)) OrElse ((obj.K1 == 4) AndAlso (obj.K2 == 1))))", | ||
loadOptions.ExpressionLog[1] | ||
); | ||
} | ||
|
||
[Fact] | ||
public void NotUsedWoSkip() { | ||
var loadOptions = new SampleLoadOptions { | ||
PaginateViaPrimaryKey = true, | ||
Take = 123 | ||
}; | ||
|
||
DataSourceLoader.Load(new object[0], loadOptions); | ||
|
||
Assert.All( | ||
loadOptions.ExpressionLog, | ||
i => Assert.DoesNotContain(".Select(", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void RequiresPrimaryKey() { | ||
var error = Record.Exception(delegate { | ||
DataSourceLoader.Load(new string[0], new SampleLoadOptions { | ||
PaginateViaPrimaryKey = true, | ||
Skip = 1 | ||
}); | ||
}); | ||
Assert.True(error is InvalidOperationException); | ||
} | ||
|
||
static string DataToString(object data) { | ||
return JsonConvert.SerializeObject(data).Replace("\"", ""); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.