SPB Git

Repository Public BSD-3-Clause

Fixture.swift 17.6 KB 238 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Packages/ORIXSearch/Tests/ORIXSearchTests/Fixture.swift10// Module           : ORIXSearchTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import ORIXGeoCore90@testable import ORIXSearch9192/// A tiny region around downtown Montréal and one around Québec City, built through the public writer API.93enum Fixture {94    static let dataDate = Date(timeIntervalSince1970: 1_795_000_000)95    static let downtown = Coordinate(latitude: 45.5030, longitude: -73.5750)96    static let oldQuebec = Coordinate(latitude: 46.8120, longitude: -71.2050)9798    static func temporaryURL(_ name: String = "search.sqlite") -> URL {99        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("orix-search-tests-\(UUID().uuidString)")100        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)101        return dir.appendingPathComponent(name)102    }103104    static func node(_ id: Int64) -> OSMElementRef { .init(type: .node, id: id) }105    static func way(_ id: Int64) -> OSMElementRef { .init(type: .way, id: id) }106107    /// Montréal fixture: places, streets with addresses, points of interest.108    @discardableResult109    static func buildMontreal(at url: URL, regionID: String = "test-montreal", dataDate: Date? = Fixture.dataDate) throws -> SearchIndexSummary {110        let w = try SearchIndexWriter(url: url, regionID: regionID, options: .init(builderVersion: "tests", source: "fixture"))111        let montreal = w.addArea(name: "Montréal", level: .admin(8))112        let westmount = w.addArea(name: "Westmount", level: .admin(8))113        let villeMarie = w.addArea(name: "Ville-Marie", level: .admin(9))114        let plateau = w.addArea(name: "Le Plateau-Mont-Royal", level: .admin(9))115        let laval = w.addArea(name: "Laval", level: .admin(8))116        let approx = w.addArea(name: "Saint-Zénon", level: .nearestPlace)117        let mtl = LocalityContext(city: montreal, district: villeMarie)118119        w.add(PlaceRecord(osm: node(1), coordinate: .init(latitude: 45.5032, longitude: -73.5698), names: NameSet(primary: "Montréal", variants: ["Montreal", "Ville de Montréal"]),120                          type: .city, population: 1_762_949, hasWikidata: true, locality: mtl))121        w.add(PlaceRecord(osm: node(2), coordinate: .init(latitude: 45.4042, longitude: -71.8929), names: NameSet(primary: "Sherbrooke"), type: .city, population: 172_950, hasWikidata: true))122        w.add(PlaceRecord(osm: node(3), coordinate: .init(latitude: 45.5700, longitude: -73.7400), names: NameSet(primary: "Laval"), type: .city, population: 438_366, hasWikidata: true,123                          locality: .init(city: laval)))124        w.add(PlaceRecord(osm: node(4), coordinate: .init(latitude: 45.4850, longitude: -73.5970), names: NameSet(primary: "Westmount"), type: .town, population: 20_000,125                          locality: .init(city: westmount)))126        w.add(PlaceRecord(osm: node(5), coordinate: .init(latitude: 45.5230, longitude: -73.5800), names: NameSet(primary: "Le Plateau-Mont-Royal", variants: ["Plateau Mont-Royal"]),127                          type: .suburb, locality: .init(city: montreal, district: plateau)))128        w.add(PlaceRecord(osm: node(6), coordinate: .init(latitude: 46.5500, longitude: -73.8200), names: NameSet(primary: "Lac-Sherbrooke"), type: .hamlet,129                          locality: .init(city: approx, cityIsApproximate: true)))130131        // Rue Sherbrooke Ouest in Montréal: runs west–east along latitude 45.5030.132        let sherbrooke: [Coordinate] = stride(from: -73.5900, through: -73.5600, by: 0.0025).map { Coordinate(latitude: 45.5030, longitude: $0) }133        w.add(StreetRecord(134            osm: way(100), names: NameSet(primary: "Rue Sherbrooke Ouest", variants: ["Sherbrooke Street West"]), ref: "138", streetClass: .primary,135            geometry: [sherbrooke], coordinate: sherbrooke[6], locality: .init(city: montreal),136            addresses: [137                AddressRecord(osm: node(1000), houseNumber: "1000", coordinate: .init(latitude: 45.50310, longitude: -73.5760)),138                AddressRecord(osm: way(1001), houseNumber: "1000", coordinate: .init(latitude: 45.50312, longitude: -73.57602)), // same address on the building139                AddressRecord(osm: node(1002), houseNumber: "1002A", coordinate: .init(latitude: 45.50310, longitude: -73.5758)),140                AddressRecord(osm: way(1010), houseNumber: "1010-1014", coordinate: .init(latitude: 45.50310, longitude: -73.5754)),141                AddressRecord(osm: node(845), houseNumber: "845", coordinate: .init(latitude: 45.50290, longitude: -73.5720)),142            ],143            interpolations: [144                InterpolationRecord(wayID: 7000, start: 1100, end: 1200, step: 2,145                                    line: [.init(latitude: 45.5031, longitude: -73.5800), .init(latitude: 45.5031, longitude: -73.5780)]),146            ]))147        // The same street continues in Westmount: another municipality, another entry, no address data.148        let westmountPart: [Coordinate] = stride(from: -73.6100, through: -73.5925, by: 0.0025).map { Coordinate(latitude: 45.4860, longitude: $0) }149        w.add(StreetRecord(osm: way(101), names: NameSet(primary: "Rue Sherbrooke Ouest"), ref: "138", streetClass: .primary, geometry: [westmountPart],150                           coordinate: westmountPart[3], locality: .init(city: westmount)))151        let stDenis: [Coordinate] = stride(from: 45.5100, through: 45.5300, by: 0.0025).map { Coordinate(latitude: $0, longitude: -73.5680) }152        w.add(StreetRecord(osm: way(102), names: NameSet(primary: "Rue Saint-Denis"), ref: "335", streetClass: .secondary, geometry: [stDenis], coordinate: stDenis[4],153                           locality: .init(city: montreal),154                           addresses: [AddressRecord(osm: node(3990), houseNumber: "3990", coordinate: .init(latitude: 45.5200, longitude: -73.5682))]))155        let parc: [Coordinate] = stride(from: 45.5080, through: 45.5280, by: 0.0025).map { Coordinate(latitude: $0, longitude: -73.5850) }156        w.add(StreetRecord(osm: way(103), names: NameSet(primary: "Avenue du Parc", variants: ["Park Avenue"]), streetClass: .secondary, geometry: [parc], coordinate: parc[4],157                           locality: .init(city: montreal)))158        let rl: [Coordinate] = stride(from: -73.5800, through: -73.5550, by: 0.0025).map { Coordinate(latitude: 45.4990, longitude: $0) }159        w.add(StreetRecord(osm: way(104), names: NameSet(primary: "Boulevard René-Lévesque Ouest"), streetClass: .secondary, geometry: [rl], coordinate: rl[5],160                           locality: .init(city: montreal),161                           addresses: [AddressRecord(osm: node(700), houseNumber: "700", coordinate: .init(latitude: 45.49915, longitude: -73.5660))]))162        let a40: [Coordinate] = stride(from: -73.7000, through: -73.6000, by: 0.01).map { Coordinate(latitude: 45.5400, longitude: $0) }163        w.add(StreetRecord(osm: way(105), names: NameSet(primary: "Autoroute Métropolitaine"), ref: "40", streetClass: .motorway, geometry: [a40], coordinate: a40[5],164                           locality: .init(city: montreal)))165        // A street known only from addr:street tags.166        w.add(StreetRecord(osm: node(5001), names: NameSet(primary: "Rue Fictive-du-Test"), streetClass: .residential, geometry: [],167                           coordinate: .init(latitude: 45.5300, longitude: -73.6200), locality: .init(city: montreal),168                           addresses: [AddressRecord(osm: node(5001), houseNumber: "12", coordinate: .init(latitude: 45.5300, longitude: -73.6200))]))169        w.add(StreetRecord(osm: way(106), names: NameSet(primary: "Rue Principale"), streetClass: .residential,170                           geometry: [[.init(latitude: 45.5700, longitude: -73.7450), .init(latitude: 45.5700, longitude: -73.7350)]],171                           coordinate: .init(latitude: 45.5700, longitude: -73.7400), locality: .init(city: laval)))172173        // Points of interest.174        w.add(POIRecord(osm: way(200), coordinate: .init(latitude: 45.5580, longitude: -73.5515), boundingBox: .init(minLat: 45.556, minLon: -73.554, maxLat: 45.560, maxLon: -73.549),175                        names: NameSet(primary: "Stade olympique", variants: ["Olympic Stadium"]), category: .sports, houseNumber: "4545", street: "Avenue Pierre-De Coubertin",176                        hasWikidata: true, locality: .init(city: montreal)))177        w.add(POIRecord(osm: node(201), coordinate: .init(latitude: 45.5033, longitude: -73.5752), names: NameSet(primary: "Café Olimpico"), category: .cafe, locality: mtl))178        w.add(POIRecord(osm: node(202), coordinate: .init(latitude: 45.5060, longitude: -73.5700), names: NameSet(primary: "Dispatch Coffee"), category: .cafe, locality: mtl))179        w.add(POIRecord(osm: node(203), coordinate: .init(latitude: 45.5250, longitude: -73.5850), names: NameSet(primary: "Café Névé"), category: .cafe,180                        locality: .init(city: montreal, district: plateau)))181        w.add(POIRecord(osm: node(204), coordinate: .init(latitude: 45.5720, longitude: -73.7400), names: NameSet(primary: "Café du Nord"), category: .cafe, locality: .init(city: laval)))182        w.add(POIRecord(osm: node(205), coordinate: .init(latitude: 45.5040, longitude: -73.5740), names: NameSet(primary: "SAQ", variants: ["Société des alcools du Québec"]),183                        category: .liquorStore, hasBrand: true, locality: mtl))184        w.add(POIRecord(osm: node(206), coordinate: .init(latitude: 45.5152, longitude: -73.5611), names: NameSet(primary: "Berri-UQAM"), category: .transitStation,185                        hasWikidata: true, locality: mtl))186        w.add(POIRecord(osm: way(207), coordinate: .init(latitude: 45.5270, longitude: -73.5690), boundingBox: .init(minLat: 45.523, minLon: -73.573, maxLat: 45.531, maxLon: -73.565),187                        names: NameSet(primary: "Parc La Fontaine"), category: .park, hasWikidata: true, locality: .init(city: montreal, district: plateau)))188        w.add(POIRecord(osm: node(208), coordinate: .init(latitude: 45.5020, longitude: -73.5745), names: NameSet(primary: nil), category: .fuel, locality: mtl))189        w.add(POIRecord(osm: node(209), coordinate: .init(latitude: 45.5200, longitude: -73.6000), names: NameSet(primary: "Shell"), category: .fuel, hasBrand: true,190                        locality: .init(city: montreal)))191        w.add(POIRecord(osm: node(210), coordinate: .init(latitude: 45.5035, longitude: -73.5749), names: NameSet(primary: "Pharmaprix"), category: .pharmacy, houseNumber: "1000",192                        street: "Rue Sherbrooke Ouest", hasBrand: true, locality: .init(city: w.addArea(name: "Montréal", level: .tagged))))193        w.add(POIRecord(osm: node(211), coordinate: .init(latitude: 45.5710, longitude: -73.7410), names: NameSet(primary: "Pharmacie Jean Coutu"), category: .pharmacy,194                        hasBrand: true, locality: .init(city: laval)))195        w.add(POIRecord(osm: node(212), coordinate: .init(latitude: 45.5031, longitude: -73.5690), names: NameSet(primary: "Pharmacie Jean Coutu"), category: .pharmacy,196                        hasBrand: true, locality: mtl))197        w.add(POIRecord(osm: node(213), coordinate: .init(latitude: 45.5045, longitude: -73.5772), names: NameSet(primary: "Les 3 Brasseurs"), category: .bar, locality: mtl))198        w.add(POIRecord(osm: way(214), coordinate: .init(latitude: 45.5048, longitude: -73.5772), names: NameSet(primary: "Université McGill", variants: ["McGill University"]),199                        category: .university, hasWikidata: true, locality: mtl))200        return try w.finish(boundingBox: BoundingBox(minLat: 45.40, minLon: -73.98, maxLat: 45.71, maxLon: -73.47), dataTimestamp: dataDate)201    }202203    /// Québec City fixture, a second region. It repeats one Montréal element (node 1) to exercise cross-region merging.204    @discardableResult205    static func buildQuebec(at url: URL, dataDate: Date? = Fixture.dataDate.addingTimeInterval(86_400)) throws -> SearchIndexSummary {206        let w = try SearchIndexWriter(url: url, regionID: "test-quebec", options: .init(builderVersion: "tests"))207        let quebec = w.addArea(name: "Québec", level: .admin(8))208        let ctx = LocalityContext(city: quebec)209        w.add(PlaceRecord(osm: node(10), coordinate: .init(latitude: 46.8131, longitude: -71.2075), names: NameSet(primary: "Québec", variants: ["Quebec City"]), type: .city,210                          population: 549_459, hasWikidata: true, locality: ctx))211        w.add(PlaceRecord(osm: node(1), coordinate: .init(latitude: 45.5032, longitude: -73.5698), names: NameSet(primary: "Montréal"), type: .city, population: 1_762_949, hasWikidata: true))212        w.add(POIRecord(osm: way(300), coordinate: .init(latitude: 46.8119, longitude: -71.2052), names: NameSet(primary: "Fairmont Le Château Frontenac", variants: ["Château Frontenac"]),213                        category: .lodging, houseNumber: "1", street: "Rue des Carrières", hasWikidata: true, locality: ctx))214        w.add(POIRecord(osm: node(301), coordinate: .init(latitude: 46.8135, longitude: -71.2080), names: NameSet(primary: "Pharmacie Brunet"), category: .pharmacy, locality: ctx))215        w.add(POIRecord(osm: node(302), coordinate: .init(latitude: 46.8300, longitude: -71.2500), names: NameSet(primary: "Pharmacie Uniprix"), category: .pharmacy, locality: ctx))216        let carrieres: [Coordinate] = [.init(latitude: 46.8115, longitude: -71.2060), .init(latitude: 46.8122, longitude: -71.2048)]217        w.add(StreetRecord(osm: way(310), names: NameSet(primary: "Rue des Carrières"), streetClass: .residential, geometry: [carrieres], coordinate: carrieres[0], locality: ctx,218                           addresses: [AddressRecord(osm: way(300), houseNumber: "1", coordinate: .init(latitude: 46.8119, longitude: -71.2052))]))219        return try w.finish(dataTimestamp: dataDate)220    }221222    static func engine(_ builders: ((URL) throws -> Void)...) async throws -> (SearchEngine, [URL]) {223        let engine = SearchEngine()224        var urls: [URL] = []225        for b in builders {226            let url = temporaryURL()227            try b(url)228            try await engine.openIndex(at: url)229            urls.append(url)230        }231        return (engine, urls)232    }233234    static func montrealEngine() async throws -> SearchEngine {235        try await engine({ try buildMontreal(at: $0) }).0236    }237}238
Files main
Menu

Theme