-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
850da9c
commit 2730ca3
Showing
10 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using ChromaDB.Client.Models.Requests; | ||
using ChromaDB.Client.Services.Implementations; | ||
using NUnit.Framework; | ||
|
||
namespace ChromaDB.Client.Tests; | ||
|
||
[TestFixture] | ||
public class ChromaDBCollectionTests : ChromaDBTestsBase | ||
{ | ||
[Test] | ||
public async Task CreateCollectionWithoutMetadata() | ||
{ | ||
var name = $"collection{Random.Shared.Next()}"; | ||
|
||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = new ChromaDBClient(ConfigurationOptions, httpClient); | ||
var result = await client.CreateCollection(new DBCreateCollectionRequest() | ||
{ | ||
Name = name, | ||
}); | ||
Assert.That(result.Success, Is.True); | ||
Assert.That(result.Data, Is.Not.Null); | ||
Assert.That(result.Data.Name, Is.EqualTo(name)); | ||
} | ||
|
||
[Test] | ||
public async Task CreateCollectionWithMetadata() | ||
{ | ||
var name = $"collection{Random.Shared.Next()}"; | ||
var metadata = new Dictionary<string, object>() | ||
{ | ||
{ "test", "foo" }, | ||
{ "test2", 10 }, | ||
}; | ||
|
||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = new ChromaDBClient(ConfigurationOptions, httpClient); | ||
var result = await client.CreateCollection(new DBCreateCollectionRequest() | ||
{ | ||
Name = name, | ||
Metadata = metadata, | ||
}); | ||
Assert.That(result.Success, Is.True); | ||
Assert.That(result.Data, Is.Not.Null); | ||
Assert.That(result.Data.Name, Is.EqualTo(name)); | ||
Assert.That(result.Data.Metadata, Is.Not.Null); | ||
Assert.That(result.Data.Metadata["test"], Is.EqualTo(metadata["test"])); | ||
Assert.That(result.Data.Metadata["test2"], Is.EqualTo(metadata["test2"])); | ||
} | ||
|
||
[Test] | ||
public async Task CreateCollectionAlreadyExists() | ||
{ | ||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = new ChromaDBClient(ConfigurationOptions, httpClient); | ||
await client.CreateCollection(new DBCreateCollectionRequest() | ||
{ | ||
Name = $"collection_exists", | ||
}); | ||
var result = await client.CreateCollection(new DBCreateCollectionRequest() | ||
{ | ||
Name = $"collection_exists", | ||
}); | ||
Assert.That(result.Success, Is.False); | ||
Assert.That(result.ReasonPhrase, Is.Not.Null.And.Not.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ChromaDB.Client/Common/Helpers/ObjectToInferredTypesJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChromaDB.Client.Common.Helpers; | ||
|
||
public class ObjectToInferredTypesJsonConverter : JsonConverter<object> | ||
{ | ||
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
=> reader.TokenType switch | ||
{ | ||
JsonTokenType.True => true, | ||
JsonTokenType.False => false, | ||
JsonTokenType.Number when reader.TryGetInt64(out long l) => l, | ||
JsonTokenType.Number => reader.GetDouble(), | ||
JsonTokenType.String when reader.TryGetDateTime(out DateTime datetime) => datetime, | ||
JsonTokenType.String => reader.GetString()!, | ||
_ => JsonDocument.ParseValue(ref reader).RootElement.Clone() | ||
}; | ||
|
||
public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) => throw new NotSupportedException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
ChromaDB.Client/Models/Requests/DBCreateCollectionRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChromaDB.Client.Models.Requests; | ||
|
||
public class DBCreateCollectionRequest | ||
{ | ||
[JsonPropertyName("name")] | ||
public required string Name { get; init; } | ||
|
||
[JsonPropertyName("metadata")] | ||
public IDictionary<string, object>? Metadata { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters