Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spread test cases #247

Merged
merged 21 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
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 };
}),
);