Skip to content

Commit

Permalink
feat: generate api コマンドを追加 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
itigoore01 committed Sep 13, 2022
1 parent f83f041 commit 987ba96
Show file tree
Hide file tree
Showing 37 changed files with 1,676 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
run: yarn --check-files --frozen-lockfile --non-interactive
- name: Test
run: yarn test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3

e2e:
runs-on: ubuntu-latest
Expand Down
4 changes: 1 addition & 3 deletions jest-e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ module.exports = {
testPathIgnorePatterns: ['/__tests__/'],
watchPathIgnorePatterns: ['/dist/'],
clearMocks: true,
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
collectCoverage: false,
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"access": "public",
"tag": "next"
},
"type": "commonjs",
"bin": {
"adg": "bin/adg.js"
},
Expand All @@ -19,15 +20,22 @@
"postbuild": "node -r @swc-node/register ./scripts/postbuild.ts",
"type-check": "tsc",
"test": "jest",
"pree2e": "yarn build",
"e2e": "jest -c jest-e2e.config.js",
"lint": "eslint . --ext ts --ext tsx --ext js",
"lint:fix": "yarn lint --fix",
"format": "prettier --write .",
"prepare": "husky install"
},
"dependencies": {
"@supercharge/promise-pool": "^2.3.2",
"commander": "9.4.0",
"fast-glob": "3.2.12"
"csv-parse": "^5.3.0",
"fast-glob": "3.2.12",
"iconv-lite": "^0.6.3",
"jszip": "^3.10.1",
"listr2": "^5.0.5",
"progress": "^2.0.3"
},
"devDependencies": {
"@commitlint/cli": "17.1.2",
Expand All @@ -39,6 +47,7 @@
"@swc/jest": "0.2.22",
"@types/jest": "29.0.1",
"@types/node": "18.7.16",
"@types/progress": "^2.0.5",
"@types/react": "18.0.19",
"@typescript-eslint/eslint-plugin": "5.36.2",
"@typescript-eslint/parser": "5.36.2",
Expand Down
3 changes: 1 addition & 2 deletions src/__e2e__/__snapshots__/adg-e2e.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`show help and exit code 1 1`] = `
"warning package.json: License should be a valid SPDX license expression
Usage: adg [options] [command]
"Usage: adg [options] [command]
Options:
-h, --help display help for command
Expand Down
33 changes: 20 additions & 13 deletions src/__e2e__/adg-e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test, beforeAll, beforeEach, expect } from '@jest/globals';
import { spawnSync } from 'child_process';
import fastGlob from 'fast-glob';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { join } from 'path';

Expand All @@ -8,27 +9,21 @@ const workDir = join(process.cwd(), 'tmp/e2e');
const outputDir = join(workDir, 'output');

beforeAll(() => {
const buildResult = spawnSync('yarn', ['build'], { encoding: 'utf-8' });
console.log(buildResult.output.join(''));
// ビルドに成功しているかチェック
expect(buildResult.status).toBe(0);

// インストールする
rmSync(workDir, { force: true, recursive: true });
mkdirSync(workDir, { recursive: true });
writeFileSync(
join(workDir, 'package.json'),
JSON.stringify({
name: 'address-data-generator-e2e',
license: 'none',
license: 'MIT',
}),
'utf-8'
);
const yarnAddResult = spawnSync('yarn', ['add', distDir], {
encoding: 'utf-8',
cwd: workDir,
});
expect(yarnAddResult.output.join(''));
expect(yarnAddResult.status).toBe(0);
});

Expand All @@ -44,12 +39,20 @@ test('show help and exit code 1', () => {
expect(result.stderr).toMatchSnapshot();
});

test('generate api', () => {
const result = runAdg(['generate', 'api', outputDir]);
test('generate api', async () => {
const result = runAdg(['generate', 'api', outputDir], {
maxBuffer: 1024 * 1024 * 10,
});

expect(result.status).toBe(1);
expect(result.stderr).toMatch('not implemented.');
});
expect(result.output.join('')).toMatch(`[SUCCESS] Write addresses json`);
expect(result.status).toBe(0);

const paths = await fastGlob('**/*.json', {
cwd: outputDir,
});

expect(paths.length).toBeGreaterThan(0);
}, 60000);

test('minify', () => {
const result = runAdg(['minify']);
Expand All @@ -58,9 +61,13 @@ test('minify', () => {
expect(result.stderr).toMatch('not implemented.');
});

function runAdg(args: string[] = []) {
function runAdg(
args: string[] = [],
options?: Parameters<typeof spawnSync>['2']
) {
return spawnSync('yarn', ['adg', ...args], {
encoding: 'utf-8',
cwd: workDir,
...options,
});
}
186 changes: 186 additions & 0 deletions src/__tests__/actions/api-action.test.ts

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions src/__tests__/actions/generate/api-action.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/__tests__/actions/minify-action.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`write addresses json: pretty = false 1`] = `"[{"postalCode":"0600000","regionId":1,"region":"北海道","locality":"札幌市中央区","address1":"","address2":"","regionKana":"ホッカイドウ","localityKana":"サッポロシチュウオウク","address1Kana":"","address2Kana":""}]"`;

exports[`write addresses json: pretty = false 2`] = `"[{"postalCode":"0360223","regionId":2,"region":"青森県","locality":"平川市","address1":"西野曽江川崎","address2":"","regionKana":"アオモリケン","localityKana":"ヒラカワシ","address1Kana":"ニシノゾエカワサキ","address2Kana":""},{"postalCode":"0360223","regionId":2,"region":"青森県","locality":"平川市","address1":"西野曽江橋元","address2":"","regionKana":"アオモリケン","localityKana":"ヒラカワシ","address1Kana":"ニシノゾエハシモト","address2Kana":""},{"postalCode":"0360223","regionId":2,"region":"青森県","locality":"平川市","address1":"西野曽江広田","address2":"","regionKana":"アオモリケン","localityKana":"ヒラカワシ","address1Kana":"ニシノゾエヒロタ","address2Kana":""}]"`;

exports[`write addresses json: pretty = true 1`] = `
"[
{
"postalCode": "0600000",
"regionId": 1,
"region": "北海道",
"locality": "札幌市中央区",
"address1": "",
"address2": "",
"regionKana": "ホッカイドウ",
"localityKana": "サッポロシチュウオウク",
"address1Kana": "",
"address2Kana": ""
}
]"
`;

exports[`write addresses json: pretty = true 2`] = `
"[
{
"postalCode": "0360223",
"regionId": 2,
"region": "青森県",
"locality": "平川市",
"address1": "西野曽江川崎",
"address2": "",
"regionKana": "アオモリケン",
"localityKana": "ヒラカワシ",
"address1Kana": "ニシノゾエカワサキ",
"address2Kana": ""
},
{
"postalCode": "0360223",
"regionId": 2,
"region": "青森県",
"locality": "平川市",
"address1": "西野曽江橋元",
"address2": "",
"regionKana": "アオモリケン",
"localityKana": "ヒラカワシ",
"address1Kana": "ニシノゾエハシモト",
"address2Kana": ""
},
{
"postalCode": "0360223",
"regionId": 2,
"region": "青森県",
"locality": "平川市",
"address1": "西野曽江広田",
"address2": "",
"regionKana": "アオモリケン",
"localityKana": "ヒラカワシ",
"address1Kana": "ニシノゾエヒロタ",
"address2Kana": ""
}
]"
`;
135 changes: 135 additions & 0 deletions src/__tests__/generate/create-postal-code-addresses-map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { createPostalCodeAddressesMap } from '@/lib/generate/create-postal-code-addresses-map';
import { Address } from '@/lib/types/address';
import { KenAllRecord } from '@/lib/types/ken-all-record';
import { expect, test } from '@jest/globals';

test('returns map', async () => {
const map = await createPostalCodeAddressesMap(mockRecordGenerator());

expect([...map.keys()]).toEqual(['0600000', '0360223']);

expect(map.get('0600000')).toEqual([
{
postalCode: '0600000',
regionId: 1,
region: '北海道',
locality: '札幌市中央区',
address1: '',
address2: '',
regionKana: 'ホッカイドウ',
localityKana: 'サッポロシチュウオウク',
address1Kana: '',
address2Kana: '',
},
] as Address[]);

expect(map.get('0360223')).toEqual([
{
postalCode: '0360223',
regionId: 2,
region: '青森県',
locality: '平川市',
address1: '西野曽江川崎',
address2: '',
regionKana: 'アオモリケン',
localityKana: 'ヒラカワシ',
address1Kana: 'ニシノゾエカワサキ',
address2Kana: '',
},
{
postalCode: '0360223',
regionId: 2,
region: '青森県',
locality: '平川市',
address1: '西野曽江橋元',
address2: '',
regionKana: 'アオモリケン',
localityKana: 'ヒラカワシ',
address1Kana: 'ニシノゾエハシモト',
address2Kana: '',
},
{
postalCode: '0360223',
regionId: 2,
region: '青森県',
locality: '平川市',
address1: '西野曽江広田',
address2: '',
regionKana: 'アオモリケン',
localityKana: 'ヒラカワシ',
address1Kana: 'ニシノゾエヒロタ',
address2Kana: '',
},
] as Address[]);
});

async function* mockRecordGenerator(): AsyncGenerator<KenAllRecord> {
yield [
'01101',
'060 ',
'0600000',
'ホッカイドウ',
'サッポロシチュウオウク',
'イカニケイサイガナイバアイ',
'北海道',
'札幌市中央区',
'以下に掲載がない場合',
'0',
'0',
'0',
'0',
'0',
'0',
];
yield [
'02210',
'03602',
'0360223',
'アオモリケン',
'ヒラカワシ',
'ニシノゾエカワサキ',
'青森県',
'平川市',
'西野曽江川崎',
'0',
'1',
'0',
'1',
'0',
'0',
];
yield [
'02210',
'03602',
'0360223',
'アオモリケン',
'ヒラカワシ',
'ニシノゾエハシモト',
'青森県',
'平川市',
'西野曽江橋元',
'0',
'1',
'0',
'1',
'0',
'0',
];
yield [
'02210',
'03602',
'0360223',
'アオモリケン',
'ヒラカワシ',
'ニシノゾエヒロタ',
'青森県',
'平川市',
'西野曽江広田',
'0',
'1',
'0',
'1',
'0',
'0',
];
}

0 comments on commit 987ba96

Please sign in to comment.