Skip to content

Commit

Permalink
add a test case for issue nhibernate#3623
Browse files Browse the repository at this point in the history
  • Loading branch information
emwl committed Nov 11, 2024
1 parent 0c9e442 commit 813451f
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3623/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//------------------------------------------------------------------------------
// <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;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3623
{
using System.Threading.Tasks;
[TestFixture]
public class ComponentsListFixtureAsync : TestCaseMappingByCode
{
private Guid _id1;
private Guid _id2;

protected override void OnSetUp()
{
using var session = OpenSession();
using var tr = session.BeginTransaction();
var root1 = new Entity1();
root1.Entries.Add(new ComponentListEntry { DummyString = "one", Property1 = "first", Property2 = "second" });

var root2 = new Entity2();
root2.Entries.Add(new ComponentListEntry { DummyString = "two", Property1 = "third", Property2 = "fourth" });

session.Save(root1);
session.Save(root2);
tr.Commit();
_id1 = root1.Id;
_id2 = root2.Id;
}

[Test]
public async Task LoadingAsync()
{
using var newSession = OpenSession();
var entity1 = await (newSession.GetAsync<Entity1>(_id1));
var entity1Entry = entity1.Entries.First();
var entity2 = await (newSession.GetAsync<Entity2>(_id2));
var entity2Entry = entity2.Entries.First();

Assert.That(entity1Entry.DummyString, Is.EqualTo("one"));
Assert.That(entity2Entry.DummyString, Is.EqualTo("two"));
Assert.That(entity1Entry.Property1, Is.EqualTo("first"));
Assert.That(entity2Entry.Property2, Is.EqualTo("fourth"));

Assert.That(entity1Entry.Property2, Is.Null);
Assert.That(entity2Entry.Property1, Is.Null);
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
session.Delete("from System.Object");
transaction.Commit();
}

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Entity1>(rc =>
{
rc.Table("Entity1");
rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb));
rc.Lazy(false);
rc.Property(x => x.Name);

rc.Bag(
x => x.Entries,
v =>
{
v.Table("Entity1Entry");
v.Key(x => x.Column("Entity1Id"));
},
h => h.Component(cmp =>
{
cmp.Property(x => x.DummyString);
cmp.Property(x => x.Property1);
}));
});

mapper.Class<Entity2>(rc =>
{
rc.Table("Entity2");
rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb));
rc.Lazy(false);
rc.Property(x => x.Name);

rc.Bag(
x => x.Entries,
v =>
{
v.Table("Entity2Entry");
v.Key(x => x.Column("Entity2Id"));
},
h => h.Component(cmp =>
{
cmp.Property(x => x.DummyString);
cmp.Property(x => x.Property2);
}));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3623/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3623
{
public class Entity1
{
public Guid Id { get; set; }
public string Name { get; set; }
public IList<ComponentListEntry> Entries { get; set; } = new List<ComponentListEntry>();
}

public class Entity2
{
public Guid Id { get; set; }
public string Name { get; set; }
public IList<ComponentListEntry> Entries { get; set; } = new List<ComponentListEntry>();
}

public class ComponentListEntry
{
public string DummyString { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
}
}
108 changes: 108 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3623/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3623
{
[TestFixture]
public class ComponentsListFixture : TestCaseMappingByCode
{
private Guid _id1;
private Guid _id2;

protected override void OnSetUp()
{
using var session = OpenSession();
using var tr = session.BeginTransaction();
var root1 = new Entity1();
root1.Entries.Add(new ComponentListEntry { DummyString = "one", Property1 = "first", Property2 = "second" });

var root2 = new Entity2();
root2.Entries.Add(new ComponentListEntry { DummyString = "two", Property1 = "third", Property2 = "fourth" });

session.Save(root1);
session.Save(root2);
tr.Commit();
_id1 = root1.Id;
_id2 = root2.Id;
}

[Test]
public void Loading()
{
using var newSession = OpenSession();
var entity1 = newSession.Get<Entity1>(_id1);
var entity1Entry = entity1.Entries.First();
var entity2 = newSession.Get<Entity2>(_id2);
var entity2Entry = entity2.Entries.First();

Assert.That(entity1Entry.DummyString, Is.EqualTo("one"));
Assert.That(entity2Entry.DummyString, Is.EqualTo("two"));
Assert.That(entity1Entry.Property1, Is.EqualTo("first"));
Assert.That(entity2Entry.Property2, Is.EqualTo("fourth"));

Assert.That(entity1Entry.Property2, Is.Null);
Assert.That(entity2Entry.Property1, Is.Null);
}

protected override void OnTearDown()
{
using var session = OpenSession();
using var transaction = session.BeginTransaction();
session.Delete("from System.Object");
transaction.Commit();
}

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Entity1>(rc =>
{
rc.Table("Entity1");
rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb));
rc.Lazy(false);
rc.Property(x => x.Name);

rc.Bag(
x => x.Entries,
v =>
{
v.Table("Entity1Entry");
v.Key(x => x.Column("Entity1Id"));
},
h => h.Component(cmp =>
{
cmp.Property(x => x.DummyString);
cmp.Property(x => x.Property1);
}));
});

mapper.Class<Entity2>(rc =>
{
rc.Table("Entity2");
rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb));
rc.Lazy(false);
rc.Property(x => x.Name);

rc.Bag(
x => x.Entries,
v =>
{
v.Table("Entity2Entry");
v.Key(x => x.Column("Entity2Id"));
},
h => h.Component(cmp =>
{
cmp.Property(x => x.DummyString);
cmp.Property(x => x.Property2);
}));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}
}

0 comments on commit 813451f

Please sign in to comment.