|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import { test, expect } from "@perspective-dev/test"; |
| 14 | +import perspective from "../perspective_client.ts"; |
| 15 | + |
| 16 | +((perspective) => { |
| 17 | + test.describe("Inner joins", function () { |
| 18 | + test("inner joins two tables on a shared key", async function () { |
| 19 | + const left = await perspective.table([ |
| 20 | + { id: 1, x: 10 }, |
| 21 | + { id: 2, x: 20 }, |
| 22 | + { id: 3, x: 30 }, |
| 23 | + ]); |
| 24 | + |
| 25 | + const right = await perspective.table([ |
| 26 | + { id: 1, y: "a" }, |
| 27 | + { id: 2, y: "b" }, |
| 28 | + { id: 4, y: "d" }, |
| 29 | + ]); |
| 30 | + |
| 31 | + const joined = await perspective.join(left, right, "id"); |
| 32 | + const view = await joined.view(); |
| 33 | + const json = await view.to_json(); |
| 34 | + |
| 35 | + expect(json).toHaveLength(2); |
| 36 | + |
| 37 | + view.delete(); |
| 38 | + joined.delete(); |
| 39 | + right.delete(); |
| 40 | + left.delete(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("joined table has correct schema", async function () { |
| 44 | + const left = await perspective.table({ id: "integer", x: "float" }); |
| 45 | + |
| 46 | + const right = await perspective.table({ |
| 47 | + id: "integer", |
| 48 | + y: "string", |
| 49 | + }); |
| 50 | + |
| 51 | + const joined = await perspective.join(left, right, "id"); |
| 52 | + const schema = await joined.schema(); |
| 53 | + |
| 54 | + expect(schema).toEqual({ |
| 55 | + id: "integer", |
| 56 | + x: "float", |
| 57 | + y: "string", |
| 58 | + }); |
| 59 | + |
| 60 | + joined.delete(); |
| 61 | + right.delete(); |
| 62 | + left.delete(); |
| 63 | + }); |
| 64 | + |
| 65 | + test("joined table reacts to left table updates", async function () { |
| 66 | + const left = await perspective.table([ |
| 67 | + { id: 1, x: 10 }, |
| 68 | + { id: 2, x: 20 }, |
| 69 | + ]); |
| 70 | + |
| 71 | + const right = await perspective.table([ |
| 72 | + { id: 1, y: "a" }, |
| 73 | + { id: 2, y: "b" }, |
| 74 | + ]); |
| 75 | + |
| 76 | + const joined = await perspective.join(left, right, "id"); |
| 77 | + const view = await joined.view(); |
| 78 | + |
| 79 | + let json = await view.to_json(); |
| 80 | + expect(json).toHaveLength(2); |
| 81 | + |
| 82 | + await left.update([{ id: 1, x: 99 }]); |
| 83 | + json = await view.to_json(); |
| 84 | + |
| 85 | + expect(json).toEqual([ |
| 86 | + { id: 1, x: 10, y: "a" }, |
| 87 | + { id: 2, x: 20, y: "b" }, |
| 88 | + { id: 1, x: 99, y: "a" }, |
| 89 | + ]); |
| 90 | + |
| 91 | + view.delete(); |
| 92 | + joined.delete(); |
| 93 | + right.delete(); |
| 94 | + left.delete(); |
| 95 | + }); |
| 96 | + |
| 97 | + test("joined table reacts to right table updates", async function () { |
| 98 | + const left = await perspective.table([ |
| 99 | + { id: 1, x: 10 }, |
| 100 | + { id: 2, x: 20 }, |
| 101 | + ]); |
| 102 | + |
| 103 | + const right = await perspective.table([ |
| 104 | + { id: 1, y: "a" }, |
| 105 | + { id: 2, y: "b" }, |
| 106 | + ]); |
| 107 | + |
| 108 | + const joined = await perspective.join(left, right, "id"); |
| 109 | + const view = await joined.view(); |
| 110 | + |
| 111 | + await right.update([{ id: 1, y: "c" }]); |
| 112 | + const json = await view.to_json(); |
| 113 | + |
| 114 | + // id=3 only exists in right, so inner join should not include it |
| 115 | + expect(json).toHaveLength(3); |
| 116 | + |
| 117 | + expect(json).toEqual([ |
| 118 | + { id: 1, x: 10, y: "a" }, |
| 119 | + { id: 1, x: 10, y: "c" }, |
| 120 | + { id: 2, x: 20, y: "b" }, |
| 121 | + ]); |
| 122 | + |
| 123 | + view.delete(); |
| 124 | + joined.delete(); |
| 125 | + right.delete(); |
| 126 | + left.delete(); |
| 127 | + }); |
| 128 | + |
| 129 | + test("joined table reacts to new matching rows", async function () { |
| 130 | + const left = await perspective.table([{ id: 1, x: 10 }]); |
| 131 | + |
| 132 | + const right = await perspective.table([{ id: 2, y: "b" }]); |
| 133 | + |
| 134 | + const joined = await perspective.join(left, right, "id"); |
| 135 | + const view = await joined.view(); |
| 136 | + |
| 137 | + let json = await view.to_json(); |
| 138 | + expect(json).toHaveLength(0); |
| 139 | + |
| 140 | + // Add matching row to right |
| 141 | + await right.update([{ id: 1, y: "a" }]); |
| 142 | + json = await view.to_json(); |
| 143 | + expect(json).toHaveLength(1); |
| 144 | + expect(json).toEqual([{ id: 1, x: 10, y: "a" }]); |
| 145 | + |
| 146 | + view.delete(); |
| 147 | + joined.delete(); |
| 148 | + right.delete(); |
| 149 | + left.delete(); |
| 150 | + }); |
| 151 | + |
| 152 | + test("joined table supports views with group_by", async function () { |
| 153 | + const left = await perspective.table([ |
| 154 | + { id: 1, category: "A", x: 10 }, |
| 155 | + { id: 2, category: "A", x: 20 }, |
| 156 | + { id: 3, category: "B", x: 30 }, |
| 157 | + ]); |
| 158 | + |
| 159 | + const right = await perspective.table([ |
| 160 | + { id: 1, y: 100 }, |
| 161 | + { id: 2, y: 200 }, |
| 162 | + { id: 3, y: 300 }, |
| 163 | + ]); |
| 164 | + |
| 165 | + const joined = await perspective.join(left, right, "id"); |
| 166 | + const view = await joined.view({ |
| 167 | + group_by: ["category"], |
| 168 | + columns: ["x", "y"], |
| 169 | + }); |
| 170 | + |
| 171 | + const json = await view.to_columns(); |
| 172 | + expect(json["x"]).toEqual([60, 30, 30]); |
| 173 | + expect(json["y"]).toEqual([600, 300, 300]); |
| 174 | + |
| 175 | + view.delete(); |
| 176 | + joined.delete(); |
| 177 | + right.delete(); |
| 178 | + left.delete(); |
| 179 | + }); |
| 180 | + |
| 181 | + test("rejects column name conflicts", async function () { |
| 182 | + const left = await perspective.table([{ id: 1, value: 10 }]); |
| 183 | + const right = await perspective.table([{ id: 1, value: 20 }]); |
| 184 | + |
| 185 | + let error; |
| 186 | + try { |
| 187 | + await perspective.join(left, right, "id"); |
| 188 | + } catch (e) { |
| 189 | + error = e; |
| 190 | + } |
| 191 | + |
| 192 | + expect(error).toBeDefined(); |
| 193 | + right.delete(); |
| 194 | + left.delete(); |
| 195 | + }); |
| 196 | + |
| 197 | + test("rejects updates on joined table", async function () { |
| 198 | + const left = await perspective.table([{ id: 1, x: 10 }]); |
| 199 | + const right = await perspective.table([{ id: 1, y: "a" }]); |
| 200 | + |
| 201 | + const joined = await perspective.join(left, right, "id"); |
| 202 | + |
| 203 | + let error; |
| 204 | + try { |
| 205 | + await joined.update([{ id: 1, x: 99, y: "z" }]); |
| 206 | + } catch (e) { |
| 207 | + error = e; |
| 208 | + } |
| 209 | + |
| 210 | + expect(error).toBeDefined(); |
| 211 | + |
| 212 | + joined.delete(); |
| 213 | + right.delete(); |
| 214 | + left.delete(); |
| 215 | + }); |
| 216 | + }); |
| 217 | +})(perspective); |
0 commit comments