-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlicingDiceTester.cs
475 lines (409 loc) · 17.9 KB
/
SlicingDiceTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Slicer.Core;
using Slicer.Utils.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Slicer.Test
{
public class SlicingDiceTester
{
private SlicingDice Client { get; set; }
private bool Verbose { get; set; }
private Dictionary<string, dynamic> ColumnTranslation { get; set; }
private int SleepTime { get; set; }
private string Path { get; set; }
private string Extension { get; set; }
public int NumSuccesses { get; set; }
public int NumFails { get; set; }
public List<dynamic> FailedTests { get; set; }
private bool perTestInsert;
private bool insertSqlData = false;
public SlicingDiceTester(string apiKey, bool verbose = false)
{
this.Client = new SlicingDice(masterKey: apiKey);
this.Verbose = verbose;
// Path of examples directory, the replace is needed to point to the correct path, and it works on Unix and Windows systems
this.Path = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", @"\examples\").Replace(@"/bin/Debug", @"/examples/");
// Extension of examples files
this.Extension = ".json";
this.NumSuccesses = 0;
this.NumFails = 0;
// Sleep time in seconds
this.SleepTime = 20;
this.FailedTests = new List<dynamic>();
}
/// <summary>Run all the tests for a determined query type</summary>
/// <param name="queryType">The type of the query to test</param>
public void RunTests(string queryType)
{
List<Dictionary<string, dynamic>> testData = this.LoadTestData(queryType);
int numTests = testData.Count();
int counter = 0;
this.perTestInsert = testData[0].ContainsKey("insert");
if (!this.perTestInsert && this.insertSqlData) {
List<Dictionary<string, dynamic>> insertData = this.LoadTestData(queryType, "_insert");
foreach (Dictionary<string, dynamic> insert in insertData) {
this.Client.Insert(insert);
}
Thread.Sleep(this.SleepTime * 1000);
}
foreach (Dictionary<string, dynamic> test in testData)
{
string queryType_ = queryType;
this.EmptyColumnTranslation();
System.Console.WriteLine(string.Format("({0}/{1}) Executing test \"{2}\"", counter + 1, numTests, test["name"]));
counter += 1;
if (test.ContainsKey("description"))
{
System.Console.WriteLine(string.Format(" Description: {0}", test["description"]));
}
System.Console.WriteLine(string.Format(" Query type: {0}", queryType));
Dictionary<string, dynamic> result = null;
try
{
if (this.perTestInsert)
{
this.CreateColumns(test);
this.InsertData(test);
}
if (queryType == "update" || queryType == "delete")
{
bool resultAdditional = this.RunAdditionalOperations(queryType, test);
if (!resultAdditional)
{
continue;
}
queryType_ = "count_entity";
}
result = this.ExecuteQuery(queryType_, test);
}
catch (Exception e)
{
System.Console.WriteLine(e);
result = new Dictionary<string, dynamic>(){
{"result", new Dictionary<string, dynamic>(){
{"error", e.Message}
}}
};
if (queryType == "update" || queryType == "delete")
{
this.NumFails += 1;
this.FailedTests.Add(test["name"]);
string resultString = JsonConvert.SerializeObject(result).Trim();
System.Console.WriteLine(string.Format(" Result: {0}", resultString));
System.Console.WriteLine(" Status: Failed\n");
continue;
}
}
this.CompareResult(test, result);
}
}
private bool RunAdditionalOperations(string queryType, Dictionary<string, dynamic> test) {
Dictionary<string, dynamic> queryData = this.TranslateColumnNames(test["additional_operation"].ToObject<Dictionary<string, dynamic>>());
if (queryType == "delete")
{
System.Console.WriteLine(" Deleting");
} else
{
System.Console.WriteLine(" Updating");
}
if (this.Verbose)
{
foreach (KeyValuePair<string, dynamic> qData in queryData)
{
System.Console.WriteLine(string.Format(" - \"{0}\": {1}", qData.Key, qData.Value));
}
}
Dictionary<string, dynamic> result = null;
if (queryType == "delete")
{
result = this.Client.Delete(queryData);
} else if (queryType == "update")
{
result = this.Client.Update(queryData);
}
Dictionary<string, dynamic> expected = this.TranslateColumnNames(test["result_additional"].ToObject<Dictionary<string, dynamic>>());
if (!this.CompareDictionary(expected, result))
{
this.NumFails += 1;
this.FailedTests.Add(test["name"]);
string expectedString = JsonConvert.SerializeObject(expected).Trim();
string resultString = JsonConvert.SerializeObject(result).Trim();
System.Console.WriteLine(string.Format(" Expected: {0}", expectedString));
System.Console.WriteLine(string.Format(" Result: {0}", resultString));
System.Console.WriteLine(" Status: Failed\n");
return false;
}
this.NumSuccesses += 1;
System.Console.WriteLine(" Status: Passed\n");
return true;
}
// Erase column translation dictionary
private void EmptyColumnTranslation()
{
this.ColumnTranslation = new Dictionary<string, dynamic>();
}
/// <summary>Load test data from examples files</summary>
/// <param name="queryType">Type of the query</param>
private List<Dictionary<string, dynamic>> LoadTestData(string queryType, string suffix = "")
{
string queriesText = string.Empty;
using (StreamReader streamReader = new StreamReader(this.Path + queryType + suffix + this.Extension, Encoding.UTF8))
{
queriesText = streamReader.ReadToEnd();
}
return JsonConvert.DeserializeObject<List<Dictionary<string, dynamic>>>(queriesText);
}
/// <summary>Create columns for a given test</summary>
/// <param name="test">Dictionary containing test name, columns metadata, data to be inserted, query, and expected results.</param>
private void CreateColumns(Dictionary<string, dynamic> test)
{
JArray columnsData = test["columns"];
List<Dictionary<string, dynamic>> columns = columnsData.ToObject<List<Dictionary<string, dynamic>>>();
bool isSingular = columns.Count == 1;
string columnOrColumns = string.Empty;
if (isSingular)
{
columnOrColumns = "column";
}
else
{
columnOrColumns = "columns";
}
System.Console.WriteLine(string.Format(" Creating {0} {1}", columns.Count, columnOrColumns));
for(int i = 0; i < columns.Count; i++) {
Dictionary<string, dynamic> column = columns[i];
this.AddTimestampToColumnName(column);
this.Client.CreateColumn(column);
if (this.Verbose)
{
System.Console.WriteLine(string.Format(" - {0}", column["api-name"]));
}
}
}
/// <summary> Add timestamp to column name
/// This technique allows the same test suite to be executed over and over again, since each execution will use different column names.
/// </summary>
/// <param name="column">Dicitonary containing the column to append timestamp</param>
private void AddTimestampToColumnName(Dictionary<string, dynamic> column)
{
string oldName = string.Format("\"{0}\"", column["api-name"]);
string timestamp = this.GetTimestamp();
column["name"] = column["name"] + timestamp;
column["api-name"] = column["api-name"] + timestamp;
string newName = string.Format("\"{0}\"", column["api-name"]);
this.ColumnTranslation[oldName] = newName;
}
// Get current timestamp in string format
private string GetTimestamp()
{
return DateTime.Now.Ticks.ToString();
}
///<summary>Insert test data on SlicingDice API</summary>
///<param name="test">Dictionary containing test name, columns metadata, data to be inserted, query, and expected results.</param>
private void InsertData(Dictionary<string, dynamic> test)
{
JObject insertionObject = test["insert"];
Dictionary<string, dynamic> insert = insertionObject.ToObject<Dictionary<string, dynamic>>();
bool isSingular = insert.Count == 1;
string entityOrEntities = string.Empty;
if (isSingular)
{
entityOrEntities = "entity";
}
else
{
entityOrEntities = "entities";
}
System.Console.WriteLine(string.Format(" Inserting {0} {1}", insert.Count, entityOrEntities));
Dictionary<string, dynamic> insertionData = this.TranslateColumnNames(insert);
if (this.Verbose)
{
foreach (KeyValuePair<string, dynamic> entity in insertionData)
{
System.Console.WriteLine(string.Format(" - \"{0}\": {1}", entity.Key, entity.Value));
}
}
this.Client.Insert(insertionData);
// Wait a few seconds so the data can be inserted by SlicingDice
Thread.Sleep(this.SleepTime * 1000);
}
// Translate column name to match column name with timestamp.
private Dictionary<string, dynamic> TranslateColumnNames(Dictionary<string, dynamic> data){
string dataString = JsonConvert.SerializeObject(data);
foreach (KeyValuePair<string, dynamic> entry in this.ColumnTranslation)
{
dataString = dataString.Replace(entry.Key, (string)entry.Value);
}
return JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(dataString);
}
/// <summary>Execute a query on SlicingDice API</summary>
/// <param name="queryType">The type of the query</param>
/// <param name="test">Dictionary containing test name, columns metadata, data to be inserted, query, and expected results.</param>
private Dictionary<string, dynamic> ExecuteQuery(string queryType, Dictionary<string, dynamic> test)
{
dynamic queryData;
if (this.perTestInsert) {
JObject testQuery = test["query"];
Dictionary<string, object> query = testQuery.ToObject<Dictionary<string, object>>();
queryData = this.TranslateColumnNames(query);
} else {
queryData = test["query"];
}
System.Console.WriteLine(" Querying");
if (this.Verbose)
{
foreach (KeyValuePair<string, dynamic> queryElement in queryData)
{
System.Console.WriteLine(string.Format(" - \"{0}\": {1}", queryElement.Key, queryElement.Value));
}
}
Dictionary<string, dynamic> result = null;
if (queryType == "count_entity")
{
result = this.Client.CountEntity(queryData);
}
else if (queryType == "count_event")
{
result = this.Client.CountEvent(queryData);
}
else if (queryType == "top_values")
{
result = this.Client.TopValues(queryData);
}
else if (queryType == "aggregation")
{
result = this.Client.Aggregation(queryData);
}
else if (queryType == "result")
{
result = this.Client.Result(queryData);
}
else if (queryType == "score")
{
result = this.Client.Score(queryData);
}
else if (queryType == "sql")
{
result = this.Client.Sql(queryData);
}
return result;
}
// Compare expected result with the result obtained from SlicingDice API
private void CompareResult(Dictionary<string, dynamic> test, Dictionary<string, dynamic> result)
{
Dictionary<string, dynamic> rawExpected = test["expected"].ToObject<Dictionary<string, dynamic>>();
Dictionary<string, dynamic> expected;
if (this.perTestInsert) {
expected = this.TranslateColumnNames(rawExpected);
} else {
expected = rawExpected;
}
if (!this.CompareDictionary(expected, result)) {
this.NumFails += 1;
this.FailedTests.Add(test["name"]);
string expectedString = JsonConvert.SerializeObject(expected).Trim();
string resultString = JsonConvert.SerializeObject(result).Trim();
System.Console.WriteLine(string.Format(" Expected: {0}", expectedString));
System.Console.WriteLine(string.Format(" Result: {0}", resultString));
System.Console.WriteLine(" Status: Failed\n");
return;
}
this.NumSuccesses += 1;
System.Console.WriteLine(" Status: Passed\n");
}
private bool CompareDictionary(IDictionary<string, dynamic> expected, IDictionary<string, dynamic> got)
{
// early-exit checks
if (null == got) {
return null == expected;
}
if (null == expected) {
return false;
}
if (object.ReferenceEquals(expected, got)) {
return true;
}
// check if keys and values are the same
foreach (string k in expected.Keys) {
if (!got.ContainsKey(k)) {
return false;
}
string expectedString = expected[k].ToString();
if(expectedString == "ignore") {
continue;
}
dynamic expectedValue = expected[k];
dynamic gotValue = got[k];
if(!this.CompareDictionaryValue(expectedValue, gotValue)) {
return false;
}
}
return true;
}
private bool CompareList(List<dynamic> expected, List<dynamic> got)
{
// early-exit checks
if (null == got) {
return null == expected;
}
if (null == expected) {
return false;
}
if (object.ReferenceEquals(expected, got)) {
return true;
}
if (expected.Count != got.Count) {
return false;
}
// check if keys and values are the same
for (var i = 0; i < expected.Count; i++) {
dynamic expectedValue = expected[i];
bool hasValue = false;
for (var j = 0; j < got.Count; j++) {
dynamic gotValue = got[j];
if (this.CompareDictionaryValue(expectedValue, gotValue)) {
hasValue = true;
}
}
if (!hasValue) {
return false;
}
}
return true;
}
private bool CompareDictionaryValue(dynamic expected, dynamic got)
{
if(expected is Dictionary<string, dynamic> || expected is JObject) {
if (expected is JObject) {
expected = expected.ToObject<Dictionary<string, dynamic>>();
got = got.ToObject<Dictionary<string, dynamic>>();
}
if(!this.CompareDictionary(expected, got)) {
return false;
}
} else if(expected is List<dynamic> || expected is JArray) {
if (expected is JArray) {
expected = expected.ToObject<List<dynamic>>();
got = got.ToObject<List<dynamic>>();
}
if(!this.CompareList(expected, got)) {
return false;
}
} else {
string expectedString = expected.ToString();
string gotString = got.ToString();
if (!expectedString.Equals(gotString)) {
return false;
}
}
return true;
}
}
}