-
Notifications
You must be signed in to change notification settings - Fork 112
chore: Add unit tests to AnyTableLoader #1380
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
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0b26bfe
chore(core): add unit tests to AnyTableLoader
davidgamez 9d99240
chore(core): code cleanup
davidgamez 22eaa4d
chore(core): code formatting
davidgamez 2fa57b0
chore(core): fix missing required unit test
davidgamez a13d544
chore: replace junit assertions with google truth library
davidgamez 2f52dd4
chore: refactor TestUtils assertion method
davidgamez f85742f
chore: replace string methods in favor of toInputStream util
davidgamez 49c132e
removing input stream helper method
davidgamez 1c6e0f1
remove autovalue builder reference
davidgamez 6714d8b
chore: enhance list instantiations with immutable list
davidgamez 2c0c751
refactor test removing the times called verification in favor of equa…
davidgamez 77a4b50
rename testgtfs classes
davidgamez df25717
chore: remove times assertion from AnyTableLoaderTest.parsableTableRo…
davidgamez b82e67f
chore: code cleanup
davidgamez 84cb79b
chore: removing local toInputStream in favor of TestUtils method
davidgamez 2208d06
remove commented line
davidgamez 8c63578
Add MockitoRule and combine assertions
davidgamez bda4860
formatting code
davidgamez dec85a6
Update AnyTableLoaderTest.java
davidgamez cbb3248
fix gradle build file
davidgamez 05b5690
Merge branch 'master' into chore/table-loader-unit-tests
davidgamez 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
27 changes: 27 additions & 0 deletions
27
core/src/test/java/org/mobilitydata/gtfsvalidator/TestUtils.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.mobilitydata.gtfsvalidator; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
|
||
public class TestUtils { | ||
|
||
protected TestUtils() { | ||
// Hiding default constructor to avoid instantiation | ||
} | ||
|
||
// In TestUtils... | ||
public static List<Class<? extends ValidationNotice>> validationNoticeTypes( | ||
NoticeContainer notices) { | ||
return notices.getValidationNotices().stream().map(obj -> obj.getClass()).collect(toList()); | ||
} | ||
|
||
public static InputStream toInputStream(String s) { | ||
return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
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
173 changes: 173 additions & 0 deletions
173
core/src/test/java/org/mobilitydata/gtfsvalidator/table/AnyTableLoaderTest.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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package org.mobilitydata.gtfsvalidator.table; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mobilitydata.gtfsvalidator.TestUtils.toInputStream; | ||
import static org.mobilitydata.gtfsvalidator.TestUtils.validationNoticeTypes; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mobilitydata.gtfsvalidator.annotation.FieldLevelEnum; | ||
import org.mobilitydata.gtfsvalidator.notice.*; | ||
import org.mobilitydata.gtfsvalidator.parsing.CsvHeader; | ||
import org.mobilitydata.gtfsvalidator.testgtfs.GtfsTestEntity; | ||
import org.mobilitydata.gtfsvalidator.testgtfs.GtfsTestFileValidator; | ||
import org.mobilitydata.gtfsvalidator.testgtfs.GtfsTestTableDescriptor; | ||
import org.mobilitydata.gtfsvalidator.validator.GtfsFieldValidator; | ||
import org.mobilitydata.gtfsvalidator.validator.TableHeaderValidator; | ||
import org.mobilitydata.gtfsvalidator.validator.ValidatorProvider; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.mockito.quality.Strictness; | ||
|
||
public class AnyTableLoaderTest { | ||
|
||
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
@Mock private GtfsTableContainer mockContainer; | ||
@Mock private ValidatorProvider validatorProvider; | ||
private NoticeContainer loaderNotices; | ||
|
||
@Before | ||
public void setup() { | ||
loaderNotices = new NoticeContainer(); | ||
} | ||
|
||
@Test | ||
public void invalidInputStream() { | ||
var testTableDescriptor = mock(GtfsTableDescriptor.class); | ||
when(testTableDescriptor.gtfsFilename()).thenReturn("_not_a_valid_file_"); | ||
when(testTableDescriptor.createContainerForInvalidStatus( | ||
GtfsTableContainer.TableStatus.INVALID_HEADERS)) | ||
.thenReturn(mockContainer); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, null, loaderNotices); | ||
|
||
assertThat(validationNoticeTypes(loaderNotices)).containsExactly(CsvParsingFailedNotice.class); | ||
assertThat(loadedContainer).isEqualTo(mockContainer); | ||
} | ||
|
||
@Test | ||
public void emptyInputStream() { | ||
var testTableDescriptor = mock(GtfsTableDescriptor.class); | ||
when(testTableDescriptor.gtfsFilename()).thenReturn("filename"); | ||
when(testTableDescriptor.createContainerForInvalidStatus( | ||
GtfsTableContainer.TableStatus.EMPTY_FILE)) | ||
.thenReturn(mockContainer); | ||
InputStream csvInputStream = toInputStream(""); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, csvInputStream, loaderNotices); | ||
|
||
assertThat(loaderNotices.getValidationNotices()) | ||
.containsExactly(new EmptyFileNotice("filename")); | ||
assertThat(loadedContainer).isEqualTo(mockContainer); | ||
} | ||
|
||
@Test | ||
public void invalidHeaders() { | ||
var testTableDescriptor = mock(GtfsTableDescriptor.class); | ||
when(testTableDescriptor.gtfsFilename()).thenReturn("filename"); | ||
when(testTableDescriptor.getColumns()).thenReturn(ImmutableList.of()); | ||
when(testTableDescriptor.createContainerForInvalidStatus( | ||
GtfsTableContainer.TableStatus.INVALID_HEADERS)) | ||
.thenReturn(mockContainer); | ||
InputStream csvInputStream = toInputStream("A file with no headers"); | ||
ValidationNotice headerValidationNotice = mock(ValidationNotice.class); | ||
when(headerValidationNotice.isError()).thenReturn(true); | ||
TableHeaderValidator tableHeaderValidator = | ||
new TableHeaderValidator() { | ||
@Override | ||
public void validate( | ||
String filename, | ||
CsvHeader actualHeader, | ||
Set<String> supportedHeaders, | ||
Set<String> requiredHeaders, | ||
NoticeContainer noticeContainer) { | ||
noticeContainer.addValidationNotice(headerValidationNotice); | ||
} | ||
}; | ||
when(validatorProvider.getTableHeaderValidator()).thenReturn(tableHeaderValidator); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, csvInputStream, loaderNotices); | ||
|
||
assertThat(loaderNotices.getValidationNotices()).containsExactly(headerValidationNotice); | ||
assertThat(loadedContainer).isEqualTo(mockContainer); | ||
} | ||
|
||
@Test | ||
public void invalidRowLengthNotice() { | ||
var testTableDescriptor = spy(new GtfsTestTableDescriptor()); | ||
when(testTableDescriptor.createContainerForInvalidStatus( | ||
GtfsTableContainer.TableStatus.UNPARSABLE_ROWS)) | ||
.thenReturn(mockContainer); | ||
when(validatorProvider.getTableHeaderValidator()).thenReturn(mock(TableHeaderValidator.class)); | ||
InputStream inputStream = toInputStream("id,code\n" + "s1\n"); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, inputStream, loaderNotices); | ||
|
||
assertThat(loaderNotices.getValidationNotices()) | ||
.containsExactly(new InvalidRowLengthNotice("filename.txt", 2, 1, 2)); | ||
assertThat(loadedContainer).isEqualTo(mockContainer); | ||
} | ||
|
||
@Test | ||
public void parsableTableRows() { | ||
davidgamez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var testTableDescriptor = new GtfsTestTableDescriptor(); | ||
when(validatorProvider.getTableHeaderValidator()).thenReturn(mock(TableHeaderValidator.class)); | ||
when(validatorProvider.getFieldValidator()).thenReturn(mock(GtfsFieldValidator.class)); | ||
GtfsTestFileValidator validator = mock(GtfsTestFileValidator.class); | ||
when(validatorProvider.createSingleFileValidators(any())).thenReturn(List.of(validator)); | ||
InputStream inputStream = toInputStream("id,stop_lat,_no_name_\n" + "s1, 23.00, no_value\n"); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, inputStream, loaderNotices); | ||
|
||
assertThat(loadedContainer.getTableStatus()) | ||
.isEqualTo(GtfsTableContainer.TableStatus.PARSABLE_HEADERS_AND_ROWS); | ||
verify(validator, times(1)).validate(any()); | ||
} | ||
|
||
@Test | ||
public void missingRequiredField() { | ||
davidgamez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var testTableDescriptor = spy(new GtfsTestTableDescriptor()); | ||
when(testTableDescriptor.getColumns()) | ||
.thenReturn( | ||
ImmutableList.of( | ||
GtfsColumnDescriptor.builder() | ||
.setColumnName(GtfsTestEntity.ID_FIELD_NAME) | ||
.setHeaderRequired(true) | ||
.setFieldLevel(FieldLevelEnum.REQUIRED) | ||
.setIsMixedCase(false) | ||
.setIsCached(false) | ||
.build(), | ||
GtfsColumnDescriptor.builder() | ||
.setColumnName(GtfsTestEntity.CODE_FIELD_NAME) | ||
.setHeaderRequired(false) | ||
.setFieldLevel(FieldLevelEnum.REQUIRED) | ||
.setIsMixedCase(false) | ||
.setIsCached(false) | ||
.build())); | ||
when(testTableDescriptor.createContainerForInvalidStatus( | ||
GtfsTableContainer.TableStatus.UNPARSABLE_ROWS)) | ||
.thenReturn(mockContainer); | ||
when(validatorProvider.getTableHeaderValidator()).thenReturn(mock(TableHeaderValidator.class)); | ||
when(validatorProvider.getFieldValidator()).thenReturn(mock(GtfsFieldValidator.class)); | ||
InputStream inputStream = toInputStream("id,code\n" + "s1,\n"); | ||
|
||
var loadedContainer = | ||
AnyTableLoader.load(testTableDescriptor, validatorProvider, inputStream, loaderNotices); | ||
|
||
assertThat(loaderNotices.getValidationNotices()) | ||
.contains(new MissingRequiredFieldNotice("filename.txt", 2, "code")); | ||
assertThat(loadedContainer).isEqualTo(mockContainer); | ||
} | ||
} |
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
28 changes: 0 additions & 28 deletions
28
core/src/test/java/org/mobilitydata/gtfsvalidator/testgtfs/GtfsStop.java
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
core/src/test/java/org/mobilitydata/gtfsvalidator/testgtfs/GtfsStopTableContainer.java
This file was deleted.
Oops, something went wrong.
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.