Skip to content

Commit

Permalink
Merge pull request #31 from upstash/dx-914-expand-vectorjs-tests-with…
Browse files Browse the repository at this point in the history
…-actual-usage-convention

DX-914: Extend Tests with Index Client
  • Loading branch information
ogzhanolguncu authored Jun 3, 2024
2 parents 3091b0e + 4eb2336 commit a3706de
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 22 deletions.
38 changes: 38 additions & 0 deletions src/commands/client/delete/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import { DeleteCommand, UpsertCommand } from "@commands/index";
import { newHttpClient, randomID, range, resetIndexes } from "@utils/test-utils";
import { Index } from "@utils/test-utils";

const client = newHttpClient();

Expand Down Expand Up @@ -47,3 +48,40 @@ describe("DELETE", () => {
});
});
});

describe("DELETE with Index Client", () => {
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});
afterAll(async () => {
await index.reset();
});

test("should delete single record succesfully", async () => {
const initialVector = range(0, 384);
const id = randomID();

index.upsert({ id, vector: initialVector });

const deletionResult = await index.delete(id);

expect(deletionResult).toEqual({

Check failure on line 69 in src/commands/client/delete/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests

error: expect(received).toEqual(expected)

{ + deleted: 0, - deleted: 1, } - Expected - 1 + Received + 1 at /home/runner/work/vector-js/vector-js/src/commands/client/delete/index.test.ts:69:5

Check failure on line 69 in src/commands/client/delete/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests

error: expect(received).toEqual(expected)

{ + deleted: 0, - deleted: 1, } - Expected - 1 + Received + 1 at /home/runner/work/vector-js/vector-js/src/commands/client/delete/index.test.ts:69:5

Check failure on line 69 in src/commands/client/delete/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests

error: expect(received).toEqual(expected)

{ + deleted: 0, - deleted: 1, } - Expected - 1 + Received + 1 at /home/runner/work/vector-js/vector-js/src/commands/client/delete/index.test.ts:69:5
deleted: 1
});
});

test("should delete array of records successfully", async () => {
const initialVector = range(0, 384);
const idsToUpsert = [randomID(), randomID(), randomID()];

const upsertPromises = idsToUpsert.map((id) => index.upsert({ id, vector: initialVector }));

await Promise.all(upsertPromises);

const deletionResult = await index.delete(idsToUpsert);
expect(deletionResult).toEqual({
deleted: 3,
})
});
});
39 changes: 29 additions & 10 deletions src/commands/client/fetch/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,30 @@ describe("FETCH", () => {

expect(res).toEqual([mockData]);
});
});

test("should fetch succesfully by index.fetch", async () => {
const index = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL!,
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
});
describe("FETCH with Index Client", () => {
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});

const randomFetch = await index.fetch([randomID()], {
includeMetadata: true,
namespace: "test",
});
test("should fetch array of records by IDs succesfully", async () => {
const randomizedData = new Array(20)
.fill("")
.map(() => ({ id: randomID(), vector: range(0, 384) }));

expect(randomFetch).toEqual([null]);
await index.upsert(randomizedData);

await awaitUntilIndexed(index);

const IDs = randomizedData.map((x) => x.id);
const res = await index.fetch(IDs, { includeVectors: true });

expect(res).toEqual(randomizedData);
});

