Skip to content

Commit b265a43

Browse files
committed
Refactor Test-FabricApiResponse for parameter validation and more unit tests for the function.
1 parent 23dc130 commit b265a43

File tree

1 file changed

+106
-49
lines changed

1 file changed

+106
-49
lines changed
Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,106 @@
1-
# #Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2-
# param(
3-
# $ModuleName = "FabricTools",
4-
# $expectedParams = @(
5-
# "statusCode"
6-
# "response"
7-
# "responseHeader"
8-
# "Name"
9-
# "typeName"
10-
# "Verbose"
11-
# "Debug"
12-
# "ErrorAction"
13-
# "WarningAction"
14-
# "InformationAction"
15-
# "ProgressAction"
16-
# "ErrorVariable"
17-
# "WarningVariable"
18-
# "InformationVariable"
19-
# "OutVariable"
20-
# "OutBuffer"
21-
# "PipelineVariable"
22-
23-
# )
24-
# )
25-
26-
# Describe "Test-FabricApiResponse" -Tag "UnitTests" {
27-
28-
# BeforeDiscovery {
29-
# $command = Get-Command -Name Test-FabricApiResponse
30-
# $expected = $expectedParams
31-
# }
32-
33-
# Context "Parameter validation" {
34-
# BeforeAll {
35-
# $command = Get-Command -Name Test-FabricApiResponse
36-
# $expected = $expectedParams
37-
# }
38-
39-
# It "Has parameter: <_>" -ForEach $expected {
40-
# $command | Should -HaveParameter $PSItem
41-
# }
42-
43-
# It "Should have exactly the number of expected parameters $($expected.Count)" {
44-
# $hasparms = $command.Parameters.Values.Name
45-
# #$hasparms.Count | Should -BeExactly $expected.Count
46-
# Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
47-
# }
48-
# }
49-
# }
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2+
3+
InModuleScope FabricTools {
4+
5+
param(
6+
$ModuleName = "FabricTools",
7+
$expectedParams = @(
8+
"Response"
9+
"Name"
10+
"TypeName"
11+
"NoWait"
12+
"Verbose"
13+
"Debug"
14+
"ErrorAction"
15+
"WarningAction"
16+
"InformationAction"
17+
"ProgressAction"
18+
"ErrorVariable"
19+
"WarningVariable"
20+
"InformationVariable"
21+
"OutVariable"
22+
"OutBuffer"
23+
"PipelineVariable"
24+
)
25+
)
26+
27+
Describe "Test-FabricApiResponse" -Tag "UnitTests" {
28+
29+
BeforeDiscovery {
30+
ipmo ".\output\module\FabricTools\0.0.1\FabricTools.psd1"
31+
32+
$command = Get-Command -Name Test-FabricApiResponse
33+
$script:expected = $expectedParams
34+
}
35+
36+
Context "Parameter validation" {
37+
BeforeAll {
38+
$command = Get-Command -Name Test-FabricApiResponse
39+
$expected = $expectedParams
40+
}
41+
42+
It "Has parameter: <_>" -ForEach $expected {
43+
$command | Should -HaveParameter $PSItem
44+
}
45+
46+
It "Should have exactly the number of expected parameters $($expected.Count)" {
47+
$hasparms = $command.Parameters.Values.Name
48+
Compare-Object -ReferenceObject $script:expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
49+
}
50+
}
51+
}
52+
53+
54+
Describe "Test-FabricApiResponse - StatusCode Handling" -Tag "UnitTests" {
55+
56+
BeforeAll {
57+
# Generate a random GUID for x-ms-operation-id
58+
$script:responseHeader = @{ "x-ms-operation-id" = [guid]::NewGuid().ToString() }
59+
$script:response = @{ "foo" = "bar" }
60+
}
61+
62+
BeforeEach {
63+
# Always mock these functions
64+
Mock -CommandName Get-FabricLongRunningOperation -MockWith {
65+
return @{ "status" = "Succeeded" }
66+
}
67+
Mock -CommandName Get-FabricLongRunningOperationResult -MockWith {
68+
return "Completed"
69+
}
70+
Mock -CommandName Write-Message -MockWith { }
71+
}
72+
73+
It "Returns `$null when statusCode is 200" {
74+
$script:statusCode = 200
75+
$result = Test-FabricApiResponse -Response $script:response
76+
$result | Should -Be $null
77+
}
78+
79+
It "Returns response when statusCode is 201" {
80+
$script:statusCode = 201
81+
$result = Test-FabricApiResponse -Response $script:response
82+
$result | Should -Be $script:response
83+
}
84+
85+
It "Returns operation result when statusCode is 202 and operation succeeds" {
86+
$script:statusCode = 202
87+
$expectedResult = "Completed"
88+
$result = Test-FabricApiResponse -Response $script:response
89+
$result | Should -Be $expectedResult
90+
Should -Invoke Get-FabricLongRunningOperation -Exactly 1
91+
Should -Invoke Get-FabricLongRunningOperationResult -Exactly 1
92+
}
93+
94+
It "Returns responseHeader when statusCode is 202 and -NoWait is specified" {
95+
$script:statusCode = 202
96+
$result = Test-FabricApiResponse -Response $script:response -NoWait
97+
$result | Should -Be $script:responseHeader
98+
}
99+
100+
It "Throws when statusCode is not 200, 201, or 202" {
101+
$script:statusCode = 400
102+
{ Test-FabricApiResponse -Response $script:response -ErrorAction Stop } | Should -Throw
103+
}
104+
}
105+
106+
}

0 commit comments

Comments
 (0)