Skip to content

Commit 67cc30c

Browse files
committed
add tests
1 parent 2fac6e2 commit 67cc30c

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { TestHost, BasicTestRunner } from "@typespec/compiler/testing";
2+
import { describe, expect, it, beforeEach } from "vitest";
3+
import { createTypespecAazTestHost, createTypespecAazTestRunner, compileTypespecAAZOperations } from "./test-aaz.js";
4+
import { ModelVar, generateCompileArmResourceTemplate, findObjectsWithKey } from "./util.js";
5+
6+
describe("discriminator parsing", () => {
7+
let host: TestHost;
8+
let runner: BasicTestRunner;
9+
10+
beforeEach(async () => {
11+
host = await createTypespecAazTestHost();
12+
runner = await createTypespecAazTestRunner(host);
13+
});
14+
15+
it("validate discriminator value", async () => {
16+
const modelTmp: ModelVar = {
17+
modelKey: "targetValue",
18+
modelContent: "TargetResourceConfigurations",
19+
isComposite: true,
20+
appendModel: `
21+
union ResourceKind {
22+
string,
23+
FunctionsFlexConsumption: "FunctionsFlexConsumption",
24+
}
25+
26+
model FunctionFlexConsumptionResourceConfiguration {
27+
instanceMemoryMB: int64;
28+
httpConcurrency?: int64;
29+
}
30+
31+
@discriminator("kind")
32+
model TargetResourceConfigurations {
33+
@visibility(Lifecycle.Create, Lifecycle.Read)
34+
kind: ResourceKind;
35+
}
36+
37+
model FunctionFlexConsumptionTargetResourceConfigurations
38+
extends TargetResourceConfigurations {
39+
kind: ResourceKind.FunctionsFlexConsumption;
40+
configurations?: Record<FunctionFlexConsumptionResourceConfiguration>;
41+
}
42+
`,
43+
};
44+
45+
const code: string = generateCompileArmResourceTemplate(modelTmp);
46+
const result = await compileTypespecAAZOperations(
47+
code,
48+
{
49+
"operation": "get-resources-operations",
50+
"api-version": "A",
51+
"resources": ["/subscriptions/{}/resourcegroups/{}/providers/microsoft.mock/mockresources/{}"],
52+
},
53+
runner,
54+
);
55+
const resultObj = JSON.parse(result!);
56+
expect(Array.isArray(resultObj)).toBe(true);
57+
expect(resultObj.length).toBe(1);
58+
const targetObj = findObjectsWithKey(resultObj[0].pathItem.get.read.http.responses, "targetValue");
59+
const anyTypeObj = JSON.stringify(targetObj, null, 2);
60+
await expect(anyTypeObj).toMatchFileSnapshot("./snapshots/discriminator-prop.json");
61+
});
62+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"type": "object",
3+
"discriminators": [
4+
{
5+
"property": "kind",
6+
"value": "FunctionsFlexConsumption",
7+
"props": [
8+
{
9+
"type": "object",
10+
"additionalProps": {
11+
"item": {
12+
"type": "object",
13+
"props": [
14+
{
15+
"type": "integer64",
16+
"name": "instanceMemoryMB",
17+
"required": true
18+
},
19+
{
20+
"type": "integer64",
21+
"name": "httpConcurrency"
22+
}
23+
]
24+
}
25+
},
26+
"name": "configurations"
27+
}
28+
]
29+
}
30+
],
31+
"props": [
32+
{
33+
"type": "string",
34+
"enum": {
35+
"items": [
36+
{
37+
"value": "FunctionsFlexConsumption"
38+
}
39+
]
40+
},
41+
"name": "kind",
42+
"required": true
43+
}
44+
],
45+
"name": "targetValue",
46+
"required": true
47+
}

0 commit comments

Comments
 (0)