Cotton code
68 lines · 3.9 KB · TypeScriptRaw
/** * The loader's rows (scripts/published-rows.ts), read from tiny synthetic sets * made with the very schemas `scripts/datasets/` builds with. */import { readFileSync } from "node:fs";import { join, resolve } from "node:path";import { DatabaseSync } from "node:sqlite";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";import { batches, SETS, versionOf, type PublishedRow } from "../published-rows.ts";const SCHEMAS = resolve(fileURLToPath(import.meta.url), "../../../../../scripts/datasets/schemas");function made(): (file: string) => DatabaseSync {  const dbs = new Map<string, DatabaseSync>();  const make = (kind: string, rows: string): void => {    const db = new DatabaseSync(":memory:");    db.exec(readFileSync(join(SCHEMAS, `${kind}.sql`), "utf8"));    db.exec(`INSERT INTO dataset VALUES ('${kind}', '2026-10-14', '${kind.padEnd(16, "0")}abcdef');`);    db.exec(rows);    dbs.set(`${kind}.sqlite`, db);  };  make("places", "INSERT INTO places VALUES (1269843, 'Hyderabad', 'Bhaganagar,HYD', 'IN', 'Telangana', 17.38, 78.47, 6800000, 'Asia/Kolkata');");  make("countries", "INSERT INTO countries VALUES ('IN', 'India', '+91', 'INR', '🇮🇳');");  make("holidays", "INSERT INTO holidays VALUES ('IN-2026-01-26', '2026-01-26', 'Republic Day', 'IN', ''); INSERT INTO holiday_coverage VALUES ('IN', '', '2025-01-01', '2030-12-31');");  make("airports", "INSERT INTO airports VALUES ('ourairports:1', 'HYD', 'VOHS', 'Rajiv Gandhi International', 'Hyderabad', 'IN', 17.24, 78.43, 'Asia/Kolkata');");  make("english_words", "INSERT INTO english_words VALUES ('cotton');");  return (file) => dbs.get(file)!;}const rowsOf = (open: (file: string) => DatabaseSync, name: string): Record<string, PublishedRow[]> =>  Object.fromEntries(SETS.find((s) => s.name === name)!.tables(open).map((t) => [t.table, [...t.rows()]]));describe("Cotton's published sets", () => {  it("are the four the phones bundled, places carrying its countries", () => {    expect(SETS.map((s) => [s.name, s.files])).toEqual([      ["places", ["places.sqlite", "countries.sqlite"]],      ["holidays", ["holidays.sqlite"]],      ["airports", ["airports.sqlite"]],      ["english_words", ["english_words.sqlite"]],    ]);  });  it("read a place whole, its country's name joined, every name searchable, ranked by population", () => {    const open = made();    expect(rowsOf(open, "places")).toEqual({      places: [{ id: "geonames:1269843", row: { name: "Hyderabad", admin1: "Telangana", country: "IN", country_name: "India", zone: "Asia/Kolkata", population: 6800000 }, names: ["Hyderabad", "Bhaganagar", "HYD"], lat: 17.38, lng: 78.47, rank: 6800000, group: "IN" }],      countries: [{ id: "IN", row: { name: "India", dialling_code: "+91", currency: "INR", flag: "🇮🇳" }, names: ["India", "IN"] }],    });  });  it("read holidays by country and date from the front, airports by code or name, words by id alone", () => {    const open = made();    expect(rowsOf(open, "holidays")).toEqual({      holidays: [{ id: "IN-2026-01-26", row: { date: "2026-01-26", name: "Republic Day", country: "IN", region: "" }, names: ["IN 2026-01-26"], rank: -20260126, group: "IN" }],      coverage: [{ id: "IN|", row: { country: "IN", region: "", first_date: "2025-01-01", last_date: "2030-12-31" } }],    });    expect(rowsOf(open, "airports").airports![0]).toMatchObject({ id: "HYD", row: { source_id: "ourairports:1", iata: "HYD" }, names: ["HYD", "VOHS", "Rajiv Gandhi International", "Hyderabad"], lat: 17.24, lng: 78.43 });    expect(rowsOf(open, "english_words")).toEqual({ words: [{ id: "cotton", row: {} }] });  });  it("version each set by its build and its sources, and batch rows to a write's size", () => {    const open = made();    expect(versionOf(open, SETS[0]!)).toBe("2026-10-14-places00countrie");    expect([...batches([1, 2, 3, 4, 5], 2)]).toEqual([[1, 2], [3, 4], [5]]);  });});