private/Get-AutRunResult.ps1

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
<#
.SYNOPSIS
Controller script used to generate results of a test run
 
.DESCRIPTION
See documentation for more information : https://github.com/Fleid/asa.unittest
 
.PARAMETER solutionPath
Path to the solution (folder) containing both the Azure Stream Analytics folder and the unittest folder
 
.PARAMETER asaProjectName
Name of the Azure Stream Analytics project = name of the project folder = name of the query in that folder (.asaql) = name of the project description file (.asaproj)
 
.PARAMETER unittestFolder
Name of the folder containing the test fixture (folders 1_arrange, 2_act, 3_assert), usually "asaProjectName.Tests"
 
.PARAMETER testId
Timestamp of the test run (yyyyMMddHHmmss), will be used in the folder structure
 
.PARAMETER testCase
Current test to be run (001, 002, 003...)
 
.EXAMPLE
Get-AutRunResult -solutionPath $solutionPath -asaProjectName $asaProjectName -unittestFolder $unittestFolder -testID $testID -testCase $testCase
#>


Function Get-AutRunResult{

    [CmdletBinding()]
    [OutputType([int])]
    param (
        [string]$solutionPath = $(Throw "-solutionPath is required"),
        [string]$asaProjectName = $(Throw "-asaProjectName is required"),
        [string]$unittestFolder = $(Throw "-unittestFolder is required"),
        [string]$testID = $(Throw "-testID is required"),
        [string]$testCase = $(Throw "-testCase is required")
    )

    BEGIN {
        if (-not (Test-Path($solutionPath))) {throw "$solutionPath is not a valid path"}

        $testPath = "$solutionPath\$unittestFolder\3_assert\$testID\$testCase"
        if (-not (Test-Path($testPath))) {throw "$testPath is not a valid path"}

        $outputSourcePath = "$testPath\$asaProjectName\Inputs"
        if (-not (Test-Path($outputSourcePath))) {throw "$outputSourcePath is not a valid path"}
    }

    PROCESS {
        $errorCounter = 0

        $testDetails = (Get-ChildItem -Path $outputSourcePath -File) |
            Get-AutFieldFromFileInfo -s "~" -n 4 |
            Select-Object `
                FullName, `
                FilePath, `
                Basename, `
                @{Name = "TestCase"; Expression = {$_.Basename0}}, `
                @{Name = "FileType"; Expression = {$_.Basename1}}, `
                @{Name = "SourceName"; Expression = {$_.Basename2}}, `
                @{Name = "TestLabel"; Expression = {$_.Basename3}}

        $testDetails | Where-Object { $_.FileType -eq "Output" } |
        Select-Object `
            FullName,
            SourceName,
            TestCase,
            @{Name = "rawContent"; Expression = {"$testPath\$($_.SourceName).json"}}, #sa.exe output
            @{Name = "testableFilePath"; Expression = {"$testPath\$($_.SourceName).testable.json"}}, #to be generated
            @{Name = "testCaseOutputFile"; Expression = {"$testPath\$asaProjectName\Inputs\$($_.FullName)"}} |
        Foreach-Object -process {

            # Prepare input content (sorting)
            $referenceContent = Get-Content -Path $_.testCaseOutputFile | ConvertFrom-Json

            $referenceContentProperties = `
                    $referenceContent  | `
                    Get-Member -MemberType NoteProperty | `
                    Sort-Object -Property Name | `
                    Select-Object -ExpandProperty Name

            $referenceSortedContent = $referenceContent | Sort-Object -Property $referenceContentProperties | ConvertTo-JSON

            $sortedTestCaseOutputFile = "$($_.testCaseOutputFile).sorted.json"
            Add-Content -Path $sortedTestCaseOutputFile -Value $referenceSortedContent

            # Prepare output content (format, sorting)
            $testableContent = ("[$(Get-Content -Path $_.rawContent)]") | ConvertFrom-Json

            $testableContentProperties = `
                    $testableContent | `
                    Get-Member -MemberType NoteProperty | `
                    Sort-Object -Property Name | `
                    Select-Object -ExpandProperty Name

            $testableSortedContent = $testableContent | Sort-Object -Property $testableContentProperties | ConvertTo-JSON

            Add-Content -Path $_.testableFilePath -Value $testableSortedContent

            # Actual testing
            $testResult = Invoke-External -l "jsondiffpatch" $sortedTestCaseOutputFile $_.testableFilePath;
            $testResult | Out-File "$testPath\$($_.SourceName).Result.txt"
            if ($testResult) {$errorCounter++}
        }

        $errorCounter

    } #PROCESS
    END {}
}