I am trying to test my JsonWrite class and its write_json function that takes data from api and write it to a json file. This is my class.
import json
class JsonWriter:
def write_json(self, weights, filename):
try:
json_data = json.dumps(weights, indent=4)
with open(filename, 'w') as f:
f.write(json_data)
f.close()
except Exception as e:
return False, e
return True, None
Should I break this write_json function to smaller units or is there a way of testing it? So far I have only mocked what it returns if everything is OK. But I think that isn't the right way of doing it. What am I missing here?
class JsonWrite(unittest.TestCase):
@patch("argo_connectors.io.jsonwrite.JsonWriter.write_json")
def test_read_file_data(self, mock_write_json):
mock_write_json.return_value = True, None
if __name__ == '__main__':
unittest.main()