Repository navigation
Write slim IPA and pronunciation URL back on vocab miss #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+312
−45
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f6d004
Write slim IPA and audio URL back on a vocab database miss.
thcathy 618d257
ci: retrigger Build feature branches for PR #21
thcathy d867a62
Drop the startup job that creates the phonetic_question WORD constraint.
thcathy 957b13f
Rename the enriched-question insert.
thcathy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 68 additions & 44 deletions
112
src/test/java/com/esl/service/PhoneticQuestionServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,78 +1,102 @@ | ||
| package com.esl.service; | ||
|
|
||
| import com.esl.dao.PhoneticQuestionDAO; | ||
| import com.esl.entity.rest.DictionaryResult; | ||
| import com.esl.model.PhoneticQuestion; | ||
| import com.esl.service.rest.WebParserRestService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
| import static org.springframework.test.util.ReflectionTestUtils.setField; | ||
|
|
||
| class PhoneticQuestionServiceTest { | ||
|
|
||
| @Test | ||
| void buildQuestionByWebAPI_whenDictionaryResolvesToDifferentWord_shouldSkipPronunciationUrl() { | ||
| // Given | ||
| var webService = mock(WebParserRestService.class); | ||
| var restTemplate = mock(RestTemplate.class); | ||
|
|
||
| // Query is "learning" but dictionary resolves to base form "learn" | ||
| var dictionaryResult = new DictionaryResult( | ||
| "learn", | ||
| "https://example.com/learn.mp3", | ||
| "en-US", | ||
| "lɜːn", | ||
| "definition" | ||
| ); | ||
| when(webService.queryDictionary("learning")) | ||
| .thenReturn(CompletableFuture.completedFuture(Optional.of(dictionaryResult))); | ||
| private WebParserRestService webService; | ||
| private PhoneticQuestionDAO phoneticQuestionDAO; | ||
| private PhoneticQuestionService service; | ||
|
|
||
| var service = new PhoneticQuestionService(); | ||
| @BeforeEach | ||
| void setUp() { | ||
| webService = mock(WebParserRestService.class); | ||
| phoneticQuestionDAO = mock(PhoneticQuestionDAO.class); | ||
| service = new PhoneticQuestionService(); | ||
| setField(service, "webService", webService); | ||
| setField(service, "restTemplate", restTemplate); | ||
| setField(service, "phoneticQuestionDAO", phoneticQuestionDAO); | ||
| setField(service, "restTemplate", mock(RestTemplate.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void buildQuestionByWebAPI_whenDictionaryResolvesToDifferentWord_shouldSkipPronunciationUrlAndNotPersist() { | ||
| when(webService.queryDictionary("learning")) | ||
| .thenReturn(CompletableFuture.completedFuture(Optional.of( | ||
| new DictionaryResult("learn", "https://example.com/learn.mp3", "en-US", "lɜːn", "definition")))); | ||
|
|
||
| // When | ||
| var question = service.buildQuestionByWebAPI("learning"); | ||
|
|
||
| // Then | ||
| assertEquals("learning", question.getWord()); | ||
| assertTrue(question.isIPAUnavailable(), "Should mark IPA unavailable when dictionary word mismatches request"); | ||
| assertNull(question.getPronouncedLink(), "Should skip pronunciationUrl so client falls back to TTS"); | ||
| assertArrayEquals(new String[] { "" }, question.getPicsFullPaths()); | ||
| verify(webService, never()).searchGoogleImage(anyString(), anyInt()); | ||
| verify(phoneticQuestionDAO, never()).insertIfAbsent(any()); | ||
| } | ||
|
|
||
| @Test | ||
| void buildQuestionByWebAPI_whenDictionaryMatchesWord_shouldUsePronunciationUrl() { | ||
| // Given | ||
| var webService = mock(WebParserRestService.class); | ||
| var restTemplate = mock(RestTemplate.class); | ||
|
|
||
| var dictionaryResult = new DictionaryResult( | ||
| "learning", | ||
| "https://example.com/learning.mp3", | ||
| "en-US", | ||
| "ˈlɜːnɪŋ", | ||
| "definition" | ||
| ); | ||
| when(webService.queryDictionary("learning")) | ||
| .thenReturn(CompletableFuture.completedFuture(Optional.of(dictionaryResult))); | ||
|
|
||
| var service = new PhoneticQuestionService(); | ||
| setField(service, "webService", webService); | ||
| setField(service, "restTemplate", restTemplate); | ||
| void buildQuestionByWebAPI_whenDictionaryMatchesWord_shouldPersistSlimRowWithRankZero() { | ||
| when(webService.queryDictionary("Learning")) | ||
| .thenReturn(CompletableFuture.completedFuture(Optional.of( | ||
| new DictionaryResult("learning", "https://example.com/learning.mp3", "en-US", "ˈlɜːnɪŋ", "definition")))); | ||
|
|
||
| // When | ||
| var question = service.buildQuestionByWebAPI("learning"); | ||
| var question = service.buildQuestionByWebAPI("Learning"); | ||
|
|
||
| // Then | ||
| assertEquals("learning", question.getWord()); | ||
| assertFalse(question.isIPAUnavailable(), "Should keep IPA available when dictionary provides IPA for exact match"); | ||
| assertEquals("Learning", question.getWord()); | ||
| assertFalse(question.isIPAUnavailable()); | ||
| assertEquals("ˈlɜːnɪŋ", question.getIPA()); | ||
| assertEquals("https://example.com/learning.mp3", question.getPronouncedLink()); | ||
| assertEquals(0, question.getRank()); | ||
| assertArrayEquals(new String[] { "" }, question.getPicsFullPaths()); | ||
|
|
||
| var captor = ArgumentCaptor.forClass(PhoneticQuestion.class); | ||
| verify(phoneticQuestionDAO).insertIfAbsent(captor.capture()); | ||
| var saved = captor.getValue(); | ||
| assertEquals("Learning", saved.getWord()); | ||
| assertEquals("ˈlɜːnɪŋ", saved.getIPA()); | ||
| assertEquals("https://example.com/learning.mp3", saved.getPronouncedLink()); | ||
| assertEquals(0, saved.getRank()); | ||
| assertNull(saved.getPicFileName()); | ||
| assertNull(saved.getPronouncedLinkBackup()); | ||
| assertTrue(saved.getGrades().isEmpty()); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("incompleteDictionaryResults") | ||
| void buildQuestionByWebAPI_whenNotEnriched_shouldNotPersist(Optional<DictionaryResult> result) { | ||
| when(webService.queryDictionary("partial")).thenReturn(CompletableFuture.completedFuture(result)); | ||
|
|
||
| var question = service.buildQuestionByWebAPI("partial"); | ||
|
|
||
| assertFalse(question.enriched()); | ||
| assertArrayEquals(new String[] { "" }, question.getPicsFullPaths()); | ||
| verify(phoneticQuestionDAO, never()).insertIfAbsent(any()); | ||
| } | ||
|
|
||
| static Stream<Optional<DictionaryResult>> incompleteDictionaryResults() { | ||
| return Stream.of( | ||
| Optional.empty(), | ||
| Optional.of(new DictionaryResult("partial", null, "en-US", "ˈpɑːʃəl", "definition")), | ||
| Optional.of(new DictionaryResult("partial", " ", "en-US", "ˈpɑːʃəl", "definition")), | ||
| Optional.of(new DictionaryResult("partial", "https://example.com/partial.mp3", "en-US", null, "definition")), | ||
| Optional.of(new DictionaryResult("partial", "https://example.com/partial.mp3", "en-US", " ", "definition")) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.