Skip to content

Allow to use dialect mssql+pyodbc #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mssql/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
install_requires=[
"testcontainers-core",
"sqlalchemy",
# TODO: convert these to extras
"pymssql",
"pyodbc",
],
python_requires=">=3.7",
)
60 changes: 52 additions & 8 deletions mssql/testcontainers/mssql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from os import environ
from typing import Optional
from typing import Optional, Literal
from testcontainers.core.generic import DbContainer


Expand All @@ -19,9 +19,31 @@ class SqlServerContainer(DbContainer):
... result = e.execute("select @@VERSION")
"""

def __init__(self, image: str = "mcr.microsoft.com/mssql/server:2019-latest", user: str = "SA",
password: Optional[str] = None, port: int = 1433, dbname: str = "tempdb",
dialect: str = 'mssql+pymssql', **kwargs) -> None:
def __init__(
self,
image: str = "mcr.microsoft.com/mssql/server:2019-latest",
user: str = "SA",
password: Optional[str] = None,
port: int = 1433,
dbname: str = "tempdb",
dialect: Literal["mssql+pymssql", "mssql+pyodbc"] = "mssql+pymssql",
**kwargs,
) -> None:
"""
Initialize SqlServerContainer

Args:
image: MSSQL Server image. For example, use a specific version
user: DB user name
password: DB password
port: Port to be exposed
dbname: Database name
dialect: SQLAlchemy database dialect. Allowed values are
* 'mssql+pymssql': Uses `pymssql <https://github.com/pymssql/pymssql>`_ driver
* 'mssql+pyodbc': Uses `pyodbc <https://github.com/mkleehammer/pyodbc>`_ driver
This also defines the driver that is used to connect to the database.
kwargs: Keyword arguments passed to initialization of underlying docker container
"""
super(SqlServerContainer, self).__init__(image, **kwargs)

self.port_to_expose = port
Expand All @@ -36,10 +58,32 @@ def _configure(self) -> None:
self.with_env("SA_PASSWORD", self.SQLSERVER_PASSWORD)
self.with_env("SQLSERVER_USER", self.SQLSERVER_USER)
self.with_env("SQLSERVER_DBNAME", self.SQLSERVER_DBNAME)
self.with_env("ACCEPT_EULA", 'Y')
self.with_env("ACCEPT_EULA", "Y")

def get_connection_url(self) -> str:
return super()._create_connection_url(
dialect=self.dialect, username=self.SQLSERVER_USER, password=self.SQLSERVER_PASSWORD,
db_name=self.SQLSERVER_DBNAME, port=self.port_to_expose
url = super()._create_connection_url(
dialect=self.dialect,
username=self.SQLSERVER_USER,
password=self.SQLSERVER_PASSWORD,
db_name=self.SQLSERVER_DBNAME,
port=self.port_to_expose,
)
if self.dialect == "mssql+pyodbc":
url += f"?driver={self._get_url_suffix_for_latest_pyodbc_version()}"
return url

def _get_url_suffix_for_latest_pyodbc_version(self):
import pyodbc
import re

r = re.compile(r"ODBC Driver \d{1,2} for SQL Server")
# We sort drivers in reversed order to get the latest
drivers = sorted(list(filter(r.match, pyodbc.drivers())), reverse=True)
Copy link
Collaborator

@tillahoffmann tillahoffmann Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to convert to int first. We can probably just use max here rather than sorting and taking the first.

version_numbers = [int(v) for v in re.findall(r"\d{1,2}", "".join(drivers))]
max_version_index = version_numbers.index(max(version_numbers))
if len(drivers) > 0:
driver_str = drivers[max_version_index].replace(" ", "+")
else:
raise ImportError(f"No driver available for using dialect {self.dialect}")

return driver_str
37 changes: 28 additions & 9 deletions mssql/tests/test_mssql.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import re

import sqlalchemy
from testcontainers.mssql import SqlServerContainer
from unittest.mock import patch


def test_docker_run_mssql():
image = 'mcr.microsoft.com/azure-sql-edge'
dialect = 'mssql+pymssql'
with SqlServerContainer(image, dialect=dialect) as mssql:
e = sqlalchemy.create_engine(mssql.get_connection_url())
result = e.execute('select @@servicename')
for row in result:
assert row[0] == 'MSSQLSERVER'
image = "mcr.microsoft.com/azure-sql-edge"
dialects = ["mssql+pymssql", "mssql+pyodbc"]
ends_withs = ["tempdb", "for+SQL+Server"]
for dialect, end_with in zip(dialects, ends_withs):
with SqlServerContainer(dialect=dialect) as mssql:
url = mssql.get_connection_url()
assert url.endswith(end_with)
e = sqlalchemy.create_engine(url)
result = e.execute("select @@servicename")
for row in result:
assert row[0] == "MSSQLSERVER"

with SqlServerContainer(image, password="1Secure*Password2", dialect=dialect) as mssql:
e = sqlalchemy.create_engine(mssql.get_connection_url())
result = e.execute('select @@servicename')
result = e.execute("select @@servicename")
for row in result:
assert row[0] == 'MSSQLSERVER'
assert row[0] == "MSSQLSERVER"


def test_get_url_suffix_for_latest_pyodbc_version():
container = SqlServerContainer()

version_numbers = [10, 8]
with patch(
"pyodbc.drivers", return_value=[f"ODBC Driver {v} for SQL Server" for v in version_numbers]
):
driver_str = container._get_url_suffix_for_latest_pyodbc_version()
latest_version = int(re.findall(r"\d{1,2}", driver_str)[0])
assert latest_version == max(version_numbers)