from unittest import TestCase
from unittest.mock import patch
from unittest .mock import Mock
from resources.specificuserrent import SpecificUserRent
import json
from bson import json_util
from bson.errors import InvalidId
from pymongo.errors import PyMongoError
from werkzeug.exceptions import NotFound, BadRequest
#@patch('parsers.carsparser.CarSchema.parse', new=Mock(return_value=carsample))
#patch('resources.usercars.UserCars.db.insert', Mock(return_value=""))


@patch('mongoutils.mongoclient.MongoClient.connect', new=Mock())
class TestSpecificUserRent(TestCase):
    """test class for specificuserrent resource"""
    db_mock = {"author": "user",
               "carId": "5d0a05e5987bbf80555a8a06"}

    def test_put_invalid_objid(self):
        with self.assertRaises(BadRequest):
            SpecificUserRent().put("user", "not_obj_id")

    #@patch('resources.specificuserrent.SpecificUserRent.db.find_one', new=Mock(side_effect=PyMongoError("db error")))
    def test_put_db_error_on_read(self):
        to_use = SpecificUserRent()
        to_use.db.find_one = Mock(side_effect=PyMongoError("db error"))
        response = SpecificUserRent().put("user", "5d0a05e5987bbf80555a8a09")
        self.assertEqual({"executed": False}, response)

    #@patch('resources.specificuserrent.SpecificUserRent.db.find_one', new=Mock(return_value=None))
    def test_put_rent_not_found(self):
        to_use = SpecificUserRent()
        to_use.db.find_one = Mock(return_value=None)
        with self.assertRaises(NotFound):
            SpecificUserRent().put("user", "5d0a05e5987bbf80555a8a09")

    #@patch('resources.specificuserrent.SpecificUserRent.db.find_one', new=Mock(return_value=db_mock))
    #@patch('resources.specificuserrent.SpecificUserRent.db.update_one', new=Mock(side_effect=PyMongoError("db error")))
    def test_put_db_error_on_rent_update(self):
        to_use = SpecificUserRent()
        to_use.db.find_one = Mock(return_value=self.db_mock)
        to_use.db.update_one = Mock(side_effect=PyMongoError("db error"))
        response = SpecificUserRent().put("user", "5d0a05e5987bbf80555a8a09")
        self.assertEqual({"executed": False}, response)

    #@patch('resources.specificuserrent.SpecificUserRent.db.find_one', new=Mock(return_value=db_mock))
    #@patch('resources.specificuserrent.SpecificUserRent.db.update_one', new=Mock())
    #@patch('resources.specificuserrent.SpecificUserRent.db_cars.update_one', new=Mock(
        #side_effect=PyMongoError("db error"))
           #)
    def test_put_db_error_on_car_release(self):
        to_use = SpecificUserRent()
        to_use.db.find_one = Mock(return_value=self.db_mock)
        to_use.db.update_one = Mock()
        to_use.db_cars.update_one = Mock(side_effect=PyMongoError("db error"))
        response = SpecificUserRent().put("user", "5d0a05e5987bbf80555a8a09")
        self.assertEqual({"executed": False}, response)

    #@patch('resources.specificuserrent.SpecificUserRent.db_cars.update_one', new=Mock())
    #@patch('resources.specificuserrent.SpecificUserRent.db.update_one', new=Mock())
    #@patch('resources.specificuserrent.SpecificUserRent.db.find_one', new=Mock(return_value=db_mock))
    def test_put_on_success(self):
        to_use = SpecificUserRent()
        to_use.db.find_one = Mock(return_value=self.db_mock)
        to_use.db.update_one = Mock()
        to_use.db_cars.update_one = Mock()
        response = SpecificUserRent().put("user", "5d0a05e5987bbf80555a8a09")
        self.assertEqual({"executed": True}, response)
