Skip to content

Commit f3ac972

Browse files
committed
Revive hql ParsingFixture
1 parent fca2fe9 commit f3ac972

File tree

4 files changed

+157
-3187
lines changed

4 files changed

+157
-3187
lines changed
Lines changed: 141 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,149 @@
11
using System;
22
using System.Collections.Generic;
3-
using log4net.Config;
4-
using NHibernate.Cfg;
5-
using NHibernate.Engine;
3+
using System.Xml.Linq;
64
using NHibernate.Hql.Ast.ANTLR;
7-
using NHibernate.Tool.hbm2ddl;
85
using NUnit.Framework;
96

107
namespace NHibernate.Test.Hql.Ast
118
{
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-
//}
9+
[TestFixture]
10+
public class ParsingFixture
11+
{
12+
/// <summary>
13+
/// Key test for HQL strings -> HQL AST's - takes the query and returns a string
14+
/// representation of the resultant tree
15+
/// </summary>
16+
/// <param name="query"></param>
17+
/// <returns></returns>
18+
[Test]
19+
[TestCaseSource(typeof(QueryFactoryClass), nameof(QueryFactoryClass.TestCases))]
20+
public string HqlParse(string query)
21+
{
22+
var p = new HqlParseEngine(query, false, null);
23+
var result = p.Parse().ToStringTree();
24+
25+
return " " + result;
26+
}
27+
28+
/// <summary>
29+
/// Used to test individual queries "by hand", since td.net doesn't let me run a
30+
/// single test out of a data set
31+
/// </summary>
32+
[Test, Explicit]
33+
public void ManualTest()
34+
{
35+
var p = new HqlParseEngine(@"select all s, s.Other from s in class Simple where s = :s", false, null);
36+
37+
var result = p.Parse().ToStringTree();
38+
39+
Console.WriteLine(result);
40+
}
41+
42+
/// <summary>
43+
/// Helper "test" to display queries that are ignored
44+
/// </summary>
45+
[Test]
46+
public void ShowIgnoredQueries()
47+
{
48+
foreach (string query in QueryFactoryClass.GetIgnores)
49+
{
50+
Console.WriteLine(query);
51+
}
52+
}
53+
54+
/// <summary>
55+
/// Helper "test" to display queries that don't parse in H3
56+
/// </summary>
57+
[Test]
58+
public void ShowExceptionQueries()
59+
{
60+
foreach (string query in QueryFactoryClass.GetExceptions)
61+
{
62+
Console.WriteLine(query);
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Class used by Nunit 2.5 to drive the data into the HqlParse test
68+
/// </summary>
69+
public class QueryFactoryClass
70+
{
71+
public static IEnumerable<TestCaseData> TestCases
72+
{
73+
get
74+
{
75+
// TODO - need to handle Ignore better (it won't show in results...)
76+
return EnumerateTests(
77+
td => !td.Ignore && !td.Result.StartsWith("Exception"),
78+
td => new TestCaseData(td.Query)
79+
.Returns(td.Result)
80+
.SetCategory(td.Category)
81+
.SetName(td.Name)
82+
.SetDescription(td.Description));
83+
}
84+
}
85+
86+
public static IEnumerable<string> GetIgnores
87+
{
88+
get
89+
{
90+
return EnumerateTests(
91+
td => td.Ignore,
92+
td => td.Query);
93+
}
94+
}
95+
96+
public static IEnumerable<string> GetExceptions
97+
{
98+
get
99+
{
100+
return EnumerateTests(
101+
td => td.Result.StartsWith("Exception"),
102+
td => td.Query);
103+
}
104+
}
105+
106+
static IEnumerable<T> EnumerateTests<T>(
107+
Func<QueryTestData, bool> predicate,
108+
Func<QueryTestData, T> projection)
109+
{
110+
XDocument doc = XDocument.Load(@"Hql/Ast/TestQueriesWithResults.xml");
111+
112+
foreach (XElement testGroup in doc.Element("Tests").Elements("TestGroup"))
113+
{
114+
string category = testGroup.Attribute("Name").Value;
115+
116+
foreach (XElement test in testGroup.Elements("Test"))
117+
{
118+
QueryTestData testData = new QueryTestData(category, test);
119+
120+
if (predicate(testData))
121+
{
122+
yield return projection(testData);
123+
}
124+
}
125+
}
126+
}
127+
128+
class QueryTestData
129+
{
130+
internal QueryTestData(string category, XElement xml)
131+
{
132+
Category = category;
133+
Query = xml.Element("Query").Value.Trim();
134+
Result = xml.Element("Result")?.Value;
135+
Name = xml.Element("Name")?.Value;
136+
Description = xml.Element("Description")?.Value.Trim() ?? string.Empty;
137+
Ignore = bool.Parse(xml.Attribute("Ignore")?.Value ?? "false");
138+
}
139+
140+
internal string Category;
141+
internal string Query;
142+
internal string Result;
143+
internal string Name;
144+
internal string Description;
145+
internal bool Ignore;
146+
}
147+
}
148+
}
176149
}

0 commit comments

Comments
 (0)