Пример #1
0
    def test_take_student_avatar_pre_clean_name(self, monkeypatch):
        test_class_name = 'some class'
        test_student_name = 'clean name'
        test_avatar_filename = 'avatar file name'
        cleaned_student_name = 'file name was already clean'
        returned_filename = f'{cleaned_student_name}.png'

        def mocked_select_avatar_file_dialogue():
            return test_avatar_filename

        def mocked_clean_for_filename(student_name):
            if student_name != test_student_name:
                raise ValueError  # Ensure called with correct arg.
            return cleaned_student_name

        def mocked_copy_avatar_to_app_data(class_name, avatar_filename,
                                           target_filename):
            if (class_name, avatar_filename, target_filename) != (
                    test_class_name, test_avatar_filename, returned_filename):
                raise ValueError  # Ensure called with correct args.
            return None

        monkeypatch.setattr(class_functions, 'select_avatar_file_dialogue',
                            mocked_select_avatar_file_dialogue)
        monkeypatch.setattr(class_functions, 'clean_for_filename',
                            mocked_clean_for_filename)
        monkeypatch.setattr(class_functions, 'copy_avatar_to_app_data',
                            mocked_copy_avatar_to_app_data)

        assert take_student_avatar(test_class_name,
                                   test_student_name) == returned_filename
    def test_take_student_avatar_pre_clean_name(self, monkeypatch):
        test_class = NewClass('some_class')
        test_student_name = 'clean name'
        test_avatar_filepath = Path('avatar file name')
        cleaned_student_name = 'file name was already clean'
        returned_filename = f'{cleaned_student_name}.png'

        def mocked_select_avatar_file_dialogue():
            return test_avatar_filepath

        def mocked_clean_for_filename(student_name):
            if student_name != test_student_name:
                raise ValueError  # Ensure called with correct arg.
            return cleaned_student_name

        def mocked_copy_file(avatar_filepath, destination_filepath):
            if (avatar_filepath, destination_filepath) != (
                    test_avatar_filepath,
                    test_class.temp_avatars_dir.joinpath(returned_filename)):
                raise ValueError  # Ensure called with correct args.
            return None

        monkeypatch.setattr(class_functions, 'select_avatar_file_dialogue',
                            mocked_select_avatar_file_dialogue)
        monkeypatch.setattr(class_functions, 'clean_for_filename',
                            mocked_clean_for_filename)
        monkeypatch.setattr(class_functions, 'copy_file', mocked_copy_file)

        assert take_student_avatar(test_class,
                                   test_student_name) == returned_filename
Пример #3
0
    def test_take_student_avatar_no_avatar(self, monkeypatch):
        def mocked_select_avatar_file_dialogue():
            return None  # No file selected

        monkeypatch.setattr(class_functions, 'select_avatar_file_dialogue',
                            mocked_select_avatar_file_dialogue)

        # Ensure calls to other funcs will cause error.
        monkeypatch.delattr(class_functions, 'clean_for_filename')
        monkeypatch.delattr(class_functions, 'copy_avatar_to_app_data')

        assert take_student_avatar('some class', 'some student') is None
    def test_take_student_avatar_dirty_name(self, monkeypatch):
        test_class = NewClass('some_class')
        test_student_name = r'very unsafe */^@ :$ name'
        test_avatar_filepath = Path('avatar file name')

        returned_filename = f'{clean_for_filename(test_student_name)}.png'

        def mocked_select_avatar_file_dialogue():
            return test_avatar_filepath

        def mocked_copy_file(avatar_filepath, destination_filepath):
            if (avatar_filepath, destination_filepath) != (
                    test_avatar_filepath,
                    test_class.temp_avatars_dir.joinpath(returned_filename)):
                raise ValueError  # Ensure called with correct args.
            return None

        monkeypatch.setattr(class_functions, 'select_avatar_file_dialogue',
                            mocked_select_avatar_file_dialogue)
        monkeypatch.setattr(class_functions, 'copy_file', mocked_copy_file)

        assert take_student_avatar(test_class,
                                   test_student_name) == returned_filename
Пример #5
0
    def test_take_student_avatar_dirty_name(self, monkeypatch):
        test_class_name = 'some class'
        test_student_name = r'very unsafe */^@ :$ name'
        test_avatar_filename = 'avatar file name'

        returned_filename = f'{clean_for_filename(test_student_name)}.png'

        def mocked_select_avatar_file_dialogue():
            return test_avatar_filename

        def mocked_copy_avatar_to_app_data(class_name, avatar_filename,
                                           target_filename):
            if (class_name, avatar_filename, target_filename) != (
                    test_class_name, test_avatar_filename, returned_filename):
                raise ValueError  # Ensure called with correct args.
            return None

        monkeypatch.setattr(class_functions, 'select_avatar_file_dialogue',
                            mocked_select_avatar_file_dialogue)
        monkeypatch.setattr(class_functions, 'copy_avatar_to_app_data',
                            mocked_copy_avatar_to_app_data)

        assert take_student_avatar(test_class_name,
                                   test_student_name) == returned_filename