Integrating Test Data in AskUI Workflows from APIs, CSV Files, and Databases
Tags: javascript, typescript, tutorial
Published: 2023-07-11 09:39:43

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.

```javascript
// 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();
```

## 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:

```javascript
// 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);
```

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
```

## 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:

```javascript
// 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
  )
`);
```

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:

```javascript
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
  );
}
```

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:

```javascript
// Get all the data
const dataAsync = await aadb.all('select * from migration;')
console.log(dataAsync);
```

## 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.