So I am confused how the mocking stuff works. patch in particular with external dependencies. So far I have seen examples like this one how to mock dependency in python where it shows how to mock out one of your own objects, but then uses things like "with mock.patch.dict('os.environ', PLATFORM='X_PLATFORM'):" to mock out the os.environ.
I have also seen examples like this How to do unit testing of functions writing files using python unittest where they are using mock_open to mock os.open. But none of this seems to fit what I need.
Lets say you have a function as follows
import tensorflow as tf
def load_model(self, model_path=None):
if model_path is None:
model_path = self.model_path
graph_def = None
with tf.gfile.GFile(model_path, "rb") as file_handle:
graph_def = tf.GraphDef.FromString(file_handle.read())
if graph_def is None:
print('Cannot find inference graph in tar archive.')
raise
try:
with self.graph.as_default() as self.graph:
tf.import_graph_def(graph_def, name='')
except Exception as error:
print(str(error))
raise
self.session = tf.Session(graph=self.graph)
return True
How do you mock out all the tensorflow functions? I want to mock what the file_handle.read() call returns so I can control what is sent into the tf.GraphDef.FromString function, which I also want to mock. I just want some asserts that tell me if these functions are called with the data I feed it. If I control what FromString returns, that controls what goes into the tf.import_graph_def call as well. So I want to mock that as well and assert that it is called with the proper data as well. Anyone know how to do this?