Adds some tests, cleanup a bit

This commit is contained in:
Guillaume Tâche
2024-09-20 08:36:52 +02:00
parent 17086a87ef
commit 703a4c71ae
47 changed files with 1122 additions and 182 deletions

View File

@@ -14,7 +14,7 @@ class TestWhisperXSetupModule {
}
@Test
void testWHisperXBundledRoot() {
void testWhisperXBundledRoot() {
final var root = Paths.get("root");
assertEquals(root.resolve("whisperx"), WhisperXSetupModule.providesWhisperXBundledRoot(root));
}

View File

@@ -7,6 +7,7 @@ import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverterProvider;
import com.github.gtache.autosubtitle.whisper.WhisperModels;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -26,17 +27,23 @@ class TestWhisperXSubtitleExtractor {
private final Path venvPath;
private final SubtitleConverterProvider converterProvider;
private final SubtitleConverter<Subtitle> converter;
private final ProcessRunner processRunner;
private final OS os;
private final WhisperXSubtitleExtractor whisperXSubtitleExtractor;
private WhisperXSubtitleExtractor whisperXSubtitleExtractor;
TestWhisperXSubtitleExtractor(@Mock final SubtitleConverterProvider converterProvider, @Mock final SubtitleConverter<Subtitle> converter,
@Mock final ProcessRunner processRunner) {
this.converterProvider = Objects.requireNonNull(converterProvider);
doReturn(converter).when(converterProvider).getConverter("json");
this.converter = Objects.requireNonNull(converter);
this.processRunner = Objects.requireNonNull(processRunner);
this.venvPath = Paths.get("path");
this.os = OS.LINUX;
}
@BeforeEach
void beforeEach() {
doReturn(converter).when(converterProvider).getConverter("json");
this.whisperXSubtitleExtractor = new WhisperXSubtitleExtractor(venvPath, converterProvider, processRunner, os);
}

View File

@@ -6,6 +6,7 @@ import com.github.gtache.autosubtitle.subtitle.converter.ParseException;
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleCollectionImpl;
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleImpl;
import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@@ -22,20 +23,22 @@ import static org.mockito.Mockito.when;
class TestJSONSubtitleConverter {
private final Gson gson;
private final Preferences preferences;
private final int defaultMaxLineLength;
private final int defaultMaxLines;
private final JSONSubtitleConverter converter;
private JSONSubtitleConverter converter;
TestJSONSubtitleConverter() {
this.gson = new Gson();
this.preferences = mock(Preferences.class);
this.defaultMaxLineLength = 100;
when(preferences.getInt("maxLineLength", defaultMaxLineLength)).thenReturn(defaultMaxLineLength);
this.defaultMaxLines = 2;
}
@BeforeEach
void beforeEach() {
when(preferences.getInt("maxLineLength", defaultMaxLineLength)).thenReturn(defaultMaxLineLength);
when(preferences.getInt("maxLines", defaultMaxLines)).thenReturn(defaultMaxLines);
this.converter = new JSONSubtitleConverter(gson, preferences, defaultMaxLineLength, defaultMaxLines);
this.converter = new JSONSubtitleConverter(new Gson(), preferences, defaultMaxLineLength, defaultMaxLines);
}
@Test
@@ -45,30 +48,46 @@ class TestJSONSubtitleConverter {
@Test
void testParseFormat() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-in.json").readAllBytes(), StandardCharsets.UTF_8);
final var out = new String(getClass().getResourceAsStream("whisperx-out.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("This is a test. Yes.", List.of(new SubtitleImpl("This is a test.", 9, 410, null, null), new SubtitleImpl("Yes.", 450, 6963, null, null)), Language.FR);
assertEquals(expected, converter.parse(in));
assertEquals(out, converter.format(expected, null));
try (final var inStream = getClass().getResourceAsStream("whisperx-in.json");
final var outStream = getClass().getResourceAsStream("whisperx-out.json")) {
if (inStream == null || outStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var out = new String(outStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("This is a test. Yes.", List.of(new SubtitleImpl("This is a test.", 9, 410, null, null), new SubtitleImpl("Yes.", 450, 6963, null, null)), Language.FR);
assertEquals(expected, converter.parse(in));
assertEquals(out, converter.format(expected, null));
}
}
@Test
void testParseOverMaxWords() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-max-words.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr", 0, 18000, null, null),
new SubtitleImpl("ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz", 18000, 26000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
try (final var inStream = getClass().getResourceAsStream("whisperx-max-words.json")) {
if (inStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll mmmmmmmmmm nnnnnnnnnn oooooooooo pppppppppp qqqqqqqqqq rrrrrrrrrr", 0, 18000, null, null),
new SubtitleImpl("ssssssssss tttttttttt uuuuuuuuuu vvvvvvvvvv wwwwwwwwww xxxxxxxxxx yyyyyyyyyy zzzzzzzzzz", 18000, 26000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
}
}
@Test
void testParseOverMaxLines() throws IOException, ParseException {
final var in = new String(getClass().getResourceAsStream("whisperx-max-lines.json").readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll", 0, 18000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
try (final var inStream = getClass().getResourceAsStream("whisperx-max-lines.json")) {
if (inStream == null) {
throw new IOException("File not found");
}
final var in = new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
final var expected = new SubtitleCollectionImpl<Subtitle>("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll Yes.",
List.of(new SubtitleImpl("aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hhhhhhhhhh iiiiiiiiii\njjjjjjjjjj kkkkkkkkkk llllllllll", 0, 18000, null, null),
new SubtitleImpl("Yes.", 30000, 31000, null, null)), Language.EN);
assertEquals(expected, converter.parse(in));
}
}
@ParameterizedTest