|
| 1 | +import { test, describe } from "node:test" |
| 2 | +import assert from "node:assert/strict" |
| 3 | +import { SqliteMap } from "./index" |
| 4 | + |
| 5 | +describe("SqliteMap", () => { |
| 6 | + test("set and get", () => { |
| 7 | + const map = new SqliteMap(":memory:") |
| 8 | + map.set("foo", { bar: "baz" }) |
| 9 | + assert.deepEqual(map.get("foo"), { bar: "baz" }) |
| 10 | + }) |
| 11 | + |
| 12 | + test("get returns undefined for missing key", () => { |
| 13 | + const map = new SqliteMap(":memory:") |
| 14 | + assert.equal(map.get("missing"), undefined) |
| 15 | + }) |
| 16 | + |
| 17 | + test("has", () => { |
| 18 | + const map = new SqliteMap(":memory:") |
| 19 | + map.set("foo", 1) |
| 20 | + assert.equal(map.has("foo"), true) |
| 21 | + assert.equal(map.has("bar"), false) |
| 22 | + }) |
| 23 | + |
| 24 | + test("delete", () => { |
| 25 | + const map = new SqliteMap(":memory:") |
| 26 | + map.set("foo", 1) |
| 27 | + assert.equal(map.delete("foo"), true) |
| 28 | + assert.equal(map.delete("foo"), false) |
| 29 | + assert.equal(map.has("foo"), false) |
| 30 | + }) |
| 31 | + |
| 32 | + test("clear", () => { |
| 33 | + const map = new SqliteMap(":memory:") |
| 34 | + map.set("a", 1) |
| 35 | + map.set("b", 2) |
| 36 | + map.clear() |
| 37 | + assert.equal(map.size, 0) |
| 38 | + }) |
| 39 | + |
| 40 | + test("size", () => { |
| 41 | + const map = new SqliteMap(":memory:") |
| 42 | + assert.equal(map.size, 0) |
| 43 | + map.set("a", 1) |
| 44 | + map.set("b", 2) |
| 45 | + assert.equal(map.size, 2) |
| 46 | + map.delete("a") |
| 47 | + assert.equal(map.size, 1) |
| 48 | + }) |
| 49 | + |
| 50 | + test("set returns this (chaining)", () => { |
| 51 | + const map = new SqliteMap(":memory:") |
| 52 | + const result = map.set("a", 1).set("b", 2) |
| 53 | + assert.equal(result, map) |
| 54 | + assert.equal(map.size, 2) |
| 55 | + }) |
| 56 | + |
| 57 | + test("getOrInsert - existing key", () => { |
| 58 | + const map = new SqliteMap(":memory:") |
| 59 | + map.set("foo", 42) |
| 60 | + assert.equal(map.getOrInsert("foo", 99), 42) |
| 61 | + }) |
| 62 | + |
| 63 | + test("getOrInsert - missing key", () => { |
| 64 | + const map = new SqliteMap(":memory:") |
| 65 | + assert.equal(map.getOrInsert("foo", 99), 99) |
| 66 | + assert.equal(map.get("foo"), 99) |
| 67 | + }) |
| 68 | + |
| 69 | + test("getOrInsertComputed - existing key", () => { |
| 70 | + const map = new SqliteMap(":memory:") |
| 71 | + map.set("foo", 42) |
| 72 | + assert.equal( |
| 73 | + map.getOrInsertComputed("foo", () => 99), |
| 74 | + 42 |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + test("getOrInsertComputed - missing key", () => { |
| 79 | + const map = new SqliteMap(":memory:") |
| 80 | + assert.equal( |
| 81 | + map.getOrInsertComputed("foo", (k) => k.length), |
| 82 | + 3 |
| 83 | + ) |
| 84 | + assert.equal(map.get("foo"), 3) |
| 85 | + }) |
| 86 | + |
| 87 | + test("keys", () => { |
| 88 | + const map = new SqliteMap(":memory:") |
| 89 | + map.set("a", 1) |
| 90 | + map.set("b", 2) |
| 91 | + assert.deepEqual([...map.keys()], ["a", "b"]) |
| 92 | + }) |
| 93 | + |
| 94 | + test("values", () => { |
| 95 | + const map = new SqliteMap(":memory:") |
| 96 | + map.set("a", 1) |
| 97 | + map.set("b", 2) |
| 98 | + assert.deepEqual([...map.values()], [1, 2]) |
| 99 | + }) |
| 100 | + |
| 101 | + test("entries", () => { |
| 102 | + const map = new SqliteMap(":memory:") |
| 103 | + map.set("a", 1) |
| 104 | + map.set("b", 2) |
| 105 | + assert.deepEqual( |
| 106 | + [...map.entries()], |
| 107 | + [ |
| 108 | + ["a", 1], |
| 109 | + ["b", 2] |
| 110 | + ] |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + test("forEach", () => { |
| 115 | + const map = new SqliteMap<string, number>(":memory:") |
| 116 | + map.set("a", 1) |
| 117 | + map.set("b", 2) |
| 118 | + const result: [string, number][] = [] |
| 119 | + map.forEach((value, key) => result.push([key, value])) |
| 120 | + assert.deepEqual(result, [ |
| 121 | + ["a", 1], |
| 122 | + ["b", 2] |
| 123 | + ]) |
| 124 | + }) |
| 125 | + |
| 126 | + test("[Symbol.iterator]", () => { |
| 127 | + const map = new SqliteMap(":memory:") |
| 128 | + map.set("a", 1) |
| 129 | + map.set("b", 2) |
| 130 | + assert.deepEqual( |
| 131 | + [...map], |
| 132 | + [ |
| 133 | + ["a", 1], |
| 134 | + ["b", 2] |
| 135 | + ] |
| 136 | + ) |
| 137 | + }) |
| 138 | + |
| 139 | + test("[Symbol.toStringTag]", () => { |
| 140 | + const map = new SqliteMap(":memory:") |
| 141 | + assert.equal(Object.prototype.toString.call(map), "[object SqliteMap]") |
| 142 | + }) |
| 143 | + |
| 144 | + test("toJSON", () => { |
| 145 | + const map = new SqliteMap(":memory:") |
| 146 | + map.set("a", 1) |
| 147 | + map.set("b", 2) |
| 148 | + assert.deepEqual(map.toJSON(), { a: 1, b: 2 }) |
| 149 | + }) |
| 150 | + |
| 151 | + test("JSON.stringify", () => { |
| 152 | + const map = new SqliteMap(":memory:") |
| 153 | + map.set("a", 1) |
| 154 | + assert.equal(JSON.stringify(map), '{"a":1}') |
| 155 | + }) |
| 156 | + |
| 157 | + test("overwrites existing key", () => { |
| 158 | + const map = new SqliteMap(":memory:") |
| 159 | + map.set("foo", 1) |
| 160 | + map.set("foo", 2) |
| 161 | + assert.equal(map.get("foo"), 2) |
| 162 | + assert.equal(map.size, 1) |
| 163 | + }) |
| 164 | + |
| 165 | + test("supports complex values", () => { |
| 166 | + const map = new SqliteMap(":memory:") |
| 167 | + const val = { nested: { arr: [1, 2, 3], flag: true } } |
| 168 | + map.set("x", val) |
| 169 | + assert.deepEqual(map.get("x"), val) |
| 170 | + }) |
| 171 | +}) |
0 commit comments