Skip to content

Commit

Permalink
Merge pull request GEOLYTIX#1529 from simon-leech/layer-test
Browse files Browse the repository at this point in the history
Update Layer Test
  • Loading branch information
RobAndrewHurst authored Oct 7, 2024
2 parents 8b51a08 + d4a283f commit ea67a38
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 141 deletions.
4 changes: 3 additions & 1 deletion public/tests/integrity.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { base } from './_base.test.mjs';
import { layerTest } from './layer.test.mjs';
import { workspaceTest } from './workspace.test.mjs';

const mapview = await base();
await layerTest(mapview);
await layerTest(mapview);
await workspaceTest(mapview);
240 changes: 100 additions & 140 deletions public/tests/layer.test.mjs
Original file line number Diff line number Diff line change
@@ -1,168 +1,128 @@
export async function layerTest(mapview) {

function delayFunction(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
await codi.describe(`${mapview.host} : Layer Test`, async () => {

const default_zoom = mapview.view?.z || 0;
for (const key of Object.getOwnPropertyNames(mapview.layers)) {

await codi.describe(`${mapview.host} : Template Paths Test`, async () => {
await codi.it(`Layer test : ${key}`, async () => {

await codi.it('All the templates are valid', async () => {
// Call the /test workspace method - which should return an empty errors array if all the templates are valid.
const test = await mapp.utils.xhr(`${mapp.host}/api/workspace/test`);
const layer = mapview.layers[key];

// If the test fails, print out the invalid templates.
if (test.errors > 0) {
test.errors.forEach(template => {
console.error('INVALID PATH:', template);
});
}
layer.show();

codi.assertTrue(test.errors.length === 0, `There are ${test.errors.length} invalid paths for templates`);
if (layer.tables) {
//This is to set the zoom level so that the correct zoom level is used for the layer.
const layerZoom = parseInt(Object.entries(layer.tables).find(([key, value]) => value !== null)[0]);
mapview.Map.getView().setZoom(layerZoom);
}

});
});
if (layer.dataviews) {

await codi.describe(`${mapview.host} : Layer Test`, async () => {
for (let dataview in layer.dataview) {
dataview.show();
}
}

for (const key in mapview.layers) {
if (mapview.layers.hasOwnProperty(key)) {
await codi.it(`Layer test : ${key}`, async () => {
const layer = mapview.layers[key];
// Turn on every theme on the layer to test if they work
if (layer.style?.themes) {

if (layer.tables) {
const layerZoom = parseInt(Object.entries(layer.tables).find(([key, value]) => value !== null)[0]);
mapview.Map.getView().setZoom(layerZoom);
}
else {
if (default_zoom !== 0) {
mapview.Map.getView().setZoom(default_zoom);
}
}
for (const theme in layer.style.themes) {
console.log(`Testing theme ${theme}`);
layer.style.theme = layer.style.themes[theme];
layer.reload();

if (layer.dataviews) {
for (let dataview in layer.dataview) {
dataview = { ...dataview, display: true }
}
//This is to allow errors being logged into the console.
//There is no test being asserted on.
await new Promise(resolve => setTimeout(resolve, 1000));
}
}

layer.show();
//Location test
if (layer.infoj) {

// Turn on every theme on the layer to test if they work
if (layer.style?.themes) {
for (const theme in layer.style.themes) {
console.log(`Testing theme ${theme}`);
layer.style.theme = layer.style.themes[theme];
layer.reload();
await delayFunction(1000);
}
}
const lastLocation = await mapp.utils.xhr(`${mapp.host}/api/query?template=get_last_location&locale=${encodeURIComponent(mapview.locale.key)}&layer=${key}`);

if (!['maplibre', 'tiles'].includes(layer.format) && layer.infoj) {
if (lastLocation?.id) {

const lastLocation = await mapp.utils.xhr(`${mapp.host}/api/query?template=get_last_location&locale=${encodeURIComponent(mapview.locale.key)}&layer=${key}`);
layer.infoj = layer.infoj.map(entry => {
if (entry.type === 'dataview') {
return { ...entry, display: true };
}
return entry;
});

const location = await mapp.location.get({
layer: layer,
id: lastLocation.id,
});

codi.assertTrue(location !== undefined, 'The location is undefined');

// Create a new location
const newLocation = {
layer,
table: layer.tableCurrent(),
new: true
};

// Add a new location to the layer using the last location
if (Object.keys(layer.draw).length > 0) {

await codi.it('Add a new location to the layer using the last location coordinates', async () => {

// Use the value of the infoj pin field to create a new location
const pin = location.infoj.find(entry => entry.type === 'pin');

// If no pin, just use the center of the mapview as the location.
const center = mapview.Map.getView().getCenter();

const geometry = pin?.geometry || center;

//Creating the new point
//We need to pass a geometry for the new location query
newLocation.id = await mapp.utils.xhr({
method: 'POST',
url: `${mapp.host}/api/query?` +
mapp.utils.paramString({
template: 'location_new',
locale: layer.mapview.locale.key,
layer: layer.key,
table: newLocation.table,
}),
body: JSON.stringify({
[layer.geom]: geometry,
// Spread in defaults.
...layer.draw?.defaults,
})
});

if (lastLocation?.id) {
// Get the newly created location.
const newLoc = await mapp.location.get(newLocation);

layer.infoj = layer.infoj.map(entry => {
if (entry.type === 'dataview') {
return { ...entry, display: true };
}
return entry;
// Remove the location
newLoc.remove();
});

const location = await mapp.location.get({
layer: layer,
id: lastLocation.id,
await codi.it('Delete the location', async () => {

// Test deleting a location
await mapp.utils.xhr(`${mapp.host}/api/query?` +
mapp.utils.paramString({
template: 'location_delete',
locale: mapview.locale.key,
layer: newLocation.layer.key,
table: newLocation.table,
id: newLocation.id
}));
});
}

codi.assertTrue(location !== undefined, 'The location is undefined');

// Create a new location
const newLocation = {
layer,
table: layer.tableCurrent(),
new: true
};

// Add a new location to the layer using the last location
if (layer?.draw) {
await codi.it('Add a new location to the layer using the last location coordinates', async () => {
// Use the value of the infoj pin field to create a new location
const pin = location.infoj.find(entry => entry.type === 'pin');

// Get the geometry of the last location (for polygon layers)
const polygon = location.infoj.find(entry => entry.type === 'geometry' && entry.field === layer.geomCurrent());

// Set the pin or polygon based on the draw object
let geometry;

if (layer?.draw?.point) {
geometry = pin.geometry;
} else if (layer?.draw?.polygon || layer?.draw?.line || layer?.draw?.rectangle || layer?.draw?.circle) {
geometry = polygon.geometry;
} else {
// We don't want to test this layer as it doesn't have a core draw object method
// If may have plugin draw methods but we can't test those
return;
}

newLocation.id = await mapp.utils.xhr({
method: 'POST',
url: `${mapp.host}/api/query?` +
mapp.utils.paramString({
template: 'location_new',
locale: layer.mapview.locale.key,
layer: layer.key,
table: newLocation.table
}),
body: JSON.stringify({
[layer.geom]: geometry,

// Spread in defaults.
...layer.draw?.defaults
})
});

// Layer must be reloaded to reflect geometry changes.
layer.reload();

// Get the newly created location.
const newLoc = await mapp.location.get(newLocation);

// Remove the location
newLoc.remove();
});

// If layer.deleteLocation is defined, delete the location
if (layer.deleteLocation === true) {

await codi.it('Delete the location', async () => {
// Test deleting a location
await mapp.utils.xhr(`${mapp.host}/api/query?` +
mapp.utils.paramString({
template: 'location_delete',
locale: mapview.locale.key,
layer: newLocation.layer.key,
table: newLocation.table,
id: newLocation.id
}));
});
}
}

location.remove();
location.remove();

if (!['maplibre', 'tiles'].includes(layer.format)) {
layer.hide();
}
}
layer.hide();
}
});
}
}
});
}
});
}
19 changes: 19 additions & 0 deletions public/tests/workspace.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export async function workspaceTest(mapview) {
await codi.describe(`${mapview.host} : Template Paths Test`, async () => {

await codi.it('All the templates are valid', async () => {

// Call the /test workspace method - which should return an empty errors array if all the templates are valid.
const test = await mapp.utils.xhr(`${mapp.host}/api/workspace/test`);

// If the test fails, print out the invalid templates.
if (test.errors > 0) {
test.errors.forEach(template => {
console.error('INVALID PATH:', template);
});
}

codi.assertTrue(test.errors.length === 0, `There are ${test.errors.length} invalid paths for templates`);
});
});
}

0 comments on commit ea67a38

Please sign in to comment.