Skip to content

Commit efe3fd2

Browse files
committed
Add skip_if_rust_bson pytest marker definition
Define skip_if_rust_bson using pytest.mark.skipif to conditionally skip tests when the Rust BSON extension is being used. This marker is used to skip tests for features not yet implemented in Rust. Added to async version and let synchro generate the sync version.
1 parent 64faa6d commit efe3fd2

2 files changed

Lines changed: 32 additions & 28 deletions

File tree

test/__init__.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@
3232
import warnings
3333
from inspect import iscoroutinefunction
3434

35-
from pymongo.synchronous.uri_parser import parse_uri
35+
import pytest
36+
3637
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
3738
from pymongo.errors import AutoReconnect
39+
from pymongo.synchronous.uri_parser import parse_uri
3840

3941
try:
4042
import ipaddress
4143

4244
HAVE_IPADDRESS = True
4345
except ImportError:
4446
HAVE_IPADDRESS = False
45-
from contextlib import contextmanager, contextmanager
47+
from contextlib import contextmanager
4648
from functools import partial, wraps
4749
from typing import Any, Callable, Dict, Generator, overload
4850
from unittest import SkipTest
@@ -51,12 +53,12 @@
5153
import pymongo
5254
import pymongo.errors
5355
from bson.son import SON
54-
from pymongo.synchronous.database import Database
55-
from pymongo.synchronous.mongo_client import MongoClient
5656
from pymongo.common import partition_node
5757
from pymongo.hello import HelloCompat
5858
from pymongo.server_api import ServerApi
5959
from pymongo.ssl_support import HAVE_SSL, _ssl # type:ignore[attr-defined]
60+
from pymongo.synchronous.database import Database
61+
from pymongo.synchronous.mongo_client import MongoClient
6062

6163
sys.path[0:0] = [""]
6264

@@ -84,6 +86,14 @@
8486

8587
_IS_SYNC = True
8688

89+
import bson
90+
91+
# Skip tests when using Rust BSON extension for features not yet implemented
92+
skip_if_rust_bson = pytest.mark.skipif(
93+
bson.get_bson_implementation() == "rust",
94+
reason="Feature not yet implemented in Rust BSON extension",
95+
)
96+
8797

8898
def _connection_string(h):
8999
if h.startswith(("mongodb://", "mongodb+srv://")):
@@ -1006,9 +1016,7 @@ def _unmanaged_async_mongo_client(
10061016
client._connect()
10071017
return client
10081018

1009-
def _async_mongo_client(
1010-
self, host, port, authenticate=True, directConnection=None, **kwargs
1011-
):
1019+
def _async_mongo_client(self, host, port, authenticate=True, directConnection=None, **kwargs):
10121020
"""Create a new client over SSL/TLS if necessary."""
10131021
host = host or client_context.host
10141022
port = port or client_context.port
@@ -1055,9 +1063,7 @@ def unmanaged_single_client(
10551063
return cls._unmanaged_async_mongo_client(h, p, directConnection=True, **kwargs)
10561064

10571065
@classmethod
1058-
def unmanaged_rs_client(
1059-
cls, h: Any = None, p: Any = None, **kwargs: Any
1060-
) -> MongoClient[dict]:
1066+
def unmanaged_rs_client(cls, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient[dict]:
10611067
"""Connect to the replica set and authenticate if necessary."""
10621068
return cls._unmanaged_async_mongo_client(h, p, **kwargs)
10631069

@@ -1086,25 +1092,17 @@ def single_client_noauth(
10861092
self, h: Any = None, p: Any = None, **kwargs: Any
10871093
) -> MongoClient[dict]:
10881094
"""Make a direct connection. Don't authenticate."""
1089-
return self._async_mongo_client(
1090-
h, p, authenticate=False, directConnection=True, **kwargs
1091-
)
1095+
return self._async_mongo_client(h, p, authenticate=False, directConnection=True, **kwargs)
10921096

1093-
def single_client(
1094-
self, h: Any = None, p: Any = None, **kwargs: Any
1095-
) -> MongoClient[dict]:
1097+
def single_client(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient[dict]:
10961098
"""Make a direct connection, and authenticate if necessary."""
10971099
return self._async_mongo_client(h, p, directConnection=True, **kwargs)
10981100

1099-
def rs_client_noauth(
1100-
self, h: Any = None, p: Any = None, **kwargs: Any
1101-
) -> MongoClient[dict]:
1101+
def rs_client_noauth(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient[dict]:
11021102
"""Connect to the replica set. Don't authenticate."""
11031103
return self._async_mongo_client(h, p, authenticate=False, **kwargs)
11041104

1105-
def rs_client(
1106-
self, h: Any = None, p: Any = None, **kwargs: Any
1107-
) -> MongoClient[dict]:
1105+
def rs_client(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient[dict]:
11081106
"""Connect to the replica set and authenticate if necessary."""
11091107
return self._async_mongo_client(h, p, **kwargs)
11101108

@@ -1117,9 +1115,7 @@ def rs_or_single_client_noauth(
11171115
"""
11181116
return self._async_mongo_client(h, p, authenticate=False, **kwargs)
11191117

1120-
def rs_or_single_client(
1121-
self, h: Any = None, p: Any = None, **kwargs: Any
1122-
) -> MongoClient[Any]:
1118+
def rs_or_single_client(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient[Any]:
11231119
"""Connect to the replica set if there is one, otherwise the standalone.
11241120
11251121
Authenticates if necessary.
@@ -1135,9 +1131,7 @@ def simple_client(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoCli
11351131
return client
11361132

11371133
@classmethod
1138-
def unmanaged_simple_client(
1139-
cls, h: Any = None, p: Any = None, **kwargs: Any
1140-
) -> MongoClient:
1134+
def unmanaged_simple_client(cls, h: Any = None, p: Any = None, **kwargs: Any) -> MongoClient:
11411135
if not h and not p:
11421136
client = MongoClient(**kwargs)
11431137
else:

test/asynchronous/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import warnings
3333
from inspect import iscoroutinefunction
3434

35+
import pytest
36+
3537
from pymongo.asynchronous.uri_parser import parse_uri
3638
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
3739
from pymongo.errors import AutoReconnect
@@ -84,6 +86,14 @@
8486

8587
_IS_SYNC = False
8688

89+
import bson
90+
91+
# Skip tests when using Rust BSON extension for features not yet implemented
92+
skip_if_rust_bson = pytest.mark.skipif(
93+
bson.get_bson_implementation() == "rust",
94+
reason="Feature not yet implemented in Rust BSON extension",
95+
)
96+
8797

8898
def _connection_string(h):
8999
if h.startswith(("mongodb://", "mongodb+srv://")):

0 commit comments

Comments
 (0)