Adds tests for common whisper
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
<properties>
|
||||
<deepl.version>1.5.0</deepl.version>
|
||||
<lingua.version>1.2.2</lingua.version>
|
||||
<tika.version>2.9.2</tika.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -28,9 +29,15 @@
|
||||
<version>${deepl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.pemistahl</groupId>
|
||||
<artifactId>lingua</artifactId>
|
||||
<version>${lingua.version}</version>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
<version>${tika.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-langdetect-optimaize</artifactId>
|
||||
<version>${tika.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -3,13 +3,14 @@ package com.github.gtache.autosubtitle.modules.deepl;
|
||||
import com.github.gtache.autosubtitle.modules.setup.deepl.DeepLSetupModule;
|
||||
import com.github.gtache.autosubtitle.translation.Translator;
|
||||
import com.github.gtache.autosubtitle.translation.deepl.DeepLTranslator;
|
||||
import com.github.pemistahl.lingua.api.LanguageDetector;
|
||||
import com.github.pemistahl.lingua.api.LanguageDetectorBuilder;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import org.apache.tika.language.detect.LanguageDetector;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
/**
|
||||
* Dagger module for DeepL
|
||||
@@ -23,6 +24,10 @@ public abstract class DeepLModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
static LanguageDetector providesLanguageDetector() {
|
||||
return LanguageDetectorBuilder.fromAllSpokenLanguages().build();
|
||||
try {
|
||||
return LanguageDetector.getDefaultLanguageDetector().loadModels();
|
||||
} catch (final IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class DeepLSetupManager extends AbstractSetupManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SetupStatus getStatus() throws SetupException {
|
||||
protected SetupStatus getStatus() {
|
||||
final var key = preferences.get(DEEPL_API_KEY, null);
|
||||
return key == null ? SetupStatus.NOT_INSTALLED : SetupStatus.BUNDLE_INSTALLED;
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class DeepLSetupManager extends AbstractSetupManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() throws SetupException {
|
||||
public void update() {
|
||||
//No need to update
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.github.gtache.autosubtitle.subtitle.impl.SubtitleCollectionImpl;
|
||||
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleImpl;
|
||||
import com.github.gtache.autosubtitle.translation.TranslationException;
|
||||
import com.github.gtache.autosubtitle.translation.Translator;
|
||||
import com.github.pemistahl.lingua.api.LanguageDetector;
|
||||
import org.apache.tika.language.detect.LanguageDetector;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
@@ -37,7 +37,7 @@ public class DeepLTranslator implements Translator<Subtitle> {
|
||||
|
||||
@Override
|
||||
public Language getLanguage(final String text) {
|
||||
return Language.getLanguage(languageDetector.detectLanguageOf(text).getIsoCode639_1().toString());
|
||||
return Language.getLanguage(languageDetector.detect(text).getLanguage());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
module com.github.gtache.autosubtitle.deepl {
|
||||
requires transitive com.github.gtache.autosubtitle.core;
|
||||
requires transitive deepl.java;
|
||||
requires transitive com.github.pemistahl.lingua;
|
||||
requires transitive java.prefs;
|
||||
requires transitive org.apache.tika.core;
|
||||
requires transitive language.detector;
|
||||
exports com.github.gtache.autosubtitle.modules.deepl;
|
||||
exports com.github.gtache.autosubtitle.translation.deepl;
|
||||
exports com.github.gtache.autosubtitle.setup.deepl;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.github.gtache.autosubtitle.modules.deepl;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class TestDeepLModule {
|
||||
|
||||
@Test
|
||||
void testLanguageDetector() {
|
||||
final var languageDetector = DeepLModule.providesLanguageDetector();
|
||||
assertEquals("fr", languageDetector.detect("bonjour tout le monde").getLanguage());
|
||||
assertEquals("en", languageDetector.detect("hello everyone this is a text").getLanguage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.github.gtache.autosubtitle.setup.deepl;
|
||||
|
||||
import com.github.gtache.autosubtitle.process.ProcessRunner;
|
||||
import com.github.gtache.autosubtitle.setup.SetupException;
|
||||
import com.github.gtache.autosubtitle.setup.SetupStatus;
|
||||
import com.github.gtache.autosubtitle.setup.SetupUserBridge;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.util.Objects;
|
||||
import java.util.prefs.BackingStoreException;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TestDeepLSetupManager {
|
||||
|
||||
private final SetupUserBridge userBridge;
|
||||
private final Preferences preferences;
|
||||
private final ProcessRunner processRunner;
|
||||
private final HttpClient httpClient;
|
||||
|
||||
private final DeepLSetupManager setupManager;
|
||||
|
||||
TestDeepLSetupManager(@Mock final SetupUserBridge userBridge, @Mock final Preferences preferences,
|
||||
@Mock final ProcessRunner processRunner, @Mock final HttpClient httpClient) {
|
||||
this.userBridge = Objects.requireNonNull(userBridge);
|
||||
this.preferences = Objects.requireNonNull(preferences);
|
||||
this.processRunner = Objects.requireNonNull(processRunner);
|
||||
this.httpClient = Objects.requireNonNull(httpClient);
|
||||
this.setupManager = new DeepLSetupManager(userBridge, preferences, processRunner, httpClient);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStatus() {
|
||||
assertEquals(SetupStatus.NOT_INSTALLED, setupManager.getStatus());
|
||||
when(preferences.get("deepl.api.key", null)).thenReturn("key");
|
||||
assertEquals(SetupStatus.BUNDLE_INSTALLED, setupManager.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInstall() throws BackingStoreException, SetupException {
|
||||
final var key = "key";
|
||||
when(userBridge.askForUserInput(any())).thenReturn(key);
|
||||
setupManager.install();
|
||||
verify(preferences).put("deepl.api.key", key);
|
||||
verify(preferences).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInstallException() throws BackingStoreException {
|
||||
final var key = "key";
|
||||
when(userBridge.askForUserInput(any())).thenReturn(key);
|
||||
doThrow(BackingStoreException.class).when(preferences).flush();
|
||||
assertThrows(SetupException.class, setupManager::install);
|
||||
verify(preferences).put("deepl.api.key", key);
|
||||
verify(preferences).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInstallNull() {
|
||||
assertThrows(SetupException.class, setupManager::install);
|
||||
verifyNoInteractions(preferences);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUninstall() throws BackingStoreException, SetupException {
|
||||
setupManager.uninstall();
|
||||
verify(preferences).remove("deepl.api.key");
|
||||
verify(preferences).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUninstallException() throws BackingStoreException {
|
||||
doThrow(BackingStoreException.class).when(preferences).flush();
|
||||
assertThrows(SetupException.class, setupManager::uninstall);
|
||||
verify(preferences).remove("deepl.api.key");
|
||||
verify(preferences).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testName() {
|
||||
assertEquals("DeepL", setupManager.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIllegal() {
|
||||
assertThrows(NullPointerException.class, () -> new DeepLSetupManager(null, preferences, processRunner, httpClient));
|
||||
assertThrows(NullPointerException.class, () -> new DeepLSetupManager(userBridge, null, processRunner, httpClient));
|
||||
assertThrows(NullPointerException.class, () -> new DeepLSetupManager(userBridge, preferences, null, httpClient));
|
||||
assertThrows(NullPointerException.class, () -> new DeepLSetupManager(userBridge, preferences, processRunner, null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.github.gtache.autosubtitle.translation.deepl;
|
||||
|
||||
//TODO mock, postman, ...
|
||||
class TestDeepLTranslator {
|
||||
}
|
||||
Reference in New Issue
Block a user