-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use appName as package name in the apiResources.partOfPackage (#59)
* global.namespace => global.appName changes * add tests for the partOfPackage property * Update __tests__/ord.test.js Thanks Tai! Co-authored-by: Duc Tai Ly <124802873+ductaily@users.noreply.github.com> * mock the cds.root property * minor change - change name in the bookshop.package.json * fix linter error * suppress no-useless-escape linter error --------- Co-authored-by: Duc Tai Ly <124802873+ductaily@users.noreply.github.com>
- Loading branch information
1 parent
e9d6538
commit af9e054
Showing
4 changed files
with
105 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,86 @@ | ||
const cds = require("@sap/cds"); | ||
const ord = require("../lib/ord"); | ||
const path = require("path"); | ||
const { join } = require("path"); | ||
|
||
// Mock the @sap/cds module | ||
jest.mock("@sap/cds", () => { | ||
const { join } = require("path"); | ||
let originalCds = jest.requireActual("@sap/cds"); | ||
originalCds.root = join(__dirname, "bookshop"); | ||
return originalCds; | ||
}); | ||
|
||
const cds = require("@sap/cds"); | ||
|
||
describe("Tests for default ORD document", () => { | ||
let csn; | ||
let csn; | ||
|
||
beforeAll(async () => { | ||
csn = await cds.load(join(__dirname, "bookshop", "srv")); | ||
}); | ||
|
||
beforeAll(async () => { | ||
csn = await cds.load(path.join(__dirname, "bookshop", "srv")); | ||
}); | ||
test("Successfully create ORD Documents with defaults", () => { | ||
const document = ord(csn); | ||
expect(document).toMatchSnapshot(); | ||
}); | ||
|
||
describe("apiResources", () => { | ||
// eslint-disable-next-line no-useless-escape | ||
const PACKAGE_ID_REGEX = /^([a-z0-9]+(?:[.][a-z0-9]+)*):(package):([a-zA-Z0-9._\-]+):(v0|v[1-9][0-9]*)$/; | ||
|
||
let document; | ||
|
||
beforeAll(() => { | ||
document = ord(csn); | ||
}); | ||
|
||
test("partOfPackage values are valid ORD IDs ", () => { | ||
for (const apiResource of document.apiResources) { | ||
expect(apiResource.partOfPackage).toMatch(PACKAGE_ID_REGEX); | ||
} | ||
}); | ||
|
||
test("The partOfPackage references an existing package", () => { | ||
for (const apiResource of document.apiResources) { | ||
expect( | ||
document.packages.find( | ||
(pck) => pck.ordId === apiResource.partOfPackage | ||
) | ||
).toBeDefined(); | ||
} | ||
}); | ||
}); | ||
|
||
test("Successfully create ORD Documents with defaults", () => { | ||
const document = ord(csn); | ||
expect(document).toMatchSnapshot(); | ||
}); | ||
describe("eventResources", () => { | ||
// eslint-disable-next-line no-useless-escape | ||
const GROUP_ID_REGEX = /^([a-z0-9-]+(?:[.][a-z0-9-]+)*):([a-zA-Z0-9._\-/]+):([a-z0-9-]+(?:[.][a-z0-9-]+)*):(?<service>[a-zA-Z0-9._\-/]+)$/; | ||
|
||
let document; | ||
|
||
describe("eventResources", () => { | ||
const GROUP_ID_REGEX = /^([a-z0-9-]+(?:[.][a-z0-9-]+)*):([a-zA-Z0-9._\-/]+):([a-z0-9-]+(?:[.][a-z0-9-]+)*):(?<service>[a-zA-Z0-9._\-/]+)$/ | ||
beforeAll(() => { | ||
document = ord(csn); | ||
}); | ||
|
||
let document; | ||
test("Assigned to exactly one CDS Service group", () => { | ||
for (const eventResource of document.eventResources) { | ||
expect(eventResource.partOfGroups.length).toEqual(1); | ||
} | ||
}); | ||
|
||
beforeAll(()=> { | ||
document = ord(csn); | ||
}) | ||
test("The CDS Service Group ID includes the CDS Service identifier", () => { | ||
for (const eventResource of document.eventResources) { | ||
const [groupId] = eventResource.partOfGroups; | ||
expect(groupId).toMatch(GROUP_ID_REGEX); | ||
|
||
test("Assigned to excactly one CDS Service group", () => { | ||
for (const eventResource of document.eventResources) { | ||
expect(eventResource.partOfGroups.length).toEqual(1) | ||
} | ||
const match = GROUP_ID_REGEX.exec(groupId); | ||
if (match && match.groups?.service) { | ||
let service = match.groups?.service; | ||
if (service.startsWith("undefined")) | ||
service = service.replace("undefined.", ""); | ||
const definition = csn.definitions[service]; | ||
expect(definition).toBeDefined(); | ||
expect(definition.kind).toEqual("service"); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
test("The CDS Service Group ID includes the CDS Service identifier", () => { | ||
for (const eventResource of document.eventResources) { | ||
const [groupId] = eventResource.partOfGroups | ||
expect(groupId).toMatch(GROUP_ID_REGEX) | ||
|
||
const match = GROUP_ID_REGEX.exec(groupId) | ||
if (match && match.groups?.service) { | ||
let service = match.groups?.service | ||
if (service.startsWith("undefined")) service = service.replace("undefined.", "") | ||
const definition = csn.definitions[service] | ||
expect(definition).toBeDefined() | ||
expect(definition.kind).toEqual("service") | ||
} | ||
} | ||
}) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters