I'm trying to get my head around why I can't access the method when mocking. I have simplified the code examples to only show the content that is relevant.
Here's the class with the method that I want to test / mock:
import sqlite3
from customer import Customer
class DataChecker:
def find_customer(self, customerID):
query = "SELECT * FROM Customers WHERE ID == ?;"
customers = self.execute_db(query, customerID, True)
if len(customers) == 0:
print("Customer with given ID not found in DB")
return False
else:
print("Customer found")
return True
def execute_db(self, query, params=None, fetch_one=False):
if params:
self.cursor.execute(query, [params])
self.conn.commit()
else:
self.cursor.execute(query)
self.conn.commit()
if fetch_one:
return self.cursor.fetchone()
else:
return self.cursor.fetchall()
Mock:
from datachecker import DataChecker
from unittest import mock
@mock.patch('datachecker.find_customer')
def test_find_customer_true(mocker):
datachecker.execute_db.return_value = {'customers': 10}
assert datachecker.find_customer() == True