Skip to content

Commit

Permalink
test: add new test case CliKintoneTest-75-76-77 (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-cybo authored Dec 1, 2023
1 parent c068270 commit 20a77e1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
57 changes: 57 additions & 0 deletions features/import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,60 @@ Feature: cli-kintone import command
When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --encoding unsupported_character_code --file-path CliKintoneTest-64.csv"
Then I should get the exit code is non-zero
And The output error message should match with the pattern: "Argument: encoding, Given: \"unsupported_character_code\", Choices: \"utf8\", \"sjis\""

Scenario: CliKintoneTest-75 Should delete attachments successfully when upserting records without the attachment field.
Given The app "app_for_upsert" has no records
And I have a file "attachments/file1.txt" with content: "123"
And I have a file "attachments/file2.txt" with content: "abc"
And The app "app_for_upsert" has some records with attachments in directory "attachments" as below:
| Text | Number | Attachment |
| Alice | 10 | file1.txt |
| Bob | 20 | file2.txt |
And Load the record numbers of the app "app_for_upsert" as variable: "RECORD_NUMBERS"
And The CSV file "CliKintoneTest-75.csv" with content as below:
| Record_number | Text | Number | Attachment |
| $RECORD_NUMBERS[0] | Lisa | 30 | |
| $RECORD_NUMBERS[1] | Rose | 40 | |
| | Jenny | 50 | |
And Load app ID of the app "app_for_upsert" as env var: "APP_ID"
And Load app token of the app "app_for_upsert" with exact permissions "view,add,edit" as env var: "API_TOKEN"
When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --attachments-dir attachments --update-key Record_number --file-path CliKintoneTest-75.csv"
Then I should get the exit code is zero
And The app "app_for_upsert" should have no attachments
And The app "app_for_upsert" should have records as below:
| Record_number | Text | Number | Attachment |
| $RECORD_NUMBERS[0] | Lisa | 30 | |
| $RECORD_NUMBERS[1] | Rose | 40 | |
| \d+ | Jenny | 50 | |

Scenario: CliKintoneTest-76 Should import the records successfully including the Table field.
Given The app "app_for_import_table" has no records
And The CSV file "CliKintoneTest-75.csv" with content as below:
| * | Text_0 | Number_0 | Table | Text | Number |
| * | Alice | 10 | 1 | Alice_1 | 100 |
| | Alice | 10 | 2 | Alice_2 | 200 |
| * | Bob | 20 | 3 | Bob | 300 |
| * | Jenny | 30 | 4 | Jenny | 400 |
| * | | | 5 | Michael | 500 |
And Load app ID of the app "app_for_import_table" as env var: "APP_ID"
And Load app token of the app "app_for_import_table" with exact permissions "add" as env var: "API_TOKEN"
When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --file-path CliKintoneTest-75.csv"
Then I should get the exit code is zero
And The app "app_for_import_table" with table field should have records as below:
| * | Text_0 | Number_0 | Table | (Table.Text) | (Table.Number) |
| * | Alice | 10 | \d+ | Alice_1 | 100 |
| | Alice | 10 | \d+ | Alice_2 | 200 |
| * | Bob | 20 | \d+ | Bob | 300 |
| * | Jenny | 30 | \d+ | Jenny | 400 |
| * | | | \d+ | Michael | 500 |

Scenario: CliKintoneTest-77 Should return the error message when importing with empty input for the required field.
Given The CSV file "CliKintoneTest-77.csv" with content as below:
| Text | Number |
| Alice | 10 |
| Bob | |
And Load app ID of the app "app_for_import_required_field" as env var: "APP_ID"
And Load app token of the app "app_for_import_required_field" with exact permissions "add" as env var: "API_TOKEN"
When I run the command with args "record import --base-url $$TEST_KINTONE_BASE_URL --app $APP_ID --api-token $API_TOKEN --file-path CliKintoneTest-77.csv"
Then I should get the exit code is non-zero
And The output error message should match with the pattern: "ERROR: \[400] \[CB_VA01] Missing or invalid input."
15 changes: 15 additions & 0 deletions features/step_definitions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ Given(
},
);

Given(
"The app {string} has some records with attachments in directory {string} as below:",
async function (appKey: string, attachmentDir: string, table) {
const appCredential = this.getAppCredentialByAppKey(appKey);
const apiToken = this.getAPITokenByAppAndPermissions(appKey, ["add"]);
const csvObject = this.replacePlaceholdersInDataTables(table.raw());
const tempFilePath = await this.generateCsvFile(csvObject);
const command = `record import --file-path ${tempFilePath} --app ${appCredential.appId} --base-url $$TEST_KINTONE_BASE_URL --attachments-dir ${attachmentDir} --api-token ${apiToken}`;
this.execCliKintoneSync(command);
if (this.response.status !== 0) {
throw new Error(`Importing CSV failed. Error: \n${this.response.stderr}`);
}
},
);

When("I run the command with args {string}", function (args: string) {
this.execCliKintoneSync(args);
});
Expand Down
20 changes: 20 additions & 0 deletions features/step_definitions/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Given, Then } from "../utils/world";
import fs from "fs";
import type { SupportedEncoding } from "../utils/helper";
import { SUPPORTED_ENCODING } from "../utils/helper";
import path from "path";

Given(
"The CSV file {string} with content as below:",
Expand Down Expand Up @@ -137,3 +138,22 @@ Then(
}
},
);

Then("The app {string} should have no attachments", function (appKey) {
const attachmentDir = fs.mkdtempSync(
path.join(this.workingDir, "no-attachments-"),
);
const credential = this.getAppCredentialByAppKey(appKey);
const apiToken = this.getAPITokenByAppAndPermissions(appKey, ["view"]);
const command = `record export --app ${credential.appId} --base-url $$TEST_KINTONE_BASE_URL --api-token ${apiToken} --attachments-dir ${attachmentDir}`;
this.execCliKintoneSync(command);
if (this.response.status !== 0) {
throw new Error(`Getting records failed. Error: \n${this.response.stderr}`);
}

assert.equal(
fs.readdirSync(attachmentDir, { recursive: true }).length,
0,
`The directory ${attachmentDir} should be empty`,
);
});

0 comments on commit 20a77e1

Please sign in to comment.