From f08440b72d088023f2985969ff202fa8a62494db Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Thu, 26 Jun 2025 18:59:42 -0500 Subject: [PATCH 1/4] Fix REST API tests --- exercises/practice/rest-api/RestApi.tests.ps1 | 48 +++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/exercises/practice/rest-api/RestApi.tests.ps1 b/exercises/practice/rest-api/RestApi.tests.ps1 index db65bb1..42f8c3c 100644 --- a/exercises/practice/rest-api/RestApi.tests.ps1 +++ b/exercises/practice/rest-api/RestApi.tests.ps1 @@ -1,5 +1,35 @@ BeforeAll { . "./RestApi.ps1" + + Function Test-ApiResult([object]$got, [object]$want, [string[]]$path = $null) { + $basePath = $path -join "." + if ($want -is [hashtable]) { + # Make sure keys are the same + $gotKeys = $got.Keys | Sort-Object + $wantKeys = $want.Keys | Sort-Object + $gotKeys | Should -BeExactly $wantKeys -Because "$basePath keys" + + # Make sure values are the same + foreach ($entry in $want.GetEnumerator()) { + Test-ApiResult $got[$entry.Key] $entry.Value ($path + $entry.Key) + } + } + elseif ($want -is [array]) { + # Make number of values is the same + $gotCount = $got.Count + $wantCount = $want.Count + $gotCount | Should -BeExactly $wantCount -Because "$basePath value count" + + # Make sure values are the same + for ($i = 0; $i -lt $wantCount; $i++) { + Test-ApiResult $got[$i] $want[$i] ($path + "[$i]") + } + } + else { + # Make sure value is the same + $got | Should -BeExactly $want -Because $basePath + } + } } Describe "RestApi test cases" { @@ -9,7 +39,7 @@ Describe "RestApi test cases" { $got = $api.Get("/users") $want = @{users = @()} - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "user management -> add user" { @@ -20,7 +50,7 @@ Describe "RestApi test cases" { name = "Adam"; owes = @{}; owed_by = @{}; balance = 0.0 } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "user management -> get single user" { @@ -38,7 +68,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } # addition test to make sure you can't add existing user @@ -70,7 +100,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "iou -> borrower has negative balance" { @@ -90,7 +120,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "iou -> lender has negative balance" { @@ -110,7 +140,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "iou -> lender owes borrower" { @@ -129,7 +159,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "iou -> lender owes borrower less than new loan" { @@ -148,7 +178,7 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } It "iou -> lender owes borrower same as new loan" { @@ -167,6 +197,6 @@ Describe "RestApi test cases" { ) } - ($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5) + Test-ApiResult $got $want } } From 2ec3f494ebbe6b125a1d10747073aa0fdce79b93 Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Thu, 26 Jun 2025 19:29:50 -0500 Subject: [PATCH 2/4] Need to make sure that path is an array --- exercises/practice/rest-api/RestApi.tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/practice/rest-api/RestApi.tests.ps1 b/exercises/practice/rest-api/RestApi.tests.ps1 index 42f8c3c..c724070 100644 --- a/exercises/practice/rest-api/RestApi.tests.ps1 +++ b/exercises/practice/rest-api/RestApi.tests.ps1 @@ -2,6 +2,7 @@ BeforeAll { . "./RestApi.ps1" Function Test-ApiResult([object]$got, [object]$want, [string[]]$path = $null) { + $path = $path ?? @() $basePath = $path -join "." if ($want -is [hashtable]) { # Make sure keys are the same From c3ac75403d5b1bc842280e04a942f819ca82fbd0 Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Fri, 27 Jun 2025 17:38:54 -0500 Subject: [PATCH 3/4] Improved error messages --- exercises/practice/rest-api/RestApi.tests.ps1 | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/exercises/practice/rest-api/RestApi.tests.ps1 b/exercises/practice/rest-api/RestApi.tests.ps1 index c724070..73b5344 100644 --- a/exercises/practice/rest-api/RestApi.tests.ps1 +++ b/exercises/practice/rest-api/RestApi.tests.ps1 @@ -1,34 +1,32 @@ BeforeAll { . "./RestApi.ps1" - Function Test-ApiResult([object]$got, [object]$want, [string[]]$path = $null) { - $path = $path ?? @() - $basePath = $path -join "." + Function Test-ApiResult([object]$got, [object]$want, [string]$path = "expected") { if ($want -is [hashtable]) { # Make sure keys are the same $gotKeys = $got.Keys | Sort-Object $wantKeys = $want.Keys | Sort-Object - $gotKeys | Should -BeExactly $wantKeys -Because "$basePath keys" + $gotKeys | Should -BeExactly $wantKeys -Because "those are the expected keys for $path" # Make sure values are the same foreach ($entry in $want.GetEnumerator()) { - Test-ApiResult $got[$entry.Key] $entry.Value ($path + $entry.Key) + Test-ApiResult $got[$entry.Key] $entry.Value "$path[`"$($entry.Key)`"]" } } elseif ($want -is [array]) { # Make number of values is the same $gotCount = $got.Count $wantCount = $want.Count - $gotCount | Should -BeExactly $wantCount -Because "$basePath value count" + $gotCount | Should -BeExactly $wantCount -Because "that is the expected number of $path values" # Make sure values are the same for ($i = 0; $i -lt $wantCount; $i++) { - Test-ApiResult $got[$i] $want[$i] ($path + "[$i]") + Test-ApiResult $got[$i] $want[$i] "$path[$i]" } } else { # Make sure value is the same - $got | Should -BeExactly $want -Because $basePath + $got | Should -BeExactly $want -Because "that is the expected value for $path" } } } From 4e42b4349c733b5b46bd886ea868e15fa16f1cfc Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Tue, 1 Jul 2025 06:59:36 -0500 Subject: [PATCH 4/4] Add description for Test-ApiResult --- exercises/practice/rest-api/RestApi.tests.ps1 | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/exercises/practice/rest-api/RestApi.tests.ps1 b/exercises/practice/rest-api/RestApi.tests.ps1 index 73b5344..51f6cc5 100644 --- a/exercises/practice/rest-api/RestApi.tests.ps1 +++ b/exercises/practice/rest-api/RestApi.tests.ps1 @@ -2,30 +2,40 @@ BeforeAll { . "./RestApi.ps1" Function Test-ApiResult([object]$got, [object]$want, [string]$path = "expected") { + <# + .SYNOPSIS + Test the result returned from a REST API call to make sure that it matches the expected value. + + .DESCRIPTION + Recursively test the result returned from a REST API call against the expected value: + + - For a hash table: + - Make sure that the keys match + - Recursively check that each value matches + - For an array: + - Make sure that the number of values match + - Recursively check that each value matches + - For a simple value, make sure that the value matches + #> if ($want -is [hashtable]) { - # Make sure keys are the same $gotKeys = $got.Keys | Sort-Object $wantKeys = $want.Keys | Sort-Object $gotKeys | Should -BeExactly $wantKeys -Because "those are the expected keys for $path" - # Make sure values are the same foreach ($entry in $want.GetEnumerator()) { Test-ApiResult $got[$entry.Key] $entry.Value "$path[`"$($entry.Key)`"]" } } elseif ($want -is [array]) { - # Make number of values is the same $gotCount = $got.Count $wantCount = $want.Count $gotCount | Should -BeExactly $wantCount -Because "that is the expected number of $path values" - # Make sure values are the same for ($i = 0; $i -lt $wantCount; $i++) { Test-ApiResult $got[$i] $want[$i] "$path[$i]" } } else { - # Make sure value is the same $got | Should -BeExactly $want -Because "that is the expected value for $path" } }