Skip to content

Commit eb46f22

Browse files
authored
Merge pull request #177 from Gijsreyn/gh-174/main/increase-code-coverage
Increase code coverage
2 parents e78a8fb + 45a0e62 commit eb46f22

214 files changed

Lines changed: 17691 additions & 7160 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- Added GitHub Codespaces and DevContainerSupport (Non Functional Change)
2525
- Added function `Update-FabricCapacity`
2626
- Added Error Detailed Info in `Test-FabricApiResponse` (Debug mode) when `response.error` exists
27+
- Enhanced Pester unit tests with coverage for many public functions including:
28+
- Domain workspace assignment functions (`Add-FabricDomainWorkspaceAssignmentByCapacity`, `Add-FabricDomainWorkspaceAssignmentById`, `Add-FabricDomainWorkspaceAssignmentByPrincipal`, `Add-FabricDomainWorkspaceRoleAssignment`)
29+
- Workspace functions (`Add-FabricWorkspaceCapacityAssignment`, `Add-FabricWorkspaceIdentity`, `Add-FabricWorkspaceRoleAssignment`, `Add-FabricWorkspaceToStage`, `Update-FabricWorkspace`, `Update-FabricWorkspaceRoleAssignment`)
30+
- Utility functions (`Convert-FromBase64`, `Convert-ToBase64`, `Connect-FabricAccount`, `Export-FabricItem`)
31+
- Update functions for various Fabric items (Notebooks, Reports, Semantic Models, Warehouses, ML models, etc.)
32+
- Tests now include scenarios for successful API calls, long-running operations, error handling, and exception handling
33+
- Increased code coverage threshold from 0.1% to 50%
2734

2835
### Changed
2936

build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Pester:
106106
# - FunctionalQuality
107107
# - TestQuality
108108
Tag:
109-
CodeCoverageThreshold: 0.10 # 85 # Set to 0 to bypass
109+
CodeCoverageThreshold: 50 # 85 # Set to 0 to bypass
110110
#CodeCoverageOutputFile: JaCoCo_$OsShortName.xml
111111
#CodeCoverageOutputFileEncoding: ascii
112112
# Use this if code coverage should be merged from several pipeline test jobs.

source/Public/Domain/Get-FabricDomainWorkspace.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Author: Tiago Balabuch
6666

6767
} catch {
6868
# Step 7: Capture and log error details
69-
$errorDetails = Get-ErrorResponse($_.Exception)
69+
$errorDetails = $_.Exception.Message
7070
Write-Message -Message "Failed to retrieve domain workspaces. Error: $errorDetails" -Level Error
7171
}
7272
}

