-
Notifications
You must be signed in to change notification settings - Fork 934
NH-3918 - Nominate equality expressions in SELECT clauses #522
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/NHibernate.Test/NHSpecificTest/NH3918/FixtureByCode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Linq; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH3918 | ||
{ | ||
public class ByCodeFixture : TestCaseMappingByCode | ||
{ | ||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Owner>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); | ||
rc.Property(x => x.Name); | ||
}); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); | ||
rc.Property(x => x.Name); | ||
rc.ManyToOne(m => m.Owner, m => | ||
{ | ||
m.Column("OwnerId"); | ||
}); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (ITransaction transaction = session.BeginTransaction()) | ||
{ | ||
var bob = CreateOwner(session, "Bob"); | ||
var carl = CreateOwner(session, "Carl"); | ||
var doug = CreateOwner(session, "Doug"); | ||
|
||
CreateEntity(session, "Test 1", bob); | ||
CreateEntity(session, "Test 2", carl); | ||
CreateEntity(session, "Test 3", doug); | ||
CreateEntity(session, "Test 4", bob); | ||
CreateEntity(session, "Test 5", carl); | ||
CreateEntity(session, "Test 6", doug); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected Owner CreateOwner(ISession session, string name) | ||
{ | ||
var t = new Owner { Name = name }; | ||
session.Save(t); | ||
return t; | ||
} | ||
|
||
protected void CreateEntity(ISession session, string name, Owner owner) | ||
{ | ||
var t = new Entity | ||
{ | ||
Name = name, | ||
Owner = owner, | ||
}; | ||
session.Save(t); | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (ITransaction transaction = session.BeginTransaction()) | ||
{ | ||
session.Delete("from Entity"); | ||
session.Delete("from Owner"); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void EntityComparisonTest() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var bob = session.Query<Owner>().Single(o => o.Name == "Bob"); | ||
|
||
var queryWithWhere = session.Query<Entity>() | ||
.Where(WhereExpression(bob)) | ||
.Select(e => e.Name); | ||
var queryWithSelect = session.Query<Entity>() | ||
.Select(SelectExpression(bob)); | ||
|
||
var resultsFromWhere = queryWithWhere.ToList(); | ||
var resultsFromSelect = queryWithSelect.ToList(); | ||
|
||
Assert.That(resultsFromSelect.Where(x => (bool)x[1]).Select(x => (string)x[0]), Is.EquivalentTo(resultsFromWhere)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void EntityComparisonTestAgain() | ||
{ | ||
// When the entire fixture is run this will execute the test again within the same ISessionFactory which will test caching | ||
EntityComparisonTest(); | ||
} | ||
|
||
protected Expression<Func<Entity, bool>> WhereExpression(Owner owner) | ||
{ | ||
return e => e.Owner == owner; | ||
} | ||
|
||
protected Expression<Func<Entity, object[]>> SelectExpression(Owner owner) | ||
{ | ||
return e => new object[] | ||
{ | ||
e.Name, | ||
e.Owner == owner | ||
}; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH3918 | ||
{ | ||
public interface IModelObject | ||
{ | ||
Guid Id { get; set; } | ||
string Name { get; set; } | ||
} | ||
|
||
public class Entity : IModelObject | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
|
||
public virtual Owner Owner { get; set; } | ||
} | ||
|
||
public class Owner : IModelObject | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for future - this indenting looks wrong depending on tab-size, because tab has been used also for alignment within a statement. Tabs are for indenting, space for alignment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted. NH differs from my usual development that uses spaces for everything, so I'm used to just pressing tab to align things.