From d06264f62ab1cd00554dd848c8e9e79f76030761 Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Sat, 25 Sep 2021 12:05:37 +0300 Subject: [PATCH 1/6] Support specifying postgresql drivers --- testcontainers/postgres.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/testcontainers/postgres.py b/testcontainers/postgres.py index c60c60b39..23ca87b5f 100644 --- a/testcontainers/postgres.py +++ b/testcontainers/postgres.py @@ -21,23 +21,29 @@ class PostgresContainer(DbContainer): Example ------- - The example spins up a Postgres database and connects to it using the :code:`psycopg` driver. + The example spins up a Postgres database and connects to it using the (default) :code:`psycopg` driver. :: with PostgresContainer("postgres:9.5") as postgres: e = sqlalchemy.create_engine(postgres.get_connection_url()) result = e.execute("select version()") + + :: + with PostgresContainer("postgres:9.5", driver="pg8000") as postgres: + e = sqlalchemy.create_engine(postgres.get_connection_url()) + result = e.execute("select version()") """ POSTGRES_USER = os.environ.get("POSTGRES_USER", "test") POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test") POSTGRES_DB = os.environ.get("POSTGRES_DB", "test") - def __init__(self, image="postgres:latest", port=5432, user=None, password=None, dbname=None): + def __init__(self, image="postgres:latest", port=5432, user=None, password=None, dbname=None, driver="psycopg2"): super(PostgresContainer, self).__init__(image=image) self.POSTGRES_USER = user or self.POSTGRES_USER self.POSTGRES_PASSWORD = password or self.POSTGRES_PASSWORD self.POSTGRES_DB = dbname or self.POSTGRES_DB self.port_to_expose = port + self.driver = driver self.with_exposed_ports(self.port_to_expose) @@ -47,7 +53,7 @@ def _configure(self): self.with_env("POSTGRES_DB", self.POSTGRES_DB) def get_connection_url(self, host=None): - return super()._create_connection_url(dialect="postgresql+psycopg2", + return super()._create_connection_url(dialect="postgresql+{}".format(self.driver), username=self.POSTGRES_USER, password=self.POSTGRES_PASSWORD, db_name=self.POSTGRES_DB, From 25af97ebb8677033ff3635d01e05e15448331775 Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Sat, 25 Sep 2021 12:17:24 +0300 Subject: [PATCH 2/6] Add "postgresql-pg8000" extra dep --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 827b4d350..59a3be05c 100644 --- a/setup.py +++ b/setup.py @@ -57,6 +57,7 @@ 'mysql': ['sqlalchemy', 'pymysql'], 'oracle': ['sqlalchemy', 'cx_Oracle'], 'postgresql': ['sqlalchemy', 'psycopg2-binary'], + 'postgresql-pg8000': ['sqlalchemy', 'pg8000'], 'selenium': ['selenium'], 'google-cloud-pubsub': ['google-cloud-pubsub < 2'], 'mongo': ['pymongo'], From 4a311aa2e7442ab70c3b1dc72ad2087587a2c5d3 Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Mon, 4 Apr 2022 21:50:14 +0300 Subject: [PATCH 3/6] Update setup.py Co-authored-by: Till Hoffmann --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 59a3be05c..827b4d350 100644 --- a/setup.py +++ b/setup.py @@ -57,7 +57,6 @@ 'mysql': ['sqlalchemy', 'pymysql'], 'oracle': ['sqlalchemy', 'cx_Oracle'], 'postgresql': ['sqlalchemy', 'psycopg2-binary'], - 'postgresql-pg8000': ['sqlalchemy', 'pg8000'], 'selenium': ['selenium'], 'google-cloud-pubsub': ['google-cloud-pubsub < 2'], 'mongo': ['pymongo'], From 4fd0cba81598540fc4dc27a9b7678acd1423c4c2 Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Mon, 4 Apr 2022 21:52:15 +0300 Subject: [PATCH 4/6] Update postgres.py --- testcontainers/postgres.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/testcontainers/postgres.py b/testcontainers/postgres.py index 23ca87b5f..3f4ddb36d 100644 --- a/testcontainers/postgres.py +++ b/testcontainers/postgres.py @@ -21,17 +21,12 @@ class PostgresContainer(DbContainer): Example ------- - The example spins up a Postgres database and connects to it using the (default) :code:`psycopg` driver. + The example spins up a Postgres database and connects to it using the :code:`psycopg` driver. :: with PostgresContainer("postgres:9.5") as postgres: e = sqlalchemy.create_engine(postgres.get_connection_url()) result = e.execute("select version()") - - :: - with PostgresContainer("postgres:9.5", driver="pg8000") as postgres: - e = sqlalchemy.create_engine(postgres.get_connection_url()) - result = e.execute("select version()") """ POSTGRES_USER = os.environ.get("POSTGRES_USER", "test") POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test") From a3b183849a0b7bee9ce00f0adb3ae7c15a73296d Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Mon, 4 Apr 2022 22:38:01 +0300 Subject: [PATCH 5/6] Update postgres.py --- testcontainers/postgres.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testcontainers/postgres.py b/testcontainers/postgres.py index 3f4ddb36d..a69fac571 100644 --- a/testcontainers/postgres.py +++ b/testcontainers/postgres.py @@ -32,7 +32,12 @@ class PostgresContainer(DbContainer): POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test") POSTGRES_DB = os.environ.get("POSTGRES_DB", "test") - def __init__(self, image="postgres:latest", port=5432, user=None, password=None, dbname=None, driver="psycopg2"): + def __init__(self, + image="postgres:latest", + port=5432, user=None, + password=None, + dbname=None, + driver="psycopg2"): super(PostgresContainer, self).__init__(image=image) self.POSTGRES_USER = user or self.POSTGRES_USER self.POSTGRES_PASSWORD = password or self.POSTGRES_PASSWORD From a3bd591521e6d307680aa0b38a4121594358b81b Mon Sep 17 00:00:00 2001 From: Karolis Labrencis Date: Mon, 4 Apr 2022 22:42:53 +0300 Subject: [PATCH 6/6] fix whitespace --- testcontainers/postgres.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/testcontainers/postgres.py b/testcontainers/postgres.py index a69fac571..681fd73d4 100644 --- a/testcontainers/postgres.py +++ b/testcontainers/postgres.py @@ -32,11 +32,11 @@ class PostgresContainer(DbContainer): POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test") POSTGRES_DB = os.environ.get("POSTGRES_DB", "test") - def __init__(self, - image="postgres:latest", - port=5432, user=None, - password=None, - dbname=None, + def __init__(self, + image="postgres:latest", + port=5432, user=None, + password=None, + dbname=None, driver="psycopg2"): super(PostgresContainer, self).__init__(image=image) self.POSTGRES_USER = user or self.POSTGRES_USER