Skip to content

Commit 823a197

Browse files
committed
low level and yaml tests generators generate compilable code, 200+ yaml test fail now though
1 parent 7004799 commit 823a197

File tree

146 files changed

+294
-758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+294
-758
lines changed

src/CodeGeneration/CodeGeneration.YamlTestsRunner/Domain/DoStep.cs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45

@@ -10,6 +11,7 @@ public class DoStep : ITestStep
1011
public string Type { get { return "do"; } }
1112
public string Call { get; set; }
1213
public string Catch { get; set; }
14+
public string TestDescription { get; set; }
1315

1416
private string _rawElasticSearchCall = null;
1517
public string RawElasticSearchCall
@@ -27,25 +29,21 @@ public string RawElasticSearchCall
2729

2830
private string FindBestRawElasticSearchMatch()
2931
{
30-
if (this.Call == "create")
31-
{
32-
this.Call = "index";
33-
if (this.QueryString == null)
34-
this.QueryString = new Dictionary<string, object>();
35-
this.QueryString.Add("op_type", "create");
36-
}
32+
this.PatchCall();
3733

3834
var re = "^" + this.Call.ToPascalCase() + @"(Get|Put|Head|Post|Delete)?(ForAll)?\(";
3935
var counts = YamlTestsGenerator.RawElasticCalls
4036
.Where(c => Regex.IsMatch(c, re))
37+
.Where(c=> this.Body == null || c.Contains("object body"))
4138
.Select(s => new {Method = s, Count = QueryStringCount(s)})
39+
.OrderByDescending(s=>s.Count)
4240
.ToList();
4341

4442
var calls = YamlTestsGenerator.RawElasticCalls
4543
.Where(c => Regex.IsMatch(c, re))
4644
.Where(c=> this.Body == null || c.Contains("object body"))
4745
.OrderByDescending(QueryStringCount)
48-
.ThenByDescending(MethodPreference)
46+
//.ThenByDescending(MethodPreference)
4947
.ToList();
5048

5149
var call = calls.FirstOrDefault();
@@ -57,6 +55,46 @@ private string FindBestRawElasticSearchMatch()
5755
return this.GenerateCall(call);
5856
}
5957

58+
private void PatchCall()
59+
{
60+
if (this.Call == "create")
61+
{
62+
this.Call = "index";
63+
if (this.QueryString == null)
64+
this.QueryString = new Dictionary<string, object>();
65+
this.QueryString.Add("op_type", "create");
66+
}
67+
else if (
68+
this.Call == "indices.delete_alias"
69+
|| this.Call == "indices.delete_warmer"
70+
|| this.Call == "indices.delete_mapping"
71+
|| this.Call == "indices.put_alias"
72+
|| this.Call == "indices.put_warmer"
73+
|| this.Call == "indices.put_mapping")
74+
{
75+
Func<string, bool> m = (s) => Regex.IsMatch(this.TestDescription, "(blank|empty|missing) " + s);
76+
77+
if (!this.QueryString.ContainsKey("index") && m("index"))
78+
this.QueryString["index"] = string.Empty;
79+
if (!this.QueryString.ContainsKey("type") && m("type"))
80+
this.QueryString["type"] = string.Empty;
81+
if (!this.QueryString.ContainsKey("name")
82+
&& (m("name") || m("alias") || m("(.+_)?warmer")))
83+
this.QueryString["name"] = string.Empty;
84+
}
85+
}
86+
private double QueryStringCount(string method)
87+
{
88+
var matches = (double)this.QueryString.Keys.Count(k => method.Contains(k + ","));
89+
return 1 + matches / (method.Count(c=>c==','));
90+
91+
}
92+
private string GetQueryStringValue(string key)
93+
{
94+
var value = this.QueryString[key].ToStringRepresentation("\t\t\t\t\t");
95+
96+
return value;
97+
}
6098

6199

62100
private string GenerateCall(string call)
@@ -93,6 +131,7 @@ private string GenerateCall(string call)
93131
s += ");";
94132
return s;
95133
}
134+
96135

97136
public static string SerializeBody(DoStep o)
98137
{
@@ -123,12 +162,6 @@ private string GetMethodArgument(string key)
123162
return "\"" + v + "\"";
124163
}
125164

