Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
mauriciolauffer
Product and Topic Expert
Product and Topic Expert
3,250

One of the best features when working with CAP is the ability to use CSV files as data sources for the CDS entities. This feature makes developing and testing the applications much easier as one doesn't need to mock database connections or HTTP requests. However, one needs to manually create the files and its contents.

CAP has a command to create the CSV files, cds add data, but no data content is generated. It's just a dummy .csv file with a table header.

Some people use SAP Build Code with Joule to generate the data content. Others, ask ChatGPT and similar tools to create the data for database tables. But, not everyone uses these options and the results may not be ideal, especially when meaningful semantic data is expected for a given field. For instance, a birthday field should contain a date in the past, a full name may contain spaces, an email field should have a valid email address, a country field may be in ISO-9999 format, a Bitcoin wallet has a specific format, and the year of Linux on the desktop should always be in the future (YEAR() + 1).

Future SAP CAP versions, shown in Code Connect 2024, will offer options to generate the .csv content, not just the file. However, it's also dummy generic data respecting data types, not semantics.

With that in mind, I created a SAP CAP plugin to generate meaningful mock data based on CDS entity data types and custom annotations: CAP Mockdata Plugin. This gives developers more flexibility and better control over how those CSV files should be populated with mock data. I particularly like it to generate data for PoC and demos. No more Customer Name: customer123!

Using the CAP Mockdata plugin is as easy as installing and executing it: 

 

npm i -D cap-js-mockdata
cds add mockdata

 

 

The cds add mockdata command will create and populate CSV files for all CDS entities according to their data types.

Mock data generatedMock data generated

If someone wants more control over the generated data, the custom annotation @Mockdata can be used for that. The @Mockdata annotation is a wrapper for the Faker library, meaning all Faker modules can be used to generate mock data for the CDS entities.

Need credit card numbers? @Mockdata { finance: 'creditCardNumber' }

Need URLs? @Mockdata { internet: 'url' }

Need job titles? @Mockdata { person 'jobTitle' }

Need full names? @Mockdata { person: 'fullName' }

 

A little demo...

Let's create two entities and generate mock data for them. I like to keep the annotations in another file to improve readability. If you prefer to keep everything together, no problems. 

 

// db/schema.cds

namespace mockdataplugin.test.db;

using {
  cuid,
  managed,
} from '@sap/cds/common';

entity TestingAnnotations : cuid, managed {
  name       : String;
  sex        : String;
  creditCard : String;
  phone      : String;
  ipAddress  : String;
  email      : String;
  street     : String;
  country    : String;
};
// db/mockdata.cds

using {mockdataplugin.test.db as db} from './schema';

annotate db.TestingAnnotation with {
  name       @Mockdata: {person: 'fullName'};
  sex        @Mockdata: {person: 'sex'};
  creditCard @Mockdata: {finance: 'creditCardNumber'};
  phone      @Mockdata: {phone: 'number'};
  ipAddress  @Mockdata: {internet: 'ipv4'};
  email      @Mockdata: {internet: 'email'};
  street     @Mockdata: {location: 'streetAddress'};
  country    @Mockdata: {location: 'countryCode'};
};

 

 

Open a terminal and execute:

 

 

cds add mockdata

 

 

CDS files will be created and populated based on entity data types and annotations:

CSV file populated by cap-js-mockdataCSV file populated by cap-js-mockdata

 

Easy, right? Now, you can have better data for testing or showcasing your applications.

PS: I've published this SAP CAP plugin months back, and I believe this is the first and only plugin extending the CDS CLI tool. If you want to learn a bit more about the technical details, have a look at the project GitHub repo or ping me  😉

 

1 Comment