Bug 38503: Have a generic buildSampleObject[s]

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
(cherry picked from commit 3f880c5978)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Jonathan Druart 2024-11-21 14:07:31 +01:00 committed by Paul Derscheid
parent 7bad7ea76d
commit 00fe3f8b55
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
3 changed files with 38 additions and 42 deletions

View File

@ -2,7 +2,7 @@ import { mount } from "@cypress/vue";
describe("Generate Random Patron", () => {
it("should generate a random patron from the schema", () => {
cy.task("buildSamplePatron").then(mockPatron => {
cy.task("buildSampleObject", { object: "patron" }).then(mockPatron => {
expect(mockPatron).to.have.property("patron_id");
});
});
@ -10,26 +10,32 @@ describe("Generate Random Patron", () => {
describe("Generate Random Patrons", () => {
it("should generate 42 random patron from the schema", () => {
cy.task("buildSamplePatrons", 42).then(mockPatrons => {
expect(mockPatrons.length).to.equal(42);
expect(mockPatrons[0]).to.have.property("patron_id");
});
cy.task("buildSampleObjects", { object: "patron", count: 42 }).then(
mockPatrons => {
expect(mockPatrons.length).to.equal(42);
expect(mockPatrons[0]).to.have.property("patron_id");
}
);
});
});
describe("Generate Random Library", () => {
it("should generate a random library from the schema", () => {
cy.task("buildSampleLibrary").then(mockLibrary => {
expect(mockLibrary).to.have.property("library_id");
});
cy.task("buildSampleObject", { object: "library" }).then(
mockLibrary => {
expect(mockLibrary).to.have.property("library_id");
}
);
});
});
describe("Generate Random Libraries", () => {
it("should generate 42 random library from the schema", () => {
cy.task("buildSampleLibraries", 42).then(mockLibraries => {
expect(mockLibraries.length).to.equal(42);
expect(mockLibraries[0]).to.have.property("library_id");
});
cy.task("buildSampleObjects", { object: "library", count: 42 }).then(
mockLibraries => {
expect(mockLibraries.length).to.equal(42);
expect(mockLibraries[0]).to.have.property("library_id");
}
);
});
});

View File

@ -18,19 +18,12 @@ module.exports = (on, config) => {
mysql.configurePlugin(on);
};
const {
buildSamplePatron,
buildSamplePatrons,
buildSampleLibrary,
buildSampleLibraries,
} = require("./mockData.js");
const { buildSampleObject, buildSampleObjects } = require("./mockData.js");
module.exports = (on, config) => {
on("task", {
buildSamplePatron,
buildSamplePatrons,
buildSampleLibrary,
buildSampleLibraries,
buildSampleObject,
buildSampleObjects,
});
return config;
};

View File

@ -1,6 +1,14 @@
const { faker } = require("@faker-js/faker");
const { readYamlFile } = require("./../plugins/readYamlFile.js");
const objects = {
patron: {
spec: "patron",
},
library: {
spec: "library",
},
};
const generateMockData = type => {
switch (type) {
case "string":
@ -26,35 +34,24 @@ const generateDataFromSchema = properties => {
return mockData;
};
const buildSamplePatrons = (count = 1) => {
const yamlPath = "api/v1/swagger/definitions/patron.yaml";
const buildSampleObjects = ({ object, count = 1 }) => {
if (!objects.hasOwnProperty(object)) {
throw new Error(`Object type not supported: ${object}`);
}
const yamlPath = `api/v1/swagger/definitions/${objects[object].spec}.yaml`;
const schema = readYamlFile(yamlPath);
return Array.from({ length: count }, () =>
generateDataFromSchema(schema.properties)
);
};
const buildSamplePatron = () => {
return buildSamplePatrons()[0];
};
const buildSampleLibraries = (count = 1) => {
const yamlPath = "api/v1/swagger/definitions/library.yaml";
const schema = readYamlFile(yamlPath);
return Array.from({ length: count }, () =>
generateDataFromSchema(schema.properties)
);
};
const buildSampleLibrary = () => {
return buildSampleLibraries()[0];
const buildSampleObject = object => {
return buildSampleObjects({ object })[0];
};
module.exports = {
generateMockData,
generateDataFromSchema,
buildSamplePatron,
buildSamplePatrons,
buildSampleLibrary,
buildSampleLibraries,
buildSampleObject,
buildSampleObjects,
};