Skip to content

Commit 3222b14

Browse files
committed
v0.4.0 migration tests
1 parent 3a93c9b commit 3222b14

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Dapper;
5+
using KnowledgeBaseServer.Tests.Data;
6+
using Shouldly;
7+
using Xunit;
8+
9+
namespace KnowledgeBaseServer.Tests.DatabaseMigrationTests;
10+
11+
// ReSharper disable once InconsistentNaming
12+
public class MigrationTests_V0_4_0 : MigrationTest
13+
{
14+
/// <inheritdoc />
15+
public MigrationTests_V0_4_0(ITestOutputHelper outputHelper)
16+
: base("v0.4.0", outputHelper) { }
17+
18+
[Fact(DisplayName = "v0.4.0 database should migrate to latest version")]
19+
public void ShouldMigrateToLatest()
20+
{
21+
// arrange
22+
// Query data in the original v0.4.0 format.
23+
List<TopicV040> originalTopics;
24+
List<MemoryNodeV040> originalMemoryNodes;
25+
List<MemoryEdgeV040> originalMemoryEdges;
26+
List<MemorySearchV040> originalMemorySearches;
27+
28+
using (var arrangeConnection = ConnectionString.CreateConnection())
29+
{
30+
originalTopics = arrangeConnection.Query<TopicV040>("select * from topics").AsList();
31+
originalMemoryNodes = arrangeConnection.Query<MemoryNodeV040>("select * from memory_nodes").AsList();
32+
originalMemoryEdges = arrangeConnection.Query<MemoryEdgeV040>("select * from memory_edges").AsList();
33+
originalMemorySearches = arrangeConnection.Query<MemorySearchV040>("select * from memory_search").AsList();
34+
}
35+
36+
// act
37+
var result = Migrator.ApplyMigrations(LogFactory, ConnectionString);
38+
39+
// assert
40+
result.ShouldBeTrue();
41+
42+
originalTopics.ShouldNotBeEmpty();
43+
originalMemoryNodes.ShouldNotBeEmpty();
44+
originalMemoryEdges.ShouldNotBeEmpty();
45+
originalMemorySearches.ShouldNotBeEmpty();
46+
47+
// Validate that the data migrated correctly.
48+
using var connection = ConnectionString.CreateConnection();
49+
50+
var migratedTopics = connection.GetTopics();
51+
foreach (var originalTopic in originalTopics)
52+
{
53+
migratedTopics
54+
.FirstOrDefault(x => x.Id == originalTopic.Id)
55+
.ShouldNotBeNull()
56+
.ShouldSatisfyAllConditions(
57+
x => x.Created.ShouldBe(originalTopic.Created),
58+
x => x.Name.ShouldBe(originalTopic.Name)
59+
);
60+
}
61+
62+
var migratedMemoryNodes = connection.GetMemoryNodes();
63+
foreach (var originalMemoryNode in originalMemoryNodes)
64+
{
65+
migratedMemoryNodes
66+
.FirstOrDefault(x => x.Id == originalMemoryNode.Id)
67+
.ShouldNotBeNull()
68+
.ShouldSatisfyAllConditions(
69+
x => x.Created.ShouldBe(originalMemoryNode.Created),
70+
x => x.TopicId.ShouldBe(originalMemoryNode.TopicId),
71+
x => x.Content.ShouldBe(originalMemoryNode.Content),
72+
x => x.Context.ShouldBe(originalMemoryNode.Context),
73+
x => x.Importance.ShouldBe(originalMemoryNode.Importance),
74+
x => x.Outdated.ShouldBe(originalMemoryNode.Outdated),
75+
x => x.OutdatedReason.ShouldBe(originalMemoryNode.OutdatedReason)
76+
);
77+
}
78+
79+
// Validate that the data migrated correctly for memory edges.
80+
var migratedMemoryEdges = connection.GetMemoryEdges();
81+
foreach (var originalMemoryEdge in originalMemoryEdges)
82+
{
83+
migratedMemoryEdges
84+
.FirstOrDefault(x =>
85+
x.SourceMemoryNodeId == originalMemoryEdge.SourceMemoryNodeId
86+
&& x.TargetMemoryNodeId == originalMemoryEdge.TargetMemoryNodeId
87+
)
88+
.ShouldNotBeNull()
89+
.ShouldSatisfyAllConditions(x => x.Created.ShouldBe(originalMemoryEdge.Created));
90+
}
91+
92+
// Validate that the data migrated correctly for memory searches.
93+
var migratedMemorySearches = connection.GetMemorySearches();
94+
foreach (var originalMemorySearch in originalMemorySearches)
95+
{
96+
migratedMemorySearches
97+
.FirstOrDefault(x => x.MemoryNodeId == originalMemorySearch.MemoryNodeId)
98+
.ShouldNotBeNull()
99+
.ShouldSatisfyAllConditions(
100+
x => x.MemoryContent.ShouldBe(originalMemorySearch.MemoryContent),
101+
x => x.MemoryContext.ShouldBe(originalMemorySearch.MemoryContext)
102+
);
103+
}
104+
}
105+
106+
#pragma warning disable S1144 // Unused private types or members should be removed
107+
#pragma warning disable S3459 // Unassigned members should be removed
108+
109+
private sealed record TopicV040(Guid Id, DateTimeOffset Created, string Name);
110+
111+
private sealed class MemoryNodeV040
112+
{
113+
public Guid Id { get; init; }
114+
115+
public DateTimeOffset Created { get; init; }
116+
117+
public Guid TopicId { get; init; }
118+
119+
public required string Content { get; init; }
120+
121+
public string? Context { get; init; }
122+
123+
public DateTimeOffset? Outdated { get; init; }
124+
125+
public string? OutdatedReason { get; init; }
126+
127+
public double Importance { get; init; }
128+
}
129+
130+
private sealed record MemoryEdgeV040(Guid SourceMemoryNodeId, Guid TargetMemoryNodeId, DateTimeOffset Created);
131+
132+
private sealed class MemorySearchV040
133+
{
134+
public required Guid MemoryNodeId { get; init; }
135+
136+
public required string MemoryContent { get; init; }
137+
138+
public string? MemoryContext { get; init; }
139+
}
140+
141+
#pragma warning restore S1144, S3459
142+
}
Binary file not shown.

0 commit comments

Comments
 (0)