Skip to content

Commit

Permalink
refactor: expand on unit testing for topographic stac creation
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Feb 25, 2025
1 parent 1f8b57e commit cf4e442
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/cogify/src/cogify/__test__/extract.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { strictEqual, throws } from 'node:assert';
import { basename } from 'node:path';

Check failure on line 2 in packages/cogify/src/cogify/__test__/extract.test.ts

View workflow job for this annotation

GitHub Actions / screenshot

'basename' is declared but its value is never read.

Check failure on line 2 in packages/cogify/src/cogify/__test__/extract.test.ts

View workflow job for this annotation

GitHub Actions / smoke

'basename' is declared but its value is never read.

Check failure on line 2 in packages/cogify/src/cogify/__test__/extract.test.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'basename' is declared but its value is never read.

Check failure on line 2 in packages/cogify/src/cogify/__test__/extract.test.ts

View workflow job for this annotation

GitHub Actions / build-deploy

'basename' is declared but its value is never read.

Check failure on line 2 in packages/cogify/src/cogify/__test__/extract.test.ts

View workflow job for this annotation

GitHub Actions / build-containers (ubuntu-latest, amd64, linux/amd64)

'basename' is declared but its value is never read.
import { describe, it } from 'node:test';

import { extractMapCodeAndVersion } from '../topo/extract.js';
Expand Down Expand Up @@ -31,7 +32,7 @@ describe('extractMapCodeAndVersion', () => {

it('should not able to parse a version from file', () => {
for (const file of invalidFiles) {
throws(() => extractMapCodeAndVersion(new URL(file)), new Error('Version not found in the file name'));
throws(() => extractMapCodeAndVersion(new URL(file)), new Error(`Version not found in the file name: "${file}"`));
}
});
});
72 changes: 72 additions & 0 deletions packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from 'node:assert';
import { beforeEach, describe, it } from 'node:test';

import { fsa, FsMemory, LogConfig } from '@basemaps/shared';
import { TestTiff } from '@basemaps/test';
import { StacCollection } from 'stac-ts';

import { TopoStacItem } from '../../stac.js';

Check failure on line 8 in packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts

View workflow job for this annotation

GitHub Actions / screenshot

'TopoStacItem' is declared but its value is never read.

Check failure on line 8 in packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts

View workflow job for this annotation

GitHub Actions / smoke

'TopoStacItem' is declared but its value is never read.

Check failure on line 8 in packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'TopoStacItem' is declared but its value is never read.

Check failure on line 8 in packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts

View workflow job for this annotation

GitHub Actions / build-deploy

'TopoStacItem' is declared but its value is never read.

Check failure on line 8 in packages/cogify/src/cogify/cli/__test__/cli.topo.test.ts

View workflow job for this annotation

GitHub Actions / build-containers (ubuntu-latest, amd64, linux/amd64)

'TopoStacItem' is declared but its value is never read.
import { TopoStacCreationCommand } from '../cli.topo.js';

describe('cli.topo', () => {
const fsMemory = new FsMemory();

beforeEach(async () => {
LogConfig.get().level = 'silent';
fsa.register('memory://', fsMemory);
fsMemory.files.clear();

await fsa.write(new URL('memory://source/CJ10_GRIDLESS_GeoTifv1-00.tif'), fsa.readStream(TestTiff.Nztm2000));
await fsa.write(new URL('memory://source/CJ10_GRIDLESS_GeoTifv1-01.tif'), fsa.readStream(TestTiff.Nztm2000));
});

const baseArgs = {
paths: [new URL('memory://source/')],
target: new URL('memory://target/'),
mapSeries: 'topo50',
latestOnly: false,
title: undefined,
output: undefined,

// extra logging arguments
verbose: false,
extraVerbose: false,
};

it('should generate a covering', async () => {
const ret = await TopoStacCreationCommand.handler({ ...baseArgs }).catch((e) => String(e));
assert.equal(ret, undefined); // no errors returned

const files = [...fsMemory.files.keys()];
files.sort();

// console.log(files);

assert.deepEqual(files, [
'memory://source/CJ10_GRIDLESS_GeoTifv1-00.tif',
'memory://source/CJ10_GRIDLESS_GeoTifv1-01.tif',
'memory://target/topo50/gridless_600dpi/2193/CJ10_v1-00.json',
'memory://target/topo50/gridless_600dpi/2193/CJ10_v1-01.json',
'memory://target/topo50/gridless_600dpi/2193/collection.json',
'memory://target/topo50_latest/gridless_600dpi/2193/CJ10.json',
'memory://target/topo50_latest/gridless_600dpi/2193/collection.json',
]);

const collectionJson = await fsa.readJson<StacCollection>(
new URL('memory://target/topo50/gridless_600dpi/2193/collection.json'),
);
assert.equal(collectionJson['description'], 'Topographic maps of New Zealand');
assert.equal(collectionJson['linz:slug'], 'topo50-new-zealand-mainland');
assert.equal(collectionJson['linz:region'], 'new-zealand');

const latestItemUrl = new URL('memory://target/topo50_latest/gridless_600dpi/2193/CJ10.json');
const latestVersion = await fsa.readJson<StacCollection>(latestItemUrl);

// Latest file should be derived_from the source file
const derived = latestVersion.links.filter((f) => f.rel === 'derived_from');
assert.equal(derived.length, 1);

const derivedFile = new URL(derived[0].href, latestItemUrl);
assert.equal(derivedFile.href, 'memory://target/topo50/gridless_600dpi/2193/CJ10_v1-01.json');
});
});
4 changes: 2 additions & 2 deletions packages/cogify/src/cogify/topo/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ export function extractMapCodeAndVersion(url: URL, logger?: LogType): { mapCode:

// extract map code from head of the file name (e.g. CJ10)
const mapCode = fileName.split('_')[0];
if (mapCode == null) throw new Error('Map sheet not found in the file name');
if (mapCode == null) throw new Error(`Map sheet not found in the file name: "${url.href}"`);

// extract version from tail of the file name (e.g. v1-00)
const version = fileName.match(/v(\d)-(\d\d)/)?.[0];
if (version == null) throw new Error('Version not found in the file name');
if (version == null) throw new Error(`Version not found in the file name: "${url.href}"`);

logger?.info({ mapCode, version }, 'extractMapCodeAndVersion()');
return { mapCode, version };
Expand Down
2 changes: 1 addition & 1 deletion packages/cogify/src/cogify/topo/stac.creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function createStacItems(
//
// `../../../` takes us up to the <target> directory
href: `../../../${scale}/${resolution}/${item.epsg.code}/${item.mapCode}_${item.version}.json`,
rel: 'derived-from',
rel: 'derived_from',
type: 'application/json',
});

Expand Down

0 comments on commit cf4e442

Please sign in to comment.