Skip to content
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

Add a system property override for the maximum number of sampling unc… #325

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions wres-io/test/wres/io/database/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import wres.system.SettingsFactory;
import wres.system.SystemSettings;

/**
* Tests database queries.
*/

public class QueryTest
{
private static final Logger LOGGER = LoggerFactory.getLogger( QueryTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger( QueryTest.class );

private SystemSettings systemSettings;
// We need a way to control faked out database connections
Expand Down Expand Up @@ -65,7 +69,7 @@ public static void setup()
public void beforeEachTest() throws SQLException, DatabaseException
{
LOGGER.trace( "@Before began" );
this.systemSettings = SettingsFactory.defaultTest();
this.systemSettings = SettingsFactory.getDefaultSettings();
this.rawConnection = DriverManager.getConnection( QueryTest.testDatabase.getJdbcString() );

// Set up a bare bones database with only the schema
Expand All @@ -76,7 +80,6 @@ public void beforeEachTest() throws SQLException, DatabaseException
LOGGER.trace( "@Before ended" );
}


/**
* Test to see if a Query can execute a single static query in the database and return the desired results
* @throws SQLException Thrown if a connection could not be created
Expand Down Expand Up @@ -138,11 +141,10 @@ public void parameterizedCallTest() throws SQLException
// We told it to give us the value 1 as "one", so that should be what we get back
int value = results.getInt( "one" );

Assert.assertEquals( value, 1 );
Assert.assertEquals( 1, value );
}
}


/**
* Tests to see if a simple query can be executed
* @throws SQLException Thrown if a connection could not be created
Expand Down Expand Up @@ -175,7 +177,7 @@ public void simpleExecuteTest() throws SQLException, LiquibaseException
testQuery = new Query( this.systemSettings,
"SELECT hash, project_name FROM wres.Project;" );

try (ResultSet projects = testQuery.call( connection ))
try ( ResultSet projects = testQuery.call( connection ) )
{
// If "isBeforeFirst" is true, it means that there is at least one entry returned,
// meaning that inserts occurred as requested
Expand All @@ -186,10 +188,10 @@ public void simpleExecuteTest() throws SQLException, LiquibaseException

// The values we sent into the insert should show up in the results
Assert.assertEquals( "abcd123", projects.getString( "hash" ) );
Assert.assertEquals( "zero", projects.getString("project_name") );
Assert.assertEquals( "zero", projects.getString( "project_name" ) );

// If there is anything left, it means that too much data got added
Assert.assertFalse(projects.next());
Assert.assertFalse( projects.next() );
}
}

Expand All @@ -198,7 +200,6 @@ public void simpleExecuteTest() throws SQLException, LiquibaseException
QueryTest.testDatabase.dropLiquibaseChangeTables( this.rawConnection );
}


/**
* Test to see if many values can be inserted into the database with a single batch execution
* @throws SQLException Thrown if a connection could not be created
Expand All @@ -220,14 +221,14 @@ public void batchExecuteTest() throws SQLException, LiquibaseException
String script = "INSERT INTO wres.Project ( hash, project_name ) VALUES ( ?, ? );";

// Since we're going to run this as batch, we need a ton of parameters to pass in
List<Object[]> arguments = new ArrayList<>( );
List<Object[]> arguments = new ArrayList<>();

arguments.add(new Object[]{"0", "zero"});
arguments.add(new Object[]{"1", "one"});
arguments.add(new Object[]{"2", "two"});
arguments.add(new Object[]{"3", "three"});
arguments.add(new Object[]{"4", "four"});
arguments.add(new Object[]{"5", null});
arguments.add( new Object[] { "0", "zero" } );
arguments.add( new Object[] { "1", "one" } );
arguments.add( new Object[] { "2", "two" } );
arguments.add( new Object[] { "3", "three" } );
arguments.add( new Object[] { "4", "four" } );
arguments.add( new Object[] { "5", null } );

Query testQuery = new Query( this.systemSettings, script )
.setBatchParameters( arguments );
Expand All @@ -245,23 +246,23 @@ public void batchExecuteTest() throws SQLException, LiquibaseException
testQuery = new Query( this.systemSettings,
"SELECT hash, project_name FROM wres.Project;" );

try (ResultSet projects = testQuery.call( connection ))
try ( ResultSet projects = testQuery.call( connection ) )
{
// If "isBeforeFirst" is true, it means that there is at least one entry returned,
// meaning that inserts occurred as requested
Assert.assertTrue( "No values were previously added.", projects.isBeforeFirst() );

// Inserts alone doesn't mean it works, so we need to go through each row and check the arguments
// This was done in batch, the values should still be in order
for (Object[] argument : arguments)
for ( Object[] argument : arguments )
{
projects.next();
Assert.assertEquals( argument[0], projects.getString( "hash" ) );
Assert.assertEquals( argument[1], projects.getString( "project_name" ) );
}

// If there is anything left, it means that too much data got added
Assert.assertFalse("Too many values were returned.", projects.next());
Assert.assertFalse( "Too many values were returned.", projects.next() );
}
}

Expand All @@ -270,7 +271,6 @@ public void batchExecuteTest() throws SQLException, LiquibaseException
QueryTest.testDatabase.dropLiquibaseChangeTables( this.rawConnection );
}


/**
* Test to see if values may be added to the database with a parameterized statement
* @throws SQLException Thrown if a connection could not be created
Expand Down Expand Up @@ -307,15 +307,15 @@ public void parameterizedExecuteTest() throws SQLException, LiquibaseException
testQuery = new Query( this.systemSettings,
"SELECT hash, project_name FROM wres.Project;" );

try (ResultSet projects = testQuery.call( connection ))
try ( ResultSet projects = testQuery.call( connection ) )
{
// If "isBeforeFirst" is true, it means that there is at least one entry returned,
// meaning that inserts occurred as requested
Assert.assertTrue( projects.isBeforeFirst() );

int entryCount = 0;

while (projects.next())
while ( projects.next() )
{
entryCount++;
Assert.assertEquals( "0", projects.getString( "hash" ) );
Expand All @@ -331,7 +331,6 @@ public void parameterizedExecuteTest() throws SQLException, LiquibaseException
QueryTest.testDatabase.dropLiquibaseChangeTables( this.rawConnection );
}


/**
* The status of a connection's autocommit setting should be appropriate
* to whether the query itself was supposed to run in a transaction.
Expand All @@ -357,7 +356,7 @@ public void disableAutoCommitWhenInTransaction() throws SQLException
Assert.assertFalse( "Autocommit should have been switched "
+ "off, but the query may have been run with "
+ "autocommit on.",
connection.getAutoCommit() );
connection.getAutoCommit() );
}
}

Expand Down Expand Up @@ -409,4 +408,4 @@ public static void tearDown()
QueryTest.testDatabase = null;
LOGGER.trace( "@AfterClass ended" );
}
}
}
19 changes: 17 additions & 2 deletions wres-system/src/wres/system/SettingsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public static SystemSettings createSettingsFromXml( InputStream xmlInputStream )
}

/**
* Method for construction of default settings used in testing
* Method for construction of default settings used in testing.
* @return SystemSettings
*/
public static SystemSettings defaultTest()
public static SystemSettings getDefaultSettings()
{
DatabaseSettings.DatabaseSettingsBuilder builder = DatabaseSettings.builder();
applyDatabaseSystemPropertyOverrides( builder );
Expand Down Expand Up @@ -197,6 +197,7 @@ private static void applySystemPropertyOverrides( SystemSettingsBuilder systemBu
SettingsFactory.setMaximumReadThreads( systemBuilder, systemSettings );
SettingsFactory.setFeatureBatchSize( systemBuilder, systemSettings );
SettingsFactory.setFeatureBatchThreshold( systemBuilder, systemSettings );
SettingsFactory.setMaximumSamplingUncertaintyThreads( systemBuilder, systemSettings );
}

/**
Expand Down Expand Up @@ -395,6 +396,20 @@ private static void setMaximumReadThreads( SystemSettingsBuilder systemBuilder,
0 );
}

/**
* Sets the maximum number of sampling uncertainty threads.
* @param systemBuilder the system settings builder to update
* @param systemSettings the existing system settings for defaults
*/
private static void setMaximumSamplingUncertaintyThreads( SystemSettingsBuilder systemBuilder,
SystemSettings systemSettings )
{
SettingsFactory.setPropertyWithIntegerGreaterThanThis( "wres.maximumSamplingUncertaintyThreads",
systemSettings.getMaximumSamplingUncertaintyThreads(),
systemBuilder::maximumSamplingUncertaintyThreads,
0 );
}

/**
* Sets the feature batch size.
* @param systemBuilder the system settings builder to update
Expand Down
Loading