Repository: mdomke/pytest-mongodb (language: python)

Issue:
insert function deprecated with warning for pymongo
warning message:  
`DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.`
collection_names is deprecated
https://github.com/mdomke/pytest-mongodb/blob/1cf1f1c6798c8debbebffd6c400efd74251dccf0/pytest_mongodb/plugin.py#L80

> venv\lib\site-packages\mongomock\database.py:41: UserWarning: collection_names is deprecated. Use list_collection_names instead.
>     warnings.warn('collection_names is deprecated. Use list_collection_names instead.')



Discussion / hints:
@xialu4820723 `pytest-mongodb` doesn't provide any functionality with respect to mocking MongoDB itself, but uses `mongomock` for that purpose. On the other hand I'm just guessing what you're trying to tell me. Could you be more specific on the details of the issue?
During fixture loading, `insert` method was used, [source code](https://github.com/mdomke/pytest-mongodb/blob/develop/pytest_mongodb/plugin.py#L112), 
which was deprecated by pymongo from 3.0, [source code](https://github.com/mongodb/mongo-python-driver/blob/7f1939ebcb6f577f762bf5cd068288d2533b2bbf/pymongo/collection.py#L3155), warning messages will be printed when using pytest-mongodb with latest pymongo.  
Replacing `insert` with `insert_one` may fix  this issue, I have created a pull request for this.  

thank you for response. @mdomke 
I think this issue is a mongomock issue and not pytest-mongodb issue.
@jakobdo This issue is in pytest-mongodb, the warning just comes from the `collection_names` method in mongomock but it is pytest-mongodb that is making the call to `collection_names` instead of `list_collection_names`

Fix patch:
diff --git a/.travis.yml b/.travis.yml
index 39937ea..d08e86d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,8 @@ python:
 - '3.4'
 - '3.5'
 - '3.6'
+- '3.7'
+- '3.8'
 install:
 - pip install -U pip wheel tox-travis twine
 script:
diff --git a/ChangeLog b/ChangeLog
index 5230235..d8a181d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,11 +1,24 @@
 CHANGES
 =======
 
+* Re-implement mongo\_engine due to deprecation of pytest.config
+* Add testenvs for Python 3.7 and 3.8
+* Use unittest.mock for Python > 3.4
+* Wrap deprecated methods
+* Add mock as test-requirement
+* - Loading .yaml or .json collections without defining them in mongodb\_fixtures option
+
+2.1.1
+-----
+
+* Fix documentation bugs
+
 2.1.0
 -----
 
+* Update changelog. Switch to post-versioning
 * Added tox-travis and python 3.6 tests/annotations
-* Replaces redundant `pytestconfig.getini` with intended `getoption`
+* Replaces redundant \`pytestconfig.getini\` with intended \`getoption\`
 * Fixes a bug with file extension check
 * Adds Python 3 interpreters for Travis CI
 * Adds python 3.5 tox environment and package metadata classifier
@@ -64,7 +77,7 @@ CHANGES
 -----
 
 * Set next development target to 1.3.0
-* Use bson.json_util to load fixtures. Allow real MongoClient
+* Use bson.json\_util to load fixtures. Allow real MongoClient
 
 1.1.0
 -----
diff --git a/README.rst b/README.rst
index e12cea3..34c3286 100644
--- a/README.rst
+++ b/README.rst
@@ -85,7 +85,7 @@ like so:
 .. code-block:: python
 
     def test_players(mongodb):
-        assert 'players' in mongodb.collection_names()
+        assert 'players' in mongodb.list_collection_names()
         manuel = mongodb.players.find_one({'name': 'Manuel'})
         assert manuel['surname'] == 'Neuer'
 
@@ -103,7 +103,7 @@ like so:
 
     @mark.skipif(mongo_engine() == 'mongomock', reason="mongomock does not support that")
     def test_players(mongodb):
-        assert 'players' in mongodb.collection_names()
+        assert 'players' in mongodb.list_collection_names()
         manuel = mongodb.players.find_one({'name': 'Manuel'})
         assert manuel['surname'] == 'Neuer'
 
diff --git a/requirements.txt b/requirements.txt
index c0c4066..510e8ef 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
 mongomock
 pymongo
-pyyaml
+pyyaml>=5.1
 pytest>=2.5.2
diff --git a/tox.ini b/tox.ini
index 8356c58..d0663e7 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,11 +1,11 @@
 [tox]
-envlist = py27, py34, py35, pep8
+envlist = py27,py34,py35,py36,py37,py38,pep8
 
 [testenv]
 deps =
   -r{toxinidir}/requirements.txt
   -r{toxinidir}/test-requirements.txt
-commands = py.test --junit-xml test-results.xml {posargs} tests/unit
+commands = py.test {posargs} tests/unit
 passenv = LANG
 
 [testenv:pep8]


Test patch:
diff --git a/pytest_mongodb/plugin.py b/pytest_mongodb/plugin.py
index c2bf9b4..11dd044 100644
--- a/pytest_mongodb/plugin.py
+++ b/pytest_mongodb/plugin.py
@@ -11,6 +11,7 @@
 
 
 _cache = {}
+_engine = 'mongomock'
 
 
 def pytest_addoption(parser):
@@ -56,6 +57,11 @@ def pytest_addoption(parser):
         help='The name of the database where fixtures are created [pytest]')
 
 
+def pytest_configure(config):
+    global _engine
+    _engine = config.getoption('mongodb_engine') or config.getini('mongodb_engine')
+
+
 @pytest.fixture(scope='function')
 def mongodb(pytestconfig):
     dbname = pytestconfig.getoption('mongodb_dbname') or pytestconfig.getini('mongodb_dbname')
@@ -77,10 +83,16 @@ def make_mongo_client(config):
 
 
 def clean_database(db):
-    for name in db.collection_names(include_system_collections=False):
+    for name in list_collection_names(db):
         db.drop_collection(name)
 
 
+def list_collection_names(db):
+    if hasattr(db, 'list_collection_names'):
+        return db.list_collection_names()
+    return db.collection_names(include_system_collections=False)
+
+
 def load_fixtures(db, config):
     basedir = config.getoption('mongodb_fixture_dir') or config.getini('mongodb_fixture_dir')
     fixtures = config.getini('mongodb_fixtures')
@@ -99,7 +111,7 @@ def load_fixture(db, collection, path, file_format):
     if file_format == 'json':
         loader = functools.partial(json.load, object_hook=json_util.object_hook)
     elif file_format == 'yaml':
-        loader = yaml.load
+        loader = functools.partial(yaml.load, Loader=yaml.FullLoader)
     else:
         return
     try:
@@ -107,10 +119,14 @@ def load_fixture(db, collection, path, file_format):
     except KeyError:
         with codecs.open(path, encoding='utf-8') as fp:
             _cache[path] = docs = loader(fp)
+    insert_many(db[collection], docs)
+
 
-    for document in docs:
-        db[collection].insert(document)
+def insert_many(collection, docs):
+    if hasattr(collection, 'insert_many'):
+        return collection.insert_many(docs)
+    return collection.insert(docs)
 
 
 def mongo_engine():
-    return pytest.config.getoption('mongodb_engine') or pytest.config.getini('mongodb_engine')
+    return _engine
diff --git a/test-requirements.txt b/test-requirements.txt
index b3fcfee..394823c 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -1,2 +1,2 @@
-mock
+mock ; python_version < '3.5'
 flake8>=2.1.0
diff --git a/tests/unit/test_plugin.py b/tests/unit/test_plugin.py
index 8814867..fb87d93 100644
--- a/tests/unit/test_plugin.py
+++ b/tests/unit/test_plugin.py
@@ -1,9 +1,8 @@
 from pytest_mongodb import plugin
-from mock import patch
 
 
 def test_load(mongodb):
-    collection_names = mongodb.collection_names()
+    collection_names = mongodb.list_collection_names()
     assert 'players' in collection_names
     assert 'championships' in collection_names
     assert len(plugin._cache.keys()) == 2
@@ -12,7 +11,7 @@ def test_load(mongodb):
 
 
 def check_players(players):
-    assert players.count() == 2
+    assert players.count_documents({}) == 2
     check_keys_in_docs(players, ['name', 'surname', "position"])
     manuel = players.find_one({'name': 'Manuel'})
     assert manuel['surname'] == 'Neuer'
@@ -20,7 +19,7 @@ def check_players(players):
 
 
 def check_championships(championships):
-    assert championships.count() == 3
+    assert championships.count_documents({}) == 3
     check_keys_in_docs(championships, ['year', 'host', 'winner'])
 
 
@@ -31,20 +30,14 @@ def check_keys_in_docs(collection, keys):
 
 
 def test_insert(mongodb):
-    mongodb.players.insert({
+    mongodb.players.insert_one({
         'name': 'Bastian',
         'surname': 'Schweinsteiger',
         'position': 'midfield'
     })
-    assert mongodb.players.count() == 3
+    assert mongodb.players.count_documents({}) == 3
     assert mongodb.players.find_one({'name': 'Bastian'})
 
 
-@patch('pytest_mongodb.plugin.pytest.config')
-def test_mongo_engine(config):
-    config.getoption.return_value = None
-    config.getini.return_value = 'INI_VALUE'
-    assert 'INI_VALUE' == plugin.mongo_engine()
-
-    config.getoption.return_value = 'OPTION_VALUE'
-    assert 'OPTION_VALUE' == plugin.mongo_engine()
+def test_mongo_engine(pytestconfig):
+    assert plugin.mongo_engine() == 'mongomock'