def test_assert_json_storage_id(self, get_mock): example_json = {'foo': [1.0, 2.5, 3.14], 'bar': ['ba', 'cd']} dummy_case = ProcessTestCase.__new__(ProcessTestCase) dummy_case.files_path = '' dummy_case._debug_info = lambda _: '' # pylint: disable=protected-access setattr(dummy_case, 'assertEqual', self.assertEqual) obj_mock = mock.MagicMock() storage_id = 'no_id' storage_mock = mock.MagicMock(spec=Storage) storage_mock.id = storage_id storage_mock.json = example_json get_mock.side_effect = [storage_mock] # use in-memory binary stream object for speed and simplicity gzipped_json_file = io.BytesIO() with gzip.open(gzipped_json_file, mode='wt') as f: json.dump(example_json, f) # set seek position of the binary stream object back to 0 gzipped_json_file.seek(0) join_mock = mock.MagicMock(side_effect=[gzipped_json_file]) isfile_mock = mock.MagicMock(return_value=True) with mock.patch.object(os.path, 'join', join_mock): with mock.patch.object(os.path, 'isfile', isfile_mock): ProcessTestCase.assertJSON(dummy_case, obj_mock, storage_id, '', 'foo.gz')
def test_assert_json_file_missing(self): example_json = {'foo': [1.0, 2.5, 3.14], 'bar': ['ba', 'cd']} dummy_case = ProcessTestCase.__new__(ProcessTestCase) dummy_case.files_path = '' dummy_case._debug_info = lambda _: '' # pylint: disable=protected-access setattr(dummy_case, 'assertEqual', self.assertEqual) obj_mock = mock.MagicMock() storage_mock = mock.MagicMock(spec=Storage) storage_mock.id = 'no_id' storage_mock.json = example_json # use in-memory binary stream object for speed and simplicity gzipped_json_file = io.BytesIO() join_modified_values = [gzipped_json_file] orig_join = os.path.join # NOTE: coverage tool needs original 'os.path.join' def join_side_effect(path, *paths): if len(join_modified_values) > 0: return join_modified_values.pop(0) else: return orig_join(path, *paths) join_mock = mock.MagicMock(side_effect=join_side_effect) isfile_mock = mock.MagicMock(return_value=False) with mock.patch.object(os.path, 'join', join_mock): with mock.patch.object(os.path, 'isfile', isfile_mock): with six.assertRaisesRegex(self, AssertionError, 'Output file .* missing so it was created.'): ProcessTestCase.assertJSON(dummy_case, obj_mock, storage_mock, '', 'foo.gz') # set seek position of the binary stream object back to 0 gzipped_json_file.seek(0) with gzip.open(gzipped_json_file, mode='rt') as f: unzipped_json = json.load(f) self.assertEqual(example_json, unzipped_json)