diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f9351226..337ad755 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,14 +17,23 @@ concurrency: jobs: pytester: runs-on: ${{ matrix.os }} - environment: - name: test strategy: fail-fast: false matrix: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] - os: [ubuntu-latest, macOS-latest, windows-latest] + os: ["ubuntu-latest", "macOS-latest", "windows-latest"] backend: ["local", "db"] + exclude: + # ToDo: take if back when the connection become stable + # or resolve using `InMemoryMongoClient` + - { os: "macOS-latest", backend: "db" } + env: + CACHIER_TEST_HOST: "localhost" + CACHIER_TEST_PORT: "27017" + #CACHIER_TEST_DB: "dummy_db" + #CACHIER_TEST_USERNAME: "myuser" + #CACHIER_TEST_PASSWORD: "yourpassword" + CACHIER_TEST_VS_DOCKERIZED_MONGO: "true" steps: - uses: actions/checkout@v4 @@ -38,23 +47,46 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install -e . -r tests/requirements.txt + - name: Unit tests (local) if: matrix.backend == 'local' run: pytest -m "not mongo" + + - name: Setup docker (missing on MacOS) + if: runner.os == 'macOS' && matrix.backend == 'db' + run: | + brew install docker + colima start + # For testcontainers to find the Colima socket + sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock + # ToDo: find a way to cache docker images + #- name: Cache Container Images + # if: matrix.backend == 'db' + # uses: borda/cache-container-images-action@b32a5e804cb39af3c3d134fc03ab76eac0bfcfa9 + # with: + # prefix-key: "mongo-db" + # images: mongo:latest + - name: Start MongoDB in docker + if: matrix.backend == 'db' + run: | + # start MongoDB in a container + docker run -d -p ${{ env.CACHIER_TEST_PORT }}:27017 --name mongodb mongo:latest + # wait for MongoDB to start, which is in average 5 seconds + sleep 5 + # show running containers + docker ps -a - name: Unit tests (DB) if: matrix.backend == 'db' - env: - CACHIER_TEST_HOST: ${{ secrets.CACHIER_TEST_HOST }} - CACHIER_TEST_DB: ${{ secrets.CACHIER_TEST_DB }} - CACHIER_TEST_USERNAME: ${{ secrets.CACHIER_TEST_USERNAME }} - CACHIER_TEST_PASSWORD: ${{ secrets.CACHIER_TEST_PASSWORD }} - CACHIER_TEST_VS_LIVE_MONGO: ${{ secrets.CACHIER_TEST_VS_LIVE_MONGO }} run: pytest -m "mongo" + - name: "Upload coverage to Codecov" + continue-on-error: true uses: codecov/codecov-action@v4 with: fail_ci_if_error: true + name: codecov-umbrella token: ${{ secrets.CODECOV_TOKEN }} # required + flags: ${{ matrix.backend }} testing-guardian: runs-on: ubuntu-latest diff --git a/README.rst b/README.rst index 21e5b970..8770e8c7 100644 --- a/README.rst +++ b/README.rst @@ -404,7 +404,7 @@ To run all tests EXCEPT memory core AND MongoDB core related tests, use: 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. +**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_DOCKERIZED_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. Contributors 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. diff --git a/tests/test_mongo_core.py b/tests/test_mongo_core.py index 93c4a2ce..749048c3 100644 --- a/tests/test_mongo_core.py +++ b/tests/test_mongo_core.py @@ -28,33 +28,31 @@ class CfgKey: HOST = "TEST_HOST" - UNAME = "TEST_USERNAME" - PWD = "TEST_PASSWORD" - DB = "TEST_DB" - TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO" + PORT = "TEST_PORT" + # UNAME = "TEST_USERNAME" + # PWD = "TEST_PASSWORD" + # DB = "TEST_DB" + TEST_VS_DOCKERIZED_MONGO = "TEST_VS_DOCKERIZED_MONGO" CFG = Birch( namespace="cachier", - defaults={ - CfgKey.TEST_VS_LIVE_MONGO: False, - }, + defaults={CfgKey.TEST_VS_DOCKERIZED_MONGO: False}, ) -URI_TEMPLATE = ( - "mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority" -) +# URI_TEMPLATE = "mongodb://myUser:myPassword@localhost:27017/" +URI_TEMPLATE = "mongodb://{host}:{port}?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 + port = quote_plus(CFG[CfgKey.PORT]) + # uname = quote_plus(CFG[CfgKey.UNAME]) + # pwd = quote_plus(CFG[CfgKey.PWD]) + # db = quote_plus(CFG[CfgKey.DB]) + uri = f"mongodb://{host}:{port}?retrywrites=true&w=majority" + return MongoClient(uri) _COLLECTION_NAME = ( @@ -65,7 +63,7 @@ def _get_cachier_db_mongo_client(): def _test_mongetter(): if not hasattr(_test_mongetter, "client"): - if CFG.mget(CfgKey.TEST_VS_LIVE_MONGO, bool): + if str(CFG.mget(CfgKey.TEST_VS_DOCKERIZED_MONGO)).lower() == "true": print("Using live MongoDB instance for testing.") _test_mongetter.client = _get_cachier_db_mongo_client() else: