Skip to content

Commit 8ad3d1f

Browse files
authored
Revive hql ParsingFixture (#3412)
Various tests for hql parser generated by Hql.g
1 parent 20ec240 commit 8ad3d1f

File tree

4 files changed

+145
-3187
lines changed

4 files changed

+145
-3187
lines changed
Lines changed: 129 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,137 @@
11
using System;
22
using System.Collections.Generic;
3-
using log4net.Config;
4-
using NHibernate.Cfg;
5-
using NHibernate.Engine;
3+
using System.IO;
4+
using System.Xml.Linq;
65
using NHibernate.Hql.Ast.ANTLR;
7-
using NHibernate.Tool.hbm2ddl;
86
using NUnit.Framework;
97

108
namespace NHibernate.Test.Hql.Ast
119
{
12-
// This test need the new NUnit
13-
//[TestFixture]
14-
//public class ParsingFixture
15-
//{
16-
// /// <summary>
17-
// /// Key test for HQL strings -> HQL AST's - takes the query and returns a string
18-
// /// representation of the resultant tree
19-
// /// </summary>
20-
// /// <param name="query"></param>
21-
// /// <returns></returns>
22-
// [Test]
23-
// [TestCaseSource(typeof(QueryFactoryClass), "TestCases")]
24-
// public string HqlParse(string query)
25-
// {
26-
// // This test need the new NUnit
27-
// var p = new HqlParseEngine(query, false, null);
28-
// p.Parse();
29-
30-
// return " " + p.Ast.ToStringTree();
31-
// }
32-
33-
// /// <summary>
34-
// /// Used to test individual queries "by hand", since td.net doesn't let me run a
35-
// /// single test out of a data set
36-
// /// </summary>
37-
// [Test]
38-
// public void ManualTest()
39-
// {
40-
// var p = new HqlParseEngine(@"select all s, s.Other from s in class Simple where s = :s", false, null);
41-
42-
// p.Parse();
43-
44-
// Console.WriteLine(p.Ast.ToStringTree());
45-
// }
46-
47-
// /// <summary>
48-
// /// Helper "test" to display queries that are ignored
49-
// /// </summary>
50-
// [Test]
51-
// public void ShowIgnoredQueries()
52-
// {
53-
// foreach (string query in QueryFactoryClass.GetIgnores)
54-
// {
55-
// Console.WriteLine(query);
56-
// }
57-
// }
58-
59-
// /// <summary>
60-
// /// Helper "test" to display queries that don't parse in H3
61-
// /// </summary>
62-
// [Test]
63-
// public void ShowExceptionQueries()
64-
// {
65-
// foreach (string query in QueryFactoryClass.GetExceptions)
66-
// {
67-
// Console.WriteLine(query);
68-
// }
69-
// }
70-
71-
// /// <summary>
72-
// /// Goes all the way to the DB and back. Just here until there's a better place to put it...
73-
// /// </summary>
74-
// [Test]
75-
// public void BasicQuery()
76-
// {
77-
// string input = "select o.id, li.id from NHibernate.Test.CompositeId.Order o join o.LineItems li";// join o.LineItems li";
78-
79-
// ISessionFactoryImplementor sfi = SetupSFI();
80-
81-
// ISession session = sfi.OpenSession();
82-
// session.CreateQuery(input).List();
83-
// /*
84-
// foreach (Animal o in session.CreateQuery(input).Enumerable())
85-
// {
86-
// Console.WriteLine(o.Description);
87-
// }*/
88-
// }
89-
90-
// ISessionFactoryImplementor SetupSFI()
91-
// {
92-
// Configuration cfg = new Configuration();
93-
// cfg.AddAssembly(this.GetType().Assembly);
94-
// new SchemaExport(cfg).Create(false, true);
95-
// return (ISessionFactoryImplementor)cfg.BuildSessionFactory();
96-
// }
97-
98-
// /// <summary>
99-
// /// Class used by Nunit 2.5 to drive the data into the HqlParse test
100-
// /// </summary>
101-
// public class QueryFactoryClass
102-
// {
103-
// public static IEnumerable<TestCaseData> TestCases
104-
// {
105-
// get
106-
// {
107-
// // TODO - need to handle Ignore better (it won't show in results...)
108-
// return EnumerateTests(td => !td.Ignore && !td.Result.StartsWith("Exception"),
109-
// td => new TestCaseData(td.Query)
110-
// .Returns(td.Result)
111-
// .SetCategory(td.Category)
112-
// .SetName(td.Name)
113-
// .SetDescription(td.Description));
114-
// }
115-
// }
116-
117-
// public static IEnumerable<string> GetIgnores
118-
// {
119-
// get
120-
// {
121-
// return EnumerateTests(td => td.Ignore,
122-
// td => td.Query);
123-
// }
124-
// }
125-
126-
// public static IEnumerable<string> GetExceptions
127-
// {
128-
// get
129-
// {
130-
// return EnumerateTests(td => td.Result.StartsWith("Exception"),
131-
// td => td.Query);
132-
// }
133-
// }
134-
135-
// static IEnumerable<T> EnumerateTests<T>(Func<QueryTestData, bool> predicate, Func<QueryTestData , T> projection)
136-
// {
137-
// XDocument doc = XDocument.Load(@"HQL Parsing\TestQueriesWithResults.xml");
138-
139-
// foreach (XElement testGroup in doc.Element("Tests").Elements("TestGroup"))
140-
// {
141-
// string category = testGroup.Attribute("Name").Value;
142-
143-
// foreach (XElement test in testGroup.Elements("Test"))
144-
// {
145-
// QueryTestData testData = new QueryTestData(category, test);
146-
147-
// if (predicate(testData))
148-
// {
149-
// yield return projection(testData);
150-
// }
151-
// }
152-
// }
153-
// }
154-
155-
// class QueryTestData
156-
// {
157-
// internal QueryTestData(string category, XElement xml)
158-
// {
159-
// Category = category;
160-
// Query = xml.Element("Query").Value;
161-
// Result = xml.Element("Result") != null ? xml.Element("Result").Value : "barf";
162-
// Name = xml.Element("Name") != null ? xml.Element("Name").Value : null;
163-
// Description = xml.Element("Description") != null ? xml.Element("Description").Value : null;
164-
// Ignore = xml.Attribute("Ignore") != null ? bool.Parse(xml.Attribute("Ignore").Value) : false;
165-
// }
166-
167-
// internal string Category;
168-
// internal string Query;
169-
// internal string Result;
170-
// internal string Name;
171-
// internal string Description;
172-
// internal bool Ignore;
173-
// }
174-
// }
175-
//}
10+
[TestFixture]
11+
public class ParsingFixture
12+
{
13+
/// <summary>
14+
/// Key test for HQL strings -> HQL AST's - takes the query and returns a string
15+
/// representation of the resultant tree
16+
/// </summary>
17+
/// <param name="query"></param>
18+
/// <returns></returns>
19+
[Test]
20+
[TestCaseSource(typeof(QueryFactoryClass), nameof(QueryFactoryClass.TestCases))]
21+
public string HqlParse(string query)
22+
{
23+
var p = new HqlParseEngine(query, false, null);
24+
var result = p.Parse().ToStringTree();
25+
26+
return " " + result;
27+
}
28+
29+
/// <summary>
30+
/// Used to test individual queries "by hand", since td.net doesn't let me run a
31+
/// single test out of a data set
32+
/// </summary>
33+
[Test, Explicit]
34+
public void ManualTest()
35+
{
36+
var p = new HqlParseEngine(@"select all s, s.Other from s in class Simple where s = :s", false, null);
37+
38+
var result = p.Parse().ToStringTree();
39+
40+
Console.WriteLine(result);
41+
}
42+
43+
/// <summary>
44+
/// Helper "test" to display queries that are ignored
45+
/// </summary>
46+
[Test, Explicit]
47+
public void ShowIgnoredQueries()
48+
{
49+
foreach (string query in QueryFactoryClass.GetIgnores)
50+
{
51+
Console.WriteLine(query);
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Helper "test" to display queries that don't parse in H3
57+
/// </summary>
58+
[Test, Explicit]
59+
public void ShowExceptionQueries()
60+
{
61+
foreach (string query in QueryFactoryClass.GetExceptions)
62+
{
63+
Console.WriteLine(query);
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Class used by NUnit to drive the data into the HqlParse test.
69+
/// </summary>
70+
public class QueryFactoryClass
71+
{
72+
public static IEnumerable<TestCaseData> TestCases =>
73+
// TODO - need to handle Ignore better (it won't show in results...)
74+
EnumerateTests(
75+
td => !td.Ignore && !td.Result.StartsWith("Exception"),
76+
td => new TestCaseData(td.Query)
77+
.Returns(td.Result)
78+
.SetCategory(td.Category)
79+
.SetName(td.Name)
80+
.SetDescription(td.Description));
81+
82+
public static IEnumerable<string> GetIgnores =>
83+
EnumerateTests(
84+
td => td.Ignore,
85+
td => td.Query);
86+
87+
public static IEnumerable<string> GetExceptions =>
88+
EnumerateTests(
89+
td => td.Result.StartsWith("Exception"),
90+
td => td.Query);
91+
92+
static IEnumerable<T> EnumerateTests<T>(
93+
Func<QueryTestData, bool> predicate,
94+
Func<QueryTestData, T> projection)
95+
{
96+
97+
XDocument doc = XDocument.Load(
98+
Path.Combine(Path.GetDirectoryName(typeof(ParsingFixture).Assembly.Location), @"Hql/Ast/TestQueriesWithResults.xml"));
99+
100+
foreach (XElement testGroup in doc.Element("Tests").Elements("TestGroup"))
101+
{
102+
string category = testGroup.Attribute("Name").Value;
103+
104+
foreach (XElement test in testGroup.Elements("Test"))
105+
{
106+
QueryTestData testData = new QueryTestData(category, test);
107+
108+
if (predicate(testData))
109+
{
110+
yield return projection(testData);
111+
}
112+
}
113+
}
114+
}
115+
116+
class QueryTestData
117+
{
118+
internal QueryTestData(string category, XElement xml)
119+
{
120+
Category = category;
121+
Query = xml.Element("Query").Value.Trim();
122+
Result = xml.Element("Result")?.Value;
123+
Name = xml.Element("Name")?.Value;
124+
Description = xml.Element("Description")?.Value.Trim() ?? string.Empty;
125+
Ignore = bool.Parse(xml.Attribute("Ignore")?.Value ?? "false");
126+
}
127+
128+
internal string Category;
129+
internal string Query;
130+
internal string Result;
131+
internal string Name;
132+
internal string Description;
133+
internal bool Ignore;
134+
}
135+
}
136+
}
176137
}

0 commit comments

Comments
 (0)