Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit b6010f1

Browse files
authored
[Test] Add scenario test for executing a generated cmdlet with flattened parameters (#285)
* [Test] Added scenario test for executing a generated cmdlet with flattened parameters
1 parent 97bde15 commit b6010f1

4 files changed

+292
-23
lines changed

Tests/PSSwaggerMetadata.Scenario.Tests.ps1

+26
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,27 @@ Describe "Tests for New-PSSwaggerModule with Swagger spec and its .psmeta.json f
419419
$GeneratedModuleName = 'Generated.TestSwaggerSpecWithPSMetaFile.Module'
420420
Initialize-Test -GeneratedModuleName $GeneratedModuleName -TestApiName "psmetadatatest" `
421421
-TestSpecFileName "TestSwaggerSpecWithPSMetaFile.json" `
422+
-TestDataFileName "TestSwaggerSpecWithPSMetaFileData.json" `
422423
-PSSwaggerPath $PSSwaggerPath -TestRootPath $PSScriptRoot
423424

424425
# Import generated module
425426
Write-Verbose "Importing modules"
426427
Import-Module (Join-Path -Path $PSSwaggerPath -ChildPath "PSSwagger.Common.Helpers") -Force
427428
Import-Module (Join-Path -Path $PSSwaggerPath -ChildPath "PSSwagger.Azure.Helpers") -Force
428429
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | Join-Path -ChildPath $GeneratedModuleName) -Force
430+
431+
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName 'psmetadatatest'
432+
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
433+
$script:EnableTracer = $false
434+
Initialize-PSSwaggerDependencies -AcceptBootstrap
435+
Import-Module "$PSScriptRoot\PSSwaggerTestTracing.psm1"
436+
[Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor((New-PSSwaggerTestClientTracing))
437+
[Microsoft.Rest.ServiceClientTracing]::IsEnabled = $true
438+
}
439+
}
440+
441+
AfterAll {
442+
Stop-JsonServer -JsonServerProcess $processes.ServerProcess -NodeProcess $processes.NodeProcess
429443
}
430444

431445
Context "Validate extended cmdlet name using .psmeta.json" {
@@ -470,6 +484,18 @@ Describe "Tests for New-PSSwaggerModule with Swagger spec and its .psmeta.json f
470484
}
471485
}
472486

487+
It "Validate flattening of complex parameter by running the generated command" {
488+
Get-Command -ParameterName 'FlatCupCakeParameters' -Name 'Get-FlatCupCake' -Module $GeneratedModuleName -ErrorAction Ignore | Should BeNullOrEmpty
489+
490+
@('AgeInDays', 'AgeInYears', 'Flavor') | ForEach-Object {
491+
$ExpectedParameterName = $_
492+
Get-Command -ParameterName $ExpectedParameterName -Name 'Get-FlatCupCake' -Module $GeneratedModuleName | Where-Object {$_.Parameters.Keys -contains $ExpectedParameterName} | Should Not BeNullOrEmpty
493+
}
494+
495+
Get-FlatCupCake -AgeInYears 1 -AgeInDays 365 -Flavor mint-chocolate | Should Not BeNullOrEmpty
496+
Get-FlatCupCake -AgeInYears 2 -AgeInDays 36 -Flavor vanilla | Should BeNullOrEmpty
497+
}
498+
473499
It "Validate 'New-<Type>Object' function generation for the complex types used for the global parameters" {
474500
Get-Command -Name New-QuotaParametersObject -Module $GeneratedModuleName | Should Not BeNullOrEmpty
475501
}

Tests/data/psmetadatatest/TestSwaggerSpecWithPSMetaFile.json

+101-23
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,14 @@
1414
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
1515
}
1616
},
17-
"host": "management.azure.com",
17+
"host": "localhost:3000",
1818
"schemes": [
19-
"https"
20-
],
21-
"consumes": [
22-
"application/json"
19+
"http"
2320
],
21+
"basePath": "/",
2422
"produces": [
2523
"application/json"
2624
],
27-
"security": [
28-
{
29-
"azure_auth": [
30-
"user_impersonation"
31-
]
32-
}
33-
],
34-
"securityDefinitions": {
35-
"azure_auth": {
36-
"type": "oauth2",
37-
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
38-
"flow": "implicit",
39-
"description": "Azure Active Directory OAuth2 Flow",
40-
"scopes": {
41-
"user_impersonation": "Impersonate your user account"
42-
}
43-
}
44-
},
4525
"paths": {
4626
"/subscriptions/{subscriptionId}/providers/Microsoft.DocumentDB/databaseAccounts": {
4727
"get": {
@@ -200,6 +180,52 @@
200180
}
201181
}
202182
}
183+
},
184+
"/flatcupcakes": {
185+
"get": {
186+
"summary": "List all flatcupcakes matching parameters",
187+
"operationId": "FlatCupcake_List",
188+
"description": "Make a flatcupcake or update an existing one.",
189+
"parameters": [
190+
{
191+
"name": "flavor",
192+
"in": "query",
193+
"description": "Flavor of flatcupcake",
194+
"required": false,
195+
"type": "string",
196+
"default": "mint-chocolate"
197+
},
198+
{
199+
"name": "FlatCupcakeAgeParameter",
200+
"in": "query",
201+
"required": false,
202+
"schema": {
203+
"$ref": "#/definitions/FlatCupcakeAge"
204+
},
205+
"description": "parameter description"
206+
}
207+
],
208+
"tags": [
209+
"FlatCupcakes"
210+
],
211+
"responses": {
212+
"200": {
213+
"description": "All flatcupcake entities",
214+
"schema": {
215+
"type": "array",
216+
"items": {
217+
"$ref": "#/definitions/FlatCupcake"
218+
}
219+
}
220+
},
221+
"default": {
222+
"description": "Error",
223+
"schema": {
224+
"$ref": "#/definitions/Error"
225+
}
226+
}
227+
}
228+
}
203229
}
204230
},
205231
"definitions": {
@@ -458,6 +484,58 @@
458484
"$ref": "#/definitions/Resource"
459485
}
460486
]
487+
},
488+
"FlatCupcake": {
489+
"type": "object",
490+
"properties": {
491+
"id": {
492+
"type": "string",
493+
"description": "Unique identifier"
494+
},
495+
"flavor": {
496+
"type": "string",
497+
"description": "String test",
498+
"default": "mint-chocolate"
499+
},
500+
"ageInYears": {
501+
"type": "integer",
502+
"format": "int32",
503+
"description": "Int32 test"
504+
},
505+
"ageInDays": {
506+
"type": "integer",
507+
"format": "int64",
508+
"description": "Int64 test"
509+
}
510+
}
511+
},
512+
"FlatCupcakeAge": {
513+
"type": "object",
514+
"properties": {
515+
"ageInYears": {
516+
"type": "integer",
517+
"format": "int32",
518+
"description": "Int32 test"
519+
},
520+
"ageInDays": {
521+
"type": "integer",
522+
"format": "int64",
523+
"description": "Int64 test"
524+
}
525+
}
526+
},
527+
"Error": {
528+
"type": "object",
529+
"properties": {
530+
"code": {
531+
"type": "string",
532+
"description": "Error code"
533+
},
534+
"message": {
535+
"type": "string",
536+
"description": "Message"
537+
}
538+
}
461539
}
462540
},
463541
"parameters": {

Tests/data/psmetadatatest/TestSwaggerSpecWithPSMetaFile.psmeta.json

+155
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,34 @@
136136
}
137137
}
138138
}
139+
},
140+
"/flatcupcakes": {
141+
"get": {
142+
"x-ps-cmdlet-infos": [
143+
{
144+
"name": "Get-FlatCupcake",
145+
"description": "Make a flatcupcake or update an existing one.",
146+
"defaultParameterSet": "FlatCupcake_List",
147+
"generateCommand": true
148+
}
149+
],
150+
"parameters": {
151+
"FlatCupcakeAgeParameter": {
152+
"x-ps-parameter-info": {
153+
"name": "FlatCupcakeAgeParameter",
154+
"description": "parameter description",
155+
"flatten": true
156+
}
157+
},
158+
"flavor": {
159+
"x-ps-parameter-info": {
160+
"name": "Flavor",
161+
"description": "Flavor of flatcupcake",
162+
"flatten": false
163+
}
164+
}
165+
}
166+
}
139167
}
140168
},
141169
"definitions": {
@@ -261,6 +289,41 @@
261289
}
262290
}
263291
},
292+
"FlatCupcakeAge": {
293+
"x-ps-cmdlet-infos": [
294+
{
295+
"name": "New-FlatCupcakeAgeObject",
296+
"description": "",
297+
"defaultParameterSet": "FlatCupcakeAge",
298+
"generateCommand": true,
299+
"generateOutputFormat": true
300+
}
301+
],
302+
"properties": {
303+
"ageInYears": {
304+
"x-ps-parameter-info": {
305+
"name": "AgeInYears",
306+
"description": "Int32 test"
307+
},
308+
"x-ps-output-format-info": {
309+
"include": true,
310+
"position": 0,
311+
"width": 10
312+
}
313+
},
314+
"ageInDays": {
315+
"x-ps-parameter-info": {
316+
"name": "AgeInDays",
317+
"description": "Int64 test"
318+
},
319+
"x-ps-output-format-info": {
320+
"include": true,
321+
"position": 1,
322+
"width": 10
323+
}
324+
}
325+
}
326+
},
264327
"StorageQuota": {
265328
"x-ps-cmdlet-infos": [
266329
{
@@ -334,6 +397,63 @@
334397

335398
}
336399
},
400+
"FlatCupcake": {
401+
"x-ps-cmdlet-infos": [
402+
{
403+
"name": "New-FlatCupcakeObject",
404+
"description": "",
405+
"defaultParameterSet": "FlatCupcake",
406+
"generateCommand": true,
407+
"generateOutputFormat": true
408+
}
409+
],
410+
"properties": {
411+
"ageInYears": {
412+
"x-ps-parameter-info": {
413+
"name": "AgeInYears",
414+
"description": "Int32 test"
415+
},
416+
"x-ps-output-format-info": {
417+
"include": true,
418+
"position": 0,
419+
"width": 10
420+
}
421+
},
422+
"ageInDays": {
423+
"x-ps-parameter-info": {
424+
"name": "AgeInDays",
425+
"description": "Int64 test"
426+
},
427+
"x-ps-output-format-info": {
428+
"include": true,
429+
"position": 1,
430+
"width": 10
431+
}
432+
},
433+
"id": {
434+
"x-ps-parameter-info": {
435+
"name": "Id",
436+
"description": "Unique identifier"
437+
},
438+
"x-ps-output-format-info": {
439+
"include": true,
440+
"position": 2,
441+
"width": 10
442+
}
443+
},
444+
"flavor": {
445+
"x-ps-parameter-info": {
446+
"name": "Flavor",
447+
"description": "String test"
448+
},
449+
"x-ps-output-format-info": {
450+
"include": true,
451+
"position": 3,
452+
"width": 10
453+
}
454+
}
455+
}
456+
},
337457
"DatabaseAccountCreateUpdateParameters": {
338458
"x-ps-cmdlet-infos": [
339459
{
@@ -463,6 +583,41 @@
463583
}
464584
}
465585
},
586+
"Error": {
587+
"x-ps-cmdlet-infos": [
588+
{
589+
"name": "New-ErrorObject",
590+
"description": "",
591+
"defaultParameterSet": "Error",
592+
"generateCommand": true,
593+
"generateOutputFormat": true
594+
}
595+
],
596+
"properties": {
597+
"code": {
598+
"x-ps-parameter-info": {
599+
"name": "Code",
600+
"description": "Error code"
601+
},
602+
"x-ps-output-format-info": {
603+
"include": true,
604+
"position": 0,
605+
"width": 10
606+
}
607+
},
608+
"message": {
609+
"x-ps-parameter-info": {
610+
"name": "Message",
611+
"description": "Message"
612+
},
613+
"x-ps-output-format-info": {
614+
"include": true,
615+
"position": 1,
616+
"width": 10
617+
}
618+
}
619+
}
620+
},
466621
"Resource": {
467622
"x-ps-cmdlet-infos": [
468623
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"flatcupcakes": [
3+
{
4+
"id": "1",
5+
"flavor": "mint-chocolate",
6+
"ageInYears": 1,
7+
"ageInDays": 365
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)