Search in sources :

Example 1 with TranslateTextResult

use of com.amplifyframework.predictions.result.TranslateTextResult in project amplify-android by aws-amplify.

the class RxPredictionsBindingTest method testSuccessfulTranslateText.

/**
 * When the delegate returns a result for the {@link RxPredictionsBinding#translateText(String)}
 * call, the result should be emitted via the returned {@link Single}.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void testSuccessfulTranslateText() throws InterruptedException {
    String text = "Cat";
    TranslateTextResult result = TranslateTextResult.builder().targetLanguage(LanguageType.SPANISH).translatedText("Gato").build();
    doAnswer(invocation -> {
        // 0 = text, 1 = result, 2 = error
        final int indexOfResultConsumer = 1;
        Consumer<TranslateTextResult> onResult = invocation.getArgument(indexOfResultConsumer);
        onResult.accept(result);
        return mock(TranslateTextOperation.class);
    }).when(delegate).translateText(eq(text), anyConsumer(), anyConsumer());
    TestObserver<TranslateTextResult> observer = rxPredictions.translateText(text).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertValue(result);
}
Also used : RandomString(com.amplifyframework.testutils.random.RandomString) TranslateTextResult(com.amplifyframework.predictions.result.TranslateTextResult) Test(org.junit.Test)

Example 2 with TranslateTextResult

use of com.amplifyframework.predictions.result.TranslateTextResult in project amplify-android by aws-amplify.

the class AWSPredictionsTranslateTest method testTranslationWithLanguageOverride.

/**
 * Assert that category falls back to configured default language.
 * @throws Exception if prediction fails
 */
@Test
public void testTranslationWithLanguageOverride() throws Exception {
    final String sampleText = "¡Hola mundo!";
    // Translate english text and assert non-null result.
    // Use configured default languages
    TranslateTextResult result = predictions.translateText(sampleText, LanguageType.SPANISH, LanguageType.ENGLISH);
    assertNotNull(result);
    // Assert translated language is in English
    LanguageType language = result.getTargetLanguage();
    assertEquals(LanguageType.ENGLISH, language);
    // Assert translation
    String translation = result.getTranslatedText();
    assertEquals("Hello world!", translation);
}
Also used : TranslateTextResult(com.amplifyframework.predictions.result.TranslateTextResult) LanguageType(com.amplifyframework.predictions.models.LanguageType) Test(org.junit.Test)

Example 3 with TranslateTextResult

use of com.amplifyframework.predictions.result.TranslateTextResult in project amplify-android by aws-amplify.

the class AWSPredictionsTranslateTest method testTranslationWithDefaultLanguage.

/**
 * Assert that category falls back to configured default language.
 * @throws Exception if prediction fails
 */
@Test
public void testTranslationWithDefaultLanguage() throws Exception {
    final String sampleText = "Hello world!";
    // Translate english text and assert non-null result.
    // Use configured default languages
    TranslateTextResult result = predictions.translateText(sampleText);
    assertNotNull(result);
    // Assert translated language is in Spanish
    LanguageType language = result.getTargetLanguage();
    assertEquals(LanguageType.SPANISH, language);
    // Assert translation
    String translation = result.getTranslatedText();
    assertTrue(translation.contains("Hola mundo"));
}
Also used : TranslateTextResult(com.amplifyframework.predictions.result.TranslateTextResult) LanguageType(com.amplifyframework.predictions.models.LanguageType) Test(org.junit.Test)

Example 4 with TranslateTextResult

use of com.amplifyframework.predictions.result.TranslateTextResult in project amplify-android by aws-amplify.

the class RxPredictionsBindingTest method testFailedTranslateText.

/**
 * When the delegate emits an error for the {@link RxPredictionsBinding#translateText(String)}
 * call, that error should be propagated via the returned {@link Single}.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void testFailedTranslateText() throws InterruptedException {
    String text = "Cat";
    PredictionsException predictionsException = new PredictionsException("Uh", "Oh");
    doAnswer(invocation -> {
        // 0 = text, 1 = result, 2 = error
        final int indexOfFailureConsumer = 2;
        Consumer<PredictionsException> onFailure = invocation.getArgument(indexOfFailureConsumer);
        onFailure.accept(predictionsException);
        return mock(TranslateTextOperation.class);
    }).when(delegate).translateText(eq(text), anyConsumer(), anyConsumer());
    TestObserver<TranslateTextResult> observer = rxPredictions.translateText(text).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(predictionsException);
}
Also used : PredictionsException(com.amplifyframework.predictions.PredictionsException) RandomString(com.amplifyframework.testutils.random.RandomString) TranslateTextResult(com.amplifyframework.predictions.result.TranslateTextResult) Test(org.junit.Test)

Aggregations

TranslateTextResult (com.amplifyframework.predictions.result.TranslateTextResult)4 Test (org.junit.Test)4 LanguageType (com.amplifyframework.predictions.models.LanguageType)2 RandomString (com.amplifyframework.testutils.random.RandomString)2 PredictionsException (com.amplifyframework.predictions.PredictionsException)1