Skip to content
Permalink
Browse files

Integration Test demonstrating Bug generating sql Count statement wit…

…h PaginHelper.SplitSQL
  • Loading branch information...
butulia committed May 16, 2019
1 parent 20df502 commit 4cb1e45eb4fea4117ddc74c7d997985b25c72b7c
Showing with 32 additions and 0 deletions.
  1. +32 −0 PetaPoco.Tests.Integration/Databases/BaseQueryTests.cs
@@ -5,6 +5,7 @@
using PetaPoco.Core;
using PetaPoco.Providers;
using PetaPoco.Tests.Integration.Models;
using PetaPoco.Utilities;
using Shouldly;
using Xunit;

@@ -17,6 +18,37 @@ protected BaseQueryTests(DBTestProvider provider)
{
}

[Fact]
public virtual void Page_SqlCountStatmentNotWoksCorrectlyWithGroupBy()
{
// Add duplicate names
AddPeople(15, 5);
AddPeople(5, 3);

var pd = PocoData.ForType(typeof(Person), DB.DefaultMapper);
var columnName = DB.Provider.EscapeSqlIdentifier(pd.Columns.Values.First(c => c.PropertyInfo.Name == "Name").ColumnName);
var tableName = DB.Provider.EscapeSqlIdentifier(pd.TableInfo.TableName);
var sql = Sql.Builder
.Select(columnName)
.From(tableName)
.Where($"{columnName} = @0", "Peta1")
.GroupBy(columnName)
.OrderBy(columnName);

// Obtain items
var fetchResult = DB.Fetch<string>(sql);

PagingHelper.Instance.SplitSQL(sql.SQL, out var sqlParts);

var correctSyntax = $"SELECT COUNT(*) FROM (SELECT {sqlParts.SqlSelectRemoved.Replace(sqlParts.SqlOrderBy, string.Empty)}) countAlias";

var correctNumberOfTotalItems = DB.Single<int>(correctSyntax, sql.Arguments);
var page = DB.Page<Person>(2, 3, sql);

fetchResult.Count.ShouldBe(correctNumberOfTotalItems);
page.TotalItems.ShouldBe(fetchResult.Count, $"Statment {sqlParts.SqlCount} is not correct. Correct syntax is {correctSyntax}");
}

[Fact]
public virtual void Page_ForPocoGivenSqlWithoutOrderByParameterPageItemAndPerPage_ShouldReturnValidPocoCollection()
{

0 comments on commit 4cb1e45

Please sign in to comment.
You can’t perform that action at this time.