Skip to content
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
16 changes: 14 additions & 2 deletions pytest_mongodb/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,24 @@ def pytest_addoption(parser):
help="The name of the database where fixtures are created [pytest]",
default="pytest",
)
parser.addini(
name="mongodb_tz_aware",
help="The value of tz_aware attribute for MongoDB client [False]",
type="bool",
default=False,
)

parser.addoption("--mongodb-fixture-dir", help="Try loading fixtures from this directory")
parser.addoption("--mongodb-engine", help="The database engine to use [mongomock]")
parser.addoption("--mongodb-host", help="The host where the mongodb-server runs")
parser.addoption(
"--mongodb-dbname", help="The name of the database where fixtures are created [pytest]"
)
parser.addoption(
"--mongodb-tz-aware",
help="The value of tz_aware attribute for MongoDB client",
action="store_true",
)


def pytest_configure(config):
Expand All @@ -66,12 +77,13 @@ def mongodb(pytestconfig):
def make_mongo_client(config):
engine = config.getoption("mongodb_engine") or config.getini("mongodb_engine")
host = config.getoption("mongodb_host") or config.getini("mongodb_host")
tz_aware = config.getoption("mongodb_tz_aware") or config.getini("mongodb_tz_aware")
if engine == "pymongo":
if not HAVE_PYMONGO:
pytest.fail("PyMongo is not installed.")
client = pymongo.MongoClient(host)
client = pymongo.MongoClient(host, tz_aware=tz_aware)
else:
client = mongomock.MongoClient(host)
client = mongomock.MongoClient(host, tz_aware=tz_aware)
return client


Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,27 @@ def test_insert(mongodb):

def test_mongo_engine(pytestconfig):
assert plugin.mongo_engine() == "mongomock"


def test_tz_aware_is_false(pytester):
pytester.makepyfile(
"""
def test_func(mongodb):
assert mongodb.client._tz_aware is False
"""
)

result = pytester.runpytest()
result.assert_outcomes(passed=1)


def test_tz_aware_is_true(pytester):
pytester.makepyfile(
"""
def test_func(mongodb):
assert mongodb.client._tz_aware is True
"""
)

result = pytester.runpytest("--mongodb-tz-aware")
result.assert_outcomes(passed=1)