Class: Testcontainers::PostgresContainer
- Inherits:
-
DockerContainer
- Object
- DockerContainer
- Testcontainers::PostgresContainer
- Defined in:
- lib/testcontainers/postgres.rb
Overview
PostgresContainer class is used to manage containers that runs a PostgresQL database
Constant Summary collapse
- POSTGRES_DEFAULT_PORT =
Default port used by the container
5432
- POSTGRES_DEFAULT_IMAGE =
Default image used by the container
"postgres:latest"
- POSTGRES_DEFAULT_USERNAME =
"test"
- POSTGRES_DEFAULT_PASSWORD =
"test"
- POSTGRES_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
-
#database_url(protocol: "postgres", username: nil, password: nil, database: nil, options: {}) ⇒ String
Returns the database url (e.g. postgres://user:password@host:port/database).
-
#initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ PostgresContainer
constructor
Initializes a new instance of PostgresContainer.
-
#port ⇒ Integer
Returns the port used by the container.
-
#start ⇒ PostgresContainer
Starts the container.
-
#with_database(database) ⇒ PostgresContainer
Sets the database to use.
-
#with_password(password) ⇒ PostgresContainer
Sets the password to use.
-
#with_username(username) ⇒ PostgresContainer
Sets the username to use.
Constructor Details
#initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ PostgresContainer
Initializes a new instance of PostgresContainer
33 34 35 36 37 38 39 40 |
# File 'lib/testcontainers/postgres.rb', line 33 def initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) super(image, **kwargs) @username = username || ENV.fetch("POSTGRES_USER", POSTGRES_DEFAULT_USERNAME) @password = password || ENV.fetch("POSTGRES_PASSWORD", POSTGRES_DEFAULT_PASSWORD) @database = database || ENV.fetch("POSTGRES_DATABASE", POSTGRES_DEFAULT_DATABASE) @healthcheck ||= add_healthcheck() @wait_for ||= add_wait_for(:healthcheck) end |
Instance Attribute Details
#database ⇒ String (readonly)
used by the container
11 12 13 |
# File 'lib/testcontainers/postgres.rb', line 11 def database @database end |
#password ⇒ String (readonly)
used by the container
11 12 13 |
# File 'lib/testcontainers/postgres.rb', line 11 def password @password end |
#username ⇒ String (readonly)
used by the container
11 12 13 |
# File 'lib/testcontainers/postgres.rb', line 11 def username @username end |
Instance Method Details
#database_url(protocol: "postgres", username: nil, password: nil, database: nil, options: {}) ⇒ String
Returns the database url (e.g. postgres://user:password@host:port/database)
66 67 68 69 70 71 72 73 |
# File 'lib/testcontainers/postgres.rb', line 66 def database_url(protocol: "postgres", 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
54 55 56 |
# File 'lib/testcontainers/postgres.rb', line 54 def port POSTGRES_DEFAULT_PORT end |
#start ⇒ PostgresContainer
Starts the container
45 46 47 48 49 |
# File 'lib/testcontainers/postgres.rb', line 45 def start with_exposed_ports(port) _configure super end |
#with_database(database) ⇒ PostgresContainer
Sets the database to use
79 80 81 82 |
# File 'lib/testcontainers/postgres.rb', line 79 def with_database(database) @database = database self end |
#with_password(password) ⇒ PostgresContainer
Sets the password to use
97 98 99 100 |
# File 'lib/testcontainers/postgres.rb', line 97 def with_password(password) @password = password self end |
#with_username(username) ⇒ PostgresContainer
Sets the username to use
88 89 90 91 |
# File 'lib/testcontainers/postgres.rb', line 88 def with_username(username) @username = username self end |