| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c6ae58d commit 4f97070
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | Database containers | |
| 2 | 2 | =================== | |
| 3 | 3 | ||
| 4 | - Allows to spin up docker database images such as MySQL, PostgreSQL, MariaDB and Oracle XE. | ||
| 4 | + Allows to spin up docker database images such as MySQL, PostgreSQL, MariaDB, Oracle XE and MongoDb. | ||
| 5 | 5 | ||
| 6 | 6 | MySQL example | |
| 7 | 7 | ------------- | |
@@ -81,3 +81,35 @@ Elasticsearch | |||
| 81 | 81 | with es: | |
| 82 | 82 | es.get_url() # gives you the http URL to connect to Elasticsearch | |
| 83 | 83 | ||
| 84 | + MongoDb | ||
| 85 | + ----------- | ||
| 86 | + | ||
| 87 | + Example of MongoDb database usage: | ||
| 88 | + | ||
| 89 | + :: | ||
| 90 | + | ||
| 91 | + def test_docker_run_mongodb(): | ||
| 92 | + mongo_container = MongoDbContainer("mongo:latest") | ||
| 93 | + with mongo_container as mongo: | ||
| 94 | + db = mongo.get_connection_client().test | ||
| 95 | + result = db.restaurants.insert_one( | ||
| 96 | + { | ||
| 97 | + "address": { | ||
| 98 | + "street": "2 Avenue", | ||
| 99 | + "zipcode": "10075", | ||
| 100 | + "building": "1480", | ||
| 101 | + "coord": [-73.9557413, 40.7720266] | ||
| 102 | + }, | ||
| 103 | + "borough": "Manhattan", | ||
| 104 | + "cuisine": "Italian", | ||
| 105 | + "name": "Vella", | ||
| 106 | + "restaurant_id": "41704620" | ||
| 107 | + } | ||
| 108 | + ) | ||
| 109 | + print(result.inserted_id) | ||
| 110 | + cursor = db.restaurants.find({"borough": "Manhattan"}) | ||
| 111 | + for document in cursor: | ||
| 112 | + print(document) | ||
| 113 | + | ||
| 114 | + Connection is made using pymongo package and MongoClient class. | ||
| 115 | + Alternatively, you can use get_connection_url method to use the driver that better fits for your use case. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ Currently available features: | |||
| 16 | 16 | - PostgreSQL db container | |
| 17 | 17 | - Google Cloud PubSub emulator container | |
| 18 | 18 | - Elasticsearch container | |
| 19 | + - MongoDb container | ||
| 19 | 20 | - Generic Docker containers | |
| 20 | 21 | ||
| 21 | 22 | Installation | |
@@ -33,6 +34,7 @@ and can be installed using pip, depending on which containers you need: | |||
| 33 | 34 | pip install testcontainers[postgresql] | |
| 34 | 35 | pip install testcontainers[selenium] | |
| 35 | 36 | pip install testcontainers[google-cloud-pubsub] | |
| 37 | + pip install testcontainers[mongodb] | ||
| 36 | 38 | # or with multiple | |
| 37 | 39 | pip install testcontainers[mysql,postgresql,selenium,google-cloud-pubsub] | |
| 38 | 40 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,6 +53,7 @@ | |||
| 53 | 53 | 'postgresql': ['sqlalchemy', 'psycopg2-binary'], | |
| 54 | 54 | 'selenium': ['selenium==2.53.1'], | |
| 55 | 55 | 'google-cloud-pubsub': ['google-cloud-pubsub'], | |
| 56 | + 'mongo': ['pymongo'] | ||
| 56 | 57 | }, | |
| 57 | 58 | long_description_content_type="text/markdown", | |
| 58 | 59 | long_description=long_description, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + # | ||
| 2 | + # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| 3 | + # not use this file except in compliance with the License. You may obtain | ||
| 4 | + # a copy of the License at | ||
| 5 | + # | ||
| 6 | + # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 7 | + # | ||
| 8 | + # Unless required by applicable law or agreed to in writing, software | ||
| 9 | + # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| 10 | + # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| 11 | + # License for the specific language governing permissions and limitations | ||
| 12 | + # under the License. | ||
| 13 | + import os | ||
| 14 | + | ||
| 15 | + from pymongo import MongoClient | ||
| 16 | + | ||
| 17 | + from testcontainers.core.generic import DockerContainer | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + class MongoDbContainer(DockerContainer): | ||
| 21 | + MONGO_INITDB_ROOT_USERNAME = os.environ.get("MONGO_INITDB_ROOT_USERNAME", "test") | ||
| 22 | + MONGO_INITDB_ROOT_PASSWORD = os.environ.get("MONGO_INITDB_ROOT_PASSWORD", "test") | ||
| 23 | + MONGO_DB = os.environ.get("MONGO_DB", "test") | ||
| 24 | + | ||
| 25 | + def __init__(self, image="mongodb:latest"): | ||
| 26 | + super(MongoDbContainer, self).__init__(image=image) | ||
| 27 | + self.command="mongo" | ||
| 28 | + self.port_to_expose = 27017 | ||
| 29 | + self.with_exposed_ports(self.port_to_expose) | ||
| 30 | + | ||
| 31 | + def _configure(self): | ||
| 32 | + | ||
| 33 | + self.with_env("MONGO_INITDB_ROOT_USERNAME", self.MONGO_INITDB_ROOT_USERNAME) | ||
| 34 | + self.with_env("MONGO_INITDB_ROOT_PASSWORD", self.MONGO_INITDB_ROOT_PASSWORD) | ||
| 35 | + self.with_env("MONGO_DB", self.MONGO_DB) | ||
| 36 | + | ||
| 37 | + def get_connection_url(self): | ||
| 38 | + return "mongodb://{}:{}".format(self.get_container_host_ip(), self.get_exposed_port(self.port_to_expose)) | ||
| 39 | + | ||
| 40 | + def get_connection_client(self): | ||
| 41 | + return MongoClient("mongodb://{}:{}".format(self.get_container_host_ip(), | ||
| 42 | + self.get_exposed_port(self.port_to_expose))) | ||
| 43 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | from testcontainers.mysql import MySqlContainer, MariaDbContainer | |
| 8 | 8 | from testcontainers.oracle import OracleDbContainer | |
| 9 | 9 | from testcontainers.postgres import PostgresContainer | |
| 10 | + from testcontainers.mongodb import MongoDbContainer | ||
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | 13 | def test_docker_run_mysql(): | |
@@ -50,6 +51,27 @@ def test_docker_run_oracle(): | |||
| 50 | 51 | assert {row[0] for row in result} == versions | |
| 51 | 52 | ||
| 52 | 53 | ||
| 54 | + def test_docker_run_mongodb(): | ||
| 55 | + mongo_container = MongoDbContainer("mongo:latest") | ||
| 56 | + with mongo_container as mongo: | ||
| 57 | + db = mongo.get_connection_client().test | ||
| 58 | + doc = { | ||
| 59 | + "address": { | ||
| 60 | + "street": "2 Avenue", | ||
| 61 | + "zipcode": "10075", | ||
| 62 | + "building": "1480", | ||
| 63 | + "coord": [-73.9557413, 40.7720266] | ||
| 64 | + }, | ||
| 65 | + "borough": "Manhattan", | ||
| 66 | + "cuisine": "Italian", | ||
| 67 | + "name": "Vella", | ||
| 68 | + "restaurant_id": "41704620" | ||
| 69 | + } | ||
| 70 | + result = db.restaurants.insert_one(doc) | ||
| 71 | + cursor = db.restaurants.find({"borough": "Manhattan"}) | ||
| 72 | + assert cursor.next()['restaurant_id'] == doc['restaurant_id'] | ||
| 73 | + | ||
| 74 | + | ||
| 53 | 75 | def test_docker_generic_db(): | |
| 54 | 76 | mongo_container = GenericContainer("mongo:latest") | |
| 55 | 77 | mongo_container.with_bind_ports(27017, 27017) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments