|
| 1 | +namespace Weaviate.Client.Tests.Integration; |
| 2 | + |
| 3 | +using Weaviate.Client.Models; |
| 4 | + |
| 5 | +public class TestCollectionShards : IntegrationTests |
| 6 | +{ |
| 7 | + private class TestData { } |
| 8 | + |
| 9 | + [Fact] |
| 10 | + public async Task Test_Should_Get_Collection_Shards() |
| 11 | + { |
| 12 | + // Create a collection - it will have at least one shard |
| 13 | + var collection = await CollectionFactory<TestData>( |
| 14 | + name: "ShardsTest", |
| 15 | + properties: [Property.Text("Name")] |
| 16 | + ); |
| 17 | + |
| 18 | + // Act: Get all shards |
| 19 | + var shards = await collection.Config.GetShards( |
| 20 | + cancellationToken: TestContext.Current.CancellationToken |
| 21 | + ); |
| 22 | + |
| 23 | + // Assert |
| 24 | + Assert.NotNull(shards); |
| 25 | + Assert.NotEmpty(shards); |
| 26 | + Assert.All( |
| 27 | + shards, |
| 28 | + shard => |
| 29 | + { |
| 30 | + Assert.NotNull(shard.Name); |
| 31 | + Assert.NotEmpty(shard.Name); |
| 32 | + Assert.NotNull(shard.Status); |
| 33 | + Assert.NotEmpty(shard.Status); |
| 34 | + } |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task Test_Should_Get_Specific_Shard() |
| 40 | + { |
| 41 | + // Arrange: Create collection and get the first shard name |
| 42 | + var collection = await CollectionFactory<TestData>( |
| 43 | + name: "SpecificShardTest", |
| 44 | + properties: [Property.Text("Name")] |
| 45 | + ); |
| 46 | + |
| 47 | + var allShards = await collection.Config.GetShards( |
| 48 | + cancellationToken: TestContext.Current.CancellationToken |
| 49 | + ); |
| 50 | + Assert.NotEmpty(allShards); |
| 51 | + |
| 52 | + var firstShardName = allShards[0].Name; |
| 53 | + |
| 54 | + // Act: Get the specific shard |
| 55 | + var shard = await collection.Config.GetShard( |
| 56 | + firstShardName, |
| 57 | + cancellationToken: TestContext.Current.CancellationToken |
| 58 | + ); |
| 59 | + |
| 60 | + // Assert |
| 61 | + Assert.NotNull(shard); |
| 62 | + Assert.Equal(firstShardName, shard.Name); |
| 63 | + Assert.NotNull(shard.Status); |
| 64 | + Assert.NotEmpty(shard.Status); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task Test_Should_Update_Single_Shard_Status_To_ReadOnly() |
| 69 | + { |
| 70 | + // Arrange: Create collection and get the first shard name |
| 71 | + var collection = await CollectionFactory<TestData>( |
| 72 | + name: "UpdateShardTest", |
| 73 | + properties: [Property.Text("Name")] |
| 74 | + ); |
| 75 | + |
| 76 | + var allShards = await collection.Config.GetShards( |
| 77 | + cancellationToken: TestContext.Current.CancellationToken |
| 78 | + ); |
| 79 | + Assert.NotEmpty(allShards); |
| 80 | + |
| 81 | + var firstShardName = allShards[0].Name; |
| 82 | + |
| 83 | + // Act: Update shard status to READONLY |
| 84 | + var updatedShards = await collection.Config.UpdateShardStatus( |
| 85 | + ShardStatus.ReadOnly, |
| 86 | + firstShardName |
| 87 | + ); |
| 88 | + |
| 89 | + // Assert |
| 90 | + Assert.NotNull(updatedShards); |
| 91 | + Assert.Single(updatedShards); |
| 92 | + Assert.Equal(firstShardName, updatedShards[0].Name); |
| 93 | + Assert.Equal("READONLY", updatedShards[0].Status); |
| 94 | + Assert.Equal(ShardStatus.ReadOnly, updatedShards[0].StatusValue); |
| 95 | + |
| 96 | + // Cleanup: Set it back to READY |
| 97 | + await collection.Config.UpdateShardStatus(ShardStatus.Ready, firstShardName); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public async Task Test_Should_Update_Shard_Status_Back_To_Ready() |
| 102 | + { |
| 103 | + // Arrange: Create collection, get shard, and set it to READONLY |
| 104 | + var collection = await CollectionFactory<TestData>( |
| 105 | + name: "UpdateShardBackTest", |
| 106 | + properties: [Property.Text("Name")] |
| 107 | + ); |
| 108 | + |
| 109 | + var allShards = await collection.Config.GetShards( |
| 110 | + cancellationToken: TestContext.Current.CancellationToken |
| 111 | + ); |
| 112 | + Assert.NotEmpty(allShards); |
| 113 | + |
| 114 | + var firstShardName = allShards[0].Name; |
| 115 | + |
| 116 | + // Set to READONLY first |
| 117 | + await collection.Config.UpdateShardStatus(ShardStatus.ReadOnly, firstShardName); |
| 118 | + |
| 119 | + // Act: Update back to READY |
| 120 | + var updatedShards = await collection.Config.UpdateShardStatus( |
| 121 | + ShardStatus.Ready, |
| 122 | + firstShardName |
| 123 | + ); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Assert.NotNull(updatedShards); |
| 127 | + Assert.Single(updatedShards); |
| 128 | + Assert.Equal(firstShardName, updatedShards[0].Name); |
| 129 | + Assert.Equal("READY", updatedShards[0].Status); |
| 130 | + Assert.Equal(ShardStatus.Ready, updatedShards[0].StatusValue); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task Test_Should_Update_Multiple_Shards_With_Params() |
| 135 | + { |
| 136 | + // Arrange: Create collection with multiple shards if possible |
| 137 | + var collection = await CollectionFactory<TestData>( |
| 138 | + name: "MultipleShardTest", |
| 139 | + properties: [Property.Text("Name")], |
| 140 | + shardingConfig: new ShardingConfig { DesiredCount = 2 } |
| 141 | + ); |
| 142 | + |
| 143 | + var allShards = await collection.Config.GetShards( |
| 144 | + cancellationToken: TestContext.Current.CancellationToken |
| 145 | + ); |
| 146 | + |
| 147 | + // If we only have one shard, just test with that one |
| 148 | + var shardNames = allShards.Select(s => s.Name).ToArray(); |
| 149 | + |
| 150 | + // Act: Update all shards to READONLY |
| 151 | + var updatedShards = await collection.Config.UpdateShardStatus( |
| 152 | + ShardStatus.ReadOnly, |
| 153 | + shardNames |
| 154 | + ); |
| 155 | + |
| 156 | + // Assert |
| 157 | + Assert.NotNull(updatedShards); |
| 158 | + Assert.Equal(shardNames.Length, updatedShards.Count); |
| 159 | + Assert.All(updatedShards, shard => Assert.Equal("READONLY", shard.Status)); |
| 160 | + |
| 161 | + // Cleanup: Set them all back to READY |
| 162 | + await collection.Config.UpdateShardStatus(ShardStatus.Ready, shardNames); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public async Task Test_Should_Throw_When_No_Shard_Names_Provided() |
| 167 | + { |
| 168 | + // Arrange |
| 169 | + var collection = await CollectionFactory<TestData>( |
| 170 | + name: "NoShardNameTest", |
| 171 | + properties: [Property.Text("Name")] |
| 172 | + ); |
| 173 | + |
| 174 | + // Act & Assert |
| 175 | + await Assert.ThrowsAsync<ArgumentException>(async () => |
| 176 | + await collection.Config.UpdateShardStatus(ShardStatus.Ready) |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + [Fact] |
| 181 | + public async Task Test_Should_Throw_When_Shard_Name_Is_Empty() |
| 182 | + { |
| 183 | + // Arrange |
| 184 | + var collection = await CollectionFactory<TestData>( |
| 185 | + name: "EmptyShardNameTest", |
| 186 | + properties: [Property.Text("Name")] |
| 187 | + ); |
| 188 | + |
| 189 | + // Act & Assert |
| 190 | + await Assert.ThrowsAsync<ArgumentException>(async () => |
| 191 | + await collection.Config.GetShard( |
| 192 | + "", |
| 193 | + cancellationToken: TestContext.Current.CancellationToken |
| 194 | + ) |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
0 commit comments