Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): Migrate tests to Vitest and TypeScript #22

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"clean": "rm -rf dist/* || true",
"build": "microbundle --name quadbin --format cjs,modern,umd --no-compress",
"build:watch": "microbundle watch --name quadbin --format cjs,modern,umd --no-compress",
"test": "vitest run",
"test:watch": "vitest watch",
"lint": "prettier --check src",
"test": "yarn lint && yarn test-fast",
"test-fast": "tsx node_modules/tape/bin/tape test/**/*.spec.js",
"postversion": "yarn postversion:check && yarn postversion:commit && yarn postversion:push",
"postversion:check": "yarn lint && yarn test",
"postversion:commit": "node scripts/postversion-commit.js",
Expand All @@ -51,13 +51,13 @@
},
"devDependencies": {
"@types/geojson": "^7946.0.15",
"@types/mapbox__tile-cover": "^3.0.4",
"@types/semver": "^7",
"microbundle": "^0.15.1",
"prettier": "^3.4.2",
"semver": "^7.6.3",
"tape": "^5.3.0",
"tsx": "^4.19.2",
"typescript": "^5.7.3"
"typescript": "^5.7.3",
"vitest": "^3.0.5"
},
"engines": {
"node": ">=18"
Expand Down
119 changes: 52 additions & 67 deletions test/index.spec.js → test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'tape';
import {describe, expect, test} from 'vitest';
import {
cellToBoundary,
tileToCell,
Expand All @@ -12,28 +12,42 @@ import {

import {tileToQuadkey} from './quadkey-utils.js';

import PointGeometry from './data/PointGeometry.json' with {type: 'json'};
import MultiPointGeometry from './data/MultiPointGeometry.json' with {type: 'json'};
import LineStringGeometry from './data/LineStringGeometry.json' with {type: 'json'};
import PolygonGeometry from './data/PolygonGeometry.json' with {type: 'json'};
import PolygonAntimeridianGeometry from './data/PolygonAntimeridianGeometry.json' with {type: 'json'};
import MultiPolygonGeometry from './data/MultiPolygonGeometry.json' with {type: 'json'};

const TEST_GEOMETRIES = [
PointGeometry,
MultiPointGeometry,
LineStringGeometry,
PolygonGeometry,
PolygonAntimeridianGeometry,
MultiPolygonGeometry
];

const TEST_TILES = [
{x: 0, y: 0, z: 0, q: 5192650370358181887n},
{x: 1, y: 2, z: 3, q: 5202361257054699519n},
{x: 1023, y: 2412, z: 23, q: 5291729562728627583n}
];

const ANY_QUADBIN = BigInt(524800);
describe('tileToCell', () => {
test.each(TEST_TILES)('%s', ({x, y, z, q}) => {
expect(tileToCell({x, y, z})).toEqual(q);
});
});

test('Quadbin conversion', async t => {
for (const {x, y, z, q} of TEST_TILES) {
describe('cellToTile', () => {
test.each(TEST_TILES)('%s', ({x, y, z, q}) => {
const tile = {x, y, z};
const quadbin = tileToCell(tile);
t.deepEqual(quadbin, q, 'quadbins match');

const tile2 = cellToTile(quadbin);
t.deepEqual(tile, tile2, 'tiles match');
}

t.end();
expect(cellToTile(tileToCell(tile))).toEqual(tile);
});
});

test('Quadbin cellToParent', async t => {
test('cellToParent', () => {
let tile = {x: 134, y: 1238, z: 10};
const quadkey = tileToQuadkey(tile);

Expand All @@ -44,69 +58,41 @@ test('Quadbin cellToParent', async t => {
tile = cellToTile(parent);
const quadkey2 = tileToQuadkey(tile);

t.deepEquals(quadkey2, quadkey.slice(0, tile.z), `parent correct ${quadkey2}`);
t.deepEquals(Number(zoom), tile.z, `zoom correct ${zoom}`);
expect(quadkey2).toEqual(quadkey.slice(0, tile.z));
expect(Number(zoom)).toEqual(tile.z);
}

t.end();
});

test('Quadbin cellToChildren', async t => {
const parentTile = { z: 8, x: 59, y: 97 };
test('cellToChildren', t => {
const parentTile = {z: 8, x: 59, y: 97};
const parent = tileToCell(parentTile);

t.deepEquals(cellToChildren(parent, 8n), [parent], 'children at resolution + 0');
expect(cellToChildren(parent, 8n)).toEqual([parent]);

// Order is row major, starting from NW and ending at SE.
t.deepEquals(
cellToChildren(parent, 9n).map(cellToTile),
[
{ z: 9, x: 118, y: 194 }, // nw
{ z: 9, x: 119, y: 194 }, // ne
{ z: 9, x: 118, y: 195 }, // sw
{ z: 9, x: 119, y: 195 } // se
],
'children at resolution + 1'
);

t.deepEquals(cellToChildren(parent, 10n).length, 16, 'children at resolution + 2');

t.end();
expect(cellToChildren(parent, 9n).map(cellToTile)).toEqual([
{z: 9, x: 118, y: 194}, // nw
{z: 9, x: 119, y: 194}, // ne
{z: 9, x: 118, y: 195}, // sw
{z: 9, x: 119, y: 195} // se
]);

expect(cellToChildren(parent, 10n).length).toBe(16);
});

// Zoom:26 test not agreeing with Python
import PointGeometry from './data/PointGeometry.json' with {type: 'json'};
import MultiPointGeometry from './data/MultiPointGeometry.json' with {type: 'json'};
import LineStringGeometry from './data/LineStringGeometry.json' with {type: 'json'};
import PolygonGeometry from './data/PolygonGeometry.json' with {type: 'json'};
import PolygonAntimeridianGeometry from './data/PolygonAntimeridianGeometry.json' with {type: 'json'};
import MultiPolygonGeometry from './data/MultiPolygonGeometry.json' with {type: 'json'};
const testCases = [
PointGeometry,
MultiPointGeometry,
LineStringGeometry,
PolygonGeometry,
PolygonAntimeridianGeometry,
MultiPolygonGeometry
];

test('Quadbin geometryToCells', async t => {
for (const {name, geometry, expected} of testCases) {
describe('geometryToCells', () => {
// NOTE: zoom=26 test does not agree with Python.
test.each(TEST_GEOMETRIES)('$name', ({name, geometry, expected}) => {
for (const resolution of Object.keys(expected)) {
const expectedCells = expected[resolution].map(BigInt).sort();
const cells = geometryToCells(geometry, resolution).sort();
t.deepEquals(
cells,
expectedCells,
`Correct cells generated from ${name} geometry at resolution ${resolution}`
);
const cells = geometryToCells(geometry, BigInt(resolution)).sort();
expect(cells).toEqual(expectedCells);
}
}
t.end();
});
});

test('Quadbin cellToBoundary', t => {
for (const {quadbin, expectedPolygon} of [
describe('cellToBoundary', () => {
const TEST_CASES = [
{
quadbin: BigInt(524800),
expectedPolygon: {
Expand Down Expand Up @@ -152,10 +138,9 @@ test('Quadbin cellToBoundary', t => {
]
}
}
]) {
const result = cellToBoundary(quadbin);
t.deepEquals(result, expectedPolygon);
}
];

t.end();
test.each(TEST_CASES)('$quadbin', ({quadbin, expectedPolygon}) => {
expect(cellToBoundary(quadbin)).toEqual(expectedPolygon);
});
});
6 changes: 4 additions & 2 deletions test/quadkey-utils.js → test/quadkey-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function tileToQuadkey(tile) {
type Tile = {x: number; y: number; z: number};

export function tileToQuadkey(tile: Tile) {
let index = '';
for (let z = tile.z; z > 0; z--) {
let b = 0;
Expand All @@ -10,7 +12,7 @@ export function tileToQuadkey(tile) {
return index;
}

function quadkeyToTile(quadkey) {
function quadkeyToTile(quadkey: string) {
const tile = {x: 0, y: 0, z: quadkey.length};

for (let i = tile.z; i > 0; i--) {
Expand Down
Loading