Skip to content

Commit

Permalink
Add spread test cases (#247)
Browse files Browse the repository at this point in the history
* Update test cases for spread

* Update the spread test cases

* Add changelog

* Add test case

* Update summery

* Update summary

* Remove the test case for quantum model considerint we are still under discussion over them

* Update the comments

* Update the comments

* Update the docs

* Remove this case considering we have no clear conscencus on what it should do

* Update the summery doc

* Update the summery doc

* Update the sumary

* Reorgnized to sub-namespaces

* Update the summary

* Update the summary

* Reorganize the test cases' positions
  • Loading branch information
MaryGao authored Apr 18, 2023
1 parent 279957d commit 5a6d929
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-geckos-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/cadl-ranch-specs": patch
---

Add test case for spread operator
93 changes: 93 additions & 0 deletions packages/cadl-ranch-specs/cadl-ranch-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,99 @@ This test is testing sending a csv collection format array query parameters

This test is testing sending a csv collection format array header parameters

### Parameters_Spread_Model_spreadAsRequestBody

- Endpoint: `put /parameters/spread/model/request-body`

Test case for spread named model.

Should generate request body model named `BodyParameter`.
Should generate an operation like below:

```
spreadAsRequestBody(bodyParameter: BodyParameter)
```

Note the parameter name is guessed from the model name and it may vary by language.

Expected request body:

```json
{ "name": "foo" }
```

### Parameters_Spread_Alias_spreadAsRequestBody

- Endpoint: `put /parameters/spread/alias/request-body`

Test case for spread alias.

Should not generate any model named `BodyParameter`.
Should generate an operation like:

```
spreadAsRequestBody(name: string)
```

Expected request body:

```json
{ "name": "foo" }
```

### Parameters_Spread_Alias_spreadAsRequestParameter

- Endpoint: `put /parameters/spread/alias/request-parameter/{id}`

Test case for spread alias with path and header parameter.

Should not generate any model named `RequestParameter`.
Should generate an operation like below:

```
spreadAsRequestParameter(id: string, x_ms_test_header: string, name: string)
```

Note the parameter name may be normalized and vary by language.

Expected path parameter: id="1"
Expected header parameter: x-ms-test-header="bar"
Expected request body:

```json
{ "name": "foo" }
```

### Parameters_Spread_Alias_spreadWithMultipleParameters

- Endpoint: `put /parameters/spread/alias/multiple-parameters/{id}`

Test case for spread alias including 6 parameters. May handle as property bag for these parameters.

Should not generate any model named `AliasMultipleRequestParameters`.
Should generate an operation like below:

```
spreadWithMultipleParameters(id: string, x_ms_test_header: string, prop1: string, prop2: string, prop3: string, prop4: string, prop5: string, prop6: string)
```

Note it's also acceptable if some languages handle it as property bag.

Expected path parameter: id="1"
Expected header parameter: x-ms-test-header="bar"
Expected request body:

```json
{
"prop1": "foo1",
"prop2": "foo2",
"prop3": "foo3",
"prop4": "foo4",
"prop5": "foo5",
"prop6": "foo6"
}
```

### ProjectedName_jsonProjection

- Endpoint: `post /projection/json`
Expand Down
141 changes: 141 additions & 0 deletions packages/cadl-ranch-specs/http/parameters/spread/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import "@typespec/http";
import "@azure-tools/cadl-ranch-expect";
import "@azure-tools/typespec-client-generator-core";

using TypeSpec.Http;
using Azure.ClientGenerator.Core;

@doc("Test for the spread operator.")
@supportedBy("dpg")
@scenarioService("/parameters/spread")
namespace Parameters.Spread;

@route("/model")
@operationGroup
namespace Model {
@doc("This is a simple model.")
model BodyParameter {
name: string;
}

@scenario
@scenarioDoc("""
Test case for spread named model.

Should generate request body model named `BodyParameter`.
Should generate an operation like below:
```
spreadAsRequestBody(bodyParameter: BodyParameter)
```
Note the parameter name is guessed from the model name and it may vary by language.

Expected request body:
```json
{ "name": "foo" }
```
""")
@route("/request-body")
@put
op spreadAsRequestBody(...BodyParameter): NoContentResponse;
}

@route("/alias")
@operationGroup
namespace Alias {
alias BodyParameter = {
name: string;
};

@scenario
@scenarioDoc("""
Test case for spread alias.

Should not generate any model named `BodyParameter`.
Should generate an operation like:
```
spreadAsRequestBody(name: string)
```

Expected request body:
```json
{ "name": "foo" }
```
""")
@route("/request-body")
@put
op spreadAsRequestBody(...BodyParameter): NoContentResponse;

alias RequestParameter = {
@path
id: string;

@header
`x-ms-test-header`: string;
name: string;
};

@scenario
@scenarioDoc("""
Test case for spread alias with path and header parameter.

Should not generate any model named `RequestParameter`.
Should generate an operation like below:
```
spreadAsRequestParameter(id: string, x_ms_test_header: string, name: string)
```
Note the parameter name may be normalized and vary by language.

Expected path parameter: id="1"
Expected header parameter: x-ms-test-header="bar"
Expected request body:
```json
{ "name": "foo" }
```
""")
@route("/request-parameter/{id}")
@put
op spreadAsRequestParameter(...RequestParameter): NoContentResponse;

alias MultipleRequestParameters = {
@path
id: string;

@header
`x-ms-test-header`: string;
prop1: string;
prop2: string;
prop3: string;
prop4: string;
prop5: string;
prop6: string;
};

@scenario
@scenarioDoc("""
Test case for spread alias including 6 parameters. May handle as property bag for these parameters.

Should not generate any model named `AliasMultipleRequestParameters`.
Should generate an operation like below:
```
spreadWithMultipleParameters(id: string, x_ms_test_header: string, prop1: string, prop2: string, prop3: string, prop4: string, prop5: string, prop6: string)
```
Note it's also acceptable if some languages handle it as property bag.

Expected path parameter: id="1"
Expected header parameter: x-ms-test-header="bar"
Expected request body:
```json
{
"prop1":"foo1",
"prop2":"foo2",
"prop3":"foo3",
"prop4":"foo4",
"prop5":"foo5",
"prop6":"foo6"
}
```
""")
@route("/multiple-parameters/{id}")
@put
op spreadWithMultipleParameters(...MultipleRequestParameters): NoContentResponse;
}
41 changes: 41 additions & 0 deletions packages/cadl-ranch-specs/http/parameters/spread/mockapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { passOnSuccess, mockapi } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";

export const Scenarios: Record<string, ScenarioMockApi> = {};

Scenarios.Parameters_Spread_Model_spreadAsRequestBody = passOnSuccess(
mockapi.put("/parameters/spread/model/request-body", (req) => {
req.expect.bodyEquals({ name: "foo" });
return { status: 204 };
}),
);

Scenarios.Parameters_Spread_Alias_spreadAsRequestBody = passOnSuccess(
mockapi.put("/parameters/spread/alias/request-body", (req) => {
req.expect.bodyEquals({ name: "foo" });
return { status: 204 };
}),
);

Scenarios.Parameters_Spread_Alias_spreadAsRequestParameter = passOnSuccess(
mockapi.put("/parameters/spread/alias/request-parameter/1", (req) => {
req.expect.containsHeader("x-ms-test-header", "bar");
req.expect.bodyEquals({ name: "foo" });
return { status: 204 };
}),
);

Scenarios.Parameters_Spread_Alias_spreadWithMultipleParameters = passOnSuccess(
mockapi.put("/parameters/spread/alias/multiple-parameters/1", (req) => {
req.expect.containsHeader("x-ms-test-header", "bar");
req.expect.bodyEquals({
prop1: "foo1",
prop2: "foo2",
prop3: "foo3",
prop4: "foo4",
prop5: "foo5",
prop6: "foo6",
});
return { status: 204 };
}),
);

0 comments on commit 5a6d929

Please sign in to comment.