diff --git a/src/IO.Milvus/ApiSchema/DescribeCollectionResponse.cs b/src/IO.Milvus/ApiSchema/DescribeCollectionResponse.cs index 1b2115f..0669c03 100644 --- a/src/IO.Milvus/ApiSchema/DescribeCollectionResponse.cs +++ b/src/IO.Milvus/ApiSchema/DescribeCollectionResponse.cs @@ -1,4 +1,5 @@ -using IO.Milvus.Utils; +using System; +using IO.Milvus.Utils; using System.Collections.Generic; using System.Text.Json.Serialization; @@ -75,14 +76,13 @@ internal sealed class DescribeCollectionResponse public DetailedMilvusCollection ToDetailedMilvusCollection() { return new DetailedMilvusCollection( - Aliases, + (IReadOnlyList)Aliases ?? Array.Empty(), CollectionName, CollectionId, ConsistencyLevel, TimestampUtils.GetTimeFromTimstamp(CreatedUTCTimestamp), Schema, ShardsNum, - StartPositions - ); + StartPositions); } } diff --git a/src/IO.MilvusTests/Client/AliasTests.cs b/src/IO.MilvusTests/Client/AliasTests.cs new file mode 100644 index 0000000..c6f5747 --- /dev/null +++ b/src/IO.MilvusTests/Client/AliasTests.cs @@ -0,0 +1,85 @@ +using IO.Milvus; +using IO.Milvus.Client; +using IO.Milvus.Client.gRPC; +using Xunit; + +namespace IO.MilvusTests.Client; + +public class AliasTests : IAsyncLifetime +{ + [Theory] + [ClassData(typeof(TestClients))] + public async Task Create(IMilvusClient client) + { + await client.DropAliasAsync("a"); + await client.DropAliasAsync("b"); + + await client.CreateAliasAsync(CollectionName, "a"); + await client.CreateAliasAsync(CollectionName, "b"); + + var description = await client.DescribeCollectionAsync(CollectionName); + Assert.Collection(description.Aliases.Order(), + alias => Assert.Equal("a", alias), + alias => Assert.Equal("b", alias)); + } + + [Theory] + [ClassData(typeof(TestClients))] + public async Task Alter(IMilvusClient client) + { + await client.DropAliasAsync("a"); + await client.CreateAliasAsync(CollectionName, "a"); + + await client.DropCollectionAsync("AnotherCollection"); + await client.CreateCollectionAsync( + "AnotherCollection", + new[] { FieldType.Create("id", isPrimaryKey: true) }); + + await client.AlterAliasAsync("AnotherCollection", "a"); + + var description1 = await client.DescribeCollectionAsync(CollectionName); + Assert.DoesNotContain(description1.Aliases, alias => alias == "a"); + + var description2 = await client.DescribeCollectionAsync("AnotherCollection"); + Assert.Collection(description2.Aliases, alias => Assert.Equal("a", alias)); + } + + [Theory] + [ClassData(typeof(TestClients))] + public async Task Drop(IMilvusClient client) + { + await client.DropAliasAsync("a"); + await client.CreateAliasAsync(CollectionName, "a"); + + await client.DropAliasAsync("a"); + + var description = await client.DescribeCollectionAsync(CollectionName); + Assert.DoesNotContain(description.Aliases, alias => alias == "a"); + } + + public string CollectionName => nameof(AliasTests); + + public async Task InitializeAsync() + { + var config = MilvusConfig.Load().FirstOrDefault(); + if (config is null) + { + throw new InvalidOperationException("No client configs"); + } + + var client = config.CreateClient(); + + await client.DropCollectionAsync(CollectionName); + await client.CreateCollectionAsync( + CollectionName, + new[] + { + FieldType.Create("id", isPrimaryKey: true), + FieldType.CreateVarchar("varchar", 256), + FieldType.CreateFloatVector("float_vector", 2) + }); + } + + public Task DisposeAsync() + => Task.CompletedTask; +} diff --git a/src/IO.MilvusTests/Client/QueryTests.cs b/src/IO.MilvusTests/Client/QueryTests.cs index 6a6c542..fbad25a 100644 --- a/src/IO.MilvusTests/Client/QueryTests.cs +++ b/src/IO.MilvusTests/Client/QueryTests.cs @@ -4,7 +4,7 @@ namespace IO.MilvusTests.Client; -public class QueryTests : IClassFixture +public class QueryTests : IClassFixture { private string QueryCollectionName { get; } @@ -41,6 +41,21 @@ public async Task Query(IMilvusClient client) v => Assert.Equal(new List { 5f, 6f }, v)); } + [Theory(Skip = "Fails")] + [ClassData(typeof(TestClients))] + public async Task Query_with_offset(IMilvusClient client) + { + var queryResult = await client.QueryAsync( + QueryCollectionName, + "id in [2, 3]", + outputFields: new[] { "float_vector" }, + offset: 1); + + var idData = (Field)Assert.Single(queryResult.FieldsData, d => d.FieldName == "id"); + Assert.Equal(1, idData.RowCount); + Assert.Collection(idData.Data, id => Assert.Equal(3, id)); + } + [Theory] [ClassData(typeof(TestClients))] public async Task Query_with_limit(IMilvusClient client) @@ -55,59 +70,59 @@ public async Task Query_with_limit(IMilvusClient client) Assert.Equal(1, idData.RowCount); Assert.Collection(idData.Data, id => Assert.Equal(2, id)); } -} - -public class QueryCollectionFixture : IAsyncLifetime -{ - public string CollectionName => "QueryCollection"; - public async Task InitializeAsync() + public class QueryCollectionFixture : IAsyncLifetime { - var config = MilvusConfig.Load().FirstOrDefault(); - if (config is null) - { - throw new InvalidOperationException("No client configs"); - } + public string CollectionName => "QueryCollection"; - var client = config.CreateClient(); - - await client.DropCollectionAsync(CollectionName); - await client.CreateCollectionAsync( - CollectionName, - new[] - { - FieldType.Create("id", isPrimaryKey: true), - FieldType.CreateVarchar("varchar", 256), - FieldType.CreateFloatVector("float_vector", 2) - }); - - var ids = new long[] { 1, 2, 3, 4, 5 }; - var strings = new[] { "one", "two", "three", "four", "five" }; - var floatVectors = new[] + public async Task InitializeAsync() { - new List { 1f, 2f }, - new List { 3.5f, 4.5f }, - new List { 5f, 6f }, - new List { 7.7f, 8.8f }, - new List { 9f, 10f } - }; - - await client.CreateIndexAsync( - CollectionName, "float_vector", "float_vector_idx", MilvusIndexType.FLAT, MilvusMetricType.L2); - - await client.InsertAsync( - CollectionName, - new Field[] + var config = MilvusConfig.Load().FirstOrDefault(); + if (config is null) + { + throw new InvalidOperationException("No client configs"); + } + + var client = config.CreateClient(); + + await client.DropCollectionAsync(CollectionName); + await client.CreateCollectionAsync( + CollectionName, + new[] + { + FieldType.Create("id", isPrimaryKey: true), + FieldType.CreateVarchar("varchar", 256), + FieldType.CreateFloatVector("float_vector", 2) + }); + + await client.CreateIndexAsync( + CollectionName, "float_vector", "float_vector_idx", MilvusIndexType.FLAT, MilvusMetricType.L2); + + var ids = new long[] { 1, 2, 3, 4, 5 }; + var strings = new[] { "one", "two", "three", "four", "five" }; + var floatVectors = new[] { - Field.Create("id", ids), - Field.Create("varchar", strings), - Field.CreateFloatVector("float_vector", floatVectors) - }); + new List { 1f, 2f }, + new List { 3.5f, 4.5f }, + new List { 5f, 6f }, + new List { 7.7f, 8.8f }, + new List { 9f, 10f } + }; + + await client.InsertAsync( + CollectionName, + new Field[] + { + Field.Create("id", ids), + Field.Create("varchar", strings), + Field.CreateFloatVector("float_vector", floatVectors) + }); + + await client.LoadCollectionAsync(CollectionName); + await client.WaitForLoadingProgressCollectionAsync(CollectionName, Array.Empty(), TimeSpan.FromMilliseconds(100), TimeSpan.FromMinutes(1)); + } - await client.LoadCollectionAsync(CollectionName); - await client.WaitForLoadingProgressCollectionAsync(CollectionName, Array.Empty(), TimeSpan.FromMilliseconds(100), TimeSpan.FromMinutes(1)); + public Task DisposeAsync() + => Task.CompletedTask; } - - public Task DisposeAsync() - => Task.CompletedTask; }