|
1 | 1 | """Testing the MongoDB core of cachier.""" |
2 | 2 |
|
| 3 | +# standard library imports |
3 | 4 | import datetime |
4 | 5 | import hashlib |
5 | 6 | import platform |
6 | 7 | import queue |
7 | 8 | import sys |
8 | 9 | import threading |
9 | | -import warnings |
10 | 10 | from random import random |
11 | 11 | from time import sleep |
12 | 12 | from urllib.parse import quote_plus |
13 | 13 |
|
| 14 | +# third-party imports |
| 15 | +import pytest |
| 16 | +from birch import Birch # type: ignore[import-not-found] |
14 | 17 | try: |
15 | 18 | import pandas as pd |
16 | 19 | except (ImportError, ModuleNotFoundError): |
17 | 20 | pd = None |
18 | | - warnings.warn("pandas is not installed; tests requiring pandas will fail!") |
| 21 | + print("pandas is not installed; tests requiring pandas will fail!") |
19 | 22 |
|
20 | 23 | try: |
21 | 24 | import pymongo |
22 | 25 | from pymongo.errors import OperationFailure |
23 | 26 | from pymongo.mongo_client import MongoClient |
24 | | - from pymongo_inmemory import MongoClient as InMemoryMongoClient |
25 | 27 | except (ImportError, ModuleNotFoundError): |
| 28 | + print("pymongo is not installed; tests requiring pymongo will fail!") |
26 | 29 | pymongo = None |
27 | | - MongoClient = None |
28 | | - InMemoryMongoClient = None |
29 | 30 | OperationFailure = None |
30 | | - warnings.warn( |
31 | | - "pymongo is not installed; tests requiring pymongo will fail!" |
32 | | - ) |
33 | | -import pytest |
34 | | -from birch import Birch # type: ignore[import-not-found] |
| 31 | + # define a mock MongoClient class that will raise an exception |
| 32 | + # on init, warning that pymongo is not installed |
| 33 | + class MongoClient: |
| 34 | + def __init__(self, *args, **kwargs): |
| 35 | + raise ImportError("pymongo is not installed!") |
| 36 | +try: |
| 37 | + from pymongo_inmemory import MongoClient as InMemoryMongoClient |
| 38 | +except (ImportError, ModuleNotFoundError): |
| 39 | + class InMemoryMongoClient: |
| 40 | + def __init__(self, *args, **kwargs): |
| 41 | + raise ImportError("pymongo_inmemory is not installed!") |
| 42 | + print("pymongo_inmemory is not installed; " |
| 43 | + "in-memory MongoDB tests will fail!") |
35 | 44 |
|
| 45 | +# local imports |
36 | 46 | from cachier import cachier |
37 | 47 | from cachier.config import CacheEntry |
38 | 48 | from cachier.cores.base import RecalculationNeeded |
39 | 49 | from cachier.cores.mongo import _MongoCore |
40 | 50 |
|
| 51 | + |
41 | 52 | # === Enables testing vs a real MongoDB instance === |
42 | 53 |
|
43 | 54 |
|
|
0 commit comments