Skip to content

Commit

Permalink
Use example to define test data (#7530)
Browse files Browse the repository at this point in the history
* Use example to define test data

* Use example to define test data

* Adjust API filename in redocly configuration

* Add default language test

* Add Languages examples

* Add default SIC test

* Add Languages examples

* Add Countries examples and default test

* Add Business Types examples and default test

* Add GIFI examples and default test

* Add Warehouses examples and default test

* Add Pricegroups examples and default test

* Add Invoices examples and default test

* Fix deliverydate field mapping

* Complete Invoice API definitions

* Return actions in deterministic order for JS tests

* Fix indentation of product/warehouse example

* Make sure yaml-jest-transform is installed

---------

Co-authored-by: Yves Lavoie <ylavoie@yveslavoie.com>
  • Loading branch information
ehuelsmann and ylavoie committed Aug 24, 2023
1 parent 07c3ec1 commit 79832db
Show file tree
Hide file tree
Showing 18 changed files with 662 additions and 100 deletions.
3 changes: 2 additions & 1 deletion UI/jest.config.js
Expand Up @@ -117,7 +117,7 @@ module.exports = {

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
"^@/i18n": "<rootDir>/tests/common/i18n", // Jest doesn't support esm or top level await well
"^@/i18n": "<rootDir>/tests/common/i18n", // Jest doesn't support esm or top level await well
"^@/(.*)$": "<rootDir>/src/$1"
},

Expand Down Expand Up @@ -230,6 +230,7 @@ module.exports = {

// A map from regular expressions to paths to transformers
transform: {
"^.+\\.yaml$": "yaml-jest-transform",
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "@vue/vue3-jest"
},
Expand Down
1 change: 1 addition & 0 deletions UI/package.json
Expand Up @@ -138,6 +138,7 @@
"webpack-sources": "3.2.3",
"webpack-virtual-modules": "0.5.0",
"whatwg-fetch": "3.6.17",
"yaml-jest-transform": "^2.0.2",
"yargs": "17.7.2"
},
"homepage": "http://ledgersmb.org/",
Expand Down
18 changes: 18 additions & 0 deletions UI/tests/specs/openapi/API-contacts-business_types.spec.js
Expand Up @@ -17,6 +17,9 @@ import { server } from '../../common/mocks/server.js'
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -137,6 +140,21 @@ describe("Adding the IT Business Types", () => {
});
});

