-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUnitTest.Discover.pq
104 lines (101 loc) · 3.63 KB
/
UnitTest.Discover.pq
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
/**
Discover and run tests from all local sources.
Represent results as a table either grouped by status (compact_output = true)
or detailed with one row per each test (compact_output = false).
**/
(optional compact_output as nullable logical) =>
let
/* Default behavior is not yet decided upon and may be changed in future */
Compact = if compact_output <> null then compact_output else true,
Library = LibPQ(),
Config = LibPQ("UnitTest.Constants"),
UnitTest.Run = LibPQ("UnitTest.Run"),
SuitesFilter = Table.SelectRows(
Table.AddColumn(
Record.ToTable(Library),
"SuiteVersion",
each
try Text.From(
Record.Field(
Value.Metadata([Value]),
Config[Suite.MetaField]
)
) otherwise if LooksLikeSuiteWithError([Name], [Value])
then
ErrorSuiteMarker
else
null
),
each [SuiteVersion] <> null
),
LooksLikeSuiteWithError = (name, object) =>
let
ModuleFilename = object[Detail][LibPQ][LibPQ.Source], // error record is assumed
Read.Text = LibPQ("Read.Text"),
ModuleSource = Read.Text(ModuleFilename, true),
ModuleIsTestSuite = Text.Contains(ModuleSource, Config[Suite.MetaField]),
ObjLikeError =
Value.Is(object, type record)
and Record.FieldCount(object) = 3
and Record.FieldNames(object) = {"Reason", "Message", "Detail"},
Return = ObjLikeError and ModuleIsTestSuite // Order matters for short circuiting.
// ModuleIsTestSuite assumes that
// object is an error record
in
Return,
PropagateErrors = (suite) =>
#table(
{ "Test", "Result", "Status", "Description"},
{{ null, suite, "ERROR", suite[Reason] & ": " &suite[Message]}}
),
ErrorSuiteMarker = "This looks like a test suite with invalid code",
SuitesValid =
if Table.IsEmpty(SuitesFilter)
then error Error.Record("LibPQ.TestsNotFound", "No tests were found among local modules", null)
else SuitesFilter,
Suites = Table.RenameColumns(
SuitesValid,
List.Zip({
Table.ColumnNames(SuitesValid),
{"Suite", "Object", "Version"}
})
),
SuiteRunners =
Record.AddField(
Record.FromList(
List.Transform(
Record.FieldValues(Config[Suite.Runners]),
each LibPQ(_)
),
Record.FieldNames(Config[Suite.Runners])
),
ErrorSuiteMarker,
PropagateErrors
),
Run = Table.AddColumn(
Suites,
"Results",
each Record.Field(SuiteRunners, [Version])([Object])
),
Expanded = Table.ExpandTableColumn(
Table.RemoveColumns(Run, "Version"),
"Results",
Table.ColumnNames(Run[Results]{0})
),
LongResults =
Table.ReplaceErrorValues(
Table.RemoveColumns(Expanded, {"Object"}),
{"Status", "ERROR"}
),
ShortResults = Table.Group(
LongResults,
{"Status"},
{
{"Count", each Table.RowCount(_), type number},
{"Details", each _, type table}
}
),
ShortResultsSorted = Table.Sort(ShortResults, {{"Status", Order.Ascending}}),
Return = if Compact then ShortResultsSorted else LongResults
in
Return