testcontainers-mongo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0d719fe2e9a9aa14dd44903272285ec6446d2c9ec06ae96ec0d359aa4b0bbb17
4
+ data.tar.gz: 89c3e5567a55ff21d9c5a9df4a5ad31e44d022e7643ffd50e71b747d0fbe1f5b
5
+ SHA512:
6
+ metadata.gz: ad814ad4f2f4846fd938bb6a7eb59aad6d5b041ccf1594c66d8bc73524c19ad10521e2daadd41353da1822a5150c7cec8f5efb6051df26e303feac5ea5b254fe
7
+ data.tar.gz: 7722a2ab869d1b49047228ed7ac2f3c3efc81fd583044acf290d93abfdcdc253847cfc82d9bcc3bf27423f927590a6a0a1c1df602bc91740281c1e97d78cd0fd
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2023-06-10
2
+
3
+ ### Added
4
+
5
+ - Initial release of the Mongo 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-mongo.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,81 @@
1
+ PATH
2
+ remote: ../core
3
+ specs:
4
+ testcontainers-core (0.1.3)
5
+ docker-api (~> 2.2)
6
+
7
+ PATH
8
+ remote: .
9
+ specs:
10
+ testcontainers-mongo (0.1.0)
11
+ testcontainers-core (~> 0.1.3)
12
+
13
+ GEM
14
+ remote: https://rubygems.org/
15
+ specs:
16
+ ast (2.4.2)
17
+ bson (4.15.0)
18
+ docker-api (2.2.0)
19
+ excon (>= 0.47.0)
20
+ multi_json
21
+ excon (0.99.0)
22
+ json (2.6.3)
23
+ language_server-protocol (3.17.0.3)
24
+ lint_roller (1.0.0)
25
+ minitest (5.18.0)
26
+ minitest-hooks (1.5.0)
27
+ minitest (> 5.3)
28
+ mongo (2.18.2)
29
+ bson (>= 4.14.1, < 5.0.0)
30
+ multi_json (1.15.0)
31
+ parallel (1.23.0)
32
+ parser (3.2.2.1)
33
+ ast (~> 2.4.1)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ regexp_parser (2.8.0)
37
+ rexml (3.2.5)
38
+ rubocop (1.50.2)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.28.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.28.1)
49
+ parser (>= 3.2.1.0)
50
+ rubocop-performance (1.16.0)
51
+ rubocop (>= 1.7.0, < 2.0)
52
+ rubocop-ast (>= 0.4.0)
53
+ ruby-progressbar (1.13.0)
54
+ standard (1.28.2)
55
+ language_server-protocol (~> 3.17.0.2)
56
+ lint_roller (~> 1.0)
57
+ rubocop (~> 1.50.2)
58
+ standard-custom (~> 1.0.0)
59
+ standard-performance (~> 1.0.1)
60
+ standard-custom (1.0.0)
61
+ lint_roller (~> 1.0)
62
+ standard-performance (1.0.1)
63
+ lint_roller (~> 1.0)
64
+ rubocop-performance (~> 1.16.0)
65
+ unicode-display_width (2.4.2)
66
+
67
+ PLATFORMS
68
+ arm64-darwin-21
69
+ x86_64-darwin-22
70
+
71
+ DEPENDENCIES
72
+ minitest (~> 5.0)
73
+ minitest-hooks (~> 1.5)
74
+ mongo (~> 2.2)
75
+ rake (~> 13.0)
76
+ standard (~> 1.3)
77
+ testcontainers-core!
78
+ testcontainers-mongo!
79
+
80
+ BUNDLED WITH
81
+ 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,124 @@
1
+ # Testcontainers module for MongoDB
2
+
3
+ testcontainers-mongo simplifies the creation and management of MongoDB 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-mongo"
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-mongo
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ To use the library, you first need to require it:
30
+
31
+ ```ruby
32
+ require "testcontainers/mongo"
33
+ ```
34
+
35
+ ### Creating a MongoDB container
36
+
37
+ Create a new instance of the `Testcontainers::MongoContainer` class:
38
+
39
+ ```ruby
40
+ container = Testcontainers::MongoContainer.new
41
+ ```
42
+
43
+ This creates a new container with the default MongoL image, user, password, and database. You can customize these by passing arguments to the constructor:
44
+
45
+ ```ruby
46
+ container = Testcontainers::MongoContainer.new("mongo:5.7", username: "custom_user", password: "custom_pass", database: "custom_db")
47
+ ```
48
+
49
+ ### Starting and stopping the container
50
+
51
+ Start the container:
52
+
53
+ ```ruby
54
+ container.start
55
+ ```
56
+
57
+ Stop the container when you're done:
58
+
59
+ ```ruby
60
+ container.stop
61
+ ```
62
+
63
+ ### Connecting to the MongoDB container
64
+
65
+ Once the container is running, you can obtain the connection details using the following methods:
66
+
67
+ ```ruby
68
+ host = container.host
69
+ port = container.first_mapped_port
70
+ ```
71
+
72
+ Or, you can generate a full database URL:
73
+
74
+ ```ruby
75
+ database_url = container.database_url
76
+ ```
77
+
78
+ Use this URL to connect to the MongoDB container using your preferred MongoDB client library.
79
+
80
+ ### Customizing the container
81
+
82
+ You can also customize the container using the following methods:
83
+
84
+ ```ruby
85
+ container.with_database("custom_db")
86
+ container.with_username("custom_user")
87
+ container.with_password("custom_pass")
88
+ ```
89
+
90
+ ### Example
91
+
92
+ Here's a complete example of how to use testcontainers-mongo to create a container, connect to it, and run a simple query:
93
+
94
+ ```ruby
95
+ require "testcontainers/mongo"
96
+ require "mongo"
97
+
98
+ container = Testcontainers::MongoContainer.new
99
+ container.start
100
+
101
+ client = Mongo::Client.new(container.database_url, auth_source: "admin")
102
+
103
+ client[:artists].insert_one({:name => "FKA Twigs"})
104
+ client[:artists].find(:name => "FKA Twigs").each do |document|
105
+ document.inspect
106
+ end
107
+
108
+ client.close
109
+ container.stop
110
+ ```
111
+
112
+ This example creates a MongoDB container, connects to it using the `mongo` gem, inserts a a document, runs a query with find, and then stops the container.
113
+
114
+ ## Contributing
115
+
116
+ Bug reports and pull requests are welcome on GitHub at https://github.com/testcontainers/testcontainers-ruby. 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/testcontainers/testcontainers-ruby/blob/main/CODE_OF_CONDUCT.md).
117
+
118
+ ## License
119
+
120
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
121
+
122
+ ## Code of Conduct
123
+
124
+ 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/testcontainers/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 Mongo
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,115 @@
1
+ require_relative "mongo/version"
2
+ require "testcontainers"
3
+
4
+ module Testcontainers
5
+ # MongoContainer class is used to manage containers that runs a Mongo databese
6
+ #
7
+ # @attr_reader [String] username used by the container
8
+ # @attr_reader [String] password used by the container
9
+ # @attr_reader [String] database used by the container
10
+ class MongoContainer < ::Testcontainers::DockerContainer
11
+ # Default port used by the container
12
+ MONGO_DEFAULT_PORT = 27017
13
+
14
+ # Default image used by the container
15
+ MONGO_DEFAULT_IMAGE = "mongo:latest"
16
+
17
+ MONGO_DEFAULT_USERNAME = "test"
18
+ MONGO_DEFAULT_PASSWORD = "test"
19
+ MONGO_DEFAULT_DATABASE = "test"
20
+
21
+ attr_reader :username, :password, :database
22
+
23
+ # Initializes a new instance of MongoContainer
24
+ #
25
+ # @param image [String] the image to use
26
+ # @param username [String] the username to use
27
+ # @param password [String] the password to use
28
+ # @param database [String] the database to use
29
+ # @param port [String] the port to use
30
+ # @param kwargs [Hash] the options to pass to the container. See {DockerContainer#initialize}
31
+ # @return [MongoContainer] a new instance of MongoContainer
32
+ def initialize(image = MONGO_DEFAULT_IMAGE, username: nil, password: nil, database: nil, **kwargs)
33
+ super(image, **kwargs)
34
+ @username = username || ENV.fetch("MONGO_USERNAME", MONGO_DEFAULT_USERNAME)
35
+ @password = password || ENV.fetch("MONGO_PASSWORD", MONGO_DEFAULT_PASSWORD)
36
+ @database = database || ENV.fetch("MONGO_DATABASE", MONGO_DEFAULT_DATABASE)
37
+ @healthcheck ||= add_healthcheck(_default_healthcheck_options)
38
+ @wait_for ||= add_wait_for(:healthcheck)
39
+ end
40
+
41
+ # Starts the container
42
+ #
43
+ # @return [MongoContainer] self
44
+ def start
45
+ with_exposed_ports(port)
46
+ _configure
47
+ super
48
+ end
49
+
50
+ # Returns the port used by the container
51
+ #
52
+ # @return [Integer] the port used by the container
53
+ def port
54
+ MONGO_DEFAULT_PORT
55
+ end
56
+
57
+ # Returns the database url (e.g. mongodb://user:password@host:port/database)
58
+ #
59
+ # @param protocol [String] the protocol to use in the string (default: "mongodb")
60
+ # @param database [String] the database to use in the string (default: @database)
61
+ # @param options [Hash] the options to use in the query string (default: {})
62
+ # @return [String] the database url
63
+ # @raise [ConnectionError] If the connection to the Docker daemon fails.
64
+ # @raise [ContainerNotStartedError] If the container has not been started.
65
+ def mongo_url(protocol: "mongodb", username: nil, password: nil, database: nil, options: {})
66
+ database ||= @database
67
+ username ||= @username
68
+ password ||= @password
69
+ query_string = options.empty? ? "" : "?#{URI.encode_www_form(options)}"
70
+
71
+ "#{protocol}://#{username}:#{password}@#{host}:#{mapped_port(port)}/#{database}#{query_string}"
72
+ end
73
+
74
+ alias_method :database_url, :mongo_url
75
+
76
+ # Sets the database to use
77
+ #
78
+ # @param database [String] the database to use
79
+ # @return [MongoContainer] self
80
+ def with_database(database)
81
+ @database = database
82
+ self
83
+ end
84
+
85
+ # Sets the username to use
86
+ #
87
+ # @param username [String] the password to use
88
+ # @return [MongoContainer] self
89
+ def with_username(username)
90
+ @password = password
91
+ self
92
+ end
93
+
94
+ # Sets the password to use
95
+ #
96
+ # @param password [String] the password to use
97
+ # @return [MongoContainer] self
98
+ def with_password(password)
99
+ @password = password
100
+ self
101
+ end
102
+
103
+ private
104
+
105
+ def _configure
106
+ add_env("MONGO_INITDB_DATABASE", @database)
107
+ add_env("MONGO_INITDB_ROOT_USERNAME", @username)
108
+ add_env("MONGO_INITDB_ROOT_PASSWORD", @password)
109
+ end
110
+
111
+ def _default_healthcheck_options
112
+ {test: ["mongosh", "--eval", "db.adminCommand('ping')"], interval: 5, timeout: 5, retries: 3}
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testcontainers-mongo
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-06-10 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.3
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.3
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: mongo
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '2.2'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '2.2'
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/mongo.rb
114
+ - lib/testcontainers/mongo/version.rb
115
+ homepage: https://github.com/testcontainers/testcontainers-ruby
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ homepage_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo
120
+ source_code_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo
121
+ changelog_uri: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/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: Mongo module'
141
+ test_files: []