|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq; |
| 5 | +using Dapper; |
| 6 | +using KnowledgeBaseServer.Tests.Data; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Shouldly; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace KnowledgeBaseServer.Tests.DatabaseMigrationTests; |
| 12 | + |
| 13 | +// ReSharper disable once InconsistentNaming |
| 14 | +public class MigrationTests_V0_2_0 : MigrationTest |
| 15 | +{ |
| 16 | + private readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(b => |
| 17 | + b.AddConsole().SetMinimumLevel(LogLevel.Trace) |
| 18 | + ); |
| 19 | + |
| 20 | + /// <inheritdoc /> |
| 21 | + public MigrationTests_V0_2_0() |
| 22 | + : base("v0.2.0") { } |
| 23 | + |
| 24 | + [Fact(DisplayName = "v0.2.0 database should migrate to latest version")] |
| 25 | + public void ShouldMigrateToLatest() |
| 26 | + { |
| 27 | + // arrange |
| 28 | + // Query data in the original v0.1.0 format. |
| 29 | + List<TopicV020> originalTopics; |
| 30 | + List<MemoryContextV020> originalMemoryContexts; |
| 31 | + List<MemoryNodeV020> originalMemoryNodes; |
| 32 | + List<MemoryEdgeV020> originalMemoryEdges; |
| 33 | + List<MemorySearchV020> originalMemorySearches; |
| 34 | + |
| 35 | + using (var arrangeConnection = ConnectionString.CreateConnection()) |
| 36 | + { |
| 37 | + originalTopics = arrangeConnection.Query<TopicV020>("select * from topics").AsList(); |
| 38 | + originalMemoryContexts = arrangeConnection |
| 39 | + .Query<MemoryContextV020>("select * from memory_contexts") |
| 40 | + .AsList(); |
| 41 | + originalMemoryNodes = arrangeConnection.Query<MemoryNodeV020>("select * from memory_nodes").AsList(); |
| 42 | + originalMemoryEdges = arrangeConnection.Query<MemoryEdgeV020>("select * from memory_edges").AsList(); |
| 43 | + originalMemorySearches = arrangeConnection.Query<MemorySearchV020>("select * from memory_search").AsList(); |
| 44 | + } |
| 45 | + |
| 46 | + // act |
| 47 | + var result = Migrator.ApplyMigrations(_loggerFactory, ConnectionString); |
| 48 | + |
| 49 | + // assert |
| 50 | + result.ShouldBeTrue(); |
| 51 | + |
| 52 | + // Validate that the data migrated correctly. |
| 53 | + using var connection = ConnectionString.CreateConnection(); |
| 54 | + |
| 55 | + var migratedTopics = connection.GetTopics(); |
| 56 | + foreach (var originalTopic in originalTopics) |
| 57 | + { |
| 58 | + migratedTopics |
| 59 | + .FirstOrDefault(x => x.Id == originalTopic.Id) |
| 60 | + .ShouldNotBeNull() |
| 61 | + .ShouldSatisfyAllConditions( |
| 62 | + x => x.Created.ShouldBe(originalTopic.Created), |
| 63 | + x => x.Name.ShouldBe(originalTopic.Name) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + var migratedMemoryContexts = connection.GetMemoryContexts(); |
| 68 | + foreach (var originalMemoryContext in originalMemoryContexts) |
| 69 | + { |
| 70 | + migratedMemoryContexts |
| 71 | + .FirstOrDefault(x => x.Id == originalMemoryContext.Id) |
| 72 | + .ShouldNotBeNull() |
| 73 | + .ShouldSatisfyAllConditions( |
| 74 | + x => x.Created.ShouldBe(originalMemoryContext.Created), |
| 75 | + x => x.Value.ShouldBe(originalMemoryContext.Value) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + var migratedMemoryNodes = connection.GetMemoryNodes(); |
| 80 | + foreach (var originalMemoryNode in originalMemoryNodes) |
| 81 | + { |
| 82 | + migratedMemoryNodes |
| 83 | + .FirstOrDefault(x => x.Id == originalMemoryNode.Id) |
| 84 | + .ShouldNotBeNull() |
| 85 | + .ShouldSatisfyAllConditions( |
| 86 | + x => x.Created.ShouldBe(originalMemoryNode.Created), |
| 87 | + x => x.TopicId.ShouldBe(originalMemoryNode.TopicId), |
| 88 | + x => x.ContextId.ShouldBe(originalMemoryNode.ContextId), |
| 89 | + x => x.Content.ShouldBe(originalMemoryNode.Content), |
| 90 | + x => x.Outdated.ShouldBe(originalMemoryNode.Outdated), |
| 91 | + x => x.OutdatedReason.ShouldBe(originalMemoryNode.OutdatedReason), |
| 92 | + x => x.Importance.ShouldBe(originalMemoryNode.Importance) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + // Validate that the data migrated correctly for memory edges. |
| 97 | + var migratedMemoryEdges = connection.GetMemoryEdges(); |
| 98 | + foreach (var originalMemoryEdge in originalMemoryEdges) |
| 99 | + { |
| 100 | + migratedMemoryEdges |
| 101 | + .FirstOrDefault(x => |
| 102 | + x.SourceMemoryNodeId == originalMemoryEdge.SourceMemoryNodeId |
| 103 | + && x.TargetMemoryNodeId == originalMemoryEdge.TargetMemoryNodeId |
| 104 | + ) |
| 105 | + .ShouldNotBeNull() |
| 106 | + .ShouldSatisfyAllConditions(x => x.Created.ShouldBe(originalMemoryEdge.Created)); |
| 107 | + } |
| 108 | + |
| 109 | + // Validate that the data migrated correctly for memory searches. |
| 110 | + var migratedMemorySearches = connection.GetMemorySearches(); |
| 111 | + foreach (var originalMemorySearch in originalMemorySearches) |
| 112 | + { |
| 113 | + migratedMemorySearches |
| 114 | + .FirstOrDefault(x => x.MemoryNodeId == originalMemorySearch.MemoryNodeId) |
| 115 | + .ShouldNotBeNull() |
| 116 | + .ShouldSatisfyAllConditions( |
| 117 | + x => x.MemoryContent.ShouldBe(originalMemorySearch.MemoryContent), |
| 118 | + x => x.MemoryContext.ShouldBe(originalMemorySearch.MemoryContext) |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private sealed record TopicV020(Guid Id, DateTimeOffset Created, string Name); |
| 124 | + |
| 125 | + private sealed record MemoryContextV020(Guid Id, DateTimeOffset Created, string Value); |
| 126 | + |
| 127 | + private sealed record MemoryNodeV020( |
| 128 | + Guid Id, |
| 129 | + DateTimeOffset Created, |
| 130 | + Guid TopicId, |
| 131 | + Guid? ContextId, |
| 132 | + string Content, |
| 133 | + DateTimeOffset? Outdated, |
| 134 | + string? OutdatedReason, |
| 135 | + double Importance |
| 136 | + ); |
| 137 | + |
| 138 | + private sealed record MemoryEdgeV020(Guid SourceMemoryNodeId, Guid TargetMemoryNodeId, DateTimeOffset Created); |
| 139 | + |
| 140 | + // For some reason Dapper can't deserialize this correctly as a record. |
| 141 | + [SuppressMessage("Minor Code Smell", "S3459:Unassigned members should be removed")] |
| 142 | + [SuppressMessage("Major Code Smell", "S1144:Unused private types or members should be removed")] |
| 143 | + private sealed class MemorySearchV020 |
| 144 | + { |
| 145 | + public required Guid MemoryNodeId { get; init; } |
| 146 | + |
| 147 | + public required string MemoryContent { get; init; } |
| 148 | + |
| 149 | + public string? MemoryContext { get; init; } |
| 150 | + } |
| 151 | +} |
0 commit comments