Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLN] Improve JS client async errors tests #2135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 12 additions & 16 deletions clients/js/test/add.collections.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect, test } from "@jest/globals";
import chroma from "./initClient";
import { DOCUMENTS, EMBEDDINGS, IDS } from "./data";
import { METADATAS } from "./data";
import { IncludeEnum } from "../src/types";
import { OpenAIEmbeddingFunction } from "../src/embeddings/OpenAIEmbeddingFunction";
import { CohereEmbeddingFunction } from "../src/embeddings/CohereEmbeddingFunction";
Expand Down Expand Up @@ -102,35 +101,32 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await expect(async () => {
await collection.add({ ids: IDS, embeddings: EMBEDDINGS });
}).rejects.toThrow(InvalidCollectionError);
});

test("It should return an error when inserting duplicate IDs in the same batch", async () => {
test("should error with duplicate IDs in the same batch", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
const ids = IDS.concat(["test1"]);
const embeddings = EMBEDDINGS.concat([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]);
const metadatas = METADATAS.concat([{ test: "test1", float_value: 0.1 }]);
try {
await collection.add({ ids, embeddings, metadatas });
} catch (e: any) {
expect(e.message).toMatch("duplicates");
}
const ids = IDS.concat([IDS[0]]); // add duplicate ID
const embeddings = EMBEDDINGS.concat([EMBEDDINGS[0]]);
await expect(async () => {
await collection.add({ ids, embeddings });
}).rejects.toThrow("duplicates");
});

test("should error on empty embedding", async () => {
// Skip the test as it is failing - no error is thrown.
// TODO: Fix by validating the embeddings as in the Python client.
test.skip("should error on empty embedding", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
const ids = ["id1"];
const embeddings = [[]];
const metadatas = [{ test: "test1", float_value: 0.1 }];
try {
await expect(async () => {
await collection.add({ ids, embeddings, metadatas });
} catch (e: any) {
expect(e.message).toMatch("got empty embedding at pos");
}
}).rejects.toThrow("got empty embedding at pos");
});

if (!process.env.OLLAMA_SERVER_URL) {
Expand Down
12 changes: 5 additions & 7 deletions clients/js/test/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ test("it should set the tenant and database", async () => {
});

test("it should throw well-formatted errors", async () => {
try {
await adminClient.createDatabase({ name: "test", tenantName: "foo" });
expect(false).toBe(true);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(ChromaError);
}
await expect(
adminClient.createDatabase({ name: "test", tenantName: "foo" }),
).rejects.toThrow(
new ChromaError("", "Database test already exists for tenant foo"),
);
});
19 changes: 9 additions & 10 deletions clients/js/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,15 @@ test("wrong code returns an error", async () => {
];
const metadatas = [{ test: "test1" }, { test: "test2" }, { test: "test3" }];
await collection.add({ ids, embeddings, metadatas });
try {
await collection.get({

await expect(
collection.get({
// @ts-ignore - supposed to fail
where: { test: { $contains: "hello" } },
});
} catch (e: any) {
expect(e).toBeDefined();
expect(e).toBeInstanceOf(ChromaValueError);
expect(e.message).toMatchInlineSnapshot(
`"Expected where operator to be one of $gt, $gte, $lt, $lte, $ne, $eq, $in, $nin, got $contains"`,
);
}
}),
).rejects.toThrow(
new ChromaValueError(
"Expected where operator to be one of $gt, $gte, $lt, $lte, $ne, $eq, $in, $nin, got $contains",
),
);
});
6 changes: 3 additions & 3 deletions clients/js/test/delete.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.delete({ where: { test: "test1" } });
}).rejects.toThrow(InvalidCollectionError);
await expect(collection.delete({ where: { test: "test1" } })).rejects.toThrow(
InvalidCollectionError,
);
});
25 changes: 12 additions & 13 deletions clients/js/test/get.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ test("wrong code returns an error", async () => {
embeddings: EMBEDDINGS,
metadatas: METADATAS,
});
try {
await collection.get({

await expect(
collection.get({
where: {
//@ts-ignore supposed to fail
test: { $contains: "hello" },
},
});
} catch (error: any) {
expect(error).toBeDefined();
expect(error).toBeInstanceOf(ChromaValueError);
expect(error.message).toMatchInlineSnapshot(
`"Expected where operator to be one of $gt, $gte, $lt, $lte, $ne, $eq, $in, $nin, got $contains"`,
);
}
}),
).rejects.toThrow(
new ChromaValueError(
"Expected where operator to be one of $gt, $gte, $lt, $lte, $ne, $eq, $in, $nin, got $contains",
),
);
});

test("it should get embedding with matching documents", async () => {
Expand Down Expand Up @@ -105,9 +104,9 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.get({ ids: IDS });
}).rejects.toThrow(InvalidCollectionError);
await expect(collection.get({ ids: IDS })).rejects.toThrow(
InvalidCollectionError,
);
});

test("it should throw an error if the collection does not exist", async () => {
Expand Down
16 changes: 7 additions & 9 deletions clients/js/test/offline.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { expect, test } from "@jest/globals";
import { ChromaClient } from "../src/ChromaClient";
import { ChromaConnectionError } from "../src/Errors";

test("it fails with a nice error", async () => {
const chroma = new ChromaClient({ path: "http://example.invalid" });
try {
await chroma.createCollection({ name: "test" });
throw new Error("Should have thrown an error.");
} catch (e) {
expect(e instanceof Error).toBe(true);
expect((e as Error).message).toMatchInlineSnapshot(
`"Failed to connect to chromadb. Make sure your server is running and try again. If you are running from a browser, make sure that your chromadb instance is configured to allow requests from the current origin using the CHROMA_SERVER_CORS_ALLOW_ORIGINS environment variable."`,
);
}

await expect(chroma.createCollection({ name: "test" })).rejects.toThrow(
new ChromaConnectionError(
"Failed to connect to chromadb. Make sure your server is running and try again. If you are running from a browser, make sure that your chromadb instance is configured to allow requests from the current origin using the CHROMA_SERVER_CORS_ALLOW_ORIGINS environment variable.",
),
);
});
4 changes: 1 addition & 3 deletions clients/js/test/peek.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.peek();
}).rejects.toThrow(InvalidCollectionError);
await expect(collection.peek()).rejects.toThrow(InvalidCollectionError);
});
6 changes: 3 additions & 3 deletions clients/js/test/query.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.query({ queryEmbeddings: [1, 2, 3] });
}).rejects.toThrow(InvalidCollectionError);
await expect(
collection.query({ queryEmbeddings: [1, 2, 3] }),
).rejects.toThrow(InvalidCollectionError);
});
8 changes: 4 additions & 4 deletions clients/js/test/update.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.update({
await expect(
collection.update({
ids: ["test1"],
embeddings: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 11]],
metadatas: [{ test: "meta1" }],
documents: ["doc1"],
});
}).rejects.toThrow(InvalidCollectionError);
}),
).rejects.toThrow(InvalidCollectionError);
});

// this currently fails
Expand Down
8 changes: 4 additions & 4 deletions clients/js/test/upsert.collections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ test("should error on non existing collection", async () => {
await chroma.reset();
const collection = await chroma.createCollection({ name: "test" });
await chroma.deleteCollection({ name: "test" });
expect(async () => {
await collection.upsert({
await expect(
collection.upsert({
ids: ["test1"],
embeddings: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 11]],
metadatas: [{ test: "meta1" }],
documents: ["doc1"],
});
}).rejects.toThrow(InvalidCollectionError);
}),
).rejects.toThrow(InvalidCollectionError);
});