testcontainers-postgres 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25e023c24e32f22ba240ff88dc76549f0a09613aa68f1c36c531797bfde7ff06
4
+ data.tar.gz: f0664ba9fadfca4dac0d16128d520ca822adae889270cc7162730db5ac1bd666
5
+ SHA512:
6
+ metadata.gz: 85ef1679c31fe67156afa4bb324dd0b6b050cd8a4d62bbf044a88753799a995e458dfdc69d5836bdd253d5ceddcfce022b57c86e9e2c4f68975636a04de86aff
7
+ data.tar.gz: 78ce60e49c0a6d680fd13061fe008b7737ff437b6709399f578dd6c8a3f63ea13238dbab38f07e6b9517fdf0d4f17437426b0f66378c6b5809d09bb0afe4dc07
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2023-05-13
2
+
3
+ ### Added
4
+
5
+ - Initial release of the Postgres module
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in testcontainers-postgres.gemspec
6
+ gemspec
7
+
8
+ # Use the latest version of testcontainers-core from the local path
9
+ gem "testcontainers-core", path: "../core"
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: ../core
3
+ specs:
4
+ testcontainers-core (0.1.2)
5
+ docker-api (~> 2.2)
6
+
7
+ PATH
8
+ remote: .
9
+ specs:
10
+ testcontainers-postgres (0.1.0)
11
+ testcontainers-core (~> 0.1.1)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ ast (2.4.2)
17
+ docker-api (2.2.0)
18
+ excon (>= 0.47.0)
19
+ multi_json
20
+ excon (0.99.0)
21
+ json (2.6.3)
22
+ language_server-protocol (3.17.0.3)
23
+ lint_roller (1.0.0)
24
+ minitest (5.18.0)
25
+ minitest-hooks (1.5.0)
26
+ minitest (> 5.3)
27
+ multi_json (1.15.0)
28
+ parallel (1.23.0)
29
+ parser (3.2.2.1)
30
+ ast (~> 2.4.1)
31
+ pg (1.5.3)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.8.0)
35
+ rexml (3.2.5)
36
+ rubocop (1.50.2)
37
+ json (~> 2.3)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.2.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml (>= 3.2.5, < 4.0)
43
+ rubocop-ast (>= 1.28.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 2.4.0, < 3.0)
46
+ rubocop-ast (1.28.1)
47
+ parser (>= 3.2.1.0)
48
+ rubocop-performance (1.16.0)
49
+ rubocop (>= 1.7.0, < 2.0)
50
+ rubocop-ast (>= 0.4.0)
51
+ ruby-progressbar (1.13.0)
52
+ standard (1.28.2)
53
+ language_server-protocol (~> 3.17.0.2)
54
+ lint_roller (~> 1.0)
55
+ rubocop (~> 1.50.2)
56
+ standard-custom (~> 1.0.0)
57
+ standard-performance (~> 1.0.1)
58
+ standard-custom (1.0.0)
59
+ lint_roller (~> 1.0)
60
+ standard-performance (1.0.1)
61
+ lint_roller (~> 1.0)
62
+ rubocop-performance (~> 1.16.0)
63
+ unicode-display_width (2.4.2)
64
+
65
+ PLATFORMS
66
+ arm64-darwin-21
67
+ x86_64-darwin-22
68
+
69
+ DEPENDENCIES
70
+ minitest (~> 5.0)
71
+ minitest-hooks (~> 1.5)
72
+ pg (~> 1.5)
73
+ rake (~> 13.0)
74
+ standard (~> 1.3)
75
+ testcontainers-core!
76
+ testcontainers-postgres!
77
+
78
+ BUNDLED WITH
79
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Guillermo Iguaran
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # Testcontainers module for Postgres
2
+
3
+ testcontainers-postgres simplifies the creation and management of Postgres containers for testing purposes using the Testcontainers library.
4
+
5
+ ## Installation
6
+
7
+ Add the library to the test section in your application's Gemfile:
8
+
9
+ ```ruby
10
+ group :test do
11
+ gem "testcontainers-postgres"
12
+ end
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install testcontainers-postgres
25
+ ```
26
+
27
+
28
+ ## Usage
29
+
30
+ To use the library, you first need to require it:
31
+
32
+ ```ruby
33
+ require "testcontainers/postgres"
34
+ ```
35
+
36
+ ### Creating a Postgres container
37
+
38
+ Create a new instance of the `Testcontainers::PostgresContainer` class:
39
+
40
+ ```ruby
41
+ container = Testcontainers::PostgresContainer.new
42
+ ```
43
+
44
+
45
+ This creates a new container with the default Postgres image, user, password, and database. You can customize these by passing arguments to the constructor:
46
+
47
+ ```ruby
48
+ container = Testcontainers::PostgresContainer.new("postgres:11", username: "custom_user", password: "custom_pass", database: "custom_db")
49
+ ```
50
+
51
+ ### Starting and stopping the container
52
+
53
+ Start the container:
54
+
55
+ ```ruby
56
+ container.start
57
+ ```
58
+
59
+ Stop the container when you're done:
60
+
61
+ ```ruby
62
+ container.stop
63
+ ```
64
+
65
+ ### Connecting to the Postgres container
66
+
67
+ Once the container is running, you can obtain the connection details using the following methods:
68
+
69
+ ```ruby
70
+ host = container.host
71
+ port = container.first_mapped_port
72
+ ```
73
+
74
+ Or, you can generate a full database URL:
75
+
76
+ ```ruby
77
+ database_url = container.database_url
78
+ ```
79
+
80
+ Use this URL to connect to the Postgres container using your preferred postgres client library.
81
+
82
+ ### Customizing the container
83
+
84
+ You can also customize the container using the following methods:
85
+
86
+ ```ruby
87
+ container.with_database("custom_db")
88
+ container.with_username("custom_user")
89
+ container.with_password("custom_pass")
90
+ ```
91
+
92
+ ### Example
93
+
94
+ Here's a complete example of how to use testcontainers-postgres to create a container, connect to it, and run a simple query:
95
+
96
+ ```ruby
97
+ require "testcontainers/postgres"
98
+ require "pg"
99
+
100
+ container = Testcontainers::PostgresContainer.new
101
+ container.start
102
+
103
+ client = PG.connect(container.database_url)
104
+
105
+ result = client.exec("SELECT 1 AS number")
106
+ result.each do |row|
107
+ puts row.inspect
108
+ end
109
+
110
+ client.close
111
+ container.stop
112
+ ```
113
+
114
+ This example creates a Postgres container, connects to it using the `pg` gem, runs a simple `SELECT 1 as number` query, and then stops the container.
115
+
116
+ ### Using with RSpec
117
+
118
+ You can manage the container in the `before(:suite)` / `after(:suite)` blocks in your `spec_helper.rb`:
119
+
120
+ ```ruby
121
+ RSpec.configure do |config|
122
+ # This helps us to have access to the `RSpec.configuration.mysql_container` without using global variables.
123
+ config.add_setting :postgres_container, default: nil
124
+
125
+ config.before(:suite) do
126
+ config.postgres_container = Testcontainers::PostgresContainer.new.start
127
+ ENV["DATABASE_URL"] = config.postgres_container.database_url # or you can expose it to a fixed port and use database.yml for configuration
128
+ end
129
+
130
+ config.after(:suite) do
131
+ config.postgres_container&.stop
132
+ config.postgres_container&.remove
133
+ end
134
+ end
135
+ ```
136
+
137
+
138
+ ## Contributing
139
+
140
+ Bug reports and pull requests are welcome on GitHub at https://github.com/guilleiguaran/testcontainers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/guilleiguaran/testcontainers-ruby/blob/main/CODE_OF_CONDUCT.md).
141
+
142
+ ## License
143
+
144
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
145
+
146
+ ## Code of Conduct
147
+
148
+ Everyone interacting in the Testcontainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/guilleiguaran/testcontainers-ruby/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testcontainers
4
+ module Postgres
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,107 @@
1
+ require_relative "postgres/version"
2
+ require "testcontainers"
3
+ require "uri"
4
+
5
+ module Testcontainers
6
+ # PostgresContainer class is used to manage containers that runs a PostgresQL database
7
+ #
8
+ # @attr_reader [String] port used by the container
9
+ # @attr_reader [String] username used by the container
10
+ # @attr_reader [String] password used by the container
11
+ # @attr_reader [String] database used by the container
12
+ class PostgresContainer < ::Testcontainers::DockerContainer
13
+ # Default port used by the container
14
+ POSTGRES_DEFAULT_PORT = 5432
15
+
16
+ # Default image used by the container
17
+ POSTGRES_DEFAULT_IMAGE = "postgres:latest"
18
+
19
+ POSTGRES_DEFAULT_USERNAME = "test"
20
+ POSTGRES_DEFAULT_PASSWORD = "test"
21
+ POSTGRES_DEFAULT_DATABASE = "test"
22
+
23
+ attr_reader :port, :username, :password, :database
24
+
25
+ # Initializes a new instance of PostgresContainer
26
+ #
27
+ # @param image [String] the image to use
28
+ # @param username [String] the username to use
29
+ # @param password [String] the password to use
30
+ # @param database [String] the database to use
31
+ # @param port [String] the port to use
32
+ # @param kwargs [Hash] the options to pass to the container. See {DockerContainer#initialize}
33
+ # @return [PostgresContainer] a new instance of PostgresContainer
34
+ def initialize(image = POSTGRES_DEFAULT_IMAGE, username: nil, password: nil, database: nil, port: nil, **kwargs)
35
+ super(image, **kwargs)
36
+ @port = port || ENV.fetch("POSTGRES_PORT", POSTGRES_DEFAULT_PORT)
37
+ @username = username || ENV.fetch("POSTGRES_USER", POSTGRES_DEFAULT_USERNAME)
38
+ @password = password || ENV.fetch("POSTGRES_PASSWORD", POSTGRES_DEFAULT_PASSWORD)
39
+ @database = database || ENV.fetch("POSTGRES_DATABASE", POSTGRES_DEFAULT_DATABASE)
40
+ end
41
+
42
+ # Starts the container
43
+ #
44
+ # @return [PostgresContainer] self
45
+ def start
46
+ with_exposed_ports(@port)
47
+ _configure
48
+ super
49
+ end
50
+
51
+ # Returns the database url (e.g. postgres://user:password@host:port/database)
52
+ #
53
+ # @param protocol [String] the protocol to use in the string (default: "postgres")
54
+ # @param database [String] the database to use in the string (default: @database)
55
+ # @param options [Hash] the options to use in the query string (default: {})
56
+ # @return [String] the database url
57
+ # @raise [ConnectionError] If the connection to the Docker daemon fails.
58
+ # @raise [ContainerNotStartedError] If the container has not been started.
59
+ def database_url(protocol: "postgres", username: nil, password: nil, database: nil, options: {})
60
+ database ||= @database
61
+ username ||= @username
62
+ password ||= @password
63
+ query_string = options.empty? ? "" : "?#{URI.encode_www_form(options)}"
64
+
65
+ "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}"
66
+ end
67
+
68
+ # Sets the database to use
69
+ #
70
+ # @param database [String] the database to use
71
+ # @return [PostgresContainer] self
72
+ def with_database(database)
73
+ @database = database
74
+ self
75
+ end
76
+
77
+ # Sets the username to use
78
+ #
79
+ # @param username [String] the username to use
80
+ # @return [PostgresContainer] self
81
+ def with_username(username)
82
+ @username = username
83
+ self
84
+ end
85
+
86
+ # Sets the password to use
87
+ #
88
+ # @param password [String] the password to use
89
+ # @return [PostgresContainer] self
90
+ def with_password(password)
91
+ @password = password
92
+ self
93
+ end
94
+
95
+ private
96
+
97
+ def _configure
98
+ add_env("POSTGRES_DATABASE", @database)
99
+ add_env("POSTGRES_USER", @username)
100
+
101
+ raise ContainerLaunchException, "Password is required for non-root users" if @password.nil? || @password.empty?
102
+
103
+ add_env("POSTGRES_PASSWORD", @password)
104
+ add_env("POSTGRES_ROOT_PASSWORD", @password)
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testcontainers-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Piza
8
+ - Guillermo Iguaran
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: testcontainers-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.1.2
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.1.2
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '13.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '13.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest-hooks
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.5'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.5'
70
+ - !ruby/object:Gem::Dependency
71
+ name: standard
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.3'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pg
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.5'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.5'
98
+ description: Testcontainers makes it easy to create and clean up container-based dependencies
99
+ for automated tests.
100
+ email:
101
+ - pedro920224@gmail.com
102
+ - guilleiguaran@gmail.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/testcontainers/postgres.rb
114
+ - lib/testcontainers/postgres/version.rb
115
+ homepage: https://github.com/guilleiguaran/testcontainers-ruby
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ homepage_uri: https://github.com/guilleiguaran/testcontainers-ruby/blob/main/postgres
120
+ source_code_uri: https://github.com/guilleiguaran/testcontainers-ruby/blob/main/postgres
121
+ changelog_uri: https://github.com/guilleiguaran/testcontainers-ruby/blob/main/postgres/CHANGELOG.md
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.6.0
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.4.1
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: 'Testcontainers for Ruby: Postgres module'
141
+ test_files: []