forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_db_containers.py
More file actions
174 lines (148 loc) · 6.51 KB
/
Copy pathtest_db_containers.py
File metadata and controls
174 lines (148 loc) · 6.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pytest
import sqlalchemy
from pymongo import MongoClient
from pymongo.errors import OperationFailure
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for
from testcontainers.mongodb import MongoDbContainer
from testcontainers.mssql import SqlServerContainer
from testcontainers.mysql import MySqlContainer
from testcontainers.neo4j import Neo4jContainer
from testcontainers.oracle import OracleDbContainer
from testcontainers.postgres import PostgresContainer
def test_docker_run_mysql():
config = MySqlContainer('mysql:5.7.17')
with config as mysql:
e = sqlalchemy.create_engine(mysql.get_connection_url())
result = e.execute("select version()")
for row in result:
assert row[0] == '5.7.17'
def test_docker_run_postgress():
postgres_container = PostgresContainer("postgres:9.5")
with postgres_container as postgres:
e = sqlalchemy.create_engine(postgres.get_connection_url())
result = e.execute("select version()")
for row in result:
print("server version:", row[0])
def test_docker_run_greenplum():
postgres_container = PostgresContainer("datagrip/greenplum:6.8",
user="guest", password="guest", dbname="guest")
with postgres_container as postgres:
e = sqlalchemy.create_engine(postgres.get_connection_url())
result = e.execute("select version()")
for row in result:
print("server version:", row[0])
def test_docker_run_mariadb():
mariadb_container = MySqlContainer("mariadb:10.2.9")
with mariadb_container as mariadb:
e = sqlalchemy.create_engine(mariadb.get_connection_url())
result = e.execute("select version()")
for row in result:
assert row[0] == '10.2.9-MariaDB-10.2.9+maria~jessie'
@pytest.mark.skip(reason="needs oracle client libraries unavailable on Travis")
def test_docker_run_oracle():
oracledb_container = OracleDbContainer()
with oracledb_container as oracledb:
e = sqlalchemy.create_engine(oracledb.get_connection_url())
result = e.execute("select * from V$VERSION")
versions = {'Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production',
'PL/SQL Release 11.2.0.2.0 - Production',
'CORE\t11.2.0.2.0\tProduction',
'TNS for Linux: Version 11.2.0.2.0 - Production',
'NLSRTL Version 11.2.0.2.0 - Production'}
assert {row[0] for row in result} == versions
def test_docker_run_mongodb():
mongo_container = MongoDbContainer("mongo:latest")
with mongo_container 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"
}
db.restaurants.insert_one(doc)
cursor = db.restaurants.find({"borough": "Manhattan"})
assert cursor.next()['restaurant_id'] == doc['restaurant_id']
def test_docker_run_mongodb_connect_without_credentials():
with MongoDbContainer() as mongo:
connection_url = "mongodb://{}:{}".format(mongo.get_container_host_ip(),
mongo.get_exposed_port(mongo.port_to_expose))
db = MongoClient(connection_url).test
with pytest.raises(OperationFailure):
db.restaurants.insert_one({})
def test_docker_run_neo4j_v35():
neo4j_container = Neo4jContainer("neo4j:3.5")
with neo4j_container as neo4j:
with neo4j.get_driver() as driver:
with driver.session() as session:
result = session.run(
"""
CALL dbms.components()
YIELD name, versions, edition
UNWIND versions as version
RETURN name, version, edition
""")
record = result.single()
print("server version:", record["name"], record["version"], record["edition"])
assert record["version"].startswith("3.5")
def test_docker_run_neo4j_latest():
neo4j_container = Neo4jContainer()
with neo4j_container as neo4j:
with neo4j.get_driver() as driver:
with driver.session() as session:
result = session.run(
"""
CALL dbms.components()
YIELD name, versions, edition
UNWIND versions as version
RETURN name, version, edition
""")
record = result.single()
print("server version:", record["name"], record["version"], record["edition"])
assert record["name"].startswith("Neo4j")
def test_docker_generic_db():
mongo_container = DockerContainer("mongo:latest")
mongo_container.with_bind_ports(27017, 27017)
with mongo_container:
def connect():
return MongoClient("mongodb://{}:{}".format(mongo_container.get_container_host_ip(),
mongo_container.get_exposed_port(27017)))
db = wait_for(connect).primer
result = db.restaurants.insert_one(
{
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413, 40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"name": "Vella",
"restaurant_id": "41704620"
}
)
print(result.inserted_id)
cursor = db.restaurants.find({"borough": "Manhattan"})
for document in cursor:
print(document)
def test_docker_run_mssql():
config = SqlServerContainer()
with config as mssql:
e = sqlalchemy.create_engine(mssql.get_connection_url())
result = e.execute('select @@servicename')
for row in result:
assert row[0] == 'MSSQLSERVER'
config = SqlServerContainer(password="1Secure*Password2")
with config as mssql:
e = sqlalchemy.create_engine(mssql.get_connection_url())
result = e.execute('select @@servicename')
for row in result:
assert row[0] == 'MSSQLSERVER'