126-
private string GetQueryStringValue(string key)
127-
{
128-
var value = this.QueryString[key].ToStringRepresentation("\t\t\t\t\t");
129-
130-
return value;
131-
}
132165

133166
private IEnumerable<string> CsharpArguments(string call, bool inverse = false)
134167
{
@@ -139,29 +172,15 @@ private IEnumerable<string> CsharpArguments(string call, bool inverse = false)
139172
.Select(ki => ki.Key);
140173
return csharpArguments.ToList();
141174
}
142-
143-
private int QueryStringCount(string method)
144-
{
145-
var matches = this.QueryString.Keys.Count(k => method.Contains(k + ","));
146-
var parameters = method.Count(c => c == ',');
147-
if (!QueryString.Any() && parameters == 1)
148-
return 99;
149-
if (matches == 0)
150-
return -100;
151-
if (QueryString.Count == matches && matches == parameters - 1)
152-
return 98;
153-
return matches;
154-
155-
}
156175
private int MethodPreference(string method)
157176
{
158177
var postBoost = this.Body != null ? 10 : 0;
159178
var getBoost = this.Body == null ? 10 : 0;
160179

161-
if (method.Contains("Post(")) return 5 + postBoost;
162-
if (method.Contains("Put(")) return 4 + postBoost;
163-
if (method.Contains("Get(")) return 3 + getBoost;
164-
if (method.Contains("Head(")) return 2 + postBoost;
180+
//if (method.Contains("Post(")) return 5 + postBoost;
181+
//if (method.Contains("Put(")) return 4 + postBoost;
182+
//if (method.Contains("Get(")) return 3 + getBoost;
183+
//if (method.Contains("Head(")) return 2 + postBoost;
165184
return 0;
166185
}
167186
}

src/CodeGeneration/CodeGeneration.YamlTestsRunner/Domain/TestSuite.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public static TestSuite CreateFrom(Dictionary<string, object> untyped, string ya
2222
var untypedSuite = untyped.First();
2323
var suite = new TestSuite();
2424
suite.Description = untypedSuite.Key;
25-
suite.Steps = Actions(untypedSuite).Where(s=>s != null).ToList();
25+
suite.Steps = Actions(untypedSuite, suite.Description).Where(s=>s != null).ToList();
2626
return suite;
2727

2828
}
2929

30-
private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untypedSuite)
30+
private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untypedSuite, string description)
3131
{
3232
var actions = untypedSuite.Value as List<object>;
3333
if (actions == null)
@@ -49,7 +49,7 @@ private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untyp
4949
switch (testAction)
5050
{
5151
case "do":
52-
yield return CreateDoStep(kv.Value as Dictionary<object, object>);
52+
yield return CreateDoStep(kv.Value as Dictionary<object, object>, description);
5353
break;
5454
case "set":
5555
yield return CreateSetStep(kv.Value as Dictionary<object, object>);
@@ -158,7 +158,7 @@ private static SetStep CreateSetStep(Dictionary<object, object> value)
158158
return new SetStep() {VariableName = kv.Value as string, ResponseValue = PropertyPath(kv.Key as string)};
159159
}
160160

161-
private static DoStep CreateDoStep(Dictionary<object, object> value)
161+
private static DoStep CreateDoStep(Dictionary<object, object> value, string description)
162162
{
163163
if (value == null)
164164
return null;
@@ -176,7 +176,13 @@ private static DoStep CreateDoStep(Dictionary<object, object> value)
176176
var argumentString = arguments as string;
177177
if (argumentString != null)
178178
{
179-
return new DoStep {Call = call, Body = argumentString, Catch = catchException};
179+
return new DoStep
180+
{
181+
Call = call,
182+
Body = argumentString,
183+
Catch = catchException,
184+
TestDescription = description
185+
};
180186
}
181187
var complexArgument = arguments as Dictionary<object, object>;
182188
if (complexArgument != null)
@@ -191,9 +197,16 @@ private static DoStep CreateDoStep(Dictionary<object, object> value)
191197
foreach (var kv in complexArgument)
192198
nv.Add(kv.Key as string, kv.Value);
193199

194-
return new DoStep {Call = call, Body = body, QueryString = nv, Catch = catchException};
200+
return new DoStep
201+
{
202+
Call = call,
203+
Body = body,
204+
QueryString = nv,
205+
Catch = catchException,
206+
TestDescription = description
207+
};
195208
}
196-
return new DoStep { Call = call , Catch = catchException};
209+
return new DoStep { Call = call , Catch = catchException, TestDescription = description};
197210
}
198211
}
199212
}

