Class: Testcontainers::PostgresContainer

Inherits:
DockerContainer
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs) ⇒ PostgresContainer

Initializes a new instance of PostgresContainer

Parameters:

  • image (String) (defaults to: POSTGRES_DEFAULT_IMAGE)

    the image to use

  • username (String) (defaults to: nil)

    the username to use

  • password (String) (defaults to: nil)

    the password to use

  • database (String) (defaults to: nil)

    the database to use

  • port (String) (defaults to: nil)

    the port to use

  • kwargs (Hash)

    the options to pass to the container. See DockerContainer#initialize



34
35
36
37
38
39
40
# File 'lib/testcontainers/postgres.rb', line 34

def initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs)
  super(image, **kwargs)
  @port = port || ENV.fetch("POSTGRES_PORT", POSTGRES_DEFAULT_PORT)
  @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)
end

Instance Attribute Details

#databaseString (readonly)

used by the container

Returns:

  • (String)

    the current value of database



12
13
14
# File 'lib/testcontainers/postgres.rb', line 12

def database
  @database
end

#passwordString (readonly)

used by the container

Returns:

  • (String)

    the current value of password



12
13
14
# File 'lib/testcontainers/postgres.rb', line 12

def password
  @password
end

#portString (readonly)

used by the container

Returns:

  • (String)

    the current value of port



12
13
14
# File 'lib/testcontainers/postgres.rb', line 12

def port
  @port
end

#usernameString (readonly)

used by the container

Returns:

  • (String)

    the current value of username



12
13
14
# File 'lib/testcontainers/postgres.rb', line 12

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)

Parameters:

  • protocol (String) (defaults to: "postgres")

    the protocol to use in the string (default: “postgres”)

  • database (String) (defaults to: nil)

    the database to use in the string (default: @database)

  • options (Hash) (defaults to: {})

    the options to use in the query string (default: {})

Returns:

  • (String)

    the database url

Raises:

  • (ConnectionError)

    If the connection to the Docker daemon fails.

  • (ContainerNotStartedError)

    If the container has not been started.



59
60
61
62
63
64
65
66
# File 'lib/testcontainers/postgres.rb', line 59

def database_url(protocol: "postgres", username: nil, password: nil, database: nil, options: {})
  database ||= @database
  username ||= @username
  password ||= @password
  query_string = options.empty? ? "" : "?#{URI.encode_www_form(options)}"

  "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}"
end

#startPostgresContainer

Starts the container

Returns:



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

Parameters:

  • database (String)

    the database to use

Returns:



72
73
74
75
# File 'lib/testcontainers/postgres.rb', line 72

def with_database(database)
  @database = database
  self
end

#with_password(password) ⇒ PostgresContainer

Sets the password to use

Parameters:

  • password (String)

    the password to use

Returns:



90
91
92
93
# File 'lib/testcontainers/postgres.rb', line 90

def with_password(password)
  @password = password
  self
end

#with_username(username) ⇒ PostgresContainer

Sets the username to use

Parameters:

  • username (String)

    the username to use

Returns:



81
82
83
84
# File 'lib/testcontainers/postgres.rb', line 81

def with_username(username)
  @username = username
  self
end