|
| 1 | +using Weaviate.Client.Models; |
| 2 | + |
| 3 | +namespace Weaviate.Client.Tests.Integration; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// The object TTL tests class |
| 7 | +/// </summary> |
| 8 | +/// <seealso cref="IntegrationTests"/> |
| 9 | +public partial class ObjectTTL : IntegrationTests |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Initializes this instance |
| 13 | + /// </summary> |
| 14 | + /// <returns>The value task</returns> |
| 15 | + public override async ValueTask InitializeAsync() |
| 16 | + { |
| 17 | + await base.InitializeAsync(); |
| 18 | + |
| 19 | + RequireVersion("1.35.0"); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task Test_ObjectTTL_Creation() |
| 24 | + { |
| 25 | + var collection = await CollectionFactory( |
| 26 | + objectTTLConfig: ObjectTTLConfig.ByCreationTime( |
| 27 | + TimeSpan.FromDays(30), |
| 28 | + filterExpiredObjects: true |
| 29 | + ), |
| 30 | + invertedIndexConfig: new InvertedIndexConfig { IndexTimestamps = true } |
| 31 | + ); |
| 32 | + |
| 33 | + var config = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 34 | + Assert.NotNull(config.ObjectTTLConfig); |
| 35 | + Assert.Equal("_creationTimeUnix", config.ObjectTTLConfig.DeleteOn); |
| 36 | + Assert.Equal(30 * 24 * 3600, config.ObjectTTLConfig.DefaultTTL); |
| 37 | + Assert.True(config.ObjectTTLConfig.FilterExpiredObjects); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task Test_ObjectTTL_UpdateTime() |
| 42 | + { |
| 43 | + var collection = await CollectionFactory( |
| 44 | + objectTTLConfig: ObjectTTLConfig.ByUpdateTime( |
| 45 | + TimeSpan.FromDays(30), |
| 46 | + filterExpiredObjects: true |
| 47 | + ), |
| 48 | + invertedIndexConfig: new InvertedIndexConfig { IndexTimestamps = true } |
| 49 | + ); |
| 50 | + |
| 51 | + var config = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 52 | + Assert.NotNull(config.ObjectTTLConfig); |
| 53 | + Assert.Equal("_lastUpdateTimeUnix", config.ObjectTTLConfig.DeleteOn); |
| 54 | + Assert.True(config.ObjectTTLConfig.FilterExpiredObjects); |
| 55 | + Assert.Equal(30 * 24 * 3600, config.ObjectTTLConfig.DefaultTTL); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task Test_ObjectTTL_CustomProperty() |
| 60 | + { |
| 61 | + var collection = await CollectionFactory( |
| 62 | + properties: new[] |
| 63 | + { |
| 64 | + new Property { Name = "customDate", DataType = DataType.Date }, |
| 65 | + }, |
| 66 | + objectTTLConfig: ObjectTTLConfig.ByDateProperty( |
| 67 | + "customDate", |
| 68 | + -1, |
| 69 | + filterExpiredObjects: false |
| 70 | + ), |
| 71 | + invertedIndexConfig: new InvertedIndexConfig { IndexTimestamps = true } |
| 72 | + ); |
| 73 | + |
| 74 | + var config = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 75 | + Assert.NotNull(config.ObjectTTLConfig); |
| 76 | + Assert.Equal("customDate", config.ObjectTTLConfig.DeleteOn); |
| 77 | + Assert.Equal(-1, config.ObjectTTLConfig.DefaultTTL); |
| 78 | + Assert.False(config.ObjectTTLConfig.FilterExpiredObjects); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public async Task Test_ObjectTTL_Update() |
| 83 | + { |
| 84 | + var collection = await CollectionFactory<object>( |
| 85 | + properties: new[] |
| 86 | + { |
| 87 | + new Property { Name = "customDate", DataType = DataType.Date }, |
| 88 | + new Property { Name = "customDate2", DataType = DataType.Date }, |
| 89 | + }, |
| 90 | + invertedIndexConfig: new InvertedIndexConfig { IndexTimestamps = true } |
| 91 | + ); |
| 92 | + |
| 93 | + var conf = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 94 | + Assert.NotNull(conf.ObjectTTLConfig); |
| 95 | + Assert.False(conf.ObjectTTLConfig.Enabled); |
| 96 | + |
| 97 | + // Update to customDate |
| 98 | + await collection.Config.Update( |
| 99 | + c => c.ObjectTTLConfig.ByDateProperty("customDate", 3600, filterExpiredObjects: true), |
| 100 | + cancellationToken: TestContext.Current.CancellationToken |
| 101 | + ); |
| 102 | + conf = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 103 | + Assert.NotNull(conf.ObjectTTLConfig); |
| 104 | + Assert.Equal("customDate", conf.ObjectTTLConfig.DeleteOn); |
| 105 | + Assert.Equal(3600, conf.ObjectTTLConfig.DefaultTTL); |
| 106 | + Assert.True(conf.ObjectTTLConfig.FilterExpiredObjects); |
| 107 | + |
| 108 | + // Update to update time |
| 109 | + await collection.Config.Update( |
| 110 | + c => c.ObjectTTLConfig.ByUpdateTime(3600, filterExpiredObjects: false), |
| 111 | + cancellationToken: TestContext.Current.CancellationToken |
| 112 | + ); |
| 113 | + conf = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 114 | + Assert.NotNull(conf.ObjectTTLConfig); |
| 115 | + Assert.Equal("_lastUpdateTimeUnix", conf.ObjectTTLConfig.DeleteOn); |
| 116 | + Assert.Equal(3600, conf.ObjectTTLConfig.DefaultTTL); |
| 117 | + Assert.False(conf.ObjectTTLConfig.FilterExpiredObjects); |
| 118 | + |
| 119 | + // Update to creation time |
| 120 | + await collection.Config.Update( |
| 121 | + c => c.ObjectTTLConfig.ByCreationTime(600), |
| 122 | + cancellationToken: TestContext.Current.CancellationToken |
| 123 | + ); |
| 124 | + conf = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 125 | + Assert.NotNull(conf.ObjectTTLConfig); |
| 126 | + Assert.Equal("_creationTimeUnix", conf.ObjectTTLConfig.DeleteOn); |
| 127 | + Assert.Equal(600, conf.ObjectTTLConfig.DefaultTTL); |
| 128 | + Assert.False(conf.ObjectTTLConfig.FilterExpiredObjects); |
| 129 | + |
| 130 | + // Disable TTL |
| 131 | + await collection.Config.Update( |
| 132 | + c => c.ObjectTTLConfig.Disable(), |
| 133 | + cancellationToken: TestContext.Current.CancellationToken |
| 134 | + ); |
| 135 | + conf = await collection.Config.Get(TestContext.Current.CancellationToken); |
| 136 | + Assert.NotNull(conf.ObjectTTLConfig); |
| 137 | + Assert.False(conf.ObjectTTLConfig.Enabled); |
| 138 | + } |
| 139 | +} |
0 commit comments