Skip to content

Commit 241026e

Browse files
committed
Refactor shard status handling to use enum and update related tests
1 parent 7ca6d0b commit 241026e

File tree

3 files changed

+24
-39
lines changed

3 files changed

+24
-39
lines changed

src/Weaviate.Client.Tests/Integration/TestCollectionShards.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public async Task Test_Should_Get_Collection_Shards()
2929
{
3030
Assert.NotNull(shard.Name);
3131
Assert.NotEmpty(shard.Name);
32-
Assert.NotNull(shard.Status);
33-
Assert.NotEmpty(shard.Status);
32+
// Status is an enum, so it always has a value
33+
Assert.True(Enum.IsDefined(typeof(ShardStatus), shard.Status));
3434
}
3535
);
3636
}
@@ -60,8 +60,7 @@ public async Task Test_Should_Get_Specific_Shard()
6060
// Assert
6161
Assert.NotNull(shard);
6262
Assert.Equal(firstShardName, shard.Name);
63-
Assert.NotNull(shard.Status);
64-
Assert.NotEmpty(shard.Status);
63+
Assert.True(Enum.IsDefined(typeof(ShardStatus), shard.Status));
6564
}
6665

6766
[Fact]
@@ -90,8 +89,7 @@ public async Task Test_Should_Update_Single_Shard_Status_To_ReadOnly()
9089
Assert.NotNull(updatedShards);
9190
Assert.Single(updatedShards);
9291
Assert.Equal(firstShardName, updatedShards[0].Name);
93-
Assert.Equal("READONLY", updatedShards[0].Status);
94-
Assert.Equal(ShardStatus.ReadOnly, updatedShards[0].StatusValue);
92+
Assert.Equal(ShardStatus.ReadOnly, updatedShards[0].Status);
9593

9694
// Cleanup: Set it back to READY
9795
await collection.Config.UpdateShardStatus(ShardStatus.Ready, firstShardName);
@@ -126,8 +124,7 @@ public async Task Test_Should_Update_Shard_Status_Back_To_Ready()
126124
Assert.NotNull(updatedShards);
127125
Assert.Single(updatedShards);
128126
Assert.Equal(firstShardName, updatedShards[0].Name);
129-
Assert.Equal("READY", updatedShards[0].Status);
130-
Assert.Equal(ShardStatus.Ready, updatedShards[0].StatusValue);
127+
Assert.Equal(ShardStatus.Ready, updatedShards[0].Status);
131128
}
132129

133130
[Fact]
@@ -156,7 +153,7 @@ public async Task Test_Should_Update_Multiple_Shards_With_Params()
156153
// Assert
157154
Assert.NotNull(updatedShards);
158155
Assert.Equal(shardNames.Length, updatedShards.Count);
159-
Assert.All(updatedShards, shard => Assert.Equal("READONLY", shard.Status));
156+
Assert.All(updatedShards, shard => Assert.Equal(ShardStatus.ReadOnly, shard.Status));
160157

161158
// Cleanup: Set them all back to READY
162159
await collection.Config.UpdateShardStatus(ShardStatus.Ready, shardNames);
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Serialization;
2+
13
namespace Weaviate.Client.Models;
24

35
/// <summary>
@@ -8,16 +10,19 @@ public enum ShardStatus
810
/// <summary>
911
/// The shard is ready to serve requests.
1012
/// </summary>
13+
[EnumMember(Value = "READY")]
1114
Ready,
1215

1316
/// <summary>
1417
/// The shard is in read-only mode.
1518
/// </summary>
19+
[EnumMember(Value = "READONLY")]
1620
ReadOnly,
1721

1822
/// <summary>
1923
/// The shard is indexing data.
2024
/// </summary>
25+
[EnumMember(Value = "INDEXING")]
2126
Indexing,
2227
}
2328

@@ -32,27 +37,14 @@ public record ShardInfo
3237
public string Name { get; set; } = string.Empty;
3338

3439
/// <summary>
35-
/// Gets or sets the status of the shard (e.g., "READY", "READONLY", "INDEXING").
40+
/// Gets or sets the status of the shard.
3641
/// </summary>
37-
public string Status { get; set; } = string.Empty;
42+
public ShardStatus Status { get; set; } = ShardStatus.Ready;
3843

3944
/// <summary>
4045
/// Gets or sets the size of the vector queue for the shard.
4146
/// </summary>
4247
public int? VectorQueueSize { get; set; }
43-
44-
/// <summary>
45-
/// Gets the parsed status as a ShardStatus enum.
46-
/// Returns null if the status cannot be parsed.
47-
/// </summary>
48-
public ShardStatus? StatusValue =>
49-
Status?.ToUpperInvariant() switch
50-
{
51-
"READY" => ShardStatus.Ready,
52-
"READONLY" => ShardStatus.ReadOnly,
53-
"INDEXING" => ShardStatus.Indexing,
54-
_ => null,
55-
};
5648
}
5749

5850
/// <summary>
@@ -63,18 +55,14 @@ internal static class ShardStatusExtensions
6355
/// <summary>
6456
/// Converts a ShardStatus enum to its string representation for the API.
6557
/// </summary>
66-
internal static string ToApiString(this ShardStatus status)
67-
{
68-
return status switch
69-
{
70-
ShardStatus.Ready => "READY",
71-
ShardStatus.ReadOnly => "READONLY",
72-
ShardStatus.Indexing => "INDEXING",
73-
_ => throw new ArgumentOutOfRangeException(
74-
nameof(status),
75-
status,
76-
"Unknown shard status value"
77-
),
78-
};
79-
}
58+
internal static string ToApiString(this ShardStatus status) =>
59+
WeaviateExtensions.ToEnumMemberString(status);
60+
61+
/// <summary>
62+
/// Parses a string value to a ShardStatus enum.
63+
/// </summary>
64+
internal static ShardStatus ParseStatus(string? value) =>
65+
string.IsNullOrEmpty(value)
66+
? ShardStatus.Ready
67+
: WeaviateExtensions.FromEnumMemberString<ShardStatus>(value);
8068
}

src/Weaviate.Client/Rest/Dto/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Models.ShardInfo ToModel()
117117
return new Models.ShardInfo
118118
{
119119
Name = Name ?? string.Empty,
120-
Status = Status ?? string.Empty,
120+
Status = Models.ShardStatusExtensions.ParseStatus(Status),
121121
VectorQueueSize = VectorQueueSize,
122122
};
123123
}

0 commit comments

Comments
 (0)