Class: Testcontainers::MongoContainer
- Inherits:
-
DockerContainer
- Object
- DockerContainer
- Testcontainers::MongoContainer
- Defined in:
- lib/testcontainers/mongo.rb
Overview
MongoContainer class is used to manage containers that runs a Mongo databese
Constant Summary collapse
- MONGO_DEFAULT_PORT =
Default port used by the container
27017- MONGO_DEFAULT_IMAGE =
Default image used by the container
"mongo:latest"- MONGO_DEFAULT_USERNAME =
"test"- MONGO_DEFAULT_PASSWORD =
"test"- MONGO_DEFAULT_DATABASE =
"test"
Instance Attribute Summary collapse
-
#database ⇒ String
readonly
used by the container.
-
#password ⇒ String
readonly
used by the container.
-
#username ⇒ String
readonly
used by the container.
Instance Method Summary collapse
-
#initialize(image = MONGO_DEFAULT_IMAGE, username: nil, password: nil, database: nil, **kwargs) ⇒ MongoContainer
constructor
Initializes a new instance of MongoContainer.
-
#mongo_url(protocol: "mongodb", username: nil, password: nil, database: nil, options: {}) ⇒ String
(also: #database_url)
Returns the database url (e.g. mongodb://user:password@host:port/database).
-
#port ⇒ Integer
Returns the port used by the container.
-
#start ⇒ MongoContainer
Starts the container.
-
#with_database(database) ⇒ MongoContainer
Sets the database to use.
-
#with_password(password) ⇒ MongoContainer
Sets the password to use.
-
#with_username(username) ⇒ MongoContainer
Sets the username to use.
Constructor Details
#initialize(image = MONGO_DEFAULT_IMAGE, username: nil, password: nil, database: nil, **kwargs) ⇒ MongoContainer
Initializes a new instance of MongoContainer
32 33 34 35 36 37 38 39 |
# File 'lib/testcontainers/mongo.rb', line 32 def initialize(image = MONGO_DEFAULT_IMAGE, username: nil, password: nil, database: nil, **kwargs) super(image, **kwargs) @username = username || ENV.fetch("MONGO_USERNAME", MONGO_DEFAULT_USERNAME) @password = password || ENV.fetch("MONGO_PASSWORD", MONGO_DEFAULT_PASSWORD) @database = database || ENV.fetch("MONGO_DATABASE", MONGO_DEFAULT_DATABASE) @healthcheck ||= add_healthcheck() @wait_for ||= add_wait_for(:healthcheck) end |
Instance Attribute Details
#database ⇒ String (readonly)
used by the container
10 11 12 |
# File 'lib/testcontainers/mongo.rb', line 10 def database @database end |
#password ⇒ String (readonly)
used by the container
10 11 12 |
# File 'lib/testcontainers/mongo.rb', line 10 def password @password end |
#username ⇒ String (readonly)
used by the container
10 11 12 |
# File 'lib/testcontainers/mongo.rb', line 10 def username @username end |
Instance Method Details
#mongo_url(protocol: "mongodb", username: nil, password: nil, database: nil, options: {}) ⇒ String Also known as: database_url
Returns the database url (e.g. mongodb://user:password@host:port/database)
65 66 67 68 69 70 71 72 |
# File 'lib/testcontainers/mongo.rb', line 65 def mongo_url(protocol: "mongodb", username: nil, password: nil, database: nil, options: {}) database ||= @database username ||= @username password ||= @password query_string = .empty? ? "" : "?#{URI.encode_www_form()}" "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}" end |
#port ⇒ Integer
Returns the port used by the container
53 54 55 |
# File 'lib/testcontainers/mongo.rb', line 53 def port MONGO_DEFAULT_PORT end |
#start ⇒ MongoContainer
Starts the container
44 45 46 47 48 |
# File 'lib/testcontainers/mongo.rb', line 44 def start with_exposed_ports(port) _configure super end |
#with_database(database) ⇒ MongoContainer
Sets the database to use
80 81 82 83 |
# File 'lib/testcontainers/mongo.rb', line 80 def with_database(database) @database = database self end |
#with_password(password) ⇒ MongoContainer
Sets the password to use
98 99 100 101 |
# File 'lib/testcontainers/mongo.rb', line 98 def with_password(password) @password = password self end |
#with_username(username) ⇒ MongoContainer
Sets the username to use
89 90 91 92 |
# File 'lib/testcontainers/mongo.rb', line 89 def with_username(username) @password = password self end |