Skip to content

Commit

Permalink
Added the UTs for sample gen
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryGao committed Oct 25, 2023
1 parent 5406746 commit abec42a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/rlc-common/src/helpers/valueGenerationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function mockUnionValues(
) {
const schema = schemaMap.get(type);
if (schema && schema.enum && schema.enum.length > 0) {
addToSchemaMap(schemaMap, schema.enum[0]);
return generateParameterTypeValue(
getAccurateTypeName(schema.enum[0]) ?? schema.enum[0],
parameterName,
Expand Down Expand Up @@ -242,6 +243,9 @@ function getAccurateTypeName(schema: Schema) {

function addToSchemaMap(schemaMap: Map<string, Schema>, schema: Schema) {
const type = getAccurateTypeName(schema);
if (!type) {
return;
}
if (
!schemaMap.has(type) &&
!["string", "number", "boolean"].includes(schema.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,45 @@ describe("Integration test for mocking sample", () => {
assert.deepEqual(JSON.parse(mockStr!), res);
});

it("anonymous model", async () => {
const schemaMap = await emitSchemasFromTypeSpec(`
model Test {
prop: string;
test: {
prop: string[];
foo?: int32;
bar: {
prop: string;
baz: boolean[];
}[];
baz: Record<{ t: string}>;
unionTest: { a: string } | { b: string };
}
}
@route("/models")
@get
op getModel(@body body: Test): void;
`);
const mockStr = generateParameterTypeValue(
"Test",
"input",
buildSchemaObjectMap({ schemas: schemaMap } as RLCModel),
new Set()
);
// console.log(mockStr);
const res = {
prop: "{Your prop}",
test: {
prop: ["{Your prop}"],
foo: 123,
bar: [{ prop: "{Your prop}", baz: [true] }],
baz: { key: { t: "{Your t}" } },
unionTest: { a: "{Your a}" }
}
};
assert.deepEqual(JSON.parse(mockStr!), res);
});

describe("complex model", () => {
describe("object", () => {
it("self-referenced model", async () => {
Expand Down

0 comments on commit abec42a

Please sign in to comment.