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

Implement Where filter as strongly typed. #65

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions ChromaDB.Client.Tests/CollectionClientCUDTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ await client.Add([id1, id2],
{ "key", "value" },
}]);
await client.Delete([id1, id2],
where: new Dictionary<string, object>
{
{ "key", "value" },
});
where: ChromaWhere.Equal("key", "value"));
}

[Test]
Expand Down
124 changes: 104 additions & 20 deletions ChromaDB.Client.Tests/CollectionClientGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ public async Task GetLimitOffsetIncludeAll()
}

[Test]
public async Task GetWhereIncludeAll()
public async Task GetWhereEqualIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: new Dictionary<string, object> { { MetadataKey2, Metadata2[MetadataKey2] } },
where: ChromaWhere.Equal(MetadataKey2, Metadata2[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Expand All @@ -207,11 +207,67 @@ public async Task GetWhereIncludeAll()
}

[Test]
public async Task GetWhereOperatorIncludeAll()
public async Task GetWhereNotEqualIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: new Dictionary<string, object> { { MetadataKey2, new Dictionary<string, object> { { "$lt", Metadata2[MetadataKey2] } } } },
where: ChromaWhere.NotEqual(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereInIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.In(MetadataKey2, Metadata2[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereNotInIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.NotIn(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereGreaterThanIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.GreaterThan(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereLessThanIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.LessThan(MetadataKey2, Metadata2[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id1));
Expand All @@ -221,39 +277,67 @@ public async Task GetWhereOperatorIncludeAll()
}

[Test]
public async Task GetWhereDocumentContainsIncludeDocuments()
public async Task GetWhereGreaterThanOrEqualIncludeAll()
{
var client = await Init();
var result = await client.Get(
whereDocument: ChromaWhereDocument.Contains(Doc1[^1]),
include: ChromaGetInclude.Documents);
where: ChromaWhere.GreaterThanOrEqual(MetadataKey2, Metadata2[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id1));
Assert.That(result[0].Embeddings, Is.Null);
Assert.That(result[0].Metadata, Is.Null);
Assert.That(result[0].Document, Is.EqualTo(Doc1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereDocumentNotContainsIncludeDocuments()
public async Task GetWhereLessThanOrEqualIncludeAll()
{
var client = await Init();
var result = await client.Get(
whereDocument: ChromaWhereDocument.NotContains(Doc2[^1]),
include: ChromaGetInclude.Documents);
where: ChromaWhere.LessThanOrEqual(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id1));
Assert.That(result[0].Embeddings, Is.Null);
Assert.That(result[0].Metadata, Is.Null);
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings1).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata1));
Assert.That(result[0].Document, Is.EqualTo(Doc1));
}

[Test]
public async Task GetWhereDocumentAndIncludeDocuments()
public async Task GetWhereAndIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.Equal(MetadataKey2, Metadata2[MetadataKey2]) && ChromaWhere.NotEqual(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereOrIncludeAll()
{
var client = await Init();
var result = await client.Get(
where: ChromaWhere.Equal(MetadataKey2, Metadata2[MetadataKey2]) || ChromaWhere.NotEqual(MetadataKey2, Metadata1[MetadataKey2]),
include: ChromaGetInclude.Embeddings | ChromaGetInclude.Metadatas | ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id2));
Assert.That(result[0].Embeddings, Is.EqualTo(Embeddings2).Using(EmbeddingsComparer.Instance));
Assert.That(result[0].Metadata, Is.EqualTo(Metadata2));
Assert.That(result[0].Document, Is.EqualTo(Doc2));
}

[Test]
public async Task GetWhereDocumentContainsIncludeDocuments()
{
var client = await Init();
var result = await client.Get(
whereDocument: ChromaWhereDocument.Contains(Doc1[^1]) && ChromaWhereDocument.NotContains(Doc2[^1]),
whereDocument: ChromaWhereDocument.Contains(Doc1[^1]),
include: ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id1));
Expand All @@ -263,11 +347,11 @@ public async Task GetWhereDocumentAndIncludeDocuments()
}

[Test]
public async Task GetWhereDocumentOrIncludeDocuments()
public async Task GetWhereDocumentNotContainsIncludeDocuments()
{
var client = await Init();
var result = await client.Get(
whereDocument: ChromaWhereDocument.Contains(Doc1[^1]) || ChromaWhereDocument.NotContains(Doc2[^1]),
whereDocument: ChromaWhereDocument.NotContains(Doc2[^1]),
include: ChromaGetInclude.Documents);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Id, Is.EqualTo(Id1));
Expand Down
Loading