Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 committed Jan 7, 2025
1 parent 05482a4 commit 67db862
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 113 deletions.
13 changes: 7 additions & 6 deletions src/99_create_stats.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import main from './processes/99_create_stats.js';

main().then(() => {
process.exit(0);
}).catch((e) => {
console.error(e);
process.exit(1);
});
main(process.argv)
.then(() => {
process.exit(0);
}).catch((e) => {
console.error(e);
process.exit(1);
});
2 changes: 1 addition & 1 deletion src/processes/01_make_prefecture_city.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function outputCityData(outDir: string, prefName: string, apiData: CityApi

async function main(argv: string[]) {
const updated = Math.floor(Date.now() / 1000);
const outDir = argv[2] || path.join(import.meta.dirname, '..', 'out', 'api');
const outDir = argv[2] || path.join(import.meta.dirname, '..', '..', 'out', 'api');
fs.mkdirSync(outDir, { recursive: true });

const [
Expand Down
28 changes: 28 additions & 0 deletions src/processes/02_make_machi_aza.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import assert from 'node:assert';
import test from 'node:test';

import fs from 'node:fs/promises';
import main from './02_make_machi_aza.js';
import { MachiAzaApi } from '../data.js';

test.describe('with filter for 452092 (宮崎県えびの市)', async () => {
test.before(async () => {
process.env.SETTINGS_JSON = JSON.stringify({ lgCodes: ['452092'] });
});

test.after(async () => {
delete process.env.SETTINGS_JSON;
});

test('it generates the API', async () => {
await fs.rm('./out/api_miyazaki_ebino', { recursive: true, force: true });
await main(['', '', './out/api_miyazaki_ebino']);
assert.ok(true);

const e = JSON.parse(await fs.readFile('./out/api_miyazaki_ebino/ja/宮崎県/えびの市.json', 'utf-8')) as MachiAzaApi;
const eData = e.data;
assert(eData.length > 100);
assert(eData.find((city) => city.machiaza_id === '0000110')?.koaza === '下村');
});
});

2 changes: 1 addition & 1 deletion src/processes/02_make_machi_aza.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function outputMachiAzaData(

async function main(argv: string[]) {
const updated = Math.floor(Date.now() / 1000);
const outDir = argv[2] || path.join(import.meta.dirname, '..', 'out', 'api');
const outDir = argv[2] || path.join(import.meta.dirname, '..', '..', 'out', 'api');
fs.mkdirSync(outDir, { recursive: true });


Expand Down
26 changes: 26 additions & 0 deletions src/processes/03_make_rsdt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'node:assert';
import test from 'node:test';

import fs from 'node:fs/promises';
import main from './03_make_rsdt.js';
import { getRangesFromCSV } from './10_refresh_csv_ranges.js';

test.describe('with filter for 131059 (東京都文京区)', async () => {
test.before(async () => {
process.env.SETTINGS_JSON = JSON.stringify({ lgCodes: ['131059'] });
});

test.after(async () => {
delete process.env.SETTINGS_JSON;
});

test('it generates the API', async () => {
await fs.rm('./out/api_tokyo_bunkyo', { recursive: true, force: true });
await main(['', '', './out/api_tokyo_bunkyo']);
assert.ok(true);

const headers = await getRangesFromCSV('./out/api_tokyo_bunkyo/ja/東京都/文京区-住居表示.txt');
assert(typeof headers !== 'undefined');
assert.equal(headers[0].name, '白山一丁目');
});
});
3 changes: 2 additions & 1 deletion src/processes/03_make_rsdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async function outputRsdtData(outDir: string, outFilename: string, apiData: Rsdt

const outFileTXT = path.join(outDir, 'ja', outFilename + '-住居表示.txt');
const txt = serializeApiDataTxt(apiData);
await fs.promises.mkdir(path.dirname(outFileTXT), { recursive: true });
await fs.promises.writeFile(outFileTXT, txt.data);

// const outFilePbf = path.join(outDir, 'ja', outFilename + '.pbf');
Expand All @@ -148,7 +149,7 @@ async function outputRsdtData(outDir: string, outFilename: string, apiData: Rsdt
}

async function main(argv: string[]) {
const outDir = argv[2] || path.join(import.meta.dirname, '..', 'out', 'api');
const outDir = argv[2] || path.join(import.meta.dirname, '..', '..', 'out', 'api');
fs.mkdirSync(outDir, { recursive: true });

const machiAzaData = await getAndParseCSVDataForId<MachiAzaData>('ba-o1-000000_g2-000003'); // 市区町村 & 町字
Expand Down
26 changes: 26 additions & 0 deletions src/processes/04_make_chiban.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'node:assert';
import test from 'node:test';

import fs from 'node:fs/promises';
import main from './04_make_chiban.js';
import { getRangesFromCSV } from './10_refresh_csv_ranges.js';

test.describe('with filter for 465054 (鹿児島県熊毛郡屋久島町)', async () => {
test.before(async () => {
process.env.SETTINGS_JSON = JSON.stringify({ lgCodes: ['465054'] });
});

test.after(async () => {
delete process.env.SETTINGS_JSON;
});

test('it generates the API', async () => {
await fs.rm('./out/api_kagoshima_yakushima', { recursive: true, force: true });
await main(['', '', './out/api_kagoshima_yakushima']);
assert.ok(true);

const headers = await getRangesFromCSV('./out/api_kagoshima_yakushima/ja/鹿児島県/熊毛郡屋久島町-地番.txt');
assert(typeof headers !== 'undefined');
assert.equal(headers[0].name, '安房');
});
});
139 changes: 72 additions & 67 deletions src/processes/04_make_chiban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ async function outputChibanData(outDir: string, outFilename: string, apiData: Ch

const outFileTXT = path.join(outDir, 'ja', outFilename + '-地番.txt');
const txt = serializeApiDataTxt(apiData);
await fs.promises.mkdir(path.dirname(outFileTXT), { recursive: true });
await fs.promises.writeFile(outFileTXT, txt.data);

console.log(`${outFilename}: ${apiData.length.toString(10).padEnd(4, ' ')} 件の町字の地番を出力した`);
}

async function main(argv: string[]) {
const outDir = argv[2] || path.join(import.meta.dirname, '..', 'out', 'api');
const outDir = argv[2] || path.join(import.meta.dirname, '..', '..', 'out', 'api');
fs.mkdirSync(outDir, { recursive: true });

console.log('事前準備: 町字データを取得中...');
Expand All @@ -108,7 +109,7 @@ async function main(argv: string[]) {
}
console.log('事前準備: 町字データを取得しました');

const progressInst = new cliProgress.MultiBar({
const progress = new cliProgress.SingleBar({
format: ' {bar} {percentage}% | ETA: {eta_formatted} | {value}/{total}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
Expand All @@ -117,80 +118,84 @@ async function main(argv: string[]) {
// No-TTY output is required for CI/CD environments
noTTYOutput: true,
});
const progress = progressInst.create(machiAzas.length, 0);

let currentLgCode: string | undefined = undefined;
for (const ma of machiAzas) {
if (currentLgCode && ma.lg_code === currentLgCode) {
// we have already processed this lg_code, so we can skip it
continue;
} else if (currentLgCode !== ma.lg_code) {
currentLgCode = ma.lg_code;
}
let area = `${ma.pref} ${ma.county}${ma.city}`;
if (ma.ward !== '') {
area += ` ${ma.ward}`;
}
let searchQuery = `${area} 地番マスター`;
const results = await ckanPackageSearch(searchQuery);
const chibanDataRef = findResultByTypeAndArea(results, '地番マスター(市区町村)', area);
const chibanPosDataRef = findResultByTypeAndArea(results, '地番マスター位置参照拡張(市区町村)', area);
if (!chibanDataRef) {
console.error(`Insufficient data found for ${searchQuery} (地番マスター)`);
progress.increment();
continue;
}

const mainStream = getAndStreamCSVDataForId<ChibanData>(chibanDataRef.name);
const posStream = chibanPosDataRef ?
getAndStreamCSVDataForId<ChibanPosData>(chibanPosDataRef.name)
:
// 位置参照拡張データが無い場合もある
(async function*() {})();

const rawData = mergeDataLeftJoin(mainStream, posStream, ['lg_code', 'machiaza_id', 'prc_id'], true);
// console.log(`処理: ${ma.pref} ${ma.county}${ma.city} ${ma.ward} の地番データを処理中...`);

let currentMachiAza: MachiAzaData | undefined = undefined;
const apiData: ChibanApi = [];
let currentChibanList: SingleChiban[] = [];
for await (const raw of rawData) {
let ma = machiAzaDataByCode.get(`${raw.lg_code}|${raw.machiaza_id}`);
if (!ma) {
progress.start(machiAzas.length, 0);
try {

let currentLgCode: string | undefined = undefined;
for (const ma of machiAzas) {
if (currentLgCode && ma.lg_code === currentLgCode) {
// we have already processed this lg_code, so we can skip it
progress.increment();
continue;
} else if (currentLgCode !== ma.lg_code) {
currentLgCode = ma.lg_code;
}
let area = `${ma.pref} ${ma.county}${ma.city}`;
if (ma.ward !== '') {
area += ` ${ma.ward}`;
}
let searchQuery = `${area} 地番マスター`;
const results = await ckanPackageSearch(searchQuery);
const chibanDataRef = findResultByTypeAndArea(results, '地番マスター(市区町村)', area);
const chibanPosDataRef = findResultByTypeAndArea(results, '地番マスター位置参照拡張(市区町村)', area);
if (!chibanDataRef) {
console.error(`Insufficient data found for ${searchQuery} (地番マスター)`);
progress.increment();
continue;
}
if (currentMachiAza && (currentMachiAza.machiaza_id !== ma.machiaza_id || currentMachiAza.lg_code !== ma.lg_code)) {

const mainStream = getAndStreamCSVDataForId<ChibanData>(chibanDataRef.name);
const posStream = chibanPosDataRef ?
getAndStreamCSVDataForId<ChibanPosData>(chibanPosDataRef.name)
:
// 位置参照拡張データが無い場合もある
(async function*() {})();

const rawData = mergeDataLeftJoin(mainStream, posStream, ['lg_code', 'machiaza_id', 'prc_id'], true);
// console.log(`処理: ${ma.pref} ${ma.county}${ma.city} ${ma.ward} の地番データを処理中...`);

let currentMachiAza: MachiAzaData | undefined = undefined;
const apiData: ChibanApi = [];
let currentChibanList: SingleChiban[] = [];
for await (const raw of rawData) {
let ma = machiAzaDataByCode.get(`${raw.lg_code}|${raw.machiaza_id}`);
if (!ma) {
continue;
}
if (currentMachiAza && (currentMachiAza.machiaza_id !== ma.machiaza_id || currentMachiAza.lg_code !== ma.lg_code)) {
apiData.push({
machiAza: currentMachiAza,
chibans: currentChibanList,
});
currentChibanList = [];
currentMachiAza = ma;
}
if (!currentMachiAza) {
currentMachiAza = ma;
}

currentChibanList.push({
prc_num1: raw.prc_num1,
prc_num2: raw.prc_num2 !== '' ? raw.prc_num2 : undefined,
prc_num3: raw.prc_num3 !== '' ? raw.prc_num3 : undefined,
point: 'rep_srid' in raw ? projectABRData(raw) : undefined,
});
}
if (currentMachiAza && currentChibanList.length > 0) {
apiData.push({
machiAza: currentMachiAza,
chibans: currentChibanList,
});
currentChibanList = [];
currentMachiAza = ma;
}
if (!currentMachiAza) {
currentMachiAza = ma;
}

currentChibanList.push({
prc_num1: raw.prc_num1,
prc_num2: raw.prc_num2 !== '' ? raw.prc_num2 : undefined,
prc_num3: raw.prc_num3 !== '' ? raw.prc_num3 : undefined,
point: 'rep_srid' in raw ? projectABRData(raw) : undefined,
});
}
if (currentMachiAza && currentChibanList.length > 0) {
apiData.push({
machiAza: currentMachiAza,
chibans: currentChibanList,
});
await outputChibanData(outDir, path.join(
ma.pref,
`${ma.county}${ma.city}${ma.ward}`,
), apiData);
progress.increment();
}
await outputChibanData(outDir, path.join(
ma.pref,
`${ma.county}${ma.city}${ma.ward}`,
), apiData);
progress.increment();
} finally {
progress.stop();
}
progress.stop();
}

export default main;
Loading

0 comments on commit 67db862

Please sign in to comment.