@@ -2,10 +2,12 @@ Get-Module cosmos-db | Remove-Module -Force
22Import-Module $PSScriptRoot \..\cosmos- db\cosmos- db.psm1 - Force
33
44InModuleScope 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