Skip to content

Commit a16afd8

Browse files
committed
Add migration integration test
1 parent 551db97 commit a16afd8

4 files changed

Lines changed: 116 additions & 62 deletions

File tree

docker-compose.mongo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
# then hang forever so the container does not exit
1414
command:
1515
[
16-
'/bin/sh',
17-
'-c',
18-
'mongod --profile=2 --replSet myRS --bind_ip 0.0.0.0 & sleep 2s; mongosh --host localhost:27017 --eval '' config = { "_id" : "myRS", "members" : [{"_id" : 0,"host" : "mongo:27017"}] }; rs.initiate(config, { force: true }); '' ; sleep infinity'
16+
"/bin/sh",
17+
"-c",
18+
'mongod --profile=2 --replSet myRS --bind_ip 0.0.0.0 & sleep 2s; mongosh --host localhost:27017 --eval '' config = { "_id" : "myRS", "members" : [{"_id" : 0,"host" : "localhost:27017"}] }; rs.initiate(config, { force: true }); '' ; sleep infinity',
1919
]

src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -61,61 +61,7 @@ await c.Aggregate()
6161
.AppendStage<BsonDocument>(new BsonDocument("$unset", "engine"))
6262
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
6363
.ToListAsync();
64-
65-
// migrate by adding TargetQuoteConvention field populated from analysis field
66-
await c.Aggregate()
67-
.Match(Builders<Build>.Filter.Exists(b => b.TargetQuoteConvention, false))
68-
.Match(Builders<Build>.Filter.Exists("analysis"))
69-
.AppendStage<BsonDocument>(
70-
new BsonDocument(
71-
"$set",
72-
new BsonDocument(
73-
"targetQuoteConvention",
74-
new BsonDocument(
75-
"$ifNull",
76-
new BsonArray()
77-
{
78-
new BsonDocument(
79-
"$first",
80-
new BsonDocument(
81-
"$map",
82-
new BsonDocument
83-
{
84-
{
85-
"input",
86-
new BsonDocument(
87-
"$filter",
88-
new BsonDocument
89-
{
90-
{ "input", "$analysis" },
91-
{ "as", "a" },
92-
{
93-
"cond",
94-
new BsonDocument(
95-
"$ne",
96-
new BsonArray
97-
{
98-
"$$a.targetQuoteConvention",
99-
""
100-
}
101-
)
102-
}
103-
}
104-
)
105-
},
106-
{ "as", "a" },
107-
{ "in", "$$a.targetQuoteConvention" }
108-
}
109-
)
110-
),
111-
""
112-
}
113-
)
114-
)
115-
)
116-
)
117-
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
118-
.ToListAsync();
64+
await MongoMigrations.MigrateTargetQuoteConvention(c);
11965
}
12066
);
12167
configurator.AddRepository<Pretranslation>(
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
4+
namespace Serval.Translation.Configuration;
5+
6+
public class MongoMigrations
7+
{
8+
public static async Task MigrateTargetQuoteConvention(IMongoCollection<Build> c)
9+
{
10+
// migrate by adding TargetQuoteConvention field populated from analysis field
11+
await c.Aggregate()
12+
.Match(Builders<Build>.Filter.Exists(b => b.TargetQuoteConvention, false))
13+
.Match(Builders<Build>.Filter.Exists("analysis"))
14+
.AppendStage<BsonDocument>(
15+
new BsonDocument(
16+
"$set",
17+
new BsonDocument(
18+
"targetQuoteConvention",
19+
new BsonDocument(
20+
"$ifNull",
21+
new BsonArray()
22+
{
23+
new BsonDocument(
24+
"$first",
25+
new BsonDocument(
26+
"$map",
27+
new BsonDocument
28+
{
29+
{
30+
"input",
31+
new BsonDocument(
32+
"$filter",
33+
new BsonDocument
34+
{
35+
{ "input", "$analysis" },
36+
{ "as", "a" },
37+
{
38+
"cond",
39+
new BsonDocument(
40+
"$ne",
41+
new BsonArray { "$$a.targetQuoteConvention", "" }
42+
)
43+
}
44+
}
45+
)
46+
},
47+
{ "as", "a" },
48+
{ "in", "$$a.targetQuoteConvention" }
49+
}
50+
)
51+
),
52+
""
53+
}
54+
)
55+
)
56+
)
57+
)
58+
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
59+
.ToListAsync();
60+
}
61+
}

src/Serval/test/Serval.ApiServer.IntegrationTests/TranslationEngineTests.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Google.Protobuf.WellKnownTypes;
2+
using Serval.Translation.Configuration;
23
using Serval.Translation.Models;
34
using Serval.Translation.V1;
45
using SIL.ServiceToolkit.Services;
@@ -2459,6 +2460,52 @@ await corporaClient.UpdateAsync(
24592460
Assert.That(newEngine2.ParallelCorpora[0].TargetCorpora[0].Files.Count, Is.EqualTo(1));
24602461
}
24612462

2463+
[Test]
2464+
public async Task MongoMigration_TargetQuoteConvention()
2465+
{
2466+
await _env.Builds.InsertAsync(
2467+
new Build()
2468+
{
2469+
Id = "111111111111111111111111",
2470+
EngineRef = NMT_ENGINE1_ID,
2471+
Owner = "client1",
2472+
Analysis =
2473+
[
2474+
new Shared.Models.ParallelCorpusAnalysis()
2475+
{
2476+
ParallelCorpusRef = "111111111111111111111112",
2477+
TargetQuoteConvention = ""
2478+
},
2479+
new Shared.Models.ParallelCorpusAnalysis()
2480+
{
2481+
ParallelCorpusRef = "111111111111111111111113",
2482+
TargetQuoteConvention = "standard_english"
2483+
}
2484+
]
2485+
}
2486+
);
2487+
2488+
Build? unmigratedBuild = await _env.Builds.GetAsync(b => b.Id == "111111111111111111111111");
2489+
Assert.That(unmigratedBuild, Is.Not.Null);
2490+
Assert.Multiple(() =>
2491+
{
2492+
Assert.That(unmigratedBuild.Analysis, Has.Count.EqualTo(2));
2493+
Assert.That(unmigratedBuild.TargetQuoteConvention, Is.Null);
2494+
});
2495+
2496+
await MongoMigrations.MigrateTargetQuoteConvention(
2497+
_env.MongoClient.GetDatabase("serval_test").GetCollection<Build>("translation.builds")
2498+
);
2499+
2500+
Build? migratedBuild = await _env.Builds.GetAsync(b => b.Id == "111111111111111111111111");
2501+
Assert.That(migratedBuild, Is.Not.Null);
2502+
Assert.Multiple(() =>
2503+
{
2504+
Assert.That(migratedBuild.Analysis, Has.Count.EqualTo(2));
2505+
Assert.That(migratedBuild.TargetQuoteConvention, Is.EqualTo("standard_english"));
2506+
});
2507+
}
2508+
24622509
[TearDown]
24632510
public void TearDown()
24642511
{
@@ -2468,11 +2515,11 @@ public void TearDown()
24682515
private class TestEnvironment : DisposableBase
24692516
{
24702517
private readonly IServiceScope _scope;
2471-
private readonly MongoClient _mongoClient;
2518+
public readonly MongoClient MongoClient;
24722519

24732520
public TestEnvironment()
24742521
{
2475-
_mongoClient = new MongoClient();
2522+
MongoClient = new MongoClient();
24762523
ResetDatabases();
24772524

24782525
Factory = new ServalWebApplicationFactory();
@@ -2795,8 +2842,8 @@ public CorporaClient CreateCorporaClient()
27952842

27962843
public void ResetDatabases()
27972844
{
2798-
_mongoClient.DropDatabase("serval_test");
2799-
_mongoClient.DropDatabase("serval_test_jobs");
2845+
MongoClient.DropDatabase("serval_test");
2846+
MongoClient.DropDatabase("serval_test_jobs");
28002847
}
28012848

28022849
private static IFileSystem CreateFileSystem(IServiceProvider sp)

0 commit comments

Comments
 (0)