| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import { Field, Float64, Schema } from "apache-arrow"; |
| 16 | import * as tmp from "tmp"; |
| 17 | import { Connection, Table, connect } from "../lancedb"; |
| 18 | |
| 19 | describe("when connecting", () => { |
| 20 | let tmpDir: tmp.DirResult; |
| 21 | beforeEach(() => { |
| 22 | tmpDir = tmp.dirSync({ unsafeCleanup: true }); |
| 23 | }); |
| 24 | afterEach(() => tmpDir.removeCallback()); |
| 25 | |
| 26 | it("should connect", async () => { |
| 27 | const db = await connect(tmpDir.name); |
| 28 | expect(db.display()).toBe( |
| 29 | `NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=None)`, |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | it("should allow read consistency interval to be specified", async () => { |
| 34 | const db = await connect(tmpDir.name, { readConsistencyInterval: 5 }); |
| 35 | expect(db.display()).toBe( |
| 36 | `NativeDatabase(uri=${tmpDir.name}, read_consistency_interval=5s)`, |
| 37 | ); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe("given a connection", () => { |
| 42 | let tmpDir: tmp.DirResult; |
| 43 | let db: Connection; |
| 44 | beforeEach(async () => { |
| 45 | tmpDir = tmp.dirSync({ unsafeCleanup: true }); |
| 46 | db = await connect(tmpDir.name); |
| 47 | }); |
| 48 | afterEach(() => tmpDir.removeCallback()); |
| 49 | |
| 50 | it("should raise an error if opening a non-existent table", async () => { |
| 51 | await expect(db.openTable("non-existent")).rejects.toThrow("was not found"); |
| 52 | }); |
| 53 | |
| 54 | it("should raise an error if any operation is tried after it is closed", async () => { |
| 55 | expect(db.isOpen()).toBe(true); |
| 56 | await db.close(); |
| 57 | expect(db.isOpen()).toBe(false); |
| 58 | await expect(db.tableNames()).rejects.toThrow("Connection is closed"); |
| 59 | }); |
| 60 | it("should be able to create a table from an object arg `createTable(options)`, or args `createTable(name, data, options)`", async () => { |
| 61 | let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]); |
| 62 | await expect(tbl.countRows()).resolves.toBe(2); |
| 63 | |
| 64 | tbl = await db.createTable({ |
| 65 | name: "test", |
| 66 | data: [{ id: 3 }], |
| 67 | mode: "overwrite", |
| 68 | }); |
| 69 | |
| 70 | await expect(tbl.countRows()).resolves.toBe(1); |
| 71 | }); |
| 72 | |
| 73 | it("should fail if creating table twice, unless overwrite is true", async () => { |
| 74 | let tbl = await db.createTable("test", [{ id: 1 }, { id: 2 }]); |
| 75 | await expect(tbl.countRows()).resolves.toBe(2); |
| 76 | await expect( |
| 77 | db.createTable("test", [{ id: 1 }, { id: 2 }]), |
| 78 | ).rejects.toThrow(); |
| 79 | tbl = await db.createTable("test", [{ id: 3 }], { mode: "overwrite" }); |
| 80 | await expect(tbl.countRows()).resolves.toBe(1); |
| 81 | }); |
| 82 | |
| 83 | it("should respect limit and page token when listing tables", async () => { |
| 84 | const db = await connect(tmpDir.name); |
| 85 | |
| 86 | await db.createTable("b", [{ id: 1 }]); |
| 87 | await db.createTable("a", [{ id: 1 }]); |
| 88 | await db.createTable("c", [{ id: 1 }]); |
| 89 | |
| 90 | let tables = await db.tableNames(); |
| 91 | expect(tables).toEqual(["a", "b", "c"]); |
| 92 | |
| 93 | tables = await db.tableNames({ limit: 1 }); |
| 94 | expect(tables).toEqual(["a"]); |
| 95 | |
| 96 | tables = await db.tableNames({ limit: 1, startAfter: "a" }); |
| 97 | expect(tables).toEqual(["b"]); |
| 98 | |
| 99 | tables = await db.tableNames({ startAfter: "a" }); |
| 100 | expect(tables).toEqual(["b", "c"]); |
| 101 | }); |
| 102 | |
| 103 | it("should create tables in v2 mode", async () => { |
| 104 | const db = await connect(tmpDir.name); |
| 105 | const data = [...Array(10000).keys()].map((i) => ({ id: i })); |
| 106 | |
| 107 | // Create in v1 mode |
| 108 | let table = await db.createTable("test", data); |
| 109 | |
| 110 | const isV2 = async (table: Table) => { |
| 111 | const data = await table.query().toArrow({ maxBatchLength: 100000 }); |
| 112 | console.log(data.batches.length); |
| 113 | return data.batches.length < 5; |
| 114 | }; |
| 115 | |
| 116 | await expect(isV2(table)).resolves.toBe(false); |
| 117 | |
| 118 | // Create in v2 mode |
| 119 | table = await db.createTable("test_v2", data, { useLegacyFormat: false }); |
| 120 | |
| 121 | await expect(isV2(table)).resolves.toBe(true); |
| 122 | |
| 123 | await table.add(data); |
| 124 | |
| 125 | await expect(isV2(table)).resolves.toBe(true); |
| 126 | |
| 127 | // Create empty in v2 mode |
| 128 | const schema = new Schema([new Field("id", new Float64(), true)]); |
| 129 | |
| 130 | table = await db.createEmptyTable("test_v2_empty", schema, { |
| 131 | useLegacyFormat: false, |
| 132 | }); |
| 133 | |
| 134 | await table.add(data); |
| 135 | await expect(isV2(table)).resolves.toBe(true); |
| 136 | }); |
| 137 | }); |