Skip to content

Unit test & Fix for GH1704 #1705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1704/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH1704
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Country = "Greece", City = "Athens", Budget = 100000m };
session.Save(e1);

var e2 = new Entity { Country = "Greece", City = "Chania", Budget = 50000m };
session.Save(e2);

var e3 = new Entity { Country = "Italy", City = "Rome", Budget = 200000m };
session.Save(e3);

var e4 = new Entity { Country = "Italy", City = "Milan", Budget = 100000m };
session.Save(e4);

var e5 = new Entity { Country = "France", City = "Paris", Budget = 300000m };
session.Save(e5);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test(Description = "GH-1704")]
public async Task GroupByCustomClassAsKeyAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var result = await (session.Query<Entity>()
.GroupBy(a => new GroupByEntity(a.Country))
.Select(a => new { groupby = a.Key, cnt = a.Count(), sum = a.Sum(o => o.Budget) })
.ToListAsync());

Assert.That(result, Has.Count.EqualTo(3));
}
}
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1704/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1704
{
class Entity
{
public virtual Guid Id { get; set; }
public virtual string Country { get; set; }
public virtual string City { get; set; }
public virtual decimal Budget { get; set; }
}

class GroupByEntity
{
public GroupByEntity(string country)
{
Country = country;
}
public virtual string Country { get; set; }
public virtual string City { get; set; }
}
}
64 changes: 64 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1704/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1704
{
[TestFixture]
public class Fixture : BugTestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the test nature, it appears to me no async code re-generation has been run, because for such a test, it should have generated an async counterpart. Please run the async generator. (See contributing.)

{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Country = "Greece", City = "Athens", Budget = 100000m };
session.Save(e1);

var e2 = new Entity { Country = "Greece", City = "Chania", Budget = 50000m };
session.Save(e2);

var e3 = new Entity { Country = "Italy", City = "Rome", Budget = 200000m };
session.Save(e3);

var e4 = new Entity { Country = "Italy", City = "Milan", Budget = 100000m };
session.Save(e4);

var e5 = new Entity { Country = "France", City = "Paris", Budget = 300000m };
session.Save(e5);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test(Description = "GH-1704")]
public void GroupByCustomClassAsKey()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var result = session.Query<Entity>()
.GroupBy(a => new GroupByEntity(a.Country))
.Select(a => new { groupby = a.Key, cnt = a.Count(), sum = a.Sum(o => o.Budget) })
.ToList();

Assert.That(result, Has.Count.EqualTo(3));
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1704/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH1704">
<class name="Entity">
<id name="Id" generator="guid.comb"/>
<property name="Country"/>
<property name="City"/>
<property name="Budget"/>
</class>
</hibernate-mapping>
5 changes: 4 additions & 1 deletion src/NHibernate/Linq/GroupBy/GroupKeyNominator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ protected override Expression VisitNew(NewExpression expression)
{
_transformed = true;
// Transform each initializer recursively (to allow for nested initializers)
if (expression.Members == null)
return Expression.New(expression.Constructor, expression.Arguments.Select(VisitInternal));

return Expression.New(expression.Constructor, expression.Arguments.Select(VisitInternal), expression.Members);
}

Expand Down Expand Up @@ -85,4 +88,4 @@ protected override Expression VisitBinary(BinaryExpression expression)
return base.VisitBinary(expression);
}
}
}
}