forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mongodb.py
More file actions
53 lines (47 loc) · 1.67 KB
/
test_mongodb.py
File metadata and controls
53 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pytest
from pymongo import MongoClient
from pymongo.errors import OperationFailure
from testcontainers.mongodb import MongoDbContainer
@pytest.mark.parametrize("version", ["7.0.7", "6.0.14", "5.0.26"])
def test_docker_run_mongodb(version: str):
with MongoDbContainer(f"mongo:{version}") as mongo:
db = mongo.get_connection_client().test
doc = {
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413, 40.7720266],
},
"borough": "Manhattan",
"cuisine": "Italian",
"name": "Vella",
"restaurant_id": "41704620",
}
result = db.restaurants.insert_one(doc)
assert result.inserted_id
cursor = db.restaurants.find({"borough": "Manhattan"})
assert cursor.next()["restaurant_id"] == doc["restaurant_id"]
# This is a feature in the generic DbContainer class
# but it can't be tested on its own
# so is tested in various database modules:
# - mysql / mariadb
# - postgresql
# - sqlserver
# - mongodb
# - db2
def test_quoted_password():
user = "root"
password = "p@$%25+0&%rd :/!=?"
quoted_password = "p%40%24%2525+0%26%25rd %3A%2F%21%3D%3F"
# driver = "pymongo"
kwargs = {
"username": user,
"password": password,
}
with MongoDbContainer("mongo:7.0.7", **kwargs) as container:
host = container.get_container_host_ip()
port = container.get_exposed_port(27017)
expected_url = f"mongodb://{user}:{quoted_password}@{host}:{port}"
url = container.get_connection_url()
assert url == expected_url