Skip to content

Added unit test for failing delete statement #2689

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

Closed
wants to merge 4 commits into from
Closed
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
41 changes: 41 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2688/BasketItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernate.Test.NHSpecificTest.GH2688
{
public class BasketItem
{
public virtual int Id { get; set; }

public virtual string Name { get; set; }

public virtual decimal Price { get; set; }

public virtual Product Product { get; set; }

public virtual int OrderBy { get; set; }
}

public class BasketItemMap : ClassMapping<BasketItem>
{
public BasketItemMap()
{
Id(x => x.Id, m => {
m.Generator(Generators.Identity);
m.Column("BasketItemId");
});
Property(x => x.Name);
Property(x => x.Price);
Property(x => x.OrderBy);
ManyToOne(x => x.Product, m => {
m.NotFound(NotFoundMode.Ignore);
m.Column("ProductId");
});
}
}
}
90 changes: 90 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2688/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2688
{
/// <summary>
/// Fixture using 'by code' mappings
/// </summary>
/// <remarks>
/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entity" /> mapping is performed
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
/// if you prefer.
/// </remarks>
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<BasketItemMap>();
mapper.AddMapping<ProductMap>();
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Product { Name = "Printer Paper", Price = 10 };
session.Save(e1);

var e2 = new BasketItem { Name = "Printer Paper", Price = 10, Product = e1, OrderBy = 1 };
session.Save(e2);

var e3 = new BasketItem { Name = "Cable", Price = 10, Product = null, OrderBy = 2 };
session.Save(e3);

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]
public void YourTestName()
{
int productId = 0;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var product = session.QueryOver<Product>()
.Where(p => p.Name == "Printer Paper")
.SingleOrDefault();
productId = product.Id;
session.Delete(product);
transaction.Commit();
}

Assert.DoesNotThrow(() => { DeleteBasketItems(productId); });
}

private void DeleteBasketItems(int productId)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from BasketItem where Product.Id = " + productId)
.ExecuteUpdate();
transaction.Commit();
}
}
}
}
47 changes: 47 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2688/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using NHibernate;
using NHibernate.Mapping;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernate.Test.NHSpecificTest.GH2688
{
public class Product
{
public virtual int Id { get; set; }

public virtual string Name { get; set; }

public virtual decimal Price { get; set; }

public virtual IList<BasketItem> BasketItems { get; set; }
}

public class ProductMap : ClassMapping<Product>
{
public ProductMap()
{
Id(x => x.Id, m => {
m.Generator(Generators.Identity);
m.Column("ProductId");
});
Property(x => x.Name);
Property(x => x.Price);
Bag(x => x.BasketItems, bag => {
bag.Inverse(true);
bag.Lazy(CollectionLazy.Lazy);
bag.Cascade(Mapping.ByCode.Cascade.Persist);
bag.OrderBy("OrderBy asc");
bag.Key(km =>
{
km.Column("ProductId");
km.ForeignKey("none");
});
}, a => a.OneToMany());
}
}
}