from werkzeug.exceptions import HTTPException
import pymongo

from flask_pymongo.tests.util import FlaskPyMongoTest


class CollectionTest(FlaskPyMongoTest):

    def test_find_one_or_404(self):
        self.mongo.db.things.remove()

        try:
            self.mongo.db.things.find_one_or_404({"_id": "thing"})
        except HTTPException as notfound:
            assert notfound.code == 404, "raised wrong exception"

        if pymongo.version_tuple[0] > 2:
            self.mongo.db.things.insert_one({"_id": "thing", "val": "foo"})
        else:
            self.mongo.db.things.insert({"_id": "thing", "val": "foo"}, w=1)

        # now it should not raise
        thing = self.mongo.db.things.find_one_or_404({"_id": "thing"})
        assert thing["val"] == "foo", "got wrong thing"

        # also test with dotted-named collections
        self.mongo.db.things.morethings.remove()
        try:
            self.mongo.db.things.morethings.find_one_or_404({"_id": "thing"})
        except HTTPException as notfound:
            assert notfound.code == 404, "raised wrong exception"

        if pymongo.version_tuple[0] > 2:
            # Write Concern is set to w=1 by default in pymongo > 3.0
            self.mongo.db.things.morethings.insert_one({"_id": "thing", "val": "foo"})
        else:
            self.mongo.db.things.morethings.insert({"_id": "thing", "val": "foo"}, w=1)

        # now it should not raise
        thing = self.mongo.db.things.morethings.find_one_or_404({"_id": "thing"})
        assert thing["val"] == "foo", "got wrong thing"

@@ -42,23 +42,8 @@
 tion
-%0Aimport pymongo
 %0A%0Afr
@@ -405,53 +405,8 @@
 n%22%0A%0A
-        if pymongo.version_tuple%5B0%5D %3E 2:%0A    
     
@@ -476,99 +476,8 @@
 o%22%7D)
-%0A        else:%0A            self.mongo.db.things.insert(%7B%22_id%22: %22thing%22, %22val%22: %22foo%22%7D, w=1)
 %0A%0A  
@@ -947,298 +947,81 @@
     
-if pymongo.version_tuple%5B0%5D %3E 2:%0A            # Write Concern is set to w=1 by default in pymongo %3E 3.0%0A            self.mongo.db.things.morethings.insert_one(%7B%22_id%22: %22thing%22, %22val%22: %22foo%22%7D)%0A        else:%0A            self.mongo.db.things.morethings.insert(%7B%22_id%22: %22thing%22, %22val%22: %22foo%22%7D, w=1
+self.mongo.db.things.morethings.insert_one(%7B%22_id%22: %22thing%22, %22val%22: %22foo%22%7D
 )%0A%0A 

remove pymongo 2.x compatibility in tests