Skip to content

Commit

Permalink
Bringing back live mongodb tests as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
shaypal5 committed Dec 4, 2023
1 parent 5776ee8 commit 0b17025
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ To run all tests EXCEPT memory core AND MongoDB core related tests, use:
pytest -m "not (mongo or memory)"
Running MongoDB tests against a live MongoDB instance
-----------------------------------------------------

**Note to developers:** By default, all MongoDB tests are run against a mocked MongoDB instance, provided by the ``pymongo_inmemory`` package. To run them against a live MongoDB instance, the ``CACHIER_TEST_VS_LIVE_MONGO`` environment variable is set to ``True`` in the ``test`` environment of this repository (and additional environment variables are populated with the appropriate credentials), used by the GitHub Action running tests on every commit and pull request.

Contributers are not expected to run these tests against a live MongoDB instance when developing, as credentials for the testing instance used will NOT be shared, but rather use the testing against the in-memory MongoDB instance as a good proxy. HOWEVER, the tests run against a live MongoDB instance when you submit a PR are the determining tests for deciding whether your code functions correctly against MongoDB.


Adding documentation
--------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

TEST_REQUIRES = [
# tests and coverages
'pytest', 'coverage', 'pytest-cov',
'pytest', 'coverage', 'pytest-cov', 'birch',
# linting and code quality
'bandit', 'flake8', 'pylint', 'safety',
# type checking
Expand Down
39 changes: 37 additions & 2 deletions tests/test_mongo_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,60 @@
from datetime import timedelta
from random import random
from time import sleep
from urllib.parse import quote_plus

from birch import Birch
import pandas as pd
import pymongo
import pytest
from pymongo.errors import OperationFailure
from pymongo_inmemory import MongoClient
from pymongo.mongo_client import MongoClient
from pymongo_inmemory import MongoClient as InMemoryMongoClient

from cachier import cachier
from cachier.base_core import RecalculationNeeded
from cachier.mongo_core import _MongoCore


# === Enables testing vs a real MongoDB instance ===
CFG = Birch("cachier")


class CfgKey():
HOST = "TEST_HOST"
UNAME = "TEST_USERNAME"
PWD = "TEST_PASSWORD"
DB = "TEST_DB"
TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO"


URI_TEMPLATE = (
"mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority")


def _get_cachier_db_mongo_client():
host = quote_plus(CFG[CfgKey.HOST])
uname = quote_plus(CFG[CfgKey.UNAME])
pwd = quote_plus(CFG[CfgKey.PWD])
db = quote_plus(CFG[CfgKey.DB])
uri = URI_TEMPLATE.format(host=host, uname=uname, pwd=pwd, db=db)
client = MongoClient(uri)
return client


_COLLECTION_NAME = 'cachier_test_{}_{}.{}.{}'.format(
platform.system(), sys.version_info[0], sys.version_info[1],
sys.version_info[2])


def _test_mongetter():
if not hasattr(_test_mongetter, 'client'):
_test_mongetter.client = MongoClient()
if CFG.mget(CfgKey.TEST_VS_LIVE_MONGO, bool):
print("Using live MongoDB instance for testing.")
_test_mongetter.client = _get_cachier_db_mongo_client()
else:
print("Using in-memory MongoDB instance for testing.")
_test_mongetter.client = InMemoryMongoClient()
db_obj = _test_mongetter.client['cachier_test']
if _COLLECTION_NAME not in db_obj.list_collection_names():
db_obj.create_collection(_COLLECTION_NAME)
Expand Down

0 comments on commit 0b17025

Please sign in to comment.