def transform_old_cld_file(filepath: Path): """ Transform .cld file to new format. NB If file is in the wrong location, or not in class data at all, a folder with the class name will be created in class_data. :param filepath: Path :return: None """ if not filepath.exists(): print(f'File {filepath} does not exist.') return try: old_class_data = load_from_json_file(filepath) # Handle corrupt or mal-formatted JSON files. except json.decoder.JSONDecodeError: print(f'Something went wrong with decoding {filepath}:\n' f'The file might be corrupted or is not a supported format.') return if data_is_new_format(old_class_data): print(f'It looks like {filepath.name} is already in the new format.') return class_name = filepath.stem new_class = transform_data(class_name, old_class_data) new_class_data_path_name = write_classlist_to_file(new_class) print(f'Transformed {new_class.name} data file ' f'to new data format in {new_class_data_path_name}')
def test_write_classlist_to_file(self): assert write_classlist_to_file(self.test_class_name, self.test_class_data_dict) is None assert os.path.exists(self.test_class_data_file_path) assert open(self.test_class_data_file_path, 'r').read() == self.test_class_json_string
def test_write_classlist_to_file_mocking_convert_to_json(self, mocked_convert_to_json): mocked_convert_to_json.return_value = self.test_class_json_string assert write_classlist_to_file(self.test_class_name, self.test_class_data_dict) is None assert os.path.exists(self.test_class_data_file_path) with open(self.test_class_data_file_path, 'r') as test_class_data_file: assert test_class_data_file.read() == self.test_class_json_string mocked_convert_to_json.assert_called_once_with(self.test_class_data_dict)
def test_write_classlist_to_file_mocking_called_functions(self, mocked_convert_to_json): mocked_convert_to_json.return_value = self.test_class_json_string mocked_open = mock_open() with patch('dionysus_app.class_functions.open', mocked_open): assert write_classlist_to_file(self.test_class_name, self.test_class_data_dict) is None mocked_convert_to_json.assert_called_once_with(self.test_class_data_dict) mocked_open.assert_called_once_with(self.test_class_data_file_path, 'w') opened_test_class_data_file = mocked_open() opened_test_class_data_file.write.assert_called_with(self.test_class_json_string)
def test_write_classlist_to_file(self): # Assert precondition: assert not os.path.exists(self.test_class_data_file_path) assert write_classlist_to_file( self.test_class_object) == self.test_class_data_file_path assert isinstance(self.test_class_data_file_path, Path) # Assert file created: assert os.path.exists(self.test_class_data_file_path) # Verify file contents: with open(self.test_class_data_file_path, 'r') as test_class_data_file: assert test_class_data_file.read( ) == self.test_class_object.to_json_str()
def test_write_classlist_to_file_mocking_open(self): mocked_open = mock_open() with patch('dionysus_app.class_functions.open', mocked_open), \ patch('dionysus_app.class_functions.Path.mkdir', autospec=True) as mocked_mkdir: assert write_classlist_to_file( self.test_class_object) == self.test_class_data_file_path assert isinstance(self.test_class_data_file_path, Path) mocked_mkdir.assert_called_once_with( self.test_class_data_file_path.parent, exist_ok=True, parents=True) mocked_open.assert_called_once_with(self.test_class_data_file_path, 'w') opened_test_class_data_file = mocked_open() opened_test_class_data_file.write.assert_called_with( self.test_class_object.to_json_str())