def test_get_error(self, m_FileExt): api_version = 1 resultid = "whatever" exception = ValueError() m_FileExt.load_from_ext_id.side_effect = exception with self.assertRaises(ValueError): api_files_ext.get(api_version, resultid)
def test_get_formatted_false(self, m_FileExt): resultid = "whatever" api_version = 1 m_file_ext = MagicMock() m_file_ext.submitter = "webui" m_FileExt.load_from_ext_id.return_value = m_file_ext ret = api_files_ext.get(api_version, resultid, formatted="no") m_FileExt.load_from_ext_id.assert_called_once_with( resultid, self.session) self.assertIsFileExt(ret)
def test_get_ok(self, m_FileExt): api_version = 1 resultid = "whatever" m_file = MagicMock() m_fw = FileExt(m_file, "filename") m_FileExt.load_from_ext_id.return_value = m_fw result = api_files_ext.get(api_version, resultid) m_FileExt.load_from_ext_id.assert_called_once_with( resultid, self.session) self.assertIsFileExt(result)
def test_get_formatted_false(self, m_FileExt): resultid = "whatever" api_version = 1 m_file_ext = MagicMock() m_file_ext.submitter = "webui" m_FileExt.load_from_ext_id.return_value = m_file_ext ret = api_files_ext.get(api_version, resultid, formatted="no") m_FileExt.load_from_ext_id.assert_called_once_with(resultid, self.session) self.assertIsFileExt(ret)
def test_get_ok(self, m_FileExt): api_version = 1 resultid = "whatever" m_file = MagicMock() m_fw = FileExt(m_file, "filename") m_FileExt.load_from_ext_id.return_value = m_fw result = api_files_ext.get(api_version, resultid) m_FileExt.load_from_ext_id.assert_called_once_with(resultid, self.session) self.assertIsFileExt(result)
def test_get_ok(self, m_FileExt): api_version = 1 resultid = "whatever" m_file = MagicMock() m_fw = FileExt(m_file, "filename") m_FileExt.load_from_ext_id.return_value = m_fw # As FileExt.other_results value is normally retrieve from database, # when requesting it, the API will not return it, and will break the # test. # A solution is to patch the property as mentionned in the # documentation: # (https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock) # Mocking the type object other_results property will break other tests # (the database one) as it will globally mocking it. with patch('api.files_ext.models.FileExt.other_results', new_callable=PropertyMock) as m_other_results: m_other_results.return_value = [m_fw] result = api_files_ext.get(api_version, resultid) m_FileExt.load_from_ext_id.assert_called_once_with(resultid, self.session) self.assertIsFileExt(result)