DEV Community

Cover image for Integrating Test Data in AskUI Workflows from APIs, CSV Files, and Databases
Johannes Dienst
Johannes Dienst

Posted on • Originally published at askui.com

Integrating Test Data in AskUI Workflows from APIs, CSV Files, and Databases

Are you struggling to integrate test data into your AskUI workflows effectively? Do you find yourself juggling APIs, CSV files, and databases without a clear approach?

Integrating test data from various sources can be a daunting task.

By the end of this post, you'll have a foundation for integrating test data into your AskUI workflows using APIs, CSV files, and databases.

Integrate API

To use an API for fetching test data, you'll need a library like axios. After installing it, import it into your AskUI workflow file and use it to call an API, extract data, and insert it into a text field.

// Top of your workflow file
import axios from 'axios';

const url = 'https://fruityvice.com/api/fruit/all';
const data = await axios.get(url);
await aui.typeIn(data.data[0].name)
         .textfield()
         .contains()
         .text()
         .withText('E-Mail Address')
         .exec();
Enter fullscreen mode Exit fullscreen mode

Reading from CSV Files

For integrating CSV files and databases, let's dive into a combined example. Begin by installing the required libraries and adding the necessary imports to your AskUIworkflow file. First, read the CSV file into your code and parse it:

// Top of your workflow file
import * as fs from 'fs';
import csv from 'async-csv';

const csvData = fs.readFileSync("./data.csv");
const rows = await csv.parse(csvData);
Enter fullscreen mode Exit fullscreen mode

Our CSV data looks like this. Save it as data.csv in the root-folder of your AskUI project:

year_month,month_of_release,passenger_type,direction,sex,age,estimate,standard_error,status
2001-01,2020-09,Long-term migrant,Arrivals,Female,0-4 years,344,0,Final
2001-01,2020-09,Long-term migrant,Arrivals,Male,0-4 years,341,0,Final
2001-01,2020-09,Long-term migrant,Arrivals,Female,10-14 years,459,0,Final
2001-01,2020-09,Long-term migrant,Arrivals,Male,10-14 years,510,0,Final
2001-01,2020-09,Long-term migrant,Arrivals,Female,15-19 years,899,0,Final
2001-01,2020-09,Long-term migrant,Arrivals,Male,15-19 years,904,0,Final
Enter fullscreen mode Exit fullscreen mode

Create and Initialize Database

Next, create a database on your file system, within the same project as your Ascii workflow file. Set up a schema by opening the database and creating a table:

// Top of your workflow file
import * as aadb from 'aa-sqlite';

// Open a new database
const filepath = "./data.db";
await aadb.open(filepath);

// Create a table
await aadb.run(`
  CREATE TABLE migration (
    year_month VARCHAR(10),
    month_of_release VARCHAR(10),
    passenger_type VARCHAR(50),
    direction VARCHAR(20),
    sex VARCHAR(10),
    age VARCHAR(50),
    estimate INT
  )
`);
Enter fullscreen mode Exit fullscreen mode

Once completed, you'll have successfully integrated test data into your AskUI workflow using both CSV files and databases.

Insert Data into Database

Now that we have our database and table set up, we'll insert the parsed CSV data into the database. To do this, iterate over each row of the CSV data and use a SQL statement to insert it:

for (const row of rows) {
  await aadb.run(`
    INSERT INTO migration (
      year_month, month_of_release, passenger_type,
      direction, sex, age, estimate
    ) VALUES (?, ?, ?, ?, ?, ?, ?)`,
    row.year_month, row.month_of_release, row.passenger_type,
    row.direction, row.sex, row.age, row.estimate
  );
}
Enter fullscreen mode Exit fullscreen mode

If you don't have a CSV file, you can follow a similar approach to the previous example, where you read data directly from the database and integrate it into your code like this:

// Get all the data
const dataAsync = await aadb.all('select * from migration;')
console.log(dataAsync);
Enter fullscreen mode Exit fullscreen mode

Conclusion

We provided examples of incorporating test data into your AskUI workflow using APIs, CSV files, or databases. While these examples are not comprehensive, they serve as a solid starting point for integrating test data into your projects.

Top comments (0)