-
Notifications
You must be signed in to change notification settings - Fork 8
Lab due1124 #7
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
base: mocking
Are you sure you want to change the base?
Lab due1124 #7
Changes from all commits
9355451
d6e7a04
9b10810
3fa7cc8
02e9361
bf5f2f5
278cb66
0b911d4
eb4bc0d
17abef5
053f2d3
609360e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,3 +60,5 @@ local.properties | |
| #.project | ||
| /target/ | ||
| api-key.txt | ||
|
|
||
| *.log | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,31 +3,59 @@ | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| 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; | ||
| import org.mockito.Mock; | ||
| import edu.mills.cs180a.wordnik.client.api.WordApi; | ||
| import edu.mills.cs180a.wordnik.client.api.WordsApi; | ||
| import edu.mills.cs180a.wordnik.client.model.FrequencySummary; | ||
| import edu.mills.cs180a.wordnik.client.model.WordOfTheDay; | ||
| import javafx.collections.FXCollections; | ||
| import javafx.collections.ObservableList; | ||
|
|
||
| class SampleDataTest { | ||
|
|
||
| @Mock | ||
| private final WordApi mockWordApi = mock(WordApi.class); | ||
| private static final Map<String, FrequencySummary> FREQS_MAP = Map.of( | ||
| "apple", makeFrequencySummary(List.of(makeMap(2000, 339), makeMap(2001, 464))), | ||
| "orange", makeFrequencySummary(List.of(makeMap(2000, 774), makeMap(2001, 941)))); | ||
| @Mock | ||
| private final WordsApi mockWordsApi = mock(WordsApi.class); | ||
| private static final String SAMPLE_WORD_DEF = | ||
| "A perennial climbing vine (Piper nigrum) native to India, widely cultivated for its long slender spikes of small fruit."; | ||
| private static final Map<Object, Object> SAMPLE_WORD_DEF_MAP = Map.of("text", SAMPLE_WORD_DEF); | ||
| private static final List<Object> SAMPLE_WORD_DEF_LIST = List.of(SAMPLE_WORD_DEF_MAP); | ||
| private static final String SAMPLE_WORD_STRING = "pepper"; | ||
| private static final WordOfTheDay SAMPLE_WORD_OF_THE_DAY = | ||
| makeWOD(SAMPLE_WORD_STRING, SAMPLE_WORD_DEF_LIST); | ||
| private static final int SAMPLE_WORD_FREQ = 1; | ||
| private static final int SAMPLE_WORD_YEAR = 2012; | ||
| private static final String SAMPLE_WORD_CSV = SAMPLE_WORD_STRING + "," + SAMPLE_WORD_YEAR | ||
| + "," + SAMPLE_WORD_FREQ; | ||
| private static final FrequencySummary SAMPLE_WORD_FREQ_SUMMARY = | ||
| makeFrequencySummary(List.of(makeFreqMap(SAMPLE_WORD_YEAR, SAMPLE_WORD_FREQ))); | ||
| private static final Map<String, FrequencySummary> FREQS_MAP = Map.of("apple", | ||
| makeFrequencySummary(List.of(makeFreqMap(2000, 339), makeFreqMap(2001, 464))), "orange", | ||
| makeFrequencySummary(List.of(makeFreqMap(2000, 774), makeFreqMap(2001, 941)))); | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| when(mockWordApi.getWordFrequency(anyString(), anyString(), anyInt(), anyInt())) | ||
| .thenAnswer(invocation -> FREQS_MAP.get(invocation.getArgument(0))); | ||
| when(mockWordApi.getWordFrequency(eq(SAMPLE_WORD_STRING), anyString(), anyInt(), anyInt())) | ||
| .thenReturn(SAMPLE_WORD_FREQ_SUMMARY); | ||
| when(mockWordsApi.getWordOfTheDay()).thenReturn(SAMPLE_WORD_OF_THE_DAY); | ||
| } | ||
|
|
||
| private static Map<Object, Object> makeMap(int year, int count) { | ||
| return Map.of(SampleData.FREQ_YEAR_KEY, String.valueOf(year), | ||
| SampleData.FREQ_COUNT_KEY, count); | ||
| private static Map<Object, Object> makeFreqMap(int year, int count) { | ||
| return Map.of(SampleData.FREQ_YEAR_KEY, String.valueOf(year), SampleData.FREQ_COUNT_KEY, | ||
| count); | ||
| } | ||
|
|
||
| private static FrequencySummary makeFrequencySummary(List<Object> freqs) { | ||
|
|
@@ -38,8 +66,37 @@ private static FrequencySummary makeFrequencySummary(List<Object> freqs) { | |
|
|
||
| @ParameterizedTest | ||
| @CsvSource({"apple,2000,339", "apple,2001,464", "apple,2020,0", "orange,2000,774", | ||
| "orange,2001,941", "orange,2050,0"}) | ||
| "orange,2001,941", "orange,2050,0", SAMPLE_WORD_CSV}) | ||
| void testGetFrequencyFromSummary(String word, int year, int count) { | ||
| assertEquals(count, SampleData.getFrequencyByYear(mockWordApi, word, year)); | ||
| } | ||
|
|
||
| private static WordOfTheDay makeWOD(String word, List<Object> defs) { | ||
| WordOfTheDay wod = mock(WordOfTheDay.class); | ||
| when(wod.getWord()).thenReturn(word); | ||
| when(wod.getDefinitions()).thenReturn(defs); | ||
| return wod; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Test | ||
| void getWordOfTheDay_True_CorrectWordReturned() { | ||
| WordOfTheDay word = SampleData.getWordOfTheDay(mockWordsApi); | ||
| assertEquals(SAMPLE_WORD_STRING, word.getWord()); | ||
| assertEquals(1, word.getDefinitions().size()); | ||
| assertEquals(SAMPLE_WORD_DEF, | ||
| ((Map<Object, Object>) word.getDefinitions().get(0)).get("text").toString()); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also test the frequency.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't call the static method. Use a local variable to reference the |
||
|
|
||
| @Test | ||
| void addWordOfTheDay_True_WordAddedToBackingList() throws IOException { | ||
| ObservableList<WordRecord> backingList = FXCollections.observableArrayList(); | ||
| assertEquals(0, backingList.size()); | ||
| SampleData.addWordOfTheDay(mockWordApi, mockWordsApi, backingList); | ||
| assertEquals(1, backingList.size()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, but it would be better to use a local variable to reference the WordRecord added to the list, and call getFrequency(), getWord(), and getDefinition() on that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converted to local variables. |
||
| WordRecord wordRecordFromList = backingList.get(0); | ||
| assertEquals(SAMPLE_WORD_STRING, wordRecordFromList.getWord()); | ||
| assertEquals(SAMPLE_WORD_DEF, wordRecordFromList.getDefinition()); | ||
| assertEquals(SAMPLE_WORD_FREQ, wordRecordFromList.getFrequency()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good.