Skip to content

Commit 68d5e3c

Browse files
committed
Throw for LINQ queries on unmapped entities
1 parent 9fc784e commit 68d5e3c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,45 @@
1313
using System.Collections.Generic;
1414
using System.Linq;
1515
using NHibernate.DomainModel.Northwind.Entities;
16+
using NHibernate.Hql.Ast.ANTLR;
17+
using NHibernate.Linq;
1618
using NSubstitute;
1719
using NUnit.Framework;
18-
using NHibernate.Linq;
1920

2021
namespace NHibernate.Test.Linq
2122
{
2223
using System.Threading.Tasks;
2324
[TestFixture]
2425
public class LinqQuerySamplesAsync : LinqTestCase
2526
{
27+
class NotMappedEntity
28+
{
29+
public virtual int Id { get; set; }
30+
public virtual string Name { get; set; }
31+
}
32+
33+
[Test]
34+
public void ShouldThrowForQueryOnNotMappedEntityAsync()
35+
{
36+
var querySyntaxException = Assert.ThrowsAsync<QuerySyntaxException>(() => session.Query<NotMappedEntity>().Select(x => x.Id).ToListAsync());
37+
Assert.That(querySyntaxException.Message, Does.Contain(nameof(NotMappedEntity)));
38+
}
39+
40+
[Test]
41+
public void ShouldThrowForQueryOnNotMappedEntityNameAsync()
42+
{
43+
var entityName = "NotMappedEntityName";
44+
var querySyntaxException = Assert.ThrowsAsync<QuerySyntaxException>(() => session.Query<NotMappedEntity>(entityName).ToListAsync());
45+
Assert.That(querySyntaxException.Message, Does.Contain(entityName));
46+
}
47+
48+
[Test]
49+
public void ShouldThrowForDmlQueryOnNotMappedEntityAsync()
50+
{
51+
var querySyntaxException = Assert.ThrowsAsync<QuerySyntaxException>(() => session.Query<NotMappedEntity>().DeleteAsync());
52+
Assert.That(querySyntaxException.Message, Does.Contain(nameof(NotMappedEntity)));
53+
}
54+
2655
[Test]
2756
public async Task GroupTwoQueriesAndSumAsync()
2857
{

src/NHibernate.Test/Linq/LinqQuerySamples.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using NHibernate.DomainModel.Northwind.Entities;
6+
using NHibernate.Hql.Ast.ANTLR;
7+
using NHibernate.Linq;
68
using NSubstitute;
79
using NUnit.Framework;
810

@@ -11,6 +13,34 @@ namespace NHibernate.Test.Linq
1113
[TestFixture]
1214
public class LinqQuerySamples : LinqTestCase
1315
{
16+
class NotMappedEntity
17+
{
18+
public virtual int Id { get; set; }
19+
public virtual string Name { get; set; }
20+
}
21+
22+
[Test]
23+
public void ShouldThrowForQueryOnNotMappedEntity()
24+
{
25+
var querySyntaxException = Assert.Throws<QuerySyntaxException>(() => session.Query<NotMappedEntity>().Select(x => x.Id).ToList());
26+
Assert.That(querySyntaxException.Message, Does.Contain(nameof(NotMappedEntity)));
27+
}
28+
29+
[Test]
30+
public void ShouldThrowForQueryOnNotMappedEntityName()
31+
{
32+
var entityName = "NotMappedEntityName";
33+
var querySyntaxException = Assert.Throws<QuerySyntaxException>(() => session.Query<NotMappedEntity>(entityName).ToList());
34+
Assert.That(querySyntaxException.Message, Does.Contain(entityName));
35+
}
36+
37+
[Test]
38+
public void ShouldThrowForDmlQueryOnNotMappedEntity()
39+
{
40+
var querySyntaxException = Assert.Throws<QuerySyntaxException>(() => session.Query<NotMappedEntity>().Delete());
41+
Assert.That(querySyntaxException.Message, Does.Contain(nameof(NotMappedEntity)));
42+
}
43+
1444
[Test]
1545
public void GroupTwoQueriesAndSum()
1646
{

src/NHibernate/Hql/Ast/ANTLR/AstPolymorphicProcessor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AstPolymorphicProcessor
99
{
1010
private readonly IASTNode _ast;
1111
private readonly ISessionFactoryImplementor _factory;
12-
private IEnumerable<KeyValuePair<IASTNode, IASTNode[]>> _nodeMapping;
12+
private Dictionary<IASTNode, IASTNode[]> _nodeMapping;
1313

1414
private AstPolymorphicProcessor(IASTNode ast, ISessionFactoryImplementor factory)
1515
{
@@ -29,8 +29,11 @@ private IASTNode[] Process()
2929
// Find all the polymorphic query sources
3030
_nodeMapping = new PolymorphicQuerySourceDetector(_factory).Process(_ast);
3131

32-
if (_nodeMapping.Count() > 0)
32+
if (_nodeMapping.Count > 0)
3333
{
34+
foreach (var kv in _nodeMapping.Where(x => x.Value.Length == 0))
35+
throw new QuerySyntaxException(kv.Key + " is not mapped");
36+
3437
return DuplicateTree().ToArray();
3538
}
3639
else
@@ -72,4 +75,4 @@ private static IASTNode DuplicateTree(IASTNode ast, IDictionary<IASTNode, IASTNo
7275
return dup;
7376
}
7477
}
75-
}
78+
}

0 commit comments

Comments
 (0)