@@ -2,10 +2,12 @@ Get-Module cosmos-db | Remove-Module -Force
2
2
Import-Module $PSScriptRoot \..\cosmos- db\cosmos- db.psm1 - Force
3
3
4
4
InModuleScope cosmos- db {
5
- Describe " Get-PartitionKeyRangesOrError" {
6
- BeforeAll {
5
+ Describe " Get-PartitionKeyRangesOrError" {
6
+ BeforeEach {
7
7
Use-CosmosDbInternalFlag - EnableCaching $false
8
+ }
8
9
10
+ BeforeAll {
9
11
. $PSScriptRoot \Utils.ps1
10
12
11
13
$global :capturedNow = $null
@@ -79,6 +81,44 @@ InModuleScope cosmos-db {
79
81
AssertArraysEqual $expectedRanges $result.Ranges
80
82
}
81
83
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
+
82
122
It " Should handle exceptions gracefully" {
83
123
$exception = [System.Net.WebException ]::new(" " , $null , [System.Net.WebExceptionStatus ]::UnknownError, [System.Net.HttpWebResponse ]@ {})
84
124
0 commit comments