-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathpython-codegen.test.ts
More file actions
48 lines (46 loc) · 2.06 KB
/
Copy pathpython-codegen.test.ts
File metadata and controls
48 lines (46 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { describe, expect, it } from "vitest";
import { generatePythonSessionEventsCode } from "../../scripts/codegen/python.ts";
describe("Python root event payload unions", () => {
it.each(["anyOf", "oneOf"] as const)("preserves referenced %s payload variants", (keyword) => {
const code = generatePythonSessionEventsCode({
definitions: {
Payload: {
[keyword]: ["status", "startup", "server_error", "incremental"].map((kind) => ({
type: "object",
properties: { kind: { const: kind }, value: { type: "string" } },
required: ["kind", "value"],
})),
},
SessionEvent: {
anyOf: [
{
type: "object",
properties: {
type: { const: "sample.union" },
data: { $ref: "#/definitions/Payload" },
},
required: ["type", "data"],
},
{
type: "object",
properties: {
type: { const: "sample.empty" },
data: { type: "object", properties: {} },
},
required: ["type", "data"],
},
],
},
},
});
expect(code).toContain("class SampleUnionData:");
expect(code).toContain("kind: SampleUnionDataKind");
expect(code).toContain("value: str");
for (const kind of ["status", "startup", "server_error", "incremental"]) {
expect(code).toContain(`= "${kind}"`);
}
expect(code).toContain('result["value"] = from_str(self.value)');
expect(code).toContain("data = SampleUnionData.from_dict(data_obj)");
expect(code).toContain("return SampleEmptyData()");
});
});