describe("Validate against the default Business Type", () => {
it("GET /contacts/business-types/1 should validate the new Business Type", async () => {
let res = await axios.get(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const businessTypeExample = API_yaml.components.examples.validBusinessType.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(businessTypeExample);
});
});

describe("Modifying the new Business Type", () => {
it("PUT /contacts/business-types/1 should allow updating Business Type", async () => {
let res = await axios.get(
Expand Down
22 changes: 22 additions & 0 deletions UI/tests/specs/openapi/API-contacts-sic.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -133,6 +137,24 @@ describe("Adding the IT SIC", () => {
});
});

describe("Validate against the example SIC", () => {
it("GET /contacts/sic/541510 should validate IT SIC", async () => {
let res = await axios.get(
serverUrl + "/" + api + "/contacts/sic/541510",
{
headers: headers
}
);
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const sicExample = API_yaml.components.examples.validSIC.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(sicExample);
});
});

describe("Modifying the new IT SIC", () => {
it("PUT /contacts/sic/541510 should allow updating IT SIC", async () => {
let res = await axios.get(serverUrl + "/" + api + "/contacts/sic/541510", {
Expand Down
19 changes: 19 additions & 0 deletions UI/tests/specs/openapi/API-country.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -104,6 +108,21 @@ describe("Retrieving all countries with old syntax should fail", () => {
});
});

describe("Validate against the example country", () => {
it("GET /countries/NL", async () => {
let res = await axios.get(serverUrl + "/" + api + "/countries/NL", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const countryExample = API_yaml.components.examples.validCountry.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(countryExample);
});
});

describe("Retrieve non-existant COUNTRY ZZ should fail", () => {
it("GET /countries/ZZ should not retrieve invalid COUNTRY", async () => {
await expect(
Expand Down
19 changes: 19 additions & 0 deletions UI/tests/specs/openapi/API-gifi.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -136,6 +140,21 @@ describe("Adding the new Test GIFI", () => {
});
});

describe("Validate against the example GIFI 99999", () => {
it("GET /gifi/99999 should validate our new GIFI", async () => {
let res = await axios.get(serverUrl + "/" + api + "/gl/gifi/99999", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const gifiExample = API_yaml.components.examples.validGIFI.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(gifiExample);
});
});

describe("Modifying the new GIFI 99999", () => {
it("PUT /gifi/99999 should allow updating Test GIFI", async () => {
let res = await axios.get(serverUrl + "/" + api + "/gl/gifi/99999", {
Expand Down
123 changes: 113 additions & 10 deletions UI/tests/specs/openapi/API-invoices.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -110,10 +114,12 @@ describe("Adding the new Invoice", () => {
due: "2022-10-01",
book: "2022-10-05"
},
"internal-notes": "Internal notes",
"invoice-number": "2389434",
lines: [
{
part: {
number:"p1"
number: "p1"
},
price: 56.78,
price_fixated: false,
Expand All @@ -124,17 +130,16 @@ describe("Adding the new Invoice", () => {
discount: 12,
discount_type: "%",
delivery_date: "2022-10-27",
description: "A description",
notes: "Notes",
"internal-notes": "Internal notes",
"invoice-number": "2389434",
"order-number": "order 345",
"po-number": "po 456",
"ship-via": "ship via",
"shipping-point": "shipping from here",
"ship-to": "ship to there"
description: "A description"
}
],
notes: "Notes",
"order-number": "order 345",
"po-number": "po 456",
"shipping-point": "shipping from here",
// TODO: Debug ship-to
// "ship-to": "ship to there",
"ship-via": "ship via",
taxes: {
"2150": {
tax: {
Expand Down Expand Up @@ -170,6 +175,89 @@ describe("Adding the new Invoice", () => {
});
});

/*
describe("Adding the new Invoice", () => {
it("POST /invoices should allow adding a new invoice", async () => {
let res;
try {
res = await axios.post(
serverUrl + "/" + api + "/invoices",
{
eca: {
number: "Customer 1",
type: "customer" // Watch for exact case or watch serverUrl stack dump
},
account: {
accno: "1200"
},
currency: "USD",
dates: {
created: "2022-09-01",
due: "2022-10-01",
book: "2022-10-05"
},
description: "Annual gizmos",
lines: [
{
part: {
number: "p1"
},
price: 56.78,
price_fixated: false,
unit: "lbs",
qty: 1,
taxform: true,
serialnumber: "1234567890",
discount: 12,
discount_type: "%",
delivery_date: "2022-10-27",
description: "A description"
}
],
notes: "Notes",
"internal-notes": "Internal notes",
"invoice-number": "2389434",
"order-number": "order 345",
"po-number": "po 456",
"ship-via": "ship via",
"shipping-point": "shipping from here",
"ship-to": "ship to there",
taxes: {
"2150": {
tax: {
category: "2150"
},
"base-amount": 50,
amount: 6.78,
source: "Part 1",
memo: "tax memo" // Could that be optional?
},
},
payments: [
{
account: {
accno: "5010"
},
date: "2022-11-05",
amount: 20,
memo: "depot",
source: "visa"
}
]
},
{
headers: headers
}
);
} catch(e) {
console.log(e.response.data);
}
expect(res.status).toEqual(StatusCodes.CREATED);
expect(res.headers.location).toMatch('./1');
});
});
*/

describe("Retrieving all invoices with old syntax should fail", () => {
it("GET /invoices/ should fail", async () => {
await expect(
Expand Down Expand Up @@ -197,6 +285,21 @@ describe("Retrieve first invoice", () => {
});
});

describe("Validate first invoice against example", () => {
it("GET /invoices/1 should validate against example", async () => {
let res = await axios.get(serverUrl + "/" + api + "/invoices/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const invoiceExample = API_yaml.components.examples.validInvoice.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(invoiceExample);
});
});

describe("Retrieve non-existant Invoice", () => {
it("GET /invoices/2 should not retrieve Invoice 2", async () => {
await expect(
Expand Down
19 changes: 19 additions & 0 deletions UI/tests/specs/openapi/API-languages.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -117,6 +121,21 @@ describe("Retrieve English language", () => {
});
});

describe("Retrieve the French Canadian language", () => {
it("GET /language/fr_CA", async () => {
let res = await axios.get(serverUrl + "/" + api + "/languages/fr_CA", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const languageExample = API_yaml.components.examples.validLanguage.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(languageExample);
});
});

describe("Retrieve non-existant Navaho language", () => {
it("GET /languages/nv should not retrieve Navajo language", async () => {
await expect(
Expand Down
19 changes: 19 additions & 0 deletions UI/tests/specs/openapi/API-products-pricegroups.spec.js
Expand Up @@ -16,6 +16,10 @@ import { server } from '../../common/mocks/server.js'
// Load an OpenAPI file (YAML or JSON) into this plugin
const openapi = process.env.PWD.replace("/UI","");
jestOpenAPI( openapi + "/openapi/API.yaml");

// Load the API definition
const API_yaml = require (openapi + "/openapi/API.yaml");

// Set API version to use
const api = "erp/api/v0";

Expand Down Expand Up @@ -131,6 +135,21 @@ describe("Adding the new Price Group", () => {
});
});

describe("Validate against the example Pricegroup", () => {
it("GET /products/pricegroups/1", async () => {
let res = await axios.get(serverUrl + "/" + api + "/products/pricegroups/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);

// Pick the example
const pricegroupExample = API_yaml.components.examples.validPricegroup.value;

// Assert that the response matches the example in the spec
expect(res.data).toEqual(pricegroupExample);
});
});

describe("Modifying the new Price Group", () => {
it("PUT /products/pricegroups/Pricegroup1 should allow updating Pricegroup1", async () => {
let res = await axios.get(
Expand Down

0 comments on commit 79832db

Please sign in to comment.