test("should fetch single record by ID", async () => {
const mockData = {
id: randomID(),
vector: range(0, 384),
Expand All @@ -87,4 +97,13 @@ describe("FETCH", () => {

expect(fetchWithID).toEqual([mockData]);
});

test("should return null when ID does not exist", async () => {
const randomFetch = await index.fetch([randomID()], {
includeMetadata: true,
namespace: "test",
});

expect(randomFetch).toEqual([null]);
});
});
132 changes: 130 additions & 2 deletions src/commands/client/query/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { afterAll, describe, expect, test } from "bun:test";
import { QueryCommand, UpsertCommand } from "@commands/index";
import { awaitUntilIndexed, newHttpClient, range, resetIndexes } from "@utils/test-utils";
import { awaitUntilIndexed, newHttpClient, randomID, range } from "@utils/test-utils";
import { Index } from "@utils/test-utils";

const client = newHttpClient();

describe("QUERY", () => {
afterAll(async () => await resetIndexes());
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});

const embeddingIndex = new Index({
token: process.env.EMBEDDING_UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.EMBEDDING_UPSTASH_VECTOR_REST_URL!,
});

afterAll(async () => {
await index.reset();
await embeddingIndex.reset();
});
test("should query records successfully", async () => {
const initialVector = range(0, 384);
const initialData = { id: 33, vector: initialVector };
Expand Down Expand Up @@ -180,3 +194,117 @@ describe("QUERY", () => {
{ timeout: 20000 }
);
});

describe("with Index Client", () => {
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});
const embeddingIndex = new Index({
token: process.env.EMBEDDING_UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.EMBEDDING_UPSTASH_VECTOR_REST_URL!,
});

afterAll(async () => {
await index.reset();
await embeddingIndex.reset();
});

test("should query records successfully", async () => {
const ID = randomID();
const initialVector = range(0, 384);
const initialData = { id: ID, vector: initialVector };
await index.upsert(initialData);

await awaitUntilIndexed(index);

const res = await index.query<{ hello: "World" }>({
includeVectors: true,
vector: initialVector,
topK: 1,
});

expect(res).toEqual([
{
id: ID,
score: 1,
vector: initialVector,
},
]);
});

test(
"should query with plain text successfully",
async () => {
await embeddingIndex.upsert([
{
id: "hello-world",
data: "with-index-plain-text-query-test",
metadata: { upstash: "test" },
},
]);

await awaitUntilIndexed(embeddingIndex);

const res = await embeddingIndex.query({
data: "with-index-plain-text-query-test",
topK: 1,
includeVectors: true,
includeMetadata: true,
});

expect(res[0].metadata).toEqual({ upstash: "test" });
},
{ timeout: 20000 }
);

test("should narrow down the query results with filter", async () => {
const ID = randomID();
const initialVector = range(0, 384);
const initialData = [
{
id: `1-${ID}`,
vector: initialVector,
metadata: {
animal: "elephant",
tags: ["mammal"],
diet: "herbivore",
},
},
{
id: `2-${ID}`,
vector: initialVector,
metadata: {
animal: "tiger",
tags: ["mammal"],
diet: "carnivore",
},
},
];

await index.upsert(initialData);

await awaitUntilIndexed(index);

const res = await index.query<{
animal: string;
tags: string[];
diet: string;
}>({
vector: initialVector,
topK: 1,
filter: "tags[0] = 'mammal' AND diet = 'carnivore'",
includeVectors: true,
includeMetadata: true,
});

expect(res).toEqual([
{
id: `2-${ID}`,
score: 1,
vector: initialVector,
metadata: { animal: "tiger", tags: ["mammal"], diet: "carnivore" },
},
]);
});
});
41 changes: 39 additions & 2 deletions src/commands/client/range/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { afterAll, describe, expect, test } from "bun:test";
import { RangeCommand, UpsertCommand } from "@commands/index";
import { newHttpClient, randomID, range, resetIndexes } from "@utils/test-utils";
import {
Index,
awaitUntilIndexed,
newHttpClient,
randomID,
range,
resetIndexes,
} from "@utils/test-utils";

const client = newHttpClient();

describe("RANGE", () => {
afterAll(async () => await resetIndexes());

test("should query records successfully", async () => {
test("should paginate records successfully", async () => {
const randomizedData = new Array(20)
.fill("")
.map(() => ({ id: randomID(), vector: range(0, 384) }));

const payloads = randomizedData.map((data) => new UpsertCommand(data).exec(client));
await Promise.all(payloads);

await awaitUntilIndexed(client);

const res = await new RangeCommand({
cursor: 0,
limit: 5,
Expand All @@ -23,3 +32,31 @@ describe("RANGE", () => {
expect(res.nextCursor).toBe("5");
});
});

describe("RANGE with Index Client", () => {
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});

afterAll(async () => {
await index.reset();
});
test("should paginate records successfully", async () => {
const randomizedData = new Array(20)
.fill("")
.map(() => ({ id: randomID(), vector: range(0, 384) }));

await index.upsert(randomizedData);

await awaitUntilIndexed(index);

const res = await index.range({
cursor: 0,
limit: 5,
includeVectors: true,
});

expect(res.nextCursor).toBe("5");
});
});
35 changes: 34 additions & 1 deletion src/commands/client/update/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import { FetchCommand, UpdateCommand, UpsertCommand } from "@commands/index";
import { awaitUntilIndexed, newHttpClient, range, resetIndexes } from "@utils/test-utils";
import { Index, awaitUntilIndexed, newHttpClient, range, resetIndexes } from "@utils/test-utils";

const client = newHttpClient();

Expand Down Expand Up @@ -31,3 +31,36 @@ describe("UPDATE", () => {
expect(fetchData[0]?.metadata?.upstash).toBe("test-update");
});
});

describe("UPDATE with Index Client", () => {
const index = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});
afterAll(async () => {
await index.reset();
});

test("should update vector metadata", async () => {
await index.upsert({
id: 1,
vector: range(0, 384),
metadata: { upstash: "test-simple" },
});

await awaitUntilIndexed(index);

const res = await index.update({
id: 1,
metadata: { upstash: "test-update" },
});

expect(res).toEqual({ updated: 1 });

await awaitUntilIndexed(client, 5000);

const fetchData = await index.fetch(["1"], { includeMetadata: true });

expect(fetchData[0]?.metadata?.upstash).toBe("test-update");
});
});
Loading

0 comments on commit a3706de

Please sign in to comment.