-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support for initialization scripts #11331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
53b3869
e2adfb1
e81a2ac
35b753b
488b5fa
b7ca531
d868b95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class MongoDBContainerTest extends AbstractMongo { | ||
|
|
@@ -38,4 +40,102 @@ void shouldTestDatabaseName() { | |
| assertThat(mongoDBContainer.getReplicaSetUrl(databaseName)).endsWith(databaseName); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldExecuteInitScript() { | ||
| try ( | ||
| MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10") | ||
| .withInitScript("init.js") | ||
| .withStartupTimeout(Duration.ofSeconds(30)) | ||
| ) { | ||
| mongoDB.start(); | ||
| assertThat(mongoDB.isRunning()).isTrue(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldExecuteInitScriptWithEdgeCases() { | ||
| try ( | ||
| MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10") | ||
| .withInitScript("initEdgeCase.js") | ||
| .withEnv("LANG", "C.UTF-8") | ||
| .withEnv("LC_ALL", "C.UTF-8") | ||
| .withStartupTimeout(Duration.ofSeconds(30)) | ||
|
||
| ) { | ||
| mongoDB.start(); | ||
|
|
||
| try ( | ||
| com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create( | ||
| mongoDB.getReplicaSetUrl() | ||
| ) | ||
| ) { | ||
| String expectedComplexName = "test_col_\"_with_specials_!@#%^&*()"; | ||
| String expectedJapaneseName = "日本語 コレクション テスト"; | ||
|
|
||
| com.mongodb.client.MongoDatabase database = client.getDatabase("test"); | ||
|
|
||
| assertThat(database.listCollectionNames()).contains(expectedComplexName, expectedJapaneseName); | ||
|
|
||
| com.mongodb.client.MongoCollection<org.bson.Document> collection = database.getCollection( | ||
| expectedComplexName | ||
| ); | ||
|
|
||
| org.bson.Document doc = collection.find(new org.bson.Document("_id", 1)).first(); | ||
|
|
||
| assertThat(doc).as("Document with _id=1 should exist").isNotNull(); | ||
|
|
||
| assertThat(doc.getString("key_with_quotes")) | ||
| .as("Double quotes should be preserved correctly") | ||
| .isEqualTo("This is a \"double quoted\" string"); | ||
|
|
||
| assertThat(doc.getString("key_with_json_chars")) | ||
| .as("JSON special chars should be treated as plain text") | ||
| .isEqualTo("{ } [ ] : ,"); | ||
|
|
||
| assertThat(doc.getString("description")) | ||
| .as("Japanese text should be preserved correctly") | ||
|
||
| .isEqualTo("特殊記号を含むコレクションへの挿入テスト"); | ||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldExecuteInitScriptWithReplicaSet() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withInitScript("init.js").withReplicaSet()) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldExecuteInitScriptWithReplicaSetConfiguredFirst() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withReplicaSet().withInitScript("init.js")) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } | ||
|
||
|
|
||
| @Test | ||
| void shouldExecuteInitScriptWithSharding() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withInitScript("init.js").withSharding()) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldExecuteInitScriptWithShardingConfiguredFirst() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withSharding().withInitScript("init.js")) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } | ||
|
||
|
|
||
| private void assertInitScriptExecuted(MongoDBContainer mongo) { | ||
| try (com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongo.getReplicaSetUrl())) { | ||
| assertThat(client.getDatabase("test").listCollectionNames()) | ||
| .as("Check if init.js created the collection") | ||
| .contains("test_collection"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| db.createCollection("test_collection"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| var complexCollectionName = 'test_col_"_with_specials_!@#%^&*()'; | ||
|
|
||
| db.createCollection(complexCollectionName); | ||
|
|
||
| var japaneseCollectionName = "日本語 コレクション テスト"; | ||
|
|
||
| db.createCollection(japaneseCollectionName); | ||
|
|
||
| db.getCollection(complexCollectionName).insertOne({ | ||
| "_id": 1, | ||
| "key_with_quotes": "This is a \"double quoted\" string", | ||
| "key_with_json_chars": "{ } [ ] : ,", | ||
| "description": "特殊記号を含むコレクションへの挿入テスト" | ||
| }); | ||
|
|
||
| print("Initialization completed: " + complexCollectionName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this required?