Skip to content
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

Allow users to specify a mongo database name #2876

Closed
doctorpangloss opened this issue Jun 14, 2020 · 7 comments
Closed

Allow users to specify a mongo database name #2876

doctorpangloss opened this issue Jun 14, 2020 · 7 comments

Comments

@doctorpangloss
Copy link

private static final String MONGODB_DATABASE_NAME_DEFAULT = "test";

@pivovarit
Copy link
Contributor

Why do you need it?

You can just access/create a custom instance using MongoClient#getDatabase()

@silaev
Copy link
Contributor

silaev commented Jul 8, 2020

When one uses Testcontainer’s reuse feature, a framework and a database per a test as a way to separate(isolate) data, then specifying a database might be helpful.
For instance, in the Spring Data MongoDB a database name is bind to the MongoTemplate/
ReactiveMongoTemplate and thus it’s a tedious process to change a database name for above-mentioned testing scenario.

Given that I’ll submit a PR to add a withDatabaseName method for the MongoDBContainer. If the withDatabaseName mothod is not called, then a database name will default to "test" as now.

silaev added a commit to silaev/testcontainers-java that referenced this issue Jul 8, 2020
silaev added a commit to silaev/testcontainers-java that referenced this issue Jul 9, 2020
@stale
Copy link

stale bot commented Oct 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

@stale stale bot added the stale label Oct 11, 2020
@silaev
Copy link
Contributor

silaev commented Oct 11, 2020

Updated a corresponding PR

@stale stale bot removed the stale label Oct 11, 2020
@CodeGTDLearn
Copy link

CodeGTDLearn commented Jul 10, 2021

@silaev How Can I solve this problem? In order to isolates each test from my test-class (my goal is to use PArallel tests, but for that I need isolates each test). Consequently, I need a different database-name in each test. For that, I need to change the TestContainer-Databasename. I am trying to use getReplicaSetUrl(String databaseName), but it is not working. How can I solve this problem?

@silaev
Copy link
Contributor

silaev commented Jul 12, 2021

@silaev How Can I solve this problem? In order to isolates each test from my test-class (my goal is to use PArallel tests, but for that I need isolates each test). Consequently, I need a different database-name in each test. For that, I need to change the TestContainer-Databasename. I am trying to use getReplicaSetUrl(String databaseName), but it is not working. How can I solve this problem?

@PauloPortfolio Do you use a framework? Separation per the method of the class might not be so easy depending on a framework.

The key idea is to provide a database name to a MongoDB client and enable the Testcontainers' reuse feature. Here is an example for the Testcontainers' MongoDB Module and Spring Data MongoDB to separate test classes (not methods!) per database names:

  1. Enable reuse in .testcontainers.properties files by adding testcontainers.reuse.enable=true
  2. Create a test class, for example:
@DataMongoTest
@ContextConfiguration(initializers = IT1Test.Initializer.class)
class IT1Test {
  private static final MongoDBContainer MONGO_DB_CONTAINER =
    new MongoDBContainer(DockerImageName.parse("mongo:4.4.2"))
      .withReuse(true);
  @Autowired
  private ProductDao productDao;

  @BeforeAll
  static void setUpAll() {
    MONGO_DB_CONTAINER.start();
  }

  @AfterAll
  static void tearDownAll() {
    if (!MONGO_DB_CONTAINER.isShouldBeReused()) {
      MONGO_DB_CONTAINER.stop();
    }
  }

  @Test
  void test1() {
    Product product1 = ProductTestUtil.generateProduct(120589L, "AAA", Brand.DOLCE, BigDecimal.valueOf(9), 9, Size.SIZE_50);
    Product product2 = ProductTestUtil.generateProduct(120590L, "BBB", Brand.DOLCE, BigDecimal.valueOf(15.69), 6, Size.SIZE_100);
    Product product3 = ProductTestUtil.generateProduct(120591L, "CCC", Brand.ENGLISH_LAUNDRY, BigDecimal.valueOf(55.12), 3, Size.SIZE_100);
    StepVerifier.create(productDao.insert(Flux.just(product1, product2, product3)))
      .expectNextCount(3L)
      .verifyComplete();
  }

  static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
      TestPropertyValues.of(
        String.format(
          "spring.data.mongodb.uri: %s",
          MONGO_DB_CONTAINER.getReplicaSetUrl("my-db" + UUID.randomUUID().getMostSignificantBits())
        )
      ).applyTo(configurableApplicationContext);
    }
  }
}
  1. Run test 4 times
  2. Expect 4 new databases via the following mongo Shell command: db.getMongo().getDBNames(), example:
[
        "admin",
        "config",
        "local",
        "my-db-5289724188915119626",
        "my-db-6086375598922775794",
        "my-db-79157823489553256",
        "my-db5367856646571576153"
]
  1. Expect collections, for example via the following mongo Shell command: db.getMongo().getDB("my-db-5289724188915119626").product.count()

@eddumelendez
Copy link
Member

Closing this issue due to it was implemented via #2980

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants