Skip to content

Commit

Permalink
test: automate setup and tear down for record export (#495)
Browse files Browse the repository at this point in the history
Co-authored-by: hung-nguyen <[email protected]>
Co-authored-by: tasshi / Masaharu TASHIRO <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2023
1 parent fb32adb commit 665c3f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 9 additions & 3 deletions features/export.feature
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
Feature: cli-kintone export command

Scenario: CliKintoneTest-81 Should return the record contents in CSV format
Given The app "$$TEST_KINTONE_APP_ID_FOR_EXPORT" with "$$TEST_KINTONE_API_TOKEN_FOR_DELETE" has no records
And The app "$$TEST_KINTONE_APP_ID_FOR_EXPORT" has some records as below:
| Text | Number |
| Alice | 10 |
| Bob | 20 |
| Jenny | 30 |
When I run the command with args "record export --base-url $$TEST_KINTONE_BASE_URL --app $$TEST_KINTONE_APP_ID_FOR_EXPORT --api-token $$TEST_KINTONE_API_TOKEN_FOR_EXPORT"
Then I should get the exit code is zero
And The header row of the output message should match with the pattern: "\"Record_number\",\"Text\",\"Number\""
And The output message should match with the pattern: "\"1\",\"Alice\",\"10\""
And The output message should match with the pattern: "\"2\",\"Bob\",\"20\""
And The output message should match with the pattern: "\"3\",\"Jenny\",\"30\""
And The output message should match with the pattern: "\"\d+\",\"Alice\",\"10\""
And The output message should match with the pattern: "\"\d+\",\"Bob\",\"20\""
And The output message should match with the pattern: "\"\d+\",\"Jenny\",\"30\""
30 changes: 29 additions & 1 deletion features/step_definitions/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as assert from "assert";
import { execCliKintoneSync, replaceTokenWithEnvVars } from "../ultils/helper";
import {
createCsvFile,
execCliKintoneSync,
replaceTokenWithEnvVars,
} from "../ultils/helper";
import { Given, When, Then } from "../ultils/world";

Given(
Expand All @@ -13,6 +17,30 @@ Given(
},
);

Given(
"The app {string} with {string} has no records",
function (appId: string, apiToken: string) {
const command = `record delete --app ${appId} --base-url $$TEST_KINTONE_BASE_URL --api-token ${apiToken} --yes`;
const response = execCliKintoneSync(replaceTokenWithEnvVars(command));
if (response.status !== 0) {
throw new Error(`Resetting app failed. Error: \n${response.stderr}`);
}
},
);

Given(
"The app {string} has some records as below:",
async function (appId, table) {
const tempFilePath = await createCsvFile(table.raw());
const command = `record import --file-path ${tempFilePath} --app ${appId} --base-url $$TEST_KINTONE_BASE_URL --username $$TEST_KINTONE_USERNAME --password $$TEST_KINTONE_PASSWORD`;

const response = execCliKintoneSync(replaceTokenWithEnvVars(command));
if (response.status !== 0) {
throw new Error(`Importing CSV failed. Error: \n${response.stderr}`);
}
},
);

When("I run the command with args {string}", function (args: string) {
this.response = execCliKintoneSync(replaceTokenWithEnvVars(args), {
env: this.env,
Expand Down
16 changes: 16 additions & 0 deletions features/ultils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { spawnSync } from "child_process";
import path from "path";
import fs from "fs/promises";
import os from "os";

export const execCliKintoneSync = (
args: string,
Expand Down Expand Up @@ -40,3 +42,17 @@ const replacer = (substring: string) => {
}
return value;
};

export const createCsvFile = async (
inputCsvObject: string[][],
): Promise<string> => {
const csvContent = inputCsvObject
.map((row) => row.map((field) => `"${field}"`).join(","))
.join("\n");

const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "cli-kintone-"));
const tempFilePath = path.join(tempDir, "records.csv");
await fs.writeFile(tempFilePath, csvContent);

return tempFilePath;
};

0 comments on commit 665c3f1

Please sign in to comment.