def take_student_avatar(new_class: NewClass, student_name: str) -> Optional[str]: """ Take user supplied avatar image file. Get avatar for student: Prompts user for path to avatar file. Copies avatar file to temp dir for class. :param new_class: NewClass class object :param student_name: str :return: str or None """ avatar_file = select_avatar_file_dialogue() if avatar_file is None: return None cleaned_student_name = clean_for_filename(student_name) target_avatar_filename = f'{cleaned_student_name}.png' # TODO: append hash to filename to prevent name collisions eg cleaned versions of 'a_b.jpg' and 'a b.jpg' will be identical. # TODO: process_student_avatar() # TODO: convert to png copy_file(avatar_file, new_class.temp_avatars_dir.joinpath(target_avatar_filename)) return target_avatar_filename
def test_copy_file_mocking_copyfile(self, mock_path_exists, mock_copyfile): mock_path_exists.return_value = True # Assume the path exists. copy_file(self.original_path, self.destination_path) mock_copyfile.assert_called_once_with(self.original_path, str(self.destination_path))
def test_copy_file_non_existent_original(self, monkeypatch): def mocked_copyfile(origin, destination): # Should not be called. raise NotImplementedError monkeypatch.setattr(file_functions, 'copyfile', mocked_copyfile) copy_file('Non-existent origin', 'No destination')
def copy_image_to_user_save_loc(app_image_location, user_save_location): """ Copies image from app_data location to user selected location. :param app_image_location: str or Path object :param user_save_location: str or Path object :return: None """ copy_file(app_image_location, user_save_location)
def copy_avatar_to_app_data(classlist_name, avatar_filename, save_filename): """ Copies given avatar image to classlist_name/avatars/ with given save_filename. No need to pre-check if file exists because it could not be selected if it did not exist. :param classlist_name: str :param avatar_filename: str or Path :param save_filename: str or Path :return: None """ save_avatar_path = CLASSLIST_DATA_PATH.joinpath(classlist_name, 'avatars', save_filename) copy_file(avatar_filename, save_avatar_path)
def test_copy_file(self, tmpdir, test_file, monkeypatch): def mocked_copyfile(origin, destination): # Test copyfile called with expected arguments. assert (origin, destination) == (str(original_filepath), str(destination_filepath)) original_filepath = test_file(tmpdir) destination_filepath = Path('some destination') monkeypatch.setattr(file_functions, 'copyfile', mocked_copyfile) assert not Path.exists(destination_filepath) copy_file(original_filepath, destination_filepath)
def test_copy_file_copies_file(self, tmpdir, test_file): original_filepath = test_file(tmpdir) original_filename = original_filepath.name destination_dir = Path(tmpdir, 'new_directory') Path.mkdir(destination_dir) destination_filepath = Path(destination_dir, original_filename) assert not Path.exists(destination_filepath) copy_file(original_filepath, destination_filepath) # Assert file in destination and still in origin. assert Path.exists(destination_filepath) and Path.exists( original_filepath)
def test_copy_file(self): assert os.path.exists(self.original_filename) assert not os.path.exists(self.destination_path) copy_file(self.original_path, self.destination_path) assert os.path.exists(self.destination_path)