Skip to content

#750 - AliasToBean failure, test case and fix #1380

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 2 commits into from
Oct 16, 2017
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
115 changes: 115 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH0750/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH0750
{
using System.Threading.Tasks;
using System.Threading;

[TestFixture]
public class FixtureAsync : BugTestCase
Copy link
Member

Choose a reason for hiding this comment

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

Its generation was forgotten. See this section of contributing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OOPS! It was very hectic to work with VS2017 since it keeps throwing itself into NOT RESPONDING state for my install while I add/edit/rename files in test project. Would keep it in mind if needed to write another one.

Copy link
Member

Choose a reason for hiding this comment

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

No troubles. Yes VS2017 is currently quite bad at handling large projects migrated to its new csproj format. That is why I had mentioned Rider, which does quite better. But maybe things will start to be better with changes like dotnet/msbuild#2572.

About writing another one, you will need to clean your master branch. But you should do it only after this PR is closed, otherwise you will need to open a new one from a dedicated branch this time.

For cleaning, in case you need instructions, it could be something like:

  • Branch away changes done on master if you want to keep them. Assuming you are currently on master, git branch newBranch.
  • Still on master, reset it hard git reset --hard head~x, where x is the number of commits to remove (2 currently git reset --hard head~2).
  • Force push it to your remote git push -f.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed instructions. I will wait for this PR to close.

{
protected override void OnSetUp()
{
using (ISession session = OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
var greenTea = new Product()
{
ProductId = 1,
Name = "Green Tea",
UnitPrice = 5
};

session.Save(greenTea);

var blackTea = new Product()
{
ProductId = 2,
Name = "Black Tea",
UnitPrice = 10
};

session.Save(blackTea);

var greenTeaOrder = new Order()
{
OrderId = 1,
OrderDate = System.DateTime.Now
};

session.Save(greenTeaOrder);

greenTeaOrder.OrderLines.Add(new OrderLine() { Order = greenTeaOrder, Product = greenTea, Quantity = 2, UnitPrice = greenTea.UnitPrice ?? 0 });

session.Save(greenTeaOrder);

var blackTeaOrder = new Order()
{
OrderId = 2,
OrderDate = System.DateTime.Now
};

session.Save(blackTeaOrder);

blackTeaOrder.OrderLines.Add(new OrderLine() { Order = blackTeaOrder, Product = blackTea, Quantity = 5, UnitPrice = blackTea.UnitPrice ?? 0 });

session.Save(blackTeaOrder);
tx.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from OrderLine").ExecuteUpdate();
s.CreateQuery("delete from System.Object").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public void MapQueryResultWithAliasToBeanTransformerAsync()
{
Assert.DoesNotThrowAsync(() => GetSaleSummariesAsync());
}

public async Task<IList<ProductSummary>> GetSaleSummariesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var criteria = session
.CreateCriteria<Order>("O")
.CreateCriteria("O.OrderLines", "OI", SqlCommand.JoinType.InnerJoin)
.CreateCriteria("OI.Product", "P", SqlCommand.JoinType.InnerJoin);

var summaeries = await (criteria
.SetProjection(Projections.ProjectionList().Add(Projections.Property("P.ProductId"), "ProductId")
.Add(Projections.Property("P.Name"), "Name")
.Add(Projections.Sum(Projections.Cast(NHibernateUtil.Int32, Projections.Property("OI.Quantity"))), "TotalQuantity")
.Add(Projections.Sum("OI.UnitPrice"), "TotalPrice")
.Add(Projections.GroupProperty("P.ProductId"))
.Add(Projections.GroupProperty("P.Name")))
.SetResultTransformer(Transformers.AliasToBean(typeof(ProductSummary)))
.ListAsync<ProductSummary>(cancellationToken));
await (tx.CommitAsync(cancellationToken));
return summaeries;
}
}
}
}
113 changes: 113 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH0750/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH0750
{
public class ProductSummary
{
public int ProductId { get; set; }

public string Name { get; set; }

public int TotalQuantity { get; set; }

public decimal TotalPrice { get; set; }
}

[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (ISession session = OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
var greenTea = new Product()
{
ProductId = 1,
Name = "Green Tea",
UnitPrice = 5
};

session.Save(greenTea);

var blackTea = new Product()
{
ProductId = 2,
Name = "Black Tea",
UnitPrice = 10
};

session.Save(blackTea);

var greenTeaOrder = new Order()
{
OrderId = 1,
OrderDate = System.DateTime.Now
};

session.Save(greenTeaOrder);

greenTeaOrder.OrderLines.Add(new OrderLine() { Order = greenTeaOrder, Product = greenTea, Quantity = 2, UnitPrice = greenTea.UnitPrice ?? 0 });

session.Save(greenTeaOrder);

var blackTeaOrder = new Order()
{
OrderId = 2,
OrderDate = System.DateTime.Now
};

session.Save(blackTeaOrder);

blackTeaOrder.OrderLines.Add(new OrderLine() { Order = blackTeaOrder, Product = blackTea, Quantity = 5, UnitPrice = blackTea.UnitPrice ?? 0 });

session.Save(blackTeaOrder);
tx.Commit();
}
}

protected override void OnTearDown()
Copy link
Member

Choose a reason for hiding this comment

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

This was missing. Deleting all data is required by our tests, otherwise it is considered as a teardown failure.

{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from OrderLine").ExecuteUpdate();
s.CreateQuery("delete from System.Object").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public void MapQueryResultWithAliasToBeanTransformer()
{
Assert.DoesNotThrow(() => GetSaleSummaries());
}

public IList<ProductSummary> GetSaleSummaries()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
var criteria = session
.CreateCriteria<Order>("O")
.CreateCriteria("O.OrderLines", "OI", SqlCommand.JoinType.InnerJoin)
.CreateCriteria("OI.Product", "P", SqlCommand.JoinType.InnerJoin);

var summaeries = criteria
.SetProjection(Projections.ProjectionList().Add(Projections.Property("P.ProductId"), "ProductId")
.Add(Projections.Property("P.Name"), "Name")
.Add(Projections.Sum(Projections.Cast(NHibernateUtil.Int32, Projections.Property("OI.Quantity"))), "TotalQuantity")
.Add(Projections.Sum("OI.UnitPrice"), "TotalPrice")
.Add(Projections.GroupProperty("P.ProductId"))
.Add(Projections.GroupProperty("P.Name")))
.SetResultTransformer(Transformers.AliasToBean(typeof(ProductSummary)))
.List<ProductSummary>();
tx.Commit();
return summaeries;
}
}
}
}
47 changes: 47 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH0750/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH0750">

