michaelkryukov / mongomock_motor

Library for mocking AsyncIOMotorClient built on top of mongomock.
MIT License
94 stars 23 forks source link

Add support for $addToSet with bool value #42

Closed nivhanin closed 6 months ago

nivhanin commented 7 months ago

The problem arises when we try to use the addToSet function with a boolean field.

In our use case, we have a boolean field 'is_flag'. When we try to use addToSet to ensure that we only have unique values in this field, the mock library does not support this operation. This is inconsistent with the real MongoDB functionality, which does support addToSet on boolean fields, and this inconsistency is impeding our ability to conduct accurate unit tests.

We kindly request that this issue be addressed, as it is critical for our code to function as expected.

Steps to reproduce the issue:

Create a boolean field in a MongoDB document. Code query example:

pipeline = [
    {"$match": {"my_id": my_id},
    {
        "$group": {
             # We group by 0 to get all the documents in the collection
            # and then we use $addToSet to get the unique values
            "_id": 0,
            "flags": {"$addToSet": "$is_flag"},
         }
    },
]
flags = await myDocument.aggregate(pipeline).to_list(1)

Attempt to use addToSet function on this boolean field. Expected Outcome: The addToSet function should work on boolean fields, ensuring that there are only unique values in this field. 'flags': [False] Actual Outcome: The addToSet function does not support boolean fields, causing unexpected behavior returns: 'flags': [None].

We look forward to your help in resolving this issue. Thank you.

nivhanin commented 7 months ago

@michaelkryukov

I think mongomock not support boolean (although mongo does support it):

elif operator == '$addToSet':
    value = []
    val_it = (val or None for val in values)
    # Don't use set in case elt in not hashable (like dicts).
    for elt in val_it:
        if elt not in value:
            value.append(elt)
    doc_dict[field] = value
michaelkryukov commented 7 months ago

Yeah, that does look like an issue with mongomock. You should create issue in https://github.com/mongomock/mongomock. I don't think that patching insides of the mongomock in this library will be maintainable solution.

nivhanin commented 6 months ago

https://github.com/mongomock/mongomock/issues/868 :)