{"action":"closed","issue":{"url":"https://api.github.com/repos/mongomock/mongomock/issues/460","repository_url":"https://api.github.com/repos/mongomock/mongomock","labels_url":"https://api.github.com/repos/mongomock/mongomock/issues/460/labels{/name}","comments_url":"https://api.github.com/repos/mongomock/mongomock/issues/460/comments","events_url":"https://api.github.com/repos/mongomock/mongomock/issues/460/events","html_url":"https://github.com/mongomock/mongomock/issues/460","id":374004254,"node_id":"MDU6SXNzdWUzNzQwMDQyNTQ=","number":460,"title":"insert_many fails insertion if called with a generator","user":{"login":"gpetretto","id":8665734,"node_id":"MDQ6VXNlcjg2NjU3MzQ=","avatar_url":"https://avatars3.githubusercontent.com/u/8665734?v=4","gravatar_id":"","url":"https://api.github.com/users/gpetretto","html_url":"https://github.com/gpetretto","followers_url":"https://api.github.com/users/gpetretto/followers","following_url":"https://api.github.com/users/gpetretto/following{/other_user}","gists_url":"https://api.github.com/users/gpetretto/gists{/gist_id}","starred_url":"https://api.github.com/users/gpetretto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gpetretto/subscriptions","organizations_url":"https://api.github.com/users/gpetretto/orgs","repos_url":"https://api.github.com/users/gpetretto/repos","events_url":"https://api.github.com/users/gpetretto/events{/privacy}","received_events_url":"https://api.github.com/users/gpetretto/received_events","type":"User","site_admin":false},"labels":[{"id":1352858,"node_id":"MDU6TGFiZWwxMzUyODU4","url":"https://api.github.com/repos/mongomock/mongomock/labels/bug","name":"bug","color":"fc2929","default":true}],"state":"closed","locked":false,"assignee":{"login":"pcorpet","id":7937848,"node_id":"MDQ6VXNlcjc5Mzc4NDg=","avatar_url":"https://avatars2.githubusercontent.com/u/7937848?v=4","gravatar_id":"","url":"https://api.github.com/users/pcorpet","html_url":"https://github.com/pcorpet","followers_url":"https://api.github.com/users/pcorpet/followers","following_url":"https://api.github.com/users/pcorpet/following{/other_user}","gists_url":"https://api.github.com/users/pcorpet/gists{/gist_id}","starred_url":"https://api.github.com/users/pcorpet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pcorpet/subscriptions","organizations_url":"https://api.github.com/users/pcorpet/orgs","repos_url":"https://api.github.com/users/pcorpet/repos","events_url":"https://api.github.com/users/pcorpet/events{/privacy}","received_events_url":"https://api.github.com/users/pcorpet/received_events","type":"User","site_admin":false},"assignees":[{"login":"pcorpet","id":7937848,"node_id":"MDQ6VXNlcjc5Mzc4NDg=","avatar_url":"https://avatars2.githubusercontent.com/u/7937848?v=4","gravatar_id":"","url":"https://api.github.com/users/pcorpet","html_url":"https://github.com/pcorpet","followers_url":"https://api.github.com/users/pcorpet/followers","following_url":"https://api.github.com/users/pcorpet/following{/other_user}","gists_url":"https://api.github.com/users/pcorpet/gists{/gist_id}","starred_url":"https://api.github.com/users/pcorpet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pcorpet/subscriptions","organizations_url":"https://api.github.com/users/pcorpet/orgs","repos_url":"https://api.github.com/users/pcorpet/repos","events_url":"https://api.github.com/users/pcorpet/events{/privacy}","received_events_url":"https://api.github.com/users/pcorpet/received_events","type":"User","site_admin":false}],"milestone":null,"comments":2,"created_at":"2018-10-25T15:25:31Z","updated_at":"2018-10-25T21:38:49Z","closed_at":"2018-10-25T21:38:49Z","author_association":"NONE","body":"Calling the `insert_many` method of a collection and giving a generator as an argument results in no document being inserted in the db, at variance with what happens in pymongo. \r\nHere is a snippet of code demonstrating the problem:\r\n\r\n```python\r\nimport mongomock\r\nimport pymongo\r\n\r\ndata = (x for x in [{\"a\": 1}, {\"b\": 2}])\r\n\r\nclient = mongomock.MongoClient(\"mongodb://host1/foo\")\r\ndb = client.get_default_database()\r\n\r\ncoll = db.test_coll\r\ncoll.insert_many(data)\r\n\r\nprint(\"results in mongomock: \", coll.find().count())\r\n\r\ndata = (x for x in [{\"a\": 1}, {\"b\": 2}])\r\n\r\nclient = pymongo.MongoClient(\"localhost\", 27017)\r\ndb = client.test_db\r\ncoll_mongo = db.test_coll\r\ncoll_mongo.remove()\r\n\r\ncoll_mongo.insert_many(data)\r\nprint(\"results in pymongo: \", coll_mongo.find().count())\r\n```\r\nwith its output:\r\n\r\n> results in mongomock:  0\r\n> results in pymongo:  2\r\n\r\n\r\nThe problem originates from the fact that in `insert_many` the generator is iterated to validate its content. So, when it is passed to the `_insert` method, the generator has already been exhausted and no document is inserted in the db.\r\n\r\nA simple fix could be to just generate the list if it is a generator:\r\n```python\r\n    def insert_many(self, documents, ordered=True, session=None):\r\n        if not isinstance(documents, Iterable) or not documents:\r\n            raise TypeError('documents must be a non-empty list')\r\n        if isinstance(documents, types.GeneratorType):\r\n            documents = list(documents)\r\n        for document in documents:\r\n            validate_is_mutable_mapping('document', document)\r\n        return InsertManyResult(self._insert(documents, session), acknowledged=True)\r\n```"}}
{"actor":{"display_login":"pcorpet"}}