src/CodeGeneration/CodeGeneration.YamlTestsRunner/Extensions.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.Data;
35
using System.Globalization;
46
using System.IO;
57
using System.Linq;
@@ -45,7 +47,7 @@ public static string ToStringRepresentation(this object o, string indendation ="
4547
return body;
4648
}
4749
var ss = o as IEnumerable<string>;
48-
if (ss != null)
50+
if (ss != null && ss.Any())
4951
{
5052
body += "new string [] {\n";
5153
body += string.Join(",\n", ss.Select(str=>indendation + "\t" + str.SurroundWithQuotes()));
@@ -54,7 +56,7 @@ public static string ToStringRepresentation(this object o, string indendation ="
5456
}
5557

5658
var si = o as IEnumerable<int>;
57-
if (si != null)
59+
if (si != null && si.Any())
5860
{
5961
body += "new int [] {\n";
6062
body += string.Join(",\n", si.Select(str=>indendation + "\t" + si.ToString()));
@@ -63,7 +65,7 @@ public static string ToStringRepresentation(this object o, string indendation ="
6365
}
6466

6567
var os = o as IEnumerable<object>;
66-
if (os != null)
68+
if (os != null && os.Any())
6769
{
6870
var inner = string.Join(",\n", os
6971
.Select(oss=>indendation + "\t" + oss.SerializeToAnonymousObject(indendation, Formatting.None)));
@@ -75,6 +77,23 @@ public static string ToStringRepresentation(this object o, string indendation ="
7577
body += "\n" + indendation + "}";
7678
return body;
7779
}
80+
else if (o is IDictionary<object, object>)
81+
{
82+
var d = o as IDictionary<object, object>;
83+
if (d.Keys.Any(k => k.ToString().Contains(".")))
84+
{
85+
body += "new Dictionary<string, object> {\n";
86+
var f = "{0}\t{{ {1}, {2} }}";
87+
var inner = (from kv in d
88+
let k = kv.Key.ToString().SurroundWithQuotes()
89+
let v = kv.Value.SerializeToAnonymousObject(format: Formatting.None)
90+
select string.Format(f, indendation, k, v)
91+
).ToList();
92+
body += string.Join(",\n", inner);
93+
body += "\n" + indendation + "}";
94+
return body;
95+
}
96+
}
7897
body += o.SerializeToAnonymousObject();
7998
return body;
8099
}
@@ -104,7 +123,7 @@ public static string SerializeToAnonymousObject(this object o, string indentatio
104123
//docs contain different types of anon objects, quick fix by making them a dynamic[]
105124
anon = anon.Replace("docs= new []", "docs= new dynamic[]");
106125
//fix empty untyped arrays, default to string
107-
anon = anon.Replace("new [] {}", "new string[] {}");
126+
anon = Regex.Replace(anon, @"new \[\] \{[\s\t\r\n]*\}", "new string[] {}");
108127
//quick fixes for settings: index.* and discovery.zen.*
109128
//needs some recursive regex love perhaps in the future
110129
anon = Regex.Replace(anon, @"(index|discovery)\.([^=]+)=([^\r\n,]+)", " { \"$1.$2\", $3 }", RegexOptions.Multiline);

src/Tests/Nest.Tests.Integration.Yaml/cat.aliases/10_basic.yaml.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public void SimpleAlias4Test()
7474
{
7575

7676
//do indices.create
77-
this.Do(()=> this._client.IndicesCreatePost("test", null));
77+
this.Do(()=> this._client.IndicesCreatePut("test", null));
7878

7979
//do indices.put_alias
80-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_alias", null));
80+
this.Do(()=> this._client.IndicesPutAlias("test", "test_alias", null));
8181

8282
//do cat.aliases
8383
this.Do(()=> this._client.CatAliasesGet());
@@ -103,7 +103,7 @@ public void ComplexAlias5Test()
103103
{
104104

105105
//do indices.create
106-
this.Do(()=> this._client.IndicesCreatePost("test", null));
106+
this.Do(()=> this._client.IndicesCreatePut("test", null));
107107

108108
//do indices.put_alias
109109
_body = new {
@@ -115,7 +115,7 @@ public void ComplexAlias5Test()
115115
}
116116
}
117117
};
118-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_alias", _body));
118+
this.Do(()=> this._client.IndicesPutAlias("test", "test_alias", _body));
119119

120120
//do cat.aliases
121121
this.Do(()=> this._client.CatAliasesGet());
@@ -141,13 +141,13 @@ public void AliasName6Test()
141141
{
142142

143143
//do indices.create
144-
this.Do(()=> this._client.IndicesCreatePost("test", null));
144+
this.Do(()=> this._client.IndicesCreatePut("test", null));
145145

146146
//do indices.put_alias
147-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_1", null));
147+
this.Do(()=> this._client.IndicesPutAlias("test", "test_1", null));
148148

149149
//do indices.put_alias
150-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_2", null));
150+
this.Do(()=> this._client.IndicesPutAlias("test", "test_2", null));
151151

152152
//do cat.aliases
153153
this.Do(()=> this._client.CatAliasesGet("test_1"));
@@ -181,10 +181,10 @@ public void ColumnHeaders7Test()
181181
{
182182

183183
//do indices.create
184-
this.Do(()=> this._client.IndicesCreatePost("test", null));
184+
this.Do(()=> this._client.IndicesCreatePut("test", null));
185185

186186
//do indices.put_alias
187-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_1", null));
187+
this.Do(()=> this._client.IndicesPutAlias("test", "test_1", null));
188188

189189
//do cat.aliases
190190
this.Do(()=> this._client.CatAliasesGet(nv=>nv
@@ -217,10 +217,10 @@ public void SelectColumns8Test()
217217
{
218218

219219
//do indices.create
220-
this.Do(()=> this._client.IndicesCreatePost("test", null));
220+
this.Do(()=> this._client.IndicesCreatePut("test", null));
221221

222222
//do indices.put_alias
223-
this.Do(()=> this._client.IndicesPutAliasPost("test", "test_1", null));
223+
this.Do(()=> this._client.IndicesPutAlias("test", "test_1", null));
224224

225225
//do cat.aliases
226226
this.Do(()=> this._client.CatAliasesGet(nv=>nv

src/Tests/Nest.Tests.Integration.Yaml/cat.allocation/10_basic.yaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void OneIndex4Test()
8888
{
8989

9090
//do indices.create
91-
this.Do(()=> this._client.IndicesCreatePost("test", null));
91+
this.Do(()=> this._client.IndicesCreatePut("test", null));
9292

9393
//do cluster.health
9494
this.Do(()=> this._client.ClusterHealthGet(nv=>nv

src/Tests/Nest.Tests.Integration.Yaml/cat.shards/10_basic.yaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void TestCatShardsOutput2Test()
6565
number_of_replicas= "0"
6666
}
6767
};
68-
this.Do(()=> this._client.IndicesCreatePost("index2", _body));
68+
this.Do(()=> this._client.IndicesCreatePut("index2", _body));
6969

7070
//do cluster.health
7171
this.Do(()=> this._client.ClusterHealthGet(nv=>nv

src/Tests/Nest.Tests.Integration.Yaml/cluster.put_settings/10_basic.yaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void TestPutSettings1Test()
3434

3535
//match _response.transient:
3636
this.IsMatch(_response.transient, new Dictionary<string, object> {
37-
{ "discovery.zen.minimum_master_nodes", "1" }
37+
{ @"discovery.zen.minimum_master_nodes", @"1" }
3838
});
3939

4040
//do cluster.get_settings
@@ -44,7 +44,7 @@ public void TestPutSettings1Test()
4444

4545
//match _response.transient:
4646
this.IsMatch(_response.transient, new Dictionary<string, object> {
47-
{ "discovery.zen.minimum_master_nodes", "1" }
47+
{ @"discovery.zen.minimum_master_nodes", @"1" }
4848
});
4949

5050
}

0 commit comments

Comments
 (0)