diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index af30ee6..d6b4362 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,4 +16,3 @@ jobs: with: token: ${{ secrets.GH_PAT }} release-type: node - package-name: cap-js-mockdata diff --git a/README.md b/README.md index 7db87f4..76cd57e 100644 --- a/README.md +++ b/README.md @@ -1 +1,55 @@ -# sap-cap-mockdata-plugin \ No newline at end of file +# SAP CAP Mockdata Plugin + +[![npm](https://img.shields.io/npm/v/cap-js-mockdata)](https://www.npmjs.com/package/cap-js-mockdata) [![test](https://github.com/mauriciolauffer/cap-js-mockdata/actions/workflows/test.yml/badge.svg)](https://github.com/mauriciolauffer/cap-js-mockdata/actions/workflows/test.yml) + +A [SAP CAP plugin](https://cap.cloud.sap/docs/node.js/cds-plugins) to generate CSV files populated with mock data for testing. + +This plugin uses [Faker](https://fakerjs.dev/) to generate meaningful mock data both automatically and through annotations. + +## Setup + +All you need to do is to install the plugin and add the `@Mockdata` annotations to your entities. + +### Installation + +Install the plugin as an npm module: + +```shell +npm i -D cap-js-mockdata +``` + +### Annotation + +The annotation tag is `@Mockdata`. You can add and combine all valid [Faker module APIs](https://fakerjs.dev/api/). + +```js +// db/schema.cds + +entity Customers : cuid, managed { + name String @Mockdata: {person: 'fullName'}; + sex String @Mockdata: {person: 'sex'}; + creditCard String @Mockdata: {finance: 'creditCardNumber'}; + phone String @Mockdata: {phone: 'number'}; + ipAddress String @Mockdata: {internet: 'ipv4'}; + email String @Mockdata: {internet: 'email'}; + street String @Mockdata: {location: 'streetAddress'}; + country String @Mockdata: {location: 'countryCode'}; +}; +``` + +### Using + +Once you have installed the plugin and added the annotations, or not, you can execute the following command in a terminal: + +```shell +cds add mockdata +``` + +The `cds add mockdata` command will create CSV files for every supported CDS Entity. The CSV files will be created into the `db/data` folder, the filenames will be something like `namespace-entityName.csv` and their content will look like: + +|ID |createdAt |createdBy |modifiedAt |modifiedBy |name |sex |creditCard |phone |ipAddress |email |street |country | +|------------------------------------|----------------------------------------------------------|---------------|----------------------------------------------------------|-----------------------|------------------|------|------------------------|-------------------|---------------|-----------------------------|-------------------|-----------------------| +|bef3c7b8-1232-4860-bded-efd51443e508|Sun Oct 06 2024 15:55:24 GMT-0300 (Brasilia Standard Time)|Julius_Hintz48 |Sun Nov 03 2024 14:38:56 GMT-0300 (Brasilia Standard Time)|Brain_Mosciski |Edgar Langosh |female|3564-4519-0923-7387 |801.226.6645 x01258|214.155.102.159|Marcellus.Fritsch@hotmail.com|640 Bramley Close |Austria | +|96ac7211-691a-4c2a-8fc0-0ae509693193|Thu Mar 28 2024 20:32:35 GMT-0300 (Brasilia Standard Time)|Clotilde_DAmore|Thu Nov 23 2023 10:49:44 GMT-0300 (Brasilia Standard Time)|Brendan.Corwin-Schinner|Erica McDermott |male |6567-6280-8822-5389-0611|(240) 938-1223 |37.230.118.100 |Shayne78@gmail.com |1803 Feeney Park |Cocos (Keeling) Islands| +|4fc525bc-e22b-4c55-a447-0e4c9fef1ffd|Fri Feb 09 2024 15:21:45 GMT-0300 (Brasilia Standard Time)|Lance78 |Fri Feb 14 2025 05:25:29 GMT-0300 (Brasilia Standard Time)|Donny_Blanda |Joseph Lehner |male |4686210527760 |312-939-7109 |14.207.231.127 |Daija_Oberbrunner94@gmail.com|79422 Collin Divide|Portugal | +|872b47a8-0bb1-47af-bc02-ef7290cff633|Sat Feb 22 2025 01:34:16 GMT-0300 (Brasilia Standard Time)|Mateo22 |Sun Sep 15 2024 18:42:41 GMT-0300 (Brasilia Standard Time)|Myrtle52 |Leigh Wintheiser I|male |6759-2563-9161-5976 |553.277.9724 x231 |10.88.166.234 |Elyssa.Adams79@gmail.com |139 Park Drive |Macao | diff --git a/examples/cap-app/db/mockdata.cds b/examples/cap-app/db/mockdata.cds index cd09ba9..91a5cf1 100644 --- a/examples/cap-app/db/mockdata.cds +++ b/examples/cap-app/db/mockdata.cds @@ -1,4 +1,4 @@ -using {validatorplugin.test.db as db} from './schema'; +using {mockdataplugin.test.db as db} from './schema'; annotate db.TestingAnnotation with { name @Mockdata: {person: 'fullName'}; diff --git a/examples/cap-app/db/schema.cds b/examples/cap-app/db/schema.cds index d4e8a6c..ebc6e94 100644 --- a/examples/cap-app/db/schema.cds +++ b/examples/cap-app/db/schema.cds @@ -1,4 +1,4 @@ -namespace validatorplugin.test.db; +namespace mockdataplugin.test.db; using { cuid, diff --git a/examples/cap-app/package.json b/examples/cap-app/package.json index 192e877..969bd12 100644 --- a/examples/cap-app/package.json +++ b/examples/cap-app/package.json @@ -6,18 +6,13 @@ "add": "cds add mockdata" }, "dependencies": { - "@sap/cds": "^7.8.1", + "@sap/cds": ">=7", "cap-js-mockdata": "*" }, "devDependencies": { "@cap-js/sqlite": "^1.6.0" }, "cds": { - "requires": {}, - "log": { - "levels": { - "validator-plugin": "error" - } - } + "requires": {} } } diff --git a/examples/cap-app/srv/services.cds b/examples/cap-app/srv/services.cds index 2e653d0..2785b65 100644 --- a/examples/cap-app/srv/services.cds +++ b/examples/cap-app/srv/services.cds @@ -1,6 +1,6 @@ -using {validatorplugin.test.db as db} from '../db/schema'; +using {mockdataplugin.test.db as db} from '../db/schema'; -service ValidatorPluginService { +service MockdataPluginService { entity TestingAnnotation as projection on db.TestingAnnotation; entity TestingNoAnnotation as projection on db.TestingNoAnnotation; entity TestingAssociation as projection on db.TestingAssociation; @@ -11,4 +11,4 @@ service ValidatorPluginService { entity TestingVirtual as projection on db.TestingVirtual; }; -annotate ValidatorPluginService.TestingDraft with @odata.draft.enabled; +annotate MockdataPluginService.TestingDraft with @odata.draft.enabled; diff --git a/examples/cap-app/test/CdsTypes.test.js b/examples/cap-app/test/CdsTypes.test.js index 5862de3..f962d3c 100644 --- a/examples/cap-app/test/CdsTypes.test.js +++ b/examples/cap-app/test/CdsTypes.test.js @@ -3,14 +3,14 @@ import supertest from 'supertest'; const cds = require('@sap/cds'); let request = {}; -const endpoint = '/odata/v4/validator-plugin/CdsTypes'; +const endpoint = '/odata/v4/mockdata-plugin/TestingCdsTypes'; const defaults = { headers: { 'content-type': 'application/json;IEEE754Compatible=true' } }; -describe('Testing CDS types with no validation', () => { +describe('Testing CDS types', () => { beforeAll(() => { request = supertest.agent(cds.app) .set(defaults.headers); @@ -21,14 +21,4 @@ describe('Testing CDS types with no validation', () => { .get(endpoint) .expect(200); }); - - it('Should POST and return OK', async () => { - const payload = { - field_String: 'something' - }; - await request - .post(endpoint) - .send(payload) - .expect(201); - }); }); diff --git a/examples/cap-app/test/ValidateCdsTypes.test.js b/examples/cap-app/test/ValidateCdsTypes.test.js deleted file mode 100644 index 82f8898..0000000 --- a/examples/cap-app/test/ValidateCdsTypes.test.js +++ /dev/null @@ -1,83 +0,0 @@ -import {describe, it, beforeAll, expect} from 'vitest'; -import supertest from 'supertest'; - -const cds = require('@sap/cds'); -let request = {}; -const endpoint = '/odata/v4/validator-plugin/ValidateCdsTypes'; -const defaults = { - headers: { - 'content-type': 'application/json;IEEE754Compatible=true' - } -}; - -describe('Testing CDS types validation', () => { - beforeAll(() => { - request = supertest.agent(cds.app) - .set(defaults.headers); - }); - - it('Should return OK for entity built and served', async () => { - await request - .get(endpoint) - .expect(200); - }); - - it('Should validate formats and return OK', async () => { - const payload = { - field_UUID: '3412101c-8f16-4d47-ad7d-eea71ec40dc6', - field_Boolean: true, - field_UInt8: 255, - field_Int16: -32768, - field_Int32: -2147483648, - field_Integer: -1, - field_Int64: -2, - field_Integer64: -3, - field_Decimal: 1.2, - field_Double: 2.3, - field_Date: '2000-12-31', - field_Time: '12:34:56', - field_DateTime: '2000-12-31T12:34:56Z', - field_Timestamp: '2000-12-31T12:34:56Z', - field_String: 'String', - field_Binary: '10000000010000001001010000101101', - field_LargeBinary: 'c29tZSBzdHJpbmcgdGV4dA==', - field_LargeString: 'LargeString' - }; - await request - .post(endpoint) - .send(payload) - .expect(201); - }); - - it('Should validate formats and return errors', async () => { - const payload = { - field_UUID: 'UUID', - field_Boolean: 'Boolean', - field_UInt8: 'UInt8', - field_Int16: 'Int16', - field_Int32: 'Int32', - field_Integer: 'Integer', - field_Int64: 'Int64', - field_Integer64: 'Integer64', - field_Decimal: 'Decimal', - field_Double: 'Double', - field_Date: 'Date', - field_Time: 'Time', - field_DateTime: 'DateTime', - field_Timestamp: 'Timestamp', - field_String: 'String', - field_Binary: 'Binary', - field_LargeBinary: 'LargeBinary', - field_LargeString: 'LargeString' - }; - const response = await request - .post(endpoint) - .send(payload) - .expect(400); - const messages = JSON.parse(response.text); - const details = messages?.error; - expect(details).toMatchObject({ - message: expect.stringMatching(/^Deserialization Error/) - }); - }); -}); diff --git a/package-lock.json b/package-lock.json index 7eb03ca..1491cac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,12 +26,12 @@ "vitest": "^1.2.2" }, "peerDependencies": { - "@sap/cds": "^7.8.1" + "@sap/cds": ">=7" } }, "examples/cap-app": { "dependencies": { - "@sap/cds": "^7.8.1", + "@sap/cds": ">=7", "cap-js-mockdata": "*" }, "devDependencies": { diff --git a/package.json b/package.json index 66b2306..cf0eefb 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "vitest": "^1.2.2" }, "peerDependencies": { - "@sap/cds": "^7.8.1" + "@sap/cds": ">=7" }, "repository": { "type": "git",