Skip to content

NH-3534 #316

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
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Linq/LinqQuerySamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,22 @@ into g
ObjectDumper.Write(q, 1);
}

[Test]
public void GroupTwoQueriesAndSum()
{
//NH-3534
var queryWithAggregation = from o1 in db.Orders
from o2 in db.Orders
where o1.Customer.CustomerId == o2.Customer.CustomerId && o1.OrderDate == o2.OrderDate
group o1 by new { o1.Customer.CustomerId, o1.OrderDate } into g
select new { CustomerId = g.Key.CustomerId, LastOrderDate = g.Max(x => x.OrderDate) };

var result = queryWithAggregation.ToList();

Assert.IsNotNull(result);
Assert.IsNotEmpty(result);
}

[Category("GROUP BY/HAVING")]
[Test(Description = "This sample uses a where clause after a group by clause " +
"to find all categories that have at least 10 products.")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand All @@ -21,6 +22,17 @@ public IDictionary Info
}
}

private class PersonWithGenericInfo
{
public int Id { get; set; }
private IDictionary<string, object> info;
public IDictionary<string, object> Info
{
get { return info; }
set { info = value; }
}
}

[Test]
public void WhenMapDynCompoThenMapItAndItsProperties()
{
Expand All @@ -38,6 +50,23 @@ public void WhenMapDynCompoThenMapItAndItsProperties()
hbmDynamicComponent.Properties.Select(x=> x.Name).Should().Have.SameValuesAs("MyInt", "MyDate");
}

[Test]
public void WhenMapDynCompoThenMapItAndItsPropertiesGeneric()
{
var mapper = new ModelMapper();
mapper.Class<PersonWithGenericInfo>(map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
hbmDynamicComponent.Should().Not.Be.Null();
hbmDynamicComponent.Properties.Select(x => x.Name).Should().Have.SameValuesAs("MyInt", "MyDate");
}

[Test]
public void WhenMapDynCompoPropertiesThenShouldAssignPropertyType()
{
Expand All @@ -54,6 +83,22 @@ public void WhenMapDynCompoPropertiesThenShouldAssignPropertyType()
hbmDynamicComponent.Properties.OfType<HbmProperty>().Select(x => x.type1).All(x=> x.Satisfy(value=> !string.IsNullOrEmpty(value)));
}

[Test]
public void WhenMapDynCompoPropertiesThenShouldAssignPropertyTypeGeneric()
{
var mapper = new ModelMapper();
mapper.Class<PersonWithGenericInfo>(map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().Single();
hbmDynamicComponent.Properties.OfType<HbmProperty>().Select(x => x.type1).All(x => x.Satisfy(value => !string.IsNullOrEmpty(value)));
}

[Test]
public void WhenMapDynCompoAttributesThenMapAttributes()
{
Expand All @@ -80,5 +125,32 @@ public void WhenMapDynCompoAttributesThenMapAttributes()
hbmDynamicComponent.optimisticlock.Should().Be.False();
hbmDynamicComponent.unique.Should().Be.True();
}

[Test]
public void WhenMapDynCompoAttributesThenMapAttributesGeneric()
{
var mapper = new ModelMapper();
mapper.Class<PersonWithGenericInfo>(map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(x => x.Info, new { MyInt = 5 }, z =>
{
z.Access(Accessor.Field);
z.Insert(false);
z.Update(false);
z.Unique(true);
z.OptimisticLock(false);
});
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
hbmDynamicComponent.access.Should().Contain("field");
hbmDynamicComponent.insert.Should().Be.False();
hbmDynamicComponent.update.Should().Be.False();
hbmDynamicComponent.optimisticlock.Should().Be.False();
hbmDynamicComponent.unique.Should().Be.True();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ private class MyClassWithDynamic
{
public IDictionary DynCompo { get; set; }
}
private class MyClassWithDynamicGeneric
{
public IDictionary<string, object> DynCompo { get; set; }
}

[Test]
public void CantCreateWithoutHbmMapping()
Expand Down Expand Up @@ -126,6 +130,15 @@ public void AddDynamicComponentProperty()
properties.Single().Should().Be.OfType<HbmDynamicComponent>().And.ValueOf.Name.Should().Be.EqualTo("DynCompo");
}

[Test]
public void AddDynamicComponentPropertyGeneric()
{
var properties = new List<object>();
var map = new StubPropertyContainerMapper<MyClassWithDynamicGeneric>(properties);
map.Component(For<MyClassWithDynamicGeneric>.Property(x => x.DynCompo), (IDynamicComponentMapper cp) => { });
properties.Single().Should().Be.OfType<HbmDynamicComponent>().And.ValueOf.Name.Should().Be.EqualTo("DynCompo");
}

[Test]
public void CallDynamicComponentMapper()
{
Expand All @@ -136,6 +149,16 @@ public void CallDynamicComponentMapper()
called.Should().Be.True();
}

[Test]
public void CallDynamicComponentMapperGeneric()
{
var properties = new List<object>();
var map = new StubPropertyContainerMapper<MyClassWithDynamicGeneric>(properties);
var called = false;
map.Component(For<MyClassWithDynamicGeneric>.Property(x => x.DynCompo), (IDynamicComponentMapper cp) => called = true);
called.Should().Be.True();
}

private class HackPropertyContainerMapper : AbstractPropertyContainerMapper
{
public HackPropertyContainerMapper(System.Type container, HbmMapping mapDoc) : base(container, mapDoc) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand All @@ -20,6 +21,16 @@ public IDictionary Info
}
}

private class PersonWithGenericInfo
{
public int Id { get; set; }
private IDictionary<string, object> info;
public IDictionary<string, object> Info
{
get { return info; }
}
}

private class MyClass
{
public int Something { get; set; }
Expand All @@ -37,6 +48,19 @@ public void WhenAddThenHas()
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("A");
}

[Test]
public void WhenAddThenHasGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");

mapper.Component(propertyInfo, (IComponentMapper x) => { });

component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("A");
}

[Test]
public void WhenCustomizeThenCallCustomizer()
{
Expand All @@ -51,6 +75,20 @@ public void WhenCustomizeThenCallCustomizer()
called.Should().Be.True();
}

[Test]
public void WhenCustomizeThenCallCustomizerGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");

var called = false;
mapper.Component(propertyInfo, (IComponentMapper x) => called = true);

called.Should().Be.True();
}

