-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
ExecutableQueryFailure.cs
59 lines (48 loc) · 1.88 KB
/
ExecutableQueryFailure.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
using ApprovalTests.Core;
using ApprovalUtilities.Persistence;
namespace ApprovalTests.Reporters;
public class ExecutableQueryFailure(IExecutableQuery query, IApprovalFailureReporter reporter) :
IApprovalFailureReporter, IApprovalReporterWithCleanUp
{
const string FileNameSuffix = ".queryresults.txt";
const string Header = """
Do NOT approve
This File will be Deleted
it is for feedback purposes only.
An additional file has been opened with only the query which you can approve.
""";
public void CleanUp(string approved, string received)
{
File.Delete(approved + FileNameSuffix);
File.Delete(received + FileNameSuffix);
}
public void Report(string approved, string received)
{
reporter.Report(approved, received);
var receivedResult = ExecuteQuery(received);
var approvedResult = ExecuteQuery(approved);
if (string.IsNullOrEmpty(approvedResult.Result) && string.IsNullOrEmpty(receivedResult.Result))
{
return;
}
var r = RunQueryAndGetPath(received, receivedResult);
var a = RunQueryAndGetPath(approved, approvedResult);
reporter.Report(a, r);
}
static string RunQueryAndGetPath(string fileName, QueryResult result)
{
var newFileName = fileName + FileNameSuffix;
File.WriteAllText(newFileName, $"{Header}query:\n\n{result.Query}\n\nresult:\n\n{result.Result}");
return newFileName;
}
QueryResult ExecuteQuery(string fileName)
{
if (!File.Exists(fileName))
{
return new();
}
var newQuery = File.ReadAllText(fileName).Trim();
var newResult = query.ExecuteQuery(newQuery);
return new() { Query = newQuery, Result = newResult };
}
}