Skip to content

Commit

Permalink
Some more test work (milvus-io#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Jul 5, 2023
1 parent 3407f03 commit e6f4b4d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 53 deletions.
8 changes: 4 additions & 4 deletions src/IO.Milvus/ApiSchema/DescribeCollectionResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IO.Milvus.Utils;
using System;
using IO.Milvus.Utils;
using System.Collections.Generic;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -75,14 +76,13 @@ internal sealed class DescribeCollectionResponse
public DetailedMilvusCollection ToDetailedMilvusCollection()
{
return new DetailedMilvusCollection(
Aliases,
(IReadOnlyList<string>)Aliases ?? Array.Empty<string>(),
CollectionName,
CollectionId,
ConsistencyLevel,
TimestampUtils.GetTimeFromTimstamp(CreatedUTCTimestamp),
Schema,
ShardsNum,
StartPositions
);
StartPositions);
}
}
85 changes: 85 additions & 0 deletions src/IO.MilvusTests/Client/AliasTests.cs
Original file line number Diff line number Diff line change
@@ -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<long>("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<long>("id", isPrimaryKey: true),
FieldType.CreateVarchar("varchar", 256),
FieldType.CreateFloatVector("float_vector", 2)
});
}

public Task DisposeAsync()
=> Task.CompletedTask;
}
113 changes: 64 additions & 49 deletions src/IO.MilvusTests/Client/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace IO.MilvusTests.Client;

public class QueryTests : IClassFixture<QueryCollectionFixture>
public class QueryTests : IClassFixture<QueryTests.QueryCollectionFixture>
{
private string QueryCollectionName { get; }

Expand Down Expand Up @@ -41,6 +41,21 @@ public async Task Query(IMilvusClient client)
v => Assert.Equal(new List<float> { 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<long>)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)
Expand All @@ -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<long>("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<float> { 1f, 2f },
new List<float> { 3.5f, 4.5f },
new List<float> { 5f, 6f },
new List<float> { 7.7f, 8.8f },
new List<float> { 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<long>("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<float> { 1f, 2f },
new List<float> { 3.5f, 4.5f },
new List<float> { 5f, 6f },
new List<float> { 7.7f, 8.8f },
new List<float> { 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<string>(), TimeSpan.FromMilliseconds(100), TimeSpan.FromMinutes(1));
}

await client.LoadCollectionAsync(CollectionName);
await client.WaitForLoadingProgressCollectionAsync(CollectionName, Array.Empty<string>(), TimeSpan.FromMilliseconds(100), TimeSpan.FromMinutes(1));
public Task DisposeAsync()
=> Task.CompletedTask;
}

public Task DisposeAsync()
=> Task.CompletedTask;
}

0 comments on commit e6f4b4d

Please sign in to comment.