Skip to content

[no important files changed] Fix REST API tests #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions exercises/practice/rest-api/RestApi.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
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]) {
$gotKeys = $got.Keys | Sort-Object
$wantKeys = $want.Keys | Sort-Object
$gotKeys | Should -BeExactly $wantKeys -Because "those are the expected keys for $path"

foreach ($entry in $want.GetEnumerator()) {
Test-ApiResult $got[$entry.Key] $entry.Value "$path[`"$($entry.Key)`"]"
}
}
elseif ($want -is [array]) {
$gotCount = $got.Count
$wantCount = $want.Count
$gotCount | Should -BeExactly $wantCount -Because "that is the expected number of $path values"

for ($i = 0; $i -lt $wantCount; $i++) {
Test-ApiResult $got[$i] $want[$i] "$path[$i]"
}
}
else {
$got | Should -BeExactly $want -Because "that is the expected value for $path"
}
}
}

Describe "RestApi test cases" {
Expand All @@ -9,7 +48,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" {
Expand All @@ -20,7 +59,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" {
Expand All @@ -38,7 +77,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
Expand Down Expand Up @@ -70,7 +109,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" {
Expand All @@ -90,7 +129,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" {
Expand All @@ -110,7 +149,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" {
Expand All @@ -129,7 +168,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" {
Expand All @@ -148,7 +187,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" {
Expand All @@ -167,6 +206,6 @@ Describe "RestApi test cases" {
)
}

($got | ConvertTo-Json -Depth 5) | Should -BeExactly ($want | ConvertTo-Json -Depth 5)
Test-ApiResult $got $want
}
}