[Test]
public void WhenCustomizeAccessorThenIgnore()
{
Expand All @@ -63,5 +101,18 @@ public void WhenCustomizeAccessorThenIgnore()

component.Properties.OfType<HbmComponent>().Single().Access.Should().Be.NullOrEmpty();
}

[Test]
public void WhenCustomizeAccessorThenIgnoreGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");

mapper.Component(propertyInfo, (IComponentMapper x) => x.Access(Accessor.Field));

component.Properties.OfType<HbmComponent>().Single().Access.Should().Be.NullOrEmpty();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand All @@ -20,6 +21,17 @@ public IDictionary Info
}
}

private class PersonWithGenericInfo
{
public int Id { get; set; }
private IDictionary<string, object> info;
public IDictionary<string, object> Info
{
get { return info; }
}
}


[Test]
public void WhenAddThenHas()
{
Expand All @@ -33,6 +45,19 @@ public void WhenAddThenHas()
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("Info");
}

[Test]
public void WhenAddThenHasGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo

mapper.Component(propertyInfo, (IDynamicComponentMapper x) => { });

component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("Info");
}

[Test]
public void WhenCustomizeThenCallCustomizer()
{
Expand All @@ -47,6 +72,20 @@ public void WhenCustomizeThenCallCustomizer()
called.Should().Be.True();
}

[Test]
public void WhenCustomizeThenCallCustomizerGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo

var called = false;
mapper.Component(propertyInfo, (IDynamicComponentMapper x) => called = true);

called.Should().Be.True();
}

[Test]
public void WhenCustomizeAccessorThenIgnore()
{
Expand All @@ -59,5 +98,18 @@ public void WhenCustomizeAccessorThenIgnore()

component.Properties.OfType<HbmDynamicComponent>().Single().Access.Should().Be.NullOrEmpty();
}

[Test]
public void WhenCustomizeAccessorThenIgnoreGeneric()
{
var mapdoc = new HbmMapping();
var component = new HbmDynamicComponent();
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo

mapper.Component(propertyInfo, (IDynamicComponentMapper x) => x.Access(Accessor.Field));

component.Properties.OfType<HbmDynamicComponent>().Single().Access.Should().Be.NullOrEmpty();
}
}
}
57 changes: 57 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1039Generic/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;


namespace NHibernate.Test.NHSpecificTest.NH1039Generic
{
[TestFixture]
public class Fixture : BugTestCase
{
public override string BugNumber
{
get { return "NH1039Generic"; }
}

protected override void OnTearDown()
{
base.OnTearDown();
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from Person");
tx.Commit();
}
}

[Test]
public void test()
{
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Person person = new Person("1");
person.Name = "John Doe";
var set = new HashSet<object>();
set.Add("555-1234");
set.Add("555-4321");
person.Properties.Add("Phones", set);

s.Save(person);
tx.Commit();
}
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Person person = (Person)s.CreateCriteria(typeof(Person)).UniqueResult();

Assert.AreEqual("1", person.ID);
Assert.AreEqual("John Doe", person.Name);
Assert.AreEqual(1, person.Properties.Count);
Assert.That(person.Properties["Phones"], Is.InstanceOf<ISet<object>>());
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-1234"));
Assert.IsTrue(((ISet<object>) person.Properties["Phones"]).Contains("555-4321"));
}
}
}
}
Loading