Skip to content

Commit 280701c

Browse files
zzhxiaofengv-jiaodiqiaozha
authored
Add coverage cases for arrays-item-types in cadl-ranch (#2103)
* generate test SDK for array * discard commit to resove conflict for update latest code * generate code with latest repo * fix array item type optionality * Update packages/typespec-ts/src/modular/helpers/operationHelpers.ts * add test case * fix test --------- Co-authored-by: Di Jiao <v-jiaodi@microsoft.com> Co-authored-by: Jiao Di (MSFT) <80496810+v-jiaodi@users.noreply.github.com> Co-authored-by: qiaozha <qiaozha@microsoft.com> Co-authored-by: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com>
1 parent a16c692 commit 280701c

40 files changed

Lines changed: 1942 additions & 1 deletion

File tree

packages/typespec-ts/src/modular/helpers/operationHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ function deserializeResponseValue(
944944
type.elementType!,
945945
"p",
946946
runtimeImports,
947-
required,
947+
true,
948948
[...typeStack, type.elementType!],
949949
type.elementType?.format
950950
)})`;

packages/typespec-ts/test/commands/cadl-ranch-list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ export const modularTsps: TypeSpecRanchConfig[] = [
318318
{
319319
outputPath: "models/empty",
320320
inputPath: "type/model/empty"
321+
},
322+
{
323+
outputPath: "arrays/items",
324+
inputPath: "type/array"
321325
}
322326
];
323327

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { assert } from "chai";
2+
import { ArrayClient } from "./generated/arrays/items/src/index.js";
3+
4+
interface TypeDetail {
5+
type: string;
6+
defaultValue: any;
7+
convertedToFn?: (_: any) => any;
8+
}
9+
10+
const testedTypes: TypeDetail[] = [
11+
{
12+
type: "int32",
13+
defaultValue: [1, 2]
14+
},
15+
{
16+
type: "int64",
17+
defaultValue: [Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER]
18+
},
19+
{
20+
type: "boolean",
21+
defaultValue: [true, false]
22+
},
23+
{
24+
type: "string",
25+
defaultValue: ["hello", ""]
26+
},
27+
{
28+
type: "float32",
29+
defaultValue: [42.42]
30+
},
31+
{
32+
type: "datetime",
33+
defaultValue: [new Date("2022-08-26T18:38:00Z")]
34+
},
35+
{
36+
type: "duration",
37+
defaultValue: ["P123DT22H14M12.011S"]
38+
},
39+
{
40+
type: "unknown",
41+
defaultValue: [1, "hello", null]
42+
},
43+
{
44+
type: "model",
45+
defaultValue: [
46+
{ property: "hello", children: undefined },
47+
{ property: "world", children: undefined }
48+
]
49+
},
50+
{
51+
type: "nullable-float",
52+
defaultValue: [1.2, null, 3.0]
53+
}
54+
];
55+
describe("Array Item-Types Client", () => {
56+
let client: ArrayClient;
57+
58+
beforeEach(() => {
59+
client = new ArrayClient({
60+
allowInsecureConnection: true,
61+
retryOptions: {
62+
maxRetries: 0
63+
}
64+
});
65+
});
66+
for (let item of testedTypes) {
67+
it(`should get ${item.type} value`, async () => {
68+
try {
69+
let result: any;
70+
switch (item.type) {
71+
case "int32":
72+
result = await client.int32Value.get();
73+
break;
74+
case "int64":
75+
result = await client.int64Value.get();
76+
break;
77+
case "boolean":
78+
result = await client.booleanValue.get();
79+
break;
80+
case "string":
81+
result = await client.stringValue.get();
82+
break;
83+
case "float32":
84+
result = await client.float32Value.get();
85+
break;
86+
case "datetime":
87+
result = await client.datetimeValue.get();
88+
break;
89+
case "duration":
90+
result = await client.durationValue.get();
91+
break;
92+
case "unknown":
93+
result = await client.unknownValue.get();
94+
break;
95+
case "model":
96+
result = await client.modelValue.get();
97+
break;
98+
case "nullable-float":
99+
result = await client.nullableFloatValue.get();
100+
break;
101+
default:
102+
break;
103+
}
104+
assert.deepEqual(result, item.defaultValue);
105+
} catch (err) {
106+
assert.fail(err as string);
107+
}
108+
});
109+
}
110+
for (let item of testedTypes) {
111+
it(`should put ${item.type} vaule`, async () => {
112+
try {
113+
let result: any;
114+
switch (item.type) {
115+
case "int32":
116+
result = await client.int32Value.put(item.defaultValue);
117+
break;
118+
case "int64":
119+
result = await client.int64Value.put(item.defaultValue);
120+
break;
121+
case "boolean":
122+
result = await client.booleanValue.put(item.defaultValue);
123+
break;
124+
case "string":
125+
result = await client.stringValue.put(item.defaultValue);
126+
break;
127+
case "float32":
128+
result = await client.float32Value.put(item.defaultValue);
129+
break;
130+
case "datetime":
131+
result = await client.datetimeValue.put(item.defaultValue);
132+
break;
133+
case "duration":
134+
result = await client.durationValue.put(item.defaultValue);
135+
break;
136+
case "unknown":
137+
result = await client.unknownValue.put(item.defaultValue);
138+
break;
139+
case "model":
140+
result = await client.modelValue.put(item.defaultValue);
141+
break;
142+
case "nullable-float":
143+
result = await client.nullableFloatValue.put(item.defaultValue);
144+
break;
145+
default:
146+
break;
147+
}
148+
assert.isUndefined(result);
149+
} catch (err) {
150+
assert.fail(err as string);
151+
}
152+
});
153+
}
154+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { Pipeline } from "@azure/core-rest-pipeline";
5+
import {
6+
getInt32ValueOperations,
7+
Int32ValueOperations,
8+
} from "./classic/int32Value/index.js";
9+
import {
10+
getInt64ValueOperations,
11+
Int64ValueOperations,
12+
} from "./classic/int64Value/index.js";
13+
import {
14+
getBooleanValueOperations,
15+
BooleanValueOperations,
16+
} from "./classic/booleanValue/index.js";
17+
import {
18+
getStringValueOperations,
19+
StringValueOperations,
20+
} from "./classic/stringValue/index.js";
21+
import {
22+
getFloat32ValueOperations,
23+
Float32ValueOperations,
24+
} from "./classic/float32Value/index.js";
25+
import {
26+
getDatetimeValueOperations,
27+
DatetimeValueOperations,
28+
} from "./classic/datetimeValue/index.js";
29+
import {
30+
getDurationValueOperations,
31+
DurationValueOperations,
32+
} from "./classic/durationValue/index.js";
33+
import {
34+
getUnknownValueOperations,
35+
UnknownValueOperations,
36+
} from "./classic/unknownValue/index.js";
37+
import {
38+
getModelValueOperations,
39+
ModelValueOperations,
40+
} from "./classic/modelValue/index.js";
41+
import {
42+
getNullableFloatValueOperations,
43+
NullableFloatValueOperations,
44+
} from "./classic/nullableFloatValue/index.js";
45+
import { createArray, ArrayClientOptions, ArrayContext } from "./api/index.js";
46+
47+
export { ArrayClientOptions } from "./api/ArrayContext.js";
48+
49+
export class ArrayClient {
50+
private _client: ArrayContext;
51+
/** The pipeline used by this client to make requests */
52+
public readonly pipeline: Pipeline;
53+
54+
/** Illustrates various of dictionaries. */
55+
constructor(options: ArrayClientOptions = {}) {
56+
this._client = createArray(options);
57+
this.pipeline = this._client.pipeline;
58+
this.int32Value = getInt32ValueOperations(this._client);
59+
this.int64Value = getInt64ValueOperations(this._client);
60+
this.booleanValue = getBooleanValueOperations(this._client);
61+
this.stringValue = getStringValueOperations(this._client);
62+
this.float32Value = getFloat32ValueOperations(this._client);
63+
this.datetimeValue = getDatetimeValueOperations(this._client);
64+
this.durationValue = getDurationValueOperations(this._client);
65+
this.unknownValue = getUnknownValueOperations(this._client);
66+
this.modelValue = getModelValueOperations(this._client);
67+
this.nullableFloatValue = getNullableFloatValueOperations(this._client);
68+
}
69+
70+
/** The operation groups for Int32Value */
71+
public readonly int32Value: Int32ValueOperations;
72+
/** The operation groups for Int64Value */
73+
public readonly int64Value: Int64ValueOperations;
74+
/** The operation groups for BooleanValue */
75+
public readonly booleanValue: BooleanValueOperations;
76+
/** The operation groups for StringValue */
77+
public readonly stringValue: StringValueOperations;
78+
/** The operation groups for Float32Value */
79+
public readonly float32Value: Float32ValueOperations;
80+
/** The operation groups for DatetimeValue */
81+
public readonly datetimeValue: DatetimeValueOperations;
82+
/** The operation groups for DurationValue */
83+
public readonly durationValue: DurationValueOperations;
84+
/** The operation groups for UnknownValue */
85+
public readonly unknownValue: UnknownValueOperations;
86+
/** The operation groups for ModelValue */
87+
public readonly modelValue: ModelValueOperations;
88+
/** The operation groups for NullableFloatValue */
89+
public readonly nullableFloatValue: NullableFloatValueOperations;
90+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { ClientOptions } from "@azure-rest/core-client";
5+
import { ArrayContext } from "../rest/index.js";
6+
import getClient from "../rest/index.js";
7+
8+
export interface ArrayClientOptions extends ClientOptions {}
9+
10+
export { ArrayContext } from "../rest/index.js";
11+
12+
/** Illustrates various of dictionaries. */
13+
export function createArray(options: ArrayClientOptions = {}): ArrayContext {
14+
const clientContext = getClient(options);
15+
return clientContext;
16+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import {
5+
ArrayContext as Client,
6+
BooleanValueGet200Response,
7+
BooleanValuePut204Response,
8+
} from "../../rest/index.js";
9+
import {
10+
StreamableMethod,
11+
operationOptionsToRequestParameters,
12+
createRestError,
13+
} from "@azure-rest/core-client";
14+
import {
15+
BooleanValueGetOptions,
16+
BooleanValuePutOptions,
17+
} from "../../models/options.js";
18+
19+
export function _booleanValueGetSend(
20+
context: Client,
21+
options: BooleanValueGetOptions = { requestOptions: {} },
22+
): StreamableMethod<BooleanValueGet200Response> {
23+
return context
24+
.path("/type/array/boolean")
25+
.get({ ...operationOptionsToRequestParameters(options) });
26+
}
27+
28+
export async function _booleanValueGetDeserialize(
29+
result: BooleanValueGet200Response,
30+
): Promise<boolean[]> {
31+
if (result.status !== "200") {
32+
throw createRestError(result);
33+
}
34+
35+
return result.body;
36+
}
37+
38+
export async function booleanValueGet(
39+
context: Client,
40+
options: BooleanValueGetOptions = { requestOptions: {} },
41+
): Promise<boolean[]> {
42+
const result = await _booleanValueGetSend(context, options);
43+
return _booleanValueGetDeserialize(result);
44+
}
45+
46+
export function _booleanValuePutSend(
47+
context: Client,
48+
body: boolean[],
49+
options: BooleanValuePutOptions = { requestOptions: {} },
50+
): StreamableMethod<BooleanValuePut204Response> {
51+
return context
52+
.path("/type/array/boolean")
53+
.put({ ...operationOptionsToRequestParameters(options), body: body });
54+
}
55+
56+
export async function _booleanValuePutDeserialize(
57+
result: BooleanValuePut204Response,
58+
): Promise<void> {
59+
if (result.status !== "204") {
60+
throw createRestError(result);
61+
}
62+
63+
return;
64+
}
65+
66+
export async function booleanValuePut(
67+
context: Client,
68+
body: boolean[],
69+
options: BooleanValuePutOptions = { requestOptions: {} },
70+
): Promise<void> {
71+
const result = await _booleanValuePutSend(context, body, options);
72+
return _booleanValuePutDeserialize(result);
73+
}

0 commit comments

Comments
 (0)