From 230ae8d8af644892cf91b2c3b50c9057792cc2e9 Mon Sep 17 00:00:00 2001 From: "T. K. Stenczel" Date: Thu, 13 Jun 2024 16:21:07 +0200 Subject: [PATCH] test for the functionality --- pyproject.toml | 1 + tests/test_mongodb_srv.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/test_mongodb_srv.py diff --git a/pyproject.toml b/pyproject.toml index b2d5c162..fc870cce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ lark = "^1.1.9" mongomock = "^4.1.2" pytest = "^8.2.2" pytest-cov = "^5.0.0" +pytest-mock = "^3.14.0" [tool.poetry.extras] tests = ["mongomock", "pytest", "pytest-cov"] diff --git a/tests/test_mongodb_srv.py b/tests/test_mongodb_srv.py new file mode 100644 index 00000000..b261b7ec --- /dev/null +++ b/tests/test_mongodb_srv.py @@ -0,0 +1,27 @@ +"""Tests for supporting `mongodb+srv://` URIs""" + +from pytest import fixture + +from abcd import ABCD +from abcd.backends.atoms_pymongo import MongoDatabase + + +@fixture +def mongo_srv(mocker): + # mongomock does not pick up what we need, so this is a bespoke mocker + mock_client = mocker.MagicMock() + mocker.patch("abcd.backends.atoms_pymongo.MongoClient", mock_client) + return mock_client + + +def test_init_mongodb_srv(mongo_srv): + # client can be created with a mongodb+srv:// URI + + # regression test + uri = "mongodb+srv://user:pass@democluster.randomstr.mongodb.net/?key=value" + + # create the client + abcd = ABCD.from_url(uri) + + assert isinstance(abcd, MongoDatabase) + mongo_srv.assert_called_once_with(host=uri, authSource="admin")