<class name="Order" table="Orders">
<id name="OrderId" column="OrderId" type="int" unsaved-value="0"
access="field.camelcase-underscore">
<generator class="assigned"/>
</id>
<property name="OrderDate" column="OrderDate" type="DateTime"
access="field.camelcase-underscore"/>
<set name="OrderLines" lazy="true" access="field.camelcase-underscore"
cascade="all-delete-orphan" inverse="true">
<key column="OrderId"/>
<one-to-many class="OrderLine"/>
</set>
</class>

<class name="OrderLine" table="OrderLines">
<id name="OrderLineId" column="OrderLineId" type="int" unsaved-value="0"
access="field.camelcase-underscore">
<generator class="native"/>
</id>
<many-to-one name="Order" class="Order" column="OrderId" not-null="true"
access="field.camelcase-underscore" fetch="select"/>
<many-to-one name="Product" class="Product" column="ProductId" not-null="true"
access="field.camelcase-underscore" fetch="select"/>
<property name="UnitPrice" column="UnitPrice" type="Decimal"
not-null="true" access="field.camelcase-underscore"/>
<property name="Quantity" column="Quantity" type="int"
not-null="true" access="field.camelcase-underscore"/>
</class>

<class name="Product" table="Products">
<id name="ProductId" column="ProductId" type="int" unsaved-value="0" access="field.camelcase-underscore">
<generator class="assigned"/>
</id>
<property name="Name" column="ProductName" type="string" length="40" not-null="true"
access="field.camelcase-underscore"/>
<property name="UnitPrice" column="UnitPrice" type="Decimal" access="field.camelcase-underscore"/>
<bag name="OrderLines" lazy="true" access="field.camelcase-underscore" cascade="none">
<key column="ProductId"/>
<one-to-many class="OrderLine"/>
</bag>
</class>

</hibernate-mapping>
52 changes: 52 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH0750/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH0750
{
public class Order
{
private readonly ISet<OrderLine> _orderLines;
private DateTime? _orderDate;
private int _orderId;

public Order()
{
_orderLines = new HashSet<OrderLine>();
}

public virtual int OrderId
{
get { return _orderId; }
set { _orderId = value; }
}

public virtual DateTime? OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}

public virtual ISet<OrderLine> OrderLines
{
get { return _orderLines; }
}

public virtual void AddOrderLine(OrderLine orderLine)
{
if (!_orderLines.Contains(orderLine))
{
orderLine.Order = this;
_orderLines.Add(orderLine);
}
}

public virtual void RemoveOrderLine(OrderLine orderLine)
{
if (_orderLines.Contains(orderLine))
{
_orderLines.Remove(orderLine);
orderLine.Order = null;
}
}
}
}
51 changes: 51 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH0750/OrderLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace NHibernate.Test.NHSpecificTest.GH0750
{
public class OrderLine
{
private int _orderLineId;
private Order _order;
private Product _product;
private int _quantity;
private decimal _unitPrice;

public OrderLine() : this(null, null)
{
}

public OrderLine(Order order, Product product)
{
_order = order;
_product = product;
}

public virtual int OrderLineId
{
get { return _orderLineId; }
set { _orderLineId = value; }
}

public virtual Order Order
{
get { return _order; }
set { _order = value; }
}

public virtual Product Product
{
get { return _product; }
set { _product = value; }
}

public virtual decimal UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
}

public virtual int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
}
}
Loading