Add test for generating the cache

This commit is contained in:
pietervdvn 2022-02-14 01:15:20 +01:00
parent 4ad4db3bbd
commit 312dbe7aff
7 changed files with 70 additions and 13 deletions

View file

@ -4,6 +4,8 @@ import {Utils} from "../../Utils";
import {exec} from "child_process" import {exec} from "child_process"
import {GeoOperations} from "../../Logic/GeoOperations"; import {GeoOperations} from "../../Logic/GeoOperations";
ScriptUtils.fixUtils()
class StatsDownloader { class StatsDownloader {
private readonly startYear = 2020 private readonly startYear = 2020
@ -75,7 +77,7 @@ class StatsDownloader {
while (url) { while (url) {
ScriptUtils.erasableLog(`Downloading stats for ${year}-${month}, page ${page} ${url}`) ScriptUtils.erasableLog(`Downloading stats for ${year}-${month}, page ${page} ${url}`)
const result = await ScriptUtils.DownloadJSON(url, headers) const result = await Utils.downloadJson(url, headers)
page++; page++;
allFeatures.push(...result.features) allFeatures.push(...result.features)
if (result.features === undefined) { if (result.features === undefined) {

View file

@ -171,7 +171,7 @@ export class GeoOperations {
return GeoOperations.pointInPolygonCoordinates(x, y, feature.geometry.coordinates) return GeoOperations.pointInPolygonCoordinates(x, y, feature.geometry.coordinates)
} }
throw "GeoOperations.inside: unsupported geometry type" throw "GeoOperations.inside: unsupported geometry type "+feature.geometry.type
} }

View file

@ -7,7 +7,6 @@ import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
import xml2js from 'xml2js'; import xml2js from 'xml2js';
export default class ScriptUtils { export default class ScriptUtils {
public static fixUtils() { public static fixUtils() {
Utils.externalDownloadFunction = ScriptUtils.DownloadJSON Utils.externalDownloadFunction = ScriptUtils.DownloadJSON
} }
@ -46,7 +45,7 @@ export default class ScriptUtils {
}) })
} }
public static DownloadJSON(url, headers?: any): Promise<any> { private static DownloadJSON(url, headers?: any): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
headers = headers ?? {} headers = headers ?? {}

View file

@ -103,7 +103,7 @@ async function downloadRaw(targetdir: string, r: TileRange, theme: LayoutConfig,
try { try {
const json = await ScriptUtils.DownloadJSON(url) const json = await Utils.downloadJson(url)
if ((<string>json.remark ?? "").startsWith("runtime error")) { if ((<string>json.remark ?? "").startsWith("runtime error")) {
console.error("Got a runtime error: ", json.remark) console.error("Got a runtime error: ", json.remark)
failed++; failed++;
@ -142,7 +142,7 @@ async function downloadExtraData(theme: LayoutConfig)/* : any[] */ {
continue; continue;
} }
console.log("Downloading extra data: ", source) console.log("Downloading extra data: ", source)
await ScriptUtils.DownloadJSON(source).then(json => allFeatures.push(...json.features)) await Utils.downloadJson(source).then(json => allFeatures.push(...json.features))
} }
return allFeatures; return allFeatures;
} }
@ -355,7 +355,7 @@ function sliceToTiles(allFeatures: FeatureSource, theme: LayoutConfig, relations
} }
async function main(args: string[]) { export async function main(args: string[]) {
console.log("Cache builder started with args ", args.join(", ")) console.log("Cache builder started with args ", args.join(", "))
if (args.length < 6) { if (args.length < 6) {
@ -389,7 +389,7 @@ async function main(args: string[]) {
} }
if (isNaN(lon1)) { if (isNaN(lon1)) {
throw "The first number (a longitude) is not a valid number" throw "The fourth number (a longitude) is not a valid number"
} }

View file

@ -5,7 +5,7 @@ import {Utils} from "../../Utils";
async function main(args: string[]) { async function main(args: string[]) {
ScriptUtils.fixUtils()
const pointCandidates = JSON.parse(readFileSync(args[0], "utf8")) const pointCandidates = JSON.parse(readFileSync(args[0], "utf8"))
const postcodes = JSON.parse(readFileSync(args[1], "utf8")) const postcodes = JSON.parse(readFileSync(args[1], "utf8"))
const output = args[2] ?? "centralCoordinates.csv" const output = args[2] ?? "centralCoordinates.csv"
@ -61,7 +61,7 @@ async function main(args: string[]) {
const depPoints: [number, number][] = Utils.NoNull(await Promise.all(candidates.map(async candidate => { const depPoints: [number, number][] = Utils.NoNull(await Promise.all(candidates.map(async candidate => {
try { try {
const result = await ScriptUtils.DownloadJSON(url + "&loc=" + candidate.join("%2C") + "&loc=3.22000%2C51.21577&profile=car.short") const result = await Utils.downloadJson(url + "&loc=" + candidate.join("%2C") + "&loc=3.22000%2C51.21577&profile=car.short")
const depPoint = result.features.filter(f => f.geometry.type === "LineString")[0].geometry.coordinates[0] const depPoint = result.features.filter(f => f.geometry.type === "LineString")[0].geometry.coordinates[0]
return <[number, number]>[depPoint[0], depPoint[1]] // Drop elevation return <[number, number]>[depPoint[0], depPoint[1]] // Drop elevation
} catch (e) { } catch (e) {

53
test/CreateCache.spec.ts Normal file

File diff suppressed because one or more lines are too long

View file

@ -17,11 +17,11 @@ import ReplaceGeometrySpec from "./ReplaceGeometry.spec";
import LegacyThemeLoaderSpec from "./LegacyThemeLoader.spec"; import LegacyThemeLoaderSpec from "./LegacyThemeLoader.spec";
import T from "./TestHelper"; import T from "./TestHelper";
import CreateNoteImportLayerSpec from "./CreateNoteImportLayer.spec"; import CreateNoteImportLayerSpec from "./CreateNoteImportLayer.spec";
import CreateCacheSpec from "./CreateCache.spec";
async function main() { async function main() {
ScriptUtils.fixUtils()
const allTests: T[] = [ const allTests: T[] = [
new OsmObjectSpec(), new OsmObjectSpec(),
new TagSpec(), new TagSpec(),
@ -38,12 +38,15 @@ async function main() {
new ActorsSpec(), new ActorsSpec(),
new ReplaceGeometrySpec(), new ReplaceGeometrySpec(),
new LegacyThemeLoaderSpec(), new LegacyThemeLoaderSpec(),
new CreateNoteImportLayerSpec() new CreateNoteImportLayerSpec(),
new CreateCacheSpec()
] ]
ScriptUtils.fixUtils();
const realDownloadFunc = Utils.externalDownloadFunction;
Utils.externalDownloadFunction = async (url) => { Utils.externalDownloadFunction = async (url) => {
console.error("Fetching ", url, "blocked in tests, use Utils.injectJsonDownloadForTests") console.error("Fetching ", url, "blocked in tests, use Utils.injectJsonDownloadForTests")
const data = await ScriptUtils.DownloadJSON(url) const data = await realDownloadFunc(url)
console.log("\n\n ----------- \nBLOCKED DATA\n Utils.injectJsonDownloadForTests(\n" + console.log("\n\n ----------- \nBLOCKED DATA\n Utils.injectJsonDownloadForTests(\n" +
" ", JSON.stringify(url), ", \n", " ", JSON.stringify(url), ", \n",
" ", JSON.stringify(data), "\n )\n------------------\n\n") " ", JSON.stringify(data), "\n )\n------------------\n\n")