mongomock / mongomock

Small library for mocking pymongo collection objects for testing purposes
ISC License
951 stars 344 forks source link

mongomock map_reduce shows "KeyError: 'value'" (works fine in pymongo) #282

Open singh2005 opened 7 years ago

singh2005 commented 7 years ago

I'm creating my fake_collection as follows. Then I want to map the primary_key with 'duration'. I want to calculate the median of all 'duration' values so I can not use reduce function (since it can not calculate median). This test fails at the map_reduce call because the mapped_list does not contain 'values' key. This error only occurs when I'm using my fake mongomock database. This error does not occur when I provide my real mongoDB client.

from unittest import TestCase
import mongomock
from aggregateCtaStatuses import AggregateCtaStatuses
from bson.code import Code

 def test_mongomock_map_reduce(self):
    # Arrange
    fake_etap = mongomock.MongoClient().db
    fake_statuses_collection = fake_etap.create_collection('statuses')
    fake_config_id = "this_is_config_id"
    test_name = "this_is_test_name"
    fake_statuses_objects = [
        {
            "testID": test_name,
            "kind": "Test",
            "duration": 8392,
            "configID": fake_config_id
        },
        {
            "testID": test_name,
            "kind": "Test",
            "duration": 8393,
            "configID": fake_config_id
        },
        {
            "testID": test_name,
            "kind": "Test",
            "duration": 8394,
            "configID": fake_config_id
        }
    ]
    fake_statuses_collection.insert_many(fake_statuses_objects)

    map_function = Code("function(){emit(this._id, this.duration);}")
    reduce_function = Code("function() {}")
    search_query = {'configID': fake_config_id, 'kind': 'Test', 'testID': test_name}

    # Act
    result = fake_etap.statuses.map_reduce(map_function, reduce_function, "my_collection", query=search_query)

    # Assert
    self.assertEqual(result.__len__(), 3)
ArthurJahn commented 7 years ago

Same problem here. My map_reduce code works fine in pymongo, but my tests break because of mongomock.

davidCarlos commented 7 years ago

Same problem here.

MaxAlmeida commented 7 years ago

same problem here. Don't work.

DeoLeung commented 7 years ago

@singh2005 seems the travis skip the map_reduce tests I added your example and it seems to have been fixed in master

pytest tests/test__mongomock.py::CollectionMapReduceTest
======================================================================================= test session starts =======================================================================================
platform darwin -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir:/mongomock, inifile:
plugins: cov-2.4.0, celery-4.0.2
collected 10 items

tests/test__mongomock.py ..........

==================================================================================== 10 passed in 1.63 seconds ====================================================================================