|
3 | 3 | using FluentAssertions.Equivalency; |
4 | 4 | using LexBoxApi.Services; |
5 | 5 | using LexData; |
| 6 | +using LinqToDB.EntityFrameworkCore; |
6 | 7 | using Microsoft.EntityFrameworkCore; |
7 | 8 | using Microsoft.Extensions.DependencyInjection; |
8 | 9 | using SIL.Harmony.Core; |
@@ -61,6 +62,58 @@ private async IAsyncEnumerable<ServerCommit> AsAsync(IEnumerable<ServerCommit> c |
61 | 62 | } |
62 | 63 | } |
63 | 64 |
|
| 65 | + //previously the value of the Change property was serialized twice, this test ensures that we can still |
| 66 | + //pull those commits out of the database |
| 67 | + [Fact] |
| 68 | + public async Task CanQueryOldCommits() |
| 69 | + { |
| 70 | + var projectId = await _lexBoxDbContext.Projects.Select(p => p.Id).FirstOrDefaultAsync(); |
| 71 | + var context = _lexBoxDbContext.CreateLinqToDBContext(); |
| 72 | + var table = LinqToDB.DataExtensions.GetTable<ServerCommit>(context); |
| 73 | + var commitId = Guid.NewGuid(); |
| 74 | + var changeEntity = new ChangeEntity<ServerJsonChange> |
| 75 | + { |
| 76 | + Index = 0, |
| 77 | + CommitId = commitId, |
| 78 | + EntityId = Guid.NewGuid(), |
| 79 | + Change = new() |
| 80 | + { |
| 81 | + Type = "MyTestType", |
| 82 | + ExtensionData = new Dictionary<string, JsonElement>() |
| 83 | + { |
| 84 | + ["MyTestProperty"] = JsonSerializer.SerializeToElement("MyTestValue") |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + var changeEntityJson = JsonSerializer.SerializeToNode(changeEntity); |
| 89 | + changeEntityJson.Should().NotBeNull(); |
| 90 | + //the old format stored json in json, this is emulating that. |
| 91 | + changeEntityJson["Change"] = changeEntityJson["Change"]?.ToJsonString(); |
| 92 | + var jsonPayload = changeEntityJson.ToJsonString(); |
| 93 | + var inlineSql = $"'[{jsonPayload}]'::jsonb"; |
| 94 | + //insert a new server commit, manually specifying the value for ChangeEntities so it will match the old format. |
| 95 | + await LinqToDB.LinqExtensions.InsertAsync(table, () => new ServerCommit(commitId) |
| 96 | + { |
| 97 | + Id = commitId, |
| 98 | + ClientId = Guid.NewGuid(), |
| 99 | + HybridDateTime = new HybridDateTime(DateTimeOffset.UtcNow, 0) |
| 100 | + { |
| 101 | + DateTime = DateTimeOffset.UtcNow, |
| 102 | + Counter = 0 |
| 103 | + }, |
| 104 | + ProjectId = projectId, |
| 105 | + Metadata = new CommitMetadata(), |
| 106 | + ChangeEntities = LinqToDB.Sql.Expr<List<ChangeEntity<ServerJsonChange>>>(inlineSql) |
| 107 | + }); |
| 108 | + var commits = await _lexBoxDbContext.CrdtCommits(projectId).ToArrayAsync(); |
| 109 | + var actualCommit = commits.Should().ContainSingle(c => c.Id == commitId).Subject; |
| 110 | + actualCommit.ChangeEntities.Should().BeEquivalentTo([changeEntity], |
| 111 | + options => options |
| 112 | + .Using<JsonElement>(ctx => ctx.Subject.ToString().Should().Be(ctx.Expectation.ToString())) |
| 113 | + .WhenTypeIs<JsonElement>() |
| 114 | + ); |
| 115 | + } |
| 116 | + |
64 | 117 |
|
65 | 118 | [Fact] |
66 | 119 | public async Task CanAddCommits() |
|
0 commit comments