SAP CAP: Generate .csv-files with test data easily

When you are dealing with SAP CAP you will need test data to test your services and Fiori UIs. This blog post guides you to all relevant steps to easily generate .csv-files with test data and deploy it to a local persistent database (local development).

SAP CAP: Test data in .csv-files
SAP CAP: Test data in .csv-files

Prerequisites

Please consider that your local development environment meets all requirements for the SAP Cloud Application Programming Model (CAP). Maybe, my settings will help you:

Generate a .csn-file of the domain model (CDS)

The domain model is defined in a schema.cds-file in the db-folder. CDS (Core Data Services) is SAP’s universal modeling language to deal with different parts of domains. CDS models comply with the Core Schema Notation (CSN) that represents CDS models as JavaScript objects and goes beyond the JSON Schema.

First, use the terminal to generate a .csn-file from the CDS model. Then, use the VS-Code extension “CAP CDS CSV Generator” that takes this .csn-file for generating .csv-files with test data. Alternatively, you can also create a csn-file with the commands mta build or cds build.

# cds command: cds compile schema.cds --to csn --dest schema.csn
# Use cds compile --help to get further information
# The following command is executed in the project root folder
cds compile db/schema.cds --to csn --dest db/schema.csn

Generate .csv-files with the VS Code extension

Next, execute the following steps to generate .csv-files with test data depending on the domain model that the schema.cds defines.

  • Call the command palette of VS Code (Ctrl + Shift + P or F1) and type “Generate csv file“.
  • Enter the namespace of the schema.cds and choose the .csn-file (e.g. db/schema.csn).
  • Choose a folder to save the .csv-files with test data (e.g. db/data/).
SAP CAP: Test data of a .csv-file
SAP CAP: Test data of a .csv-file

The extension creates 10 entries per .csv-file and this default value can be changed in the extension settings (further information: CAP CDS CSV Generator).

Use a persistent database like SQLite

Instead of using the test data in-memory, you can use a persistent database as well, for example SQLite:

# Add sqlite to dev dependencies of package.json
npm add sqlite3 -D

Deploy the test data of the .csv-files (folder db/data) to the persistent database:

# Deploy test data to db
cds deploy --to sqlite:my.db

When you run the app with cds watch you will see that the database is used.

SAP CAP: CDS watch with db connection
SAP CAP: CDS watch with db connection

Troubleshooting

In the case, you will get an error message like “Expected uri token ‘ODataIdentifier’ could not be found in […]” you can fix it by adding “@odata.Type:’Edm.String‘” after UUID in the schema.cds (further information: Problem accessing single entity using OData v4).

entity EntityName {
    key ID          : UUID @odata.Type:'Edm.String';
    name            : String;
}

Leave a Reply

%d bloggers like this: