Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_one() doesn't work with object ID #869

Open
SCSR-is-too-short-username opened this issue Dec 13, 2023 · 1 comment
Open

find_one() doesn't work with object ID #869

SCSR-is-too-short-username opened this issue Dec 13, 2023 · 1 comment

Comments

@SCSR-is-too-short-username

I'm trying to write tests for functions that rely on using object IDs. These work fine when running normally, but while writing unit tests for the functions I noticed that using object IDs doesn't work when using mongomock.

Here is some code I have:

# test_versioning.py

import os

import mongomock
import pytest
from bson import ObjectId

import app.services.documentdb as doc_db


@pytest.fixture(autouse=True)
def clean_up(mocker):
    db = mongomock.MongoClient()
    mocker.patch("app.services.documentdb.get", return_value=db)

    database = db[os.environ["DOC_DB_NAME"]]
    coll_data = database[os.environ["DOC_DB_DATA_COLLECTION"]]
    coll_vh = database[os.environ["DOC_DB_VERSION_HISTORY_COLLECTION"]]

    coll_data.insert_one(
        {"_id": ObjectId("65780d19d8df6164d4f356ad"), "Instance": "Instance 1", "Name": "Name 1", "Type": "Type 2", "SerialNumber": "01234567"}
    )
    coll_vh.insert_one(
        {
            "revision_for": ObjectId("65780d19d8df6164d4f356ad"),
            "source_collection": "datacollection",
            "current_revision": 2,
            "revisions": [
                {
                    "revision": 1,
                    "updated_by": "test_person",
                    "update_time": "7-12-2023-12-30",
                    "changed_fields": {},
                    "added_fields": {"Instance": "Instance 1", "Name": "Name 1", "Type": "Type 1", "SerialNumber": "01234567"},
                },
                {
                    "revision": 2,
                    "updated_by": "test_person",
                    "update_time": "8-12-2023-12-30",
                    "changed_fields": {"Type": "Type 2"},
                    "added_fields": {},
                },
            ],
        }
    )

    mocker.patch("app.services.access.get_user_access", return_value=True)

    yield

    db.drop_database(os.environ["DOC_DB_NAME"])


def test_get_revision_history():
    response = doc_db.get_full_revision_doc("DOC_DB_DATA_COLLECTION", "65780d19d8df6164d4f356ad")

    assert response["source_collection"] == "datacollection"

# documentdb.py

def get_full_revision_doc(source_coll, doc_id):
    db = get()
    database = db[os.environ["DOC_DB_NAME"]]
    collection = database[os.environ["DOC_DB_VERSION_HISTORY_COLLECTION"]]

    return collection.find_one({"revision_for": ObjectId(doc_id), "source_collection": os.environ[source_coll]})

This is a very important feature for our project and we need robust unit tests for it to make sure everything works. With this issue present in mongomock, it is not possible. I hope you can get this fixed soon!

@warownia1
Copy link

warownia1 commented Apr 15, 2024

I tried to isolate and reproduce the problem you described, but I couldn't. Here is the code I used in my test. I removed all pieces of code not relevant to the described issue. Tested on Python 3.12 and both mongomock 4.0.0 and 4.1.2

import mongomock
import pytest
from bson import ObjectId


DOC_DB_NAME = "mydatabasename"
DOC_DB_VERSION_HISTORY_COLLECTION = "mycollectionname"


@pytest.fixture
def database():
  db = mongomock.MongoClient()
  database = db[DOC_DB_NAME]
  yield database
  db.drop_database(database)


def test_get_revision_history(database):
  coll_vh = database[DOC_DB_VERSION_HISTORY_COLLECTION]
  coll_vh.insert_one(
    {
      "revision_for": ObjectId("65780d19d8df6164d4f356ad"),
      "source_collection": "datacollection",
      "current_revision": 2,
      # e.t.c
    }
  )
  item = coll_vh.find_one({"revision_for": ObjectId("65780d19d8df6164d4f356ad")})
  assert item is not None

The test passes, so it doesn't seem to be an issue with the mongomock. I suggest you check if the MongoClient object you set in a mock and one you access later are the same object. The way you set up the test and use global module to store and retrieve the client suggests me the problem could be with the mocking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants