| 1 | // Copyright 2023 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 | // IO tests |
| 16 | |
| 17 | import { describe } from 'mocha' |
| 18 | import { assert } from 'chai' |
| 19 | |
| 20 | import * as lancedb from '../index' |
| 21 | import { type ConnectionOptions } from '../index' |
| 22 | |
| 23 | describe('LanceDB S3 client', function () { |
| 24 | if (process.env.TEST_S3_BASE_URL != null) { |
| 25 | const baseUri = process.env.TEST_S3_BASE_URL |
| 26 | it('should have a valid url', async function () { |
| 27 | const opts = { uri: `${baseUri}/valid_url` } |
| 28 | const table = await createTestDB(opts, 2, 20) |
| 29 | const con = await lancedb.connect(opts) |
| 30 | assert.equal(con.uri, opts.uri) |
| 31 | |
| 32 | const results = await table.search([0.1, 0.3]).limit(5).execute() |
| 33 | assert.equal(results.length, 5) |
| 34 | }).timeout(10_000) |
| 35 | } else { |
| 36 | describe.skip('Skip S3 test', function () {}) |
| 37 | } |
| 38 | |
| 39 | if (process.env.TEST_S3_BASE_URL != null && process.env.TEST_AWS_ACCESS_KEY_ID != null && process.env.TEST_AWS_SECRET_ACCESS_KEY != null) { |
| 40 | const baseUri = process.env.TEST_S3_BASE_URL |
| 41 | it('use custom credentials', async function () { |
| 42 | const opts: ConnectionOptions = { |
| 43 | uri: `${baseUri}/custom_credentials`, |
| 44 | awsCredentials: { |
| 45 | accessKeyId: process.env.TEST_AWS_ACCESS_KEY_ID as string, |
| 46 | secretKey: process.env.TEST_AWS_SECRET_ACCESS_KEY as string |
| 47 | } |
| 48 | } |
| 49 | const table = await createTestDB(opts, 2, 20) |
| 50 | console.log(table) |
| 51 | const con = await lancedb.connect(opts) |
| 52 | console.log(con) |
| 53 | assert.equal(con.uri, opts.uri) |
| 54 | |
| 55 | const results = await table.search([0.1, 0.3]).limit(5).execute() |
| 56 | assert.equal(results.length, 5) |
| 57 | }).timeout(10_000) |
| 58 | } else { |
| 59 | describe.skip('Skip S3 test', function () {}) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | async function createTestDB (opts: ConnectionOptions, numDimensions: number = 2, numRows: number = 2): Promise<lancedb.Table> { |
| 64 | const con = await lancedb.connect(opts) |
| 65 | |
| 66 | const data = [] |
| 67 | for (let i = 0; i < numRows; i++) { |
| 68 | const vector = [] |
| 69 | for (let j = 0; j < numDimensions; j++) { |
| 70 | vector.push(i + (j * 0.1)) |
| 71 | } |
| 72 | data.push({ id: i + 1, name: `name_${i}`, price: i + 10, is_active: (i % 2 === 0), vector }) |
| 73 | } |
| 74 | |
| 75 | return await con.createTable('vectors_2', data) |
| 76 | } |