Skip to content

Commit

Permalink
test(lambda-tiler): implemented test suite for attribution functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Oct 21, 2024
1 parent af4ae75 commit 18af34b
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/attribution/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { strictEqual } from 'node:assert';
import { describe, it } from 'node:test';

import { StacProvider } from '@basemaps/geo';

import { copyright, createLicensorAttribution } from '../utils.js';

const defaultAttribution = `${copyright} LINZ`;

describe('utils', () => {
const FakeHost: StacProvider = {
name: 'FakeHost',
roles: ['host'],
};
const FakeLicensor1: StacProvider = {
name: 'FakeLicensor1',
roles: ['licensor'],
};
const FakeLicensor2: StacProvider = {
name: 'FakeLicensor2',
roles: ['licensor'],
};

it('default attribution: no providers', () => {
const providers = undefined;
const attribution = createLicensorAttribution(providers);

strictEqual(attribution, defaultAttribution);
});

it('default attribution: empty providers', () => {
const providers: StacProvider[] = [];
const attribution = createLicensorAttribution(providers);

strictEqual(attribution, defaultAttribution);
});

it('default attribution: one provider, no licensors', () => {
const providers = [FakeHost];

const attribution = createLicensorAttribution(providers);
strictEqual(attribution, defaultAttribution);
});

it('custom attribution: one provider, one licensor', () => {
const providers = [FakeLicensor1];

const attribution = createLicensorAttribution(providers);
strictEqual(attribution, `${copyright} ${FakeLicensor1.name}`);
});

it('custom attribution: two providers, one licensor', () => {
const providers = [FakeHost, FakeLicensor1];

const attribution = createLicensorAttribution(providers);
strictEqual(attribution, `${copyright} ${FakeLicensor1.name}`);
});

it('custom attribution: two providers, two licensors', () => {
const providers = [FakeLicensor1, FakeLicensor2];

const attribution = createLicensorAttribution(providers);
strictEqual(attribution, `${copyright} ${FakeLicensor1.name}, ${FakeLicensor2.name}`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import assert, { strictEqual } from 'node:assert';
import { afterEach, before, describe, it } from 'node:test';

import { copyright, createLicensorAttribution } from '@basemaps/attribution/build/utils/utils.js';

Check failure on line 4 in packages/lambda-tiler/src/routes/__tests__/tile.style.json.attribution.test.ts

View workflow job for this annotation

GitHub Actions / build-containers

Cannot find module '@basemaps/attribution/build/utils/utils.js' or its corresponding type declarations.

Check failure on line 4 in packages/lambda-tiler/src/routes/__tests__/tile.style.json.attribution.test.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot find module '@basemaps/attribution/build/utils/utils.js' or its corresponding type declarations.

Check failure on line 4 in packages/lambda-tiler/src/routes/__tests__/tile.style.json.attribution.test.ts

View workflow job for this annotation

GitHub Actions / build-deploy

Cannot find module '@basemaps/attribution/build/utils/utils.js' or its corresponding type declarations.

Check failure on line 4 in packages/lambda-tiler/src/routes/__tests__/tile.style.json.attribution.test.ts

View workflow job for this annotation

GitHub Actions / smoke

Cannot find module '@basemaps/attribution/build/utils/utils.js' or its corresponding type declarations.

Check failure on line 4 in packages/lambda-tiler/src/routes/__tests__/tile.style.json.attribution.test.ts

View workflow job for this annotation

GitHub Actions / screenshot

Cannot find module '@basemaps/attribution/build/utils/utils.js' or its corresponding type declarations.
import { ConfigProviderMemory, StyleJson } from '@basemaps/config';
import { StacProvider } from '@basemaps/geo';
import { Env } from '@basemaps/shared';

import { FakeData, Imagery3857 } from '../../__tests__/config.data.js';
import { Api, mockRequest } from '../../__tests__/xyz.util.js';
import { handler } from '../../index.js';
import { ConfigLoader } from '../../util/config.loader.js';

const defaultAttribution = `${copyright} LINZ`;

describe('/v1/styles', () => {
const host = 'https://tiles.test';
const config = new ConfigProviderMemory();

const FakeTileSetName = 'tileset';
const FakeLicensor1: StacProvider = {
name: 'L1',
roles: ['licensor'],
};
const FakeLicensor2: StacProvider = {
name: 'L2',
roles: ['licensor'],
};

before(() => {
process.env[Env.PublicUrlBase] = host;
});
afterEach(() => {
config.objects.clear();
});

// tileset exists, imagery not found
it('default: imagery not found', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
config.put(FakeData.tileSetRaster(FakeTileSetName));

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, defaultAttribution);
});

// tileset exists, imagery found, more than one layer
it('default: too many layers', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
const tileset = FakeData.tileSetRaster(FakeTileSetName);
assert(tileset.layers[0] != null);

tileset.layers.push(tileset.layers[0]);
assert(tileset.layers.length > 1);

config.put(tileset);
config.put(Imagery3857);

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, defaultAttribution);
});

// tileset exists, imagery found, one layer, no providers
it('default: no providers', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
const tileset = FakeData.tileSetRaster(FakeTileSetName);
assert(tileset.layers[0] != null);
assert(tileset.layers.length === 1);

const imagery = Imagery3857;
assert(imagery.providers == null);

config.put(tileset);
config.put(imagery);

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, defaultAttribution);
});

// tileset exists, imagery found, one layer, has providers, no licensors
it('default: no licensors', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
const tileset = FakeData.tileSetRaster(FakeTileSetName);
assert(tileset.layers[0] != null);
assert(tileset.layers.length === 1);

const imagery = Imagery3857;
imagery.providers = [];
assert(imagery.providers != null);

config.put(tileset);
config.put(imagery);

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, defaultAttribution);
});

// tileset exists, imagery found, one layer, has providers, one licensor
it('custom: one licensor', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
const tileset = FakeData.tileSetRaster(FakeTileSetName);
assert(tileset.layers[0] != null);
assert(tileset.layers.length === 1);

const imagery = Imagery3857;
imagery.providers = [FakeLicensor1];
assert(imagery.providers != null);

config.put(tileset);
config.put(imagery);

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, `${copyright} ${FakeLicensor1.name}`);
strictEqual(source.attribution, createLicensorAttribution([FakeLicensor1]));
});

// tileset exists, imagery found, one layer, has providers, two licensors
it('custom: two licensors', async (t) => {
t.mock.method(ConfigLoader, 'getDefaultConfig', () => Promise.resolve(config));

// insert
const tileset = FakeData.tileSetRaster(FakeTileSetName);
assert(tileset.layers[0] != null);
assert(tileset.layers.length === 1);

const imagery = Imagery3857;
imagery.providers = [FakeLicensor1, FakeLicensor2];
assert(imagery.providers != null);

config.put(tileset);
config.put(imagery);

// request
const req = mockRequest(`/v1/styles/${FakeTileSetName}.json`, 'get', Api.header);
const res = await handler.router.handle(req);
strictEqual(res.status, 200);

// extract
const body = Buffer.from(res.body, 'base64').toString();
const json = JSON.parse(body) as StyleJson;

const source = Object.values(json.sources)[0];
assert(source != null);
assert(source.attribution != null);

// verify
strictEqual(source.attribution, `${copyright} ${FakeLicensor1.name}, ${FakeLicensor2.name}`);
strictEqual(source.attribution, createLicensorAttribution([FakeLicensor1, FakeLicensor2]));
});
});

0 comments on commit 18af34b

Please sign in to comment.