source/Public/Item/Export-FabricItem.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ https://github.com/microsoft/Analysis-Services/tree/master/pbidevmode/fabricps-p
4545
param
4646
(
4747
[string]$path = '.\pbipOutput',
48-
[guid]$WorkspaceId = '',
48+
[Parameter(Mandatory = $false)]
49+
[guid]$WorkspaceId,
4950
[scriptblock]$filter = { $_.type -in @("Report", "SemanticModel", "Notebook", "SparkJobDefinitionV1") },
50-
[guid]$itemID = $null
51+
[Parameter(Mandatory = $false)]
52+
[guid]$itemID
5153
)
52-
if (-not $itemID) {
54+
if ($PSBoundParameters.ContainsKey('itemID')) {
5355
# Invoke the Fabric API to get the specific item in the workspace
5456

5557
$item = Invoke-FabricRestMethod -Uri "workspaces/$workspaceId/items/$itemID/getDefinition" -Method POST
Lines changed: 207 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,217 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2-
param(
3-
$ModuleName = "FabricTools",
4-
$expectedParams = @(
5-
"DomainId"
6-
"CapacitiesIds"
7-
"Verbose"
8-
"Debug"
9-
"ErrorAction"
10-
"WarningAction"
11-
"InformationAction"
12-
"ProgressAction"
13-
"ErrorVariable"
14-
"WarningVariable"
15-
"InformationVariable"
16-
"OutVariable"
17-
"OutBuffer"
18-
"PipelineVariable"
19-
20-
)
21-
)
22-
23-
Describe "Add-FabricDomainWorkspaceAssignmentByCapacity" -Tag "UnitTests" {
24-
25-
BeforeDiscovery {
26-
$command = Get-Command -Name Add-FabricDomainWorkspaceAssignmentByCapacity
27-
$expected = $expectedParams
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
2+
param ()
3+
4+
BeforeAll {
5+
$script:moduleName = 'FabricTools'
6+
7+
# If the module is not found, run the build task 'noop'.
8+
if (-not (Get-Module -Name $script:moduleName -ListAvailable)) {
9+
# Redirect all streams to $null, except the error stream (stream 2)
10+
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
11+
}
12+
13+
# Re-import the module using force to get any code changes between runs.
14+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
15+
16+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName
17+
$PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName
18+
$PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName
19+
}
20+
21+
AfterAll {
22+
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
23+
$PSDefaultParameterValues.Remove('Mock:ModuleName')
24+
$PSDefaultParameterValues.Remove('Should:ModuleName')
25+
26+
# Unload the module being tested so that it doesn't impact any other tests.
27+
Get-Module -Name $script:moduleName -All | Remove-Module -Force
28+
}
29+
30+
Describe 'Add-FabricDomainWorkspaceAssignmentByCapacity' -Tag 'Public' {
31+
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @(
32+
@{
33+
MockParameterSetName = '__AllParameterSets'
34+
MockExpectedParameters = '[-DomainId] <guid> [-CapacitiesIds] <guid[]> [<CommonParameters>]'
35+
}
36+
) {
37+
$result = (Get-Command -Name 'Add-FabricDomainWorkspaceAssignmentByCapacity').ParameterSets |
38+
Where-Object -FilterScript {
39+
$_.Name -eq $MockParameterSetName
40+
} |
41+
Select-Object -Property @(
42+
@{
43+
Name = 'ParameterSetName'
44+
Expression = { $_.Name }
45+
},
46+
@{
47+
Name = 'ParameterListAsString'
48+
Expression = { $_.ToString() }
49+
}
50+
)
51+
52+
$result.ParameterSetName | Should -Be $MockParameterSetName
53+
$result.ParameterListAsString | Should -Be $MockExpectedParameters
54+
}
55+
56+
Context 'When assigning workspaces by capacity successfully (201)' {
57+
BeforeAll {
58+
Mock -CommandName Confirm-TokenState -MockWith { }
59+
Mock -CommandName Write-Message -MockWith { }
60+
Mock -CommandName Invoke-FabricRestMethod -MockWith {
61+
InModuleScope -ModuleName 'FabricTools' {
62+
$script:statusCode = 201
63+
}
64+
return [pscustomobject]@{
65+
status = 'Succeeded'
66+
}
67+
}
68+
}
69+
70+
It 'Should call Invoke-FabricRestMethod with the correct parameters' {
71+
$mockDomainId = [guid]::NewGuid()
72+
$mockCapacityIds = @([guid]::NewGuid(), [guid]::NewGuid())
73+
74+
Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
75+
76+
Should -Invoke -CommandName Invoke-FabricRestMethod -Times 1 -ParameterFilter {
77+
$Uri -like "*admin/domains/*/assignWorkspacesByCapacities" -and
78+
$Method -eq 'Post'
79+
}
80+
}
81+
82+
It 'Should return the response on success' {
83+
$mockDomainId = [guid]::NewGuid()
84+
$mockCapacityIds = @([guid]::NewGuid())
85+
86+
$result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
87+
88+
$result.status | Should -Be 'Succeeded'
89+
}
90+
}
91+
92+
Context 'When assigning workspaces by capacity is in progress (202)' {
93+
BeforeAll {
94+
Mock -CommandName Confirm-TokenState -MockWith { }
95+
Mock -CommandName Write-Message -MockWith { }
96+
Mock -CommandName Invoke-FabricRestMethod -MockWith {
97+
InModuleScope -ModuleName 'FabricTools' {
98+
$script:statusCode = 202
99+
$script:responseHeader = @{
100+
'x-ms-operation-id' = 'op-12345'
101+
'Location' = 'https://api.fabric.microsoft.com/v1/operations/op-12345'
102+
'Retry-After' = '30'
103+
}
104+
}
105+
return [pscustomobject]@{}
106+
}
107+
Mock -CommandName Get-FabricLongRunningOperation -MockWith {
108+
return [pscustomobject]@{
109+
status = 'Succeeded'
110+
operationId = 'op-12345'
111+
}
112+
}
113+
}
114+
115+
It 'Should call Get-FabricLongRunningOperation when status is 202' {
116+
$mockDomainId = [guid]::NewGuid()
117+
$mockCapacityIds = @([guid]::NewGuid())
118+
119+
Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
120+
121+
Should -Invoke -CommandName Get-FabricLongRunningOperation -Times 1
122+
}
123+
124+
It 'Should return the operation status when long running operation succeeds' {
125+
$mockDomainId = [guid]::NewGuid()
126+
$mockCapacityIds = @([guid]::NewGuid())
127+
128+
$result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
129+
130+
$result.status | Should -Be 'Succeeded'
131+
}
132+
}
133+
134+
Context 'When long running operation fails' {
135+
BeforeAll {
136+
Mock -CommandName Confirm-TokenState -MockWith { }
137+
Mock -CommandName Write-Message -MockWith { }
138+
Mock -CommandName Invoke-FabricRestMethod -MockWith {
139+
InModuleScope -ModuleName 'FabricTools' {
140+
$script:statusCode = 202
141+
$script:responseHeader = @{
142+
'x-ms-operation-id' = 'op-failed'
143+
'Location' = 'https://api.fabric.microsoft.com/v1/operations/op-failed'
144+
'Retry-After' = '30'
145+
}
146+
}
147+
return [pscustomobject]@{}
148+
}
149+
Mock -CommandName Get-FabricLongRunningOperation -MockWith {
150+
return [pscustomobject]@{
151+
status = 'Failed'
152+
operationId = 'op-failed'
153+
error = @{
154+
message = 'Operation failed'
155+
}
156+
}
157+
}
158+
}
159+
160+
It 'Should return the failed operation status' {
161+
$mockDomainId = [guid]::NewGuid()
162+
$mockCapacityIds = @([guid]::NewGuid())
163+
164+
$result = Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
165+
166+
$result.status | Should -Be 'Failed'
167+
}
28168
}
29169

30-
Context "Parameter validation" {
170+
Context 'When an unexpected status code is returned' {
31171
BeforeAll {
32-
$command = Get-Command -Name Add-FabricDomainWorkspaceAssignmentByCapacity
33-
$expected = $expectedParams
172+
Mock -CommandName Confirm-TokenState -MockWith { }
173+
Mock -CommandName Write-Message -MockWith { }
174+
Mock -CommandName Invoke-FabricRestMethod -MockWith {
175+
InModuleScope -ModuleName 'FabricTools' {
176+
$script:statusCode = 400
177+
}
178+
return [pscustomobject]@{
179+
message = 'Bad Request'
180+
errorCode = 'InvalidRequest'
181+
}
182+
}
34183
}
35184

36-
It "Has parameter: <_>" -ForEach $expected {
37-
$command | Should -HaveParameter $PSItem
185+
It 'Should write an error message for unexpected status codes' {
186+
$mockDomainId = [guid]::NewGuid()
187+
$mockCapacityIds = @([guid]::NewGuid())
188+
189+
Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds
190+
191+
Should -Invoke -CommandName Write-Message -ParameterFilter {
192+
$Level -eq 'Error'
193+
}
38194
}
195+
}
196+
197+
Context 'When an exception is thrown' {
198+
BeforeAll {
199+
Mock -CommandName Confirm-TokenState -MockWith { }
200+
Mock -CommandName Write-Message -MockWith { }
201+
Mock -CommandName Invoke-FabricRestMethod -MockWith {
202+
throw 'API connection failed'
203+
}
204+
}
205+
206+
It 'Should handle exceptions gracefully' {
207+
$mockDomainId = [guid]::NewGuid()
208+
$mockCapacityIds = @([guid]::NewGuid())
209+
210+
{ Add-FabricDomainWorkspaceAssignmentByCapacity -DomainId $mockDomainId -CapacitiesIds $mockCapacityIds } | Should -Not -Throw
39211

40-
It "Should have exactly the number of expected parameters $($expected.Count)" {
41-
$hasparms = $command.Parameters.Values.Name
42-
#$hasparms.Count | Should -BeExactly $expected.Count
43-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
212+
Should -Invoke -CommandName Write-Message -ParameterFilter {
213+
$Level -eq 'Error' -and $Message -like "*Error occurred while assigning workspaces*"
214+
}
44215
}
45216
}
46217
}

0 commit comments

Comments
 (0)