-
Notifications
You must be signed in to change notification settings - Fork 38
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
Editor: Allow null values for foreign ref fields or required fields where permitted #133
Changes from 10 commits
d63d0fb
8189831
40ef82d
5b87be8
49b4611
305df47
a6943aa
78a7785
795fdcb
fbadc7b
d10aff3
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.conveyal.gtfs.loader; | ||
|
||
import com.conveyal.gtfs.TestUtils; | ||
import com.conveyal.gtfs.util.FareDTO; | ||
import com.conveyal.gtfs.util.FareRuleDTO; | ||
import com.conveyal.gtfs.util.FeedInfoDTO; | ||
import com.conveyal.gtfs.util.InvalidNamespaceException; | ||
import com.conveyal.gtfs.util.RouteDTO; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
|
@@ -28,14 +31,19 @@ public class JDBCTableWriterTest { | |
private static String testDBName; | ||
private static DataSource testDataSource; | ||
private static String testNamespace; | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private static JdbcTableWriter createTestTableWriter (Table table) throws InvalidNamespaceException { | ||
return new JdbcTableWriter(table, testDataSource, testNamespace); | ||
} | ||
|
||
@BeforeClass | ||
public static void setUpClass() throws SQLException { | ||
// create a new database | ||
testDBName = TestUtils.generateNewDB(); | ||
String dbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testDBName); | ||
testDataSource = createDataSource (dbConnectionUrl, null, null); | ||
LOG.info("creating feeds table because it isn't automtically generated unless you import a feed"); | ||
LOG.info("creating feeds table because it isn't automatically generated unless you import a feed"); | ||
Connection connection = testDataSource.getConnection(); | ||
connection.createStatement() | ||
.execute("create table if not exists feeds (namespace varchar primary key, md5 varchar, " + | ||
|
@@ -50,8 +58,10 @@ public static void setUpClass() throws SQLException { | |
} | ||
|
||
@Test | ||
public void canCreateUpdateAndDeleteFeedinfoEntities () throws IOException, SQLException, InvalidNamespaceException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
public void canCreateUpdateAndDeleteFeedInfoEntities() throws IOException, SQLException, InvalidNamespaceException { | ||
// Store Table and Class values for use in test. | ||
final Table feedInfoTable = Table.FEED_INFO; | ||
final Class<FeedInfoDTO> feedInfoDTOClass = FeedInfoDTO.class; | ||
|
||
// create new object to be saved | ||
FeedInfoDTO feedInfoInput = new FeedInfoDTO(); | ||
|
@@ -63,13 +73,13 @@ public void canCreateUpdateAndDeleteFeedinfoEntities () throws IOException, SQLE | |
feedInfoInput.default_route_type = "3"; | ||
|
||
// convert object to json and save it | ||
JdbcTableWriter createTableWriter = new JdbcTableWriter(Table.FEED_INFO, testDataSource, testNamespace); | ||
JdbcTableWriter createTableWriter = createTestTableWriter(feedInfoTable); | ||
String createOutput = createTableWriter.create(mapper.writeValueAsString(feedInfoInput), true); | ||
LOG.info("create output:"); | ||
LOG.info("create {} output:", feedInfoTable.name); | ||
LOG.info(createOutput); | ||
|
||
// parse output | ||
FeedInfoDTO createdFeedInfo = mapper.readValue(createOutput, FeedInfoDTO.class); | ||
FeedInfoDTO createdFeedInfo = mapper.readValue(createOutput, feedInfoDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(createdFeedInfo.feed_publisher_name, equalTo(publisherName)); | ||
|
@@ -79,34 +89,34 @@ public void canCreateUpdateAndDeleteFeedinfoEntities () throws IOException, SQLE | |
createdFeedInfo.feed_publisher_name = updatedPublisherName; | ||
|
||
// covert object to json and save it | ||
JdbcTableWriter updateTableWriter = new JdbcTableWriter(Table.FEED_INFO, testDataSource, testNamespace); | ||
JdbcTableWriter updateTableWriter = createTestTableWriter(feedInfoTable); | ||
String updateOutput = updateTableWriter.update( | ||
createdFeedInfo.id, | ||
mapper.writeValueAsString(createdFeedInfo), | ||
true | ||
); | ||
LOG.info("update output:"); | ||
LOG.info("update {} output:", feedInfoTable.name); | ||
LOG.info(updateOutput); | ||
|
||
FeedInfoDTO updatedFeedInfoDTO = mapper.readValue(updateOutput, FeedInfoDTO.class); | ||
FeedInfoDTO updatedFeedInfoDTO = mapper.readValue(updateOutput, feedInfoDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(updatedFeedInfoDTO.feed_publisher_name, equalTo(updatedPublisherName)); | ||
|
||
// try to delete record | ||
JdbcTableWriter deleteTableWriter = new JdbcTableWriter(Table.FEED_INFO, testDataSource, testNamespace); | ||
JdbcTableWriter deleteTableWriter = createTestTableWriter(feedInfoTable); | ||
int deleteOutput = deleteTableWriter.delete( | ||
createdFeedInfo.id, | ||
true | ||
); | ||
LOG.info("delete output:"); | ||
LOG.info("delete {} output:", feedInfoTable.name); | ||
LOG.info(updateOutput); | ||
|
||
// make sure record does not exist in DB | ||
String sql = String.format( | ||
"select * from %s.%s where id=%d", | ||
testNamespace, | ||
Table.FEED_INFO.name, | ||
feedInfoTable.name, | ||
createdFeedInfo.id | ||
); | ||
LOG.info(sql); | ||
|
@@ -116,8 +126,6 @@ public void canCreateUpdateAndDeleteFeedinfoEntities () throws IOException, SQLE | |
|
||
@Test | ||
public void canPreventSQLInjection() throws IOException, SQLException, InvalidNamespaceException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
// create new object to be saved | ||
FeedInfoDTO feedInfoInput = new FeedInfoDTO(); | ||
String publisherName = "' OR 1 = 1; SELECT '1"; | ||
|
@@ -128,7 +136,7 @@ public void canPreventSQLInjection() throws IOException, SQLException, InvalidNa | |
feedInfoInput.default_route_type = "3"; | ||
|
||
// convert object to json and save it | ||
JdbcTableWriter createTableWriter = new JdbcTableWriter(Table.FEED_INFO, testDataSource, testNamespace); | ||
JdbcTableWriter createTableWriter = createTestTableWriter(Table.FEED_INFO); | ||
String createOutput = createTableWriter.create(mapper.writeValueAsString(feedInfoInput), true); | ||
LOG.info("create output:"); | ||
LOG.info(createOutput); | ||
|
@@ -140,6 +148,166 @@ public void canPreventSQLInjection() throws IOException, SQLException, InvalidNa | |
assertThat(createdFeedInfo.feed_publisher_name, equalTo(publisherName)); | ||
} | ||
|
||
@Test | ||
public void canCreateUpdateAndDeleteFares() throws IOException, SQLException, InvalidNamespaceException { | ||
// Store Table and Class values for use in test. | ||
final Table fareTable = Table.FARE_ATTRIBUTES; | ||
final Class<FareDTO> fareDTOClass = FareDTO.class; | ||
|
||
// create new object to be saved | ||
FareDTO fareInput = new FareDTO(); | ||
String fareId = "2A"; | ||
fareInput.fare_id = fareId; | ||
fareInput.currency_type = "USD"; | ||
fareInput.price = 2.50; | ||
fareInput.agency_id = "RTA"; | ||
fareInput.payment_method = 0; | ||
// Empty value should be permitted for transfers and transfer_duration | ||
fareInput.transfers = null; | ||
fareInput.transfer_duration = null; | ||
FareRuleDTO fareRuleInput = new FareRuleDTO(); | ||
// Fare ID should be assigned to "child entity" by editor automatically. | ||
fareRuleInput.fare_id = null; | ||
fareRuleInput.route_id = null; | ||
// FIXME There is currently no check for valid zone_id values in contains_id, origin_id, and destination_id. | ||
fareRuleInput.contains_id = "any"; | ||
fareRuleInput.origin_id = "value"; | ||
fareRuleInput.destination_id = "permitted"; | ||
fareInput.fare_rules = new FareRuleDTO[]{fareRuleInput}; | ||
|
||
// convert object to json and save it | ||
JdbcTableWriter createTableWriter = createTestTableWriter(fareTable); | ||
String createOutput = createTableWriter.create(mapper.writeValueAsString(fareInput), true); | ||
LOG.info("create {} output:", fareTable.name); | ||
LOG.info(createOutput); | ||
|
||
// parse output | ||
FareDTO createdFare = mapper.readValue(createOutput, fareDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(createdFare.fare_id, equalTo(fareId)); | ||
assertThat(createdFare.fare_rules[0].fare_id, equalTo(fareId)); | ||
|
||
// try to update record | ||
String updatedFareId = "3B"; | ||
createdFare.fare_id = updatedFareId; | ||
|
||
// covert object to json and save it | ||
JdbcTableWriter updateTableWriter = createTestTableWriter(fareTable); | ||
String updateOutput = updateTableWriter.update( | ||
createdFare.id, | ||
mapper.writeValueAsString(createdFare), | ||
true | ||
); | ||
LOG.info("update {} output:", fareTable.name); | ||
LOG.info(updateOutput); | ||
|
||
FareDTO updatedFareDTO = mapper.readValue(updateOutput, fareDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(updatedFareDTO.fare_id, equalTo(updatedFareId)); | ||
assertThat(updatedFareDTO.fare_rules[0].fare_id, equalTo(updatedFareId)); | ||
|
||
// try to delete record | ||
JdbcTableWriter deleteTableWriter = createTestTableWriter(fareTable); | ||
int deleteOutput = deleteTableWriter.delete( | ||
createdFare.id, | ||
true | ||
); | ||
LOG.info("delete {} output:", fareTable.name); | ||
LOG.info(updateOutput); | ||
|
||
// make sure fare_attributes record does not exist in DB | ||
String sql = String.format( | ||
"select * from %s.%s where id=%d", | ||
testNamespace, | ||
fareTable.name, | ||
createdFare.id | ||
); | ||
LOG.info(sql); | ||
ResultSet rs = testDataSource.getConnection().prepareStatement(sql).executeQuery(); | ||
assertThat(rs.getFetchSize(), equalTo(0)); | ||
|
||
// make sure fare_rules record does not exist in DB | ||
String fareRulesSql = String.format( | ||
"select * from %s.%s where id=%d", | ||
testNamespace, | ||
Table.FARE_RULES.name, | ||
createdFare.fare_rules[0].id | ||
); | ||
LOG.info(fareRulesSql); | ||
ResultSet fareRulesResultSet = testDataSource.getConnection().prepareStatement(fareRulesSql).executeQuery(); | ||
assertThat(fareRulesResultSet.getFetchSize(), equalTo(0)); | ||
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. Now that I look at these tests, it seems like they could really use a sql check to make sure that the data was created and updated. Also, these 2 lines could be made into a reusable method 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 idea. 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. Hmm, actually I think the checking for created/updated data gets a little muddied because we would then need to ensure that we're using the same connection to see the data we expect. I think the first part of your comment is out of scope, but I will add a |
||
} | ||
|
||
@Test | ||
public void canCreateUpdateAndDeleteRoutes() throws IOException, SQLException, InvalidNamespaceException { | ||
// Store Table and Class values for use in test. | ||
final Table routeTable = Table.ROUTES; | ||
final Class<RouteDTO> routeDTOClass = RouteDTO.class; | ||
|
||
// create new object to be saved | ||
RouteDTO routeInput = new RouteDTO(); | ||
String routeId = "500"; | ||
routeInput.route_id = routeId; | ||
routeInput.agency_id = "RTA"; | ||
// Empty value should be permitted for transfers and transfer_duration | ||
routeInput.route_short_name = "500"; | ||
routeInput.route_long_name = "Hollingsworth"; | ||
routeInput.route_type = 3; | ||
|
||
// convert object to json and save it | ||
JdbcTableWriter createTableWriter = createTestTableWriter(routeTable); | ||
String createOutput = createTableWriter.create(mapper.writeValueAsString(routeInput), true); | ||
LOG.info("create {} output:", routeTable.name); | ||
LOG.info(createOutput); | ||
|
||
// parse output | ||
RouteDTO createdFare = mapper.readValue(createOutput, routeDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(createdFare.route_id, equalTo(routeId)); | ||
|
||
// try to update record | ||
String updatedRouteId = "600"; | ||
createdFare.route_id = updatedRouteId; | ||
|
||
// covert object to json and save it | ||
JdbcTableWriter updateTableWriter = createTestTableWriter(routeTable); | ||
String updateOutput = updateTableWriter.update( | ||
createdFare.id, | ||
mapper.writeValueAsString(createdFare), | ||
true | ||
); | ||
LOG.info("update {} output:", routeTable.name); | ||
LOG.info(updateOutput); | ||
|
||
RouteDTO updatedRouteDTO = mapper.readValue(updateOutput, routeDTOClass); | ||
|
||
// make sure saved data matches expected data | ||
assertThat(updatedRouteDTO.route_id, equalTo(updatedRouteId)); | ||
|
||
// try to delete record | ||
JdbcTableWriter deleteTableWriter = createTestTableWriter(routeTable); | ||
int deleteOutput = deleteTableWriter.delete( | ||
createdFare.id, | ||
true | ||
); | ||
LOG.info("delete {} output:", routeTable.name); | ||
LOG.info(updateOutput); | ||
|
||
// make sure route record does not exist in DB | ||
String sql = String.format( | ||
"select * from %s.%s where id=%d", | ||
testNamespace, | ||
routeTable.name, | ||
createdFare.id | ||
); | ||
LOG.info(sql); | ||
ResultSet rs = testDataSource.getConnection().prepareStatement(sql).executeQuery(); | ||
assertThat(rs.getFetchSize(), equalTo(0)); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
TestUtils.dropDB(testDBName); | ||
|
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.
This should probably be
deleteOutput
.