Skip to content

Commit 44455a9

Browse files
authored
Merge pull request #12 from cmbrose/pk-range-errors
Fix PK range cache value
2 parents a02fd3c + e76d94d commit 44455a9

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

cosmos-db/cosmos-db.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# RootModule = ''
1212

1313
# Version number of this module.
14-
ModuleVersion = '1.12'
14+
ModuleVersion = '1.13'
1515

1616
# Supported PSEditions
1717
# CompatiblePSEditions = @()

cosmos-db/cosmos-db.psm1

+4-2
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,11 @@ Function Get-PartitionKeyRangesOrError
257257

258258
$ranges = $response.partitionKeyRanges
259259

260-
Set-CacheValue -Key $cacheKey -Value $ranges -Cache $PARTITION_KEY_RANGE_CACHE -ExpirationHours 6
260+
$result = @{ Ranges = $ranges }
261261

262-
@{ Ranges = $ranges }
262+
Set-CacheValue -Key $cacheKey -Value $result -Cache $PARTITION_KEY_RANGE_CACHE -ExpirationHours 6
263+
264+
$result
263265
}
264266
catch {
265267
@{ ErrorRecord = $_ }

tests/Get-PartitionKeyRangesOrError.Tests.ps1

+42-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ Get-Module cosmos-db | Remove-Module -Force
22
Import-Module $PSScriptRoot\..\cosmos-db\cosmos-db.psm1 -Force
33

44
InModuleScope cosmos-db {
5-
Describe "Get-PartitionKeyRangesOrError" {
6-
BeforeAll {
5+
Describe "Get-PartitionKeyRangesOrError" {
6+
BeforeEach {
77
Use-CosmosDbInternalFlag -EnableCaching $false
8+
}
89

10+
BeforeAll {
911
. $PSScriptRoot\Utils.ps1
1012

1113
$global:capturedNow = $null
@@ -79,6 +81,44 @@ InModuleScope cosmos-db {
7981
AssertArraysEqual $expectedRanges $result.Ranges
8082
}
8183

84+
It "Handles cached results properly" {
85+
Use-CosmosDbInternalFlag -EnableCaching $true
86+
$PARTITION_KEY_RANGE_CACHE = @{}
87+
88+
$expectedRanges = @(
89+
@{ minInclusive = ""; maxExclusive = "aa"; id = 1 };
90+
@{ minInclusive = "aa"; maxExclusive = "cc"; id = 2 };
91+
)
92+
93+
$response = @{
94+
StatusCode = 200;
95+
Content = (@{ partitionKeyRanges = $expectedRanges } | ConvertTo-Json -Depth 100)
96+
}
97+
98+
Mock Invoke-CosmosDbApiRequest {
99+
param($verb, $url, $body, $headers)
100+
101+
VerifyInvokeCosmosDbApiRequest $verb $url $body $headers | Out-Null
102+
103+
$response
104+
}
105+
106+
Write-host "1"
107+
$_ = Get-PartitionKeyRangesOrError -ResourceGroup $MOCK_RG -SubscriptionId $MOCK_SUB -Database $MOCK_DB -Container $MOCK_CONTAINER -Collection $MOCK_COLLECTION
108+
109+
$urlKey = "https://$MOCK_DB.documents.azure.com/dbs/$MOCK_CONTAINER/colls/$MOCK_COLLECTION/pkranges"
110+
$cache = Get-CacheValue -Key $urlKey -Cache $PARTITION_KEY_RANGE_CACHE
111+
112+
$cache.ErrorRecord | Should -BeNull
113+
AssertArraysEqual $expectedRanges $cache.Ranges
114+
115+
$result = Get-PartitionKeyRangesOrError -ResourceGroup $MOCK_RG -SubscriptionId $MOCK_SUB -Database $MOCK_DB -Container $MOCK_CONTAINER -Collection $MOCK_COLLECTION
116+
Assert-MockCalled Invoke-CosmosDbApiRequest -Times 1
117+
118+
$result.ErrorRecord | Should -BeNull
119+
AssertArraysEqual $expectedRanges $result.Ranges
120+
}
121+
82122
It "Should handle exceptions gracefully" {
83123
$exception = [System.Net.WebException]::new("", $null, [System.Net.WebExceptionStatus]::UnknownError, [System.Net.HttpWebResponse]@{})
84124

0 commit comments

Comments
 (0)