refactoring: slight cleanup of tests

This commit is contained in:
Pieter Vander Vennet 2023-04-07 03:54:11 +02:00
parent 2e9b1016de
commit f8d34648a0
28 changed files with 252 additions and 353 deletions

View file

@ -7,10 +7,10 @@ import * as bookcaseJson from "../../../assets/generated/themes/bookcases.json"
import { UIEventSource } from "../../../Logic/UIEventSource"
import Loc from "../../../Models/Loc"
import SelectedFeatureHandler from "../../../Logic/Actors/SelectedFeatureHandler"
import { ElementStorage } from "../../../Logic/ElementStorage"
import { OsmTags } from "../../../Models/OsmFeature"
import { Feature, Geometry } from "geojson"
import { expect, it } from "vitest"
import ThemeViewState from "../../../Models/ThemeViewState";
const latestTags = {
amenity: "public_bookcase",
@ -48,7 +48,7 @@ Utils.injectJsonDownloadForTests("https://www.openstreetmap.org/api/0.6/node/556
})
it("should download the latest version", () => {
const state = new UserRelatedState(new LayoutConfig(<any>bookcaseJson, true))
const state = new ThemeViewState(new LayoutConfig(<any>bookcaseJson, true))
const feature: Feature<Geometry, OsmTags> = {
type: "Feature",
id: "node/5568693115",
@ -73,15 +73,14 @@ it("should download the latest version", () => {
coordinates: [3.2154662, 51.2179199],
},
}
state.allElements.addOrGetElement(feature)
SelectedElementTagsUpdater.installCallback(state)
state.newFeatures.features.data.push(feature)
state.newFeatures.features.ping()
new SelectedElementTagsUpdater(state)
// THis should trigger a download of the latest feaures and update the tags
// However, this doesn't work with ts-node for some reason
state.selectedElement.setData(feature)
SelectedElementTagsUpdater.applyUpdate(state, latestTags, feature.properties.id)
// The name should be updated
expect(feature.properties.name).toEqual("Stubbekwartier-buurtbibliotheek")
// The fixme should be removed
@ -100,11 +99,12 @@ it("Hash without selected element should download geojson from OSM-API", async (
expect(selected.data.properties.id).toEqual("node/5568693115")
expect(loc.data.zoom).toEqual(14)
expect(loc.data.lat).toEqual(51.2179199)
})
}
new SelectedFeatureHandler(hash, {
selectedElement: selected,
allElements: new ElementStorage(),
allElements: new(),
featurePipeline: undefined,
locationControl: loc,
layoutToUse: undefined,

View file

@ -113,7 +113,7 @@ describe("OverlapFunc", () => {
}
const params: ExtraFuncParams = {
getFeatureById: (id) => undefined,
getFeatureById: () => undefined,
getFeaturesWithin: () => [[door]],
}

View file

@ -1,164 +0,0 @@
import OsmFeatureSource from "../../../Logic/FeatureSource/TiledFeatureSource/OsmFeatureSource"
import { UIEventSource } from "../../../Logic/UIEventSource"
import ScriptUtils from "../../../scripts/ScriptUtils"
import FilteredLayer, { FilterState } from "../../../Models/FilteredLayer"
import { Tiles } from "../../../Models/TileRange"
import { readFileSync } from "fs"
import { Utils } from "../../../Utils"
import { Tag } from "../../../Logic/Tags/Tag"
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
import { describe, expect, it } from "vitest"
const expected = {
type: "Feature",
id: "relation/5759328",
properties: {
timestamp: "2022-06-10T00:46:55Z",
version: 6,
changeset: 122187206,
user: "Pieter Vander Vennet",
uid: 3818858,
amenity: "school",
"isced:2011:level": "vocational_lower_secondary;vocational_upper_secondary",
name: "Koninklijk Technisch Atheneum Pro Technica",
"school:gender": "mixed",
type: "multipolygon",
website: "http://ktahalle.be/",
id: "relation/5759328",
_backend: "https://osm.org",
},
geometry: {
type: "MultiPolygon",
coordinates: [
[
[
[4.2461832, 50.7335751],
[4.2463167, 50.7336785],
[4.2463473, 50.7337021],
[4.2464497, 50.7337814],
[4.2471698, 50.7343389],
[4.2469541, 50.7344768],
[4.2467571, 50.7346116],
[4.2467727, 50.7346199],
[4.2465714, 50.7347511],
[4.2462398, 50.7349687],
[4.2453546, 50.734601],
[4.2451895, 50.7345103],
[4.2448867, 50.7342629],
[4.244899, 50.7342069],
[4.2461832, 50.7335751],
],
],
[
[
[4.2444209, 50.7353737],
[4.2439986, 50.7352034],
[4.2440303, 50.7351755],
[4.2440602, 50.7351058],
[4.2439776, 50.7350326],
[4.2439558, 50.7350132],
[4.2438246, 50.7348961],
[4.2437848, 50.73486],
[4.2436555, 50.7347455],
[4.2435905, 50.734689],
[4.2435494, 50.7346601],
[4.2435038, 50.7346256],
[4.2434769, 50.7346026],
[4.2430948, 50.734275],
[4.2427978, 50.7340052],
[4.2430556, 50.7338391],
[4.2438957, 50.7334942],
[4.2440204, 50.7336368],
[4.2442806, 50.7338922],
[4.2444173, 50.7340119],
[4.2447379, 50.7342925],
[4.2450107, 50.7345294],
[4.2450236, 50.7346021],
[4.2449643, 50.7347019],
[4.244711, 50.7350821],
[4.2444209, 50.7353737],
],
],
],
},
}
function test(done: () => void) {
let fetchedTile = undefined
const neededTiles = new UIEventSource<number[]>([Tiles.tile_index(17, 67081, 44033)])
new OsmFeatureSource({
allowedFeatures: new Tag("amenity", "school"),
handleTile: (tile) => {
fetchedTile = tile
const data = tile.features.data[0].feature
expect(data.properties).toEqual({
id: "relation/5759328",
timestamp: "2022-06-10T00:46:55Z",
version: 6,
changeset: 122187206,
user: "Pieter Vander Vennet",
uid: 3818858,
amenity: "school",
"isced:2011:level": "vocational_lower_secondary;vocational_upper_secondary",
name: "Koninklijk Technisch Atheneum Pro Technica",
"school:gender": "mixed",
type: "multipolygon",
website: "http://ktahalle.be/",
_backend: "https://osm.org",
})
expect(data.geometry.type).toBe("MultiPolygon")
expect(data).toEqual(expected)
done()
},
isActive: new UIEventSource<boolean>(true),
neededTiles,
state: {
osmConnection: {
Backend(): string {
return "https://osm.org"
},
},
filteredLayers: new UIEventSource<FilteredLayer[]>([
{
appliedFilters: new UIEventSource<Map<string, FilterState>>(undefined),
layerDef: new LayerConfig({
id: "school",
source: {
osmTags: "amenity=school",
},
mapRendering: null,
}),
isDisplayed: new UIEventSource<boolean>(true),
},
]),
},
})
}
describe("OsmFeatureSource", () => {
it("downloading the full school should give a multipolygon", (done) => {
ScriptUtils.fixUtils()
let data = JSON.parse(readFileSync("./test/Logic/FeatureSource/osmdata.json", "utf8"))
Utils.injectJsonDownloadForTests(
"https://osm.org/api/0.6/map?bbox=4.24346923828125,50.732978448277514,4.2462158203125,50.73471682490244",
data
)
test(done)
})
it("downloading the partial school polygon should give a multipolygon", (done) => {
ScriptUtils.fixUtils()
Utils.injectJsonDownloadForTests(
"https://www.openstreetmap.org/api/0.6/relation/5759328/full",
JSON.parse(readFileSync("./test/data/relation_5759328.json", { encoding: "utf-8" }))
)
let data = JSON.parse(
readFileSync("./test/Logic/FeatureSource/small_box.json", { encoding: "utf-8" })
)
Utils.injectJsonDownloadForTests(
"https://osm.org/api/0.6/map?bbox=4.24346923828125,50.732978448277514,4.2462158203125,50.73471682490244",
data
)
test(done)
})
})

View file

@ -1,22 +0,0 @@
import TileFreshnessCalculator from "../../../Logic/FeatureSource/TileFreshnessCalculator"
import { Tiles } from "../../../Models/TileRange"
import { describe, expect, it } from "vitest"
describe("TileFreshnessCalculator", () => {
it("should get the freshness for loaded tiles", () => {
const calc = new TileFreshnessCalculator()
// 19/266407/175535
const date = new Date()
date.setTime(42)
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534), date)
expect(calc.freshnessFor(19, 266406, 175534).getTime()).toBe(42)
expect(calc.freshnessFor(20, 266406 * 2, 175534 * 2 + 1).getTime()).toBe(42)
expect(calc.freshnessFor(19, 266406, 175535)).toBeUndefined()
expect(calc.freshnessFor(18, 266406 / 2, 175534 / 2)).toBeUndefined()
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534 + 1), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534 + 1), date)
expect(calc.freshnessFor(18, 266406 / 2, 175534 / 2).getTime()).toBe(42)
})
})

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,11 +1,11 @@
import Minimap from "../../../../UI/Base/Minimap"
import { Utils } from "../../../../Utils"
import LayoutConfig from "../../../../Models/ThemeConfig/LayoutConfig"
import State from "../../../../State"
import { BBox } from "../../../../Logic/BBox"
import ReplaceGeometryAction from "../../../../Logic/Osm/Actions/ReplaceGeometryAction"
import ShowDataLayer from "../../../../UI/ShowDataLayer/ShowDataLayer"
import { describe, expect, it } from "vitest"
import { OsmConnection } from "../../../../Logic/Osm/OsmConnection"
import { ImmutableStore } from "../../../../Logic/UIEventSource"
import { Changes } from "../../../../Logic/Osm/Changes"
describe("ReplaceGeometryAction", () => {
const grbStripped = {
@ -300,8 +300,6 @@ describe("ReplaceGeometryAction", () => {
],
}
Minimap.createMiniMap = () => undefined
const coordinates = <[number, number][]>[
[3.216690793633461, 51.21474084112525],
[3.2167256623506546, 51.214696737309964],
@ -876,22 +874,27 @@ describe("ReplaceGeometryAction", () => {
it("should move nodes accordingly", async () => {
const layout = new LayoutConfig(<any>grbStripped)
ShowDataLayer.actualContstructor = (_) => undefined
const state = new State(layout)
State.state = state
const bbox = new BBox([
[3.2166673243045807, 51.21467321525788],
[3.217007964849472, 51.21482442824023],
])
const url = `https://www.openstreetmap.org/api/0.6/map.json?bbox=${bbox.minLon},${bbox.minLat},${bbox.maxLon},${bbox.maxLat}`
const data = await Utils.downloadJson(url)
state.featurePipeline.fullNodeDatabase.handleOsmJson(data, 0)
const action = new ReplaceGeometryAction(state, targetFeature, wayId, {
theme: "test",
const fullNodeDatabase = undefined // TODO new FullNodeDatabaseSource(undefined)
// TODO fullNodeDatabase.handleOsmJson(data, 0)
const changes = new Changes()
const osmConnection = new OsmConnection({
dryRun: new ImmutableStore(true),
})
const action = new ReplaceGeometryAction(
{ osmConnection, fullNodeDatabase },
targetFeature,
wayId,
{
theme: "test",
}
)
const closestIds = await action.GetClosestIds()
expect(closestIds.closestIds).toEqual([
@ -914,8 +917,8 @@ describe("ReplaceGeometryAction", () => {
expect(reproj.newLon).toEqual(3.2168880864669203)
expect(reproj.newLat).toEqual(51.214739524104694)
expect(closestIds.detachedNodes.size).toEqual(0)
const changes = await action.Perform(state.changes)
expect(changes[11].changes["coordinates"]).toEqual([
const changed = await action.Perform(changes)
expect(changed[11].changes["coordinates"]).toEqual([
[3.216690793633461, 51.21474084112525],
[3.2167256623506546, 51.214696737309964],
[3.2168880864669203, 51.214739524104694],

View file

@ -2,19 +2,21 @@ import { Utils } from "../../../Utils"
import { ChangesetHandler, ChangesetTag } from "../../../Logic/Osm/ChangesetHandler"
import { UIEventSource } from "../../../Logic/UIEventSource"
import { OsmConnection } from "../../../Logic/Osm/OsmConnection"
import { ElementStorage } from "../../../Logic/ElementStorage"
import { Changes } from "../../../Logic/Osm/Changes"
import { describe, expect, it } from "vitest"
function elstorage() {
return { addAlias: (a, b) => {} }
}
describe("ChangesetHanlder", () => {
describe("RewriteTagsOf", () => {
it("should insert new tags", () => {
const changesetHandler = new ChangesetHandler(
new UIEventSource<boolean>(true),
new OsmConnection({}),
new ElementStorage(),
new Changes(),
new UIEventSource(undefined)
elstorage(),
new Changes()
)
const oldChangesetMeta = {
@ -57,7 +59,9 @@ describe("ChangesetHanlder", () => {
const d = Utils.asDict(rewritten)
expect(d.size).toEqual(10)
expect(d.get("answer")).toEqual("5")
expect(d.get("comment")).toEqual("Adding data with #MapComplete for theme #toerisme_vlaanderen")
expect(d.get("comment")).toEqual(
"Adding data with #MapComplete for theme #toerisme_vlaanderen"
)
expect(d.get("created_by")).toEqual("MapComplete 0.16.6")
expect(d.get("host")).toEqual("https://mapcomplete.osm.be/toerisme_vlaanderen.html")
expect(d.get("imagery")).toEqual("osm")
@ -70,9 +74,8 @@ describe("ChangesetHanlder", () => {
const changesetHandler = new ChangesetHandler(
new UIEventSource<boolean>(true),
new OsmConnection({}),
new ElementStorage(),
new Changes(),
new UIEventSource(undefined)
elstorage(),
new Changes()
)
const oldChangesetMeta = {
type: "changeset",
@ -115,7 +118,9 @@ describe("ChangesetHanlder", () => {
expect(d.size).toEqual(9)
expect(d.get("answer")).toEqual("42")
expect(d.get("comment")).toEqual("Adding data with #MapComplete for theme #toerisme_vlaanderen")
expect(d.get("comment")).toEqual(
"Adding data with #MapComplete for theme #toerisme_vlaanderen"
)
expect(d.get("created_by")).toEqual("MapComplete 0.16.6")
expect(d.get("host")).toEqual("https://mapcomplete.osm.be/toerisme_vlaanderen.html")
expect(d.get("imagery")).toEqual("osm")
@ -127,9 +132,8 @@ describe("ChangesetHanlder", () => {
const changesetHandler = new ChangesetHandler(
new UIEventSource<boolean>(true),
new OsmConnection({}),
new ElementStorage(),
new Changes(),
new UIEventSource(undefined)
elstorage(),
new Changes()
)
const oldChangesetMeta = {
type: "changeset",
@ -166,7 +170,9 @@ describe("ChangesetHanlder", () => {
expect(d.size).toEqual(9)
expect(d.get("answer")).toEqual("5")
expect(d.get("comment")).toEqual("Adding data with #MapComplete for theme #toerisme_vlaanderen")
expect(d.get("comment")).toEqual(
"Adding data with #MapComplete for theme #toerisme_vlaanderen"
)
expect(d.get("created_by")).toEqual("MapComplete 0.16.6")
expect(d.get("host")).toEqual("https://mapcomplete.osm.be/toerisme_vlaanderen.html")
expect(d.get("imagery")).toEqual("osm")

View file

@ -23,7 +23,9 @@ describe("CreateNoteImportLayer", () => {
layer,
"ImportLayerGeneratorTest: convert"
)
expect(generatedLayer.isShown["and"][1].or[0].and[0]).toEqual("_tags~(^|.*;)amenity=public_bookcase($|;.*)")
expect(generatedLayer.isShown["and"][1].or[0].and[0]).toEqual(
"_tags~(^|.*;)amenity=public_bookcase($|;.*)"
)
// "Zoomlevel is to high"
expect(generatedLayer.minzoom <= layer.minzoom).toBe(true)
let renderings = Utils.NoNull(

View file

@ -1,20 +0,0 @@
import { describe } from "mocha"
import ValidatedTextField from "../../UI/Input/ValidatedTextField"
import { fail } from "assert"
import Translations from "../../UI/i18n/Translations"
describe("ValidatedTextFields", () => {
it("should all have description in the translations", () => {
const ts = Translations.t.validation
const missingTranslations = Array.from(ValidatedTextField.allTypes.keys())
.filter((key) => ts[key] === undefined || ts[key].description === undefined)
.filter((key) => key !== "distance")
if (missingTranslations.length > 0) {
fail(
"The validated text fields don't have a description defined in en.json for " +
missingTranslations.join(", ") +
". (Did you just add one? Run `npm run generate:translations`)"
)
}
})
})