-
Notifications
You must be signed in to change notification settings - Fork 13
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
FILTER-12: Producing failing tests #20
Draft
Ruhanga
wants to merge
2
commits into
openmrs:master
Choose a base branch
from
Ruhanga:FILTER-12-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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
204 changes: 204 additions & 0 deletions
204
api/src/test/java/org/openmrs/module/datafilter/impl/BaseFilterMySQLBackedTest.java
This file contains 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,204 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.datafilter.impl; | ||
|
||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.dbunit.DatabaseUnitException; | ||
import org.dbunit.DatabaseUnitRuntimeException; | ||
import org.dbunit.database.DatabaseConfig; | ||
import org.dbunit.database.DatabaseConnection; | ||
import org.dbunit.database.IDatabaseConnection; | ||
import org.dbunit.dataset.IDataSet; | ||
import org.dbunit.ext.mysql.MySqlDataTypeFactory; | ||
import org.dbunit.operation.DatabaseOperation; | ||
import org.hibernate.cfg.Environment; | ||
import org.hibernate.dialect.MySQL5Dialect; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.openmrs.GlobalProperty; | ||
import org.openmrs.api.AdministrationService; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.api.context.UsernamePasswordCredentials; | ||
import org.openmrs.liquibase.ChangeLogVersionFinder; | ||
import org.openmrs.liquibase.ChangeSetExecutorCallback; | ||
import org.openmrs.module.datafilter.TestConstants; | ||
import org.openmrs.test.BaseModuleContextSensitiveTest; | ||
import org.openmrs.test.TestUtil; | ||
import org.openmrs.util.DatabaseUpdater; | ||
import org.openmrs.util.PrivilegeConstants; | ||
import org.testcontainers.containers.MySQLContainer; | ||
import liquibase.changelog.ChangeSet; | ||
|
||
public abstract class BaseFilterMySQLBackedTest extends BaseModuleContextSensitiveTest { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(BaseFilterMySQLBackedTest.class); | ||
|
||
private static MySQLContainer<?> mysqlContainer = new MySQLContainer("mysql:5.7.31"); | ||
|
||
private static String databaseUrl = "jdbc:mysql://localhost:DATABASE_PORT/openmrs?autoReconnect=true&sessionVariables=default_storage_engine%3DInnoDB&useUnicode=true&characterEncoding=UTF-8"; | ||
|
||
private static String databaseUsername = "user"; | ||
|
||
private static String databaseUserPasswword = "12345678"; | ||
|
||
private static String databaseDialect = MySQL5Dialect.class.getName(); | ||
|
||
private static String databaseDriver = "com.mysql.jdbc.Driver"; | ||
|
||
@BeforeClass | ||
public static void setupMySqlDb() throws IOException { | ||
|
||
mysqlContainer.withDatabaseName("openmrs"); | ||
mysqlContainer.withUsername(databaseUsername); | ||
mysqlContainer.withPassword(databaseUserPasswword); | ||
mysqlContainer.start(); | ||
System.setProperty("useInMemoryDatabase", "false"); | ||
} | ||
|
||
@Override | ||
public void baseSetupWithStandardDataAndAuthentication() throws SQLException { | ||
// Open a session if needed | ||
if (!Context.isSessionOpen()) { | ||
Context.openSession(); | ||
} | ||
|
||
ChangeLogVersionFinder changeLogVersionFinder = new ChangeLogVersionFinder(); | ||
|
||
String liquibaseSchemaFileName = changeLogVersionFinder.getLatestSchemaSnapshotFilename().get(); | ||
// String liquibaseCoreDataFileName = changeLogVersionFinder.getLatestCoreDataSnapshotFilename().get(); | ||
|
||
try { | ||
DatabaseUpdater.executeChangelog(liquibaseSchemaFileName, | ||
new PrintingChangeSetExecutorCallback("OpenMRS schema file")); | ||
String datafilterLiquibase = getClass().getClassLoader().getResource("liquibase.xml").toURI().getPath(); | ||
DatabaseUpdater.executeChangelog("liquibase.xml", | ||
new PrintingChangeSetExecutorCallback("Datafilter schema file")); | ||
} | ||
catch (Exception ex) { | ||
throw new RuntimeException("Could not update database.", ex); | ||
} | ||
executeDataSet(INITIAL_XML_DATASET_PACKAGE_PATH); | ||
|
||
//Commit so that it is not rolled back after a test. | ||
getConnection().commit(); | ||
|
||
authenticate(); | ||
Context.clearSession(); | ||
} | ||
|
||
@Override | ||
public Properties getRuntimeProperties() { | ||
if (runtimeProperties == null) { | ||
runtimeProperties = TestUtil.getRuntimeProperties(getWebappName()); | ||
} | ||
runtimeProperties.setProperty(Environment.URL, | ||
databaseUrl.replaceAll("DATABASE_PORT", String.valueOf(mysqlContainer.getMappedPort(3306)))); | ||
|
||
runtimeProperties.setProperty(Environment.USER, "user"); | ||
runtimeProperties.setProperty(Environment.PASS, "12345678"); | ||
runtimeProperties.setProperty("connection.username", "user"); | ||
runtimeProperties.setProperty("connection.password", "12345678"); | ||
runtimeProperties.setProperty("connection.url", | ||
databaseUrl.replaceAll("DATABASE_PORT", String.valueOf(mysqlContainer.getMappedPort(3306)))); | ||
runtimeProperties.setProperty(Environment.HBM2DDL_AUTO, "none"); | ||
runtimeProperties.setProperty(Environment.DIALECT, databaseDialect); | ||
runtimeProperties.setProperty(Environment.DRIVER, databaseDialect); | ||
|
||
return runtimeProperties; | ||
} | ||
|
||
@Override | ||
public void executeDataSet(String dataset) { | ||
try { | ||
turnOffDBConstraints(getConnection()); | ||
super.executeDataSet(dataset); | ||
turnOnDBConstraints(getConnection()); | ||
} | ||
catch (SQLException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public IDatabaseConnection setupDatabaseConnection(Connection connection) throws DatabaseUnitException { | ||
IDatabaseConnection dbUnitConn = new DatabaseConnection(connection); | ||
|
||
DatabaseConfig config = dbUnitConn.getConfig(); | ||
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory()); | ||
|
||
return dbUnitConn; | ||
} | ||
|
||
@Before | ||
public void beforeTestMethod() throws SQLException { | ||
executeDataSet(TestConstants.MODULE_TEST_DATASET_XML); | ||
} | ||
|
||
protected void reloginAs(String username, String password) { | ||
Context.logout(); | ||
Context.authenticate(new UsernamePasswordCredentials(username, password)); | ||
} | ||
|
||
@Override | ||
public void updateSearchIndex() { | ||
//Disable the interceptor so we can update the search index | ||
String originalValue = null; | ||
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); | ||
AdministrationService as = Context.getAdministrationService(); | ||
GlobalProperty gp = as.getGlobalPropertyObject(ImplConstants.GP_RUN_IN_STRICT_MODE); | ||
if (gp == null) { | ||
gp = new GlobalProperty(ImplConstants.GP_RUN_IN_STRICT_MODE); | ||
} else { | ||
originalValue = gp.getPropertyValue(); | ||
} | ||
|
||
gp.setPropertyValue("false"); | ||
as.saveGlobalProperty(gp); | ||
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); | ||
Context.flushSession(); | ||
|
||
try { | ||
super.updateSearchIndex(); | ||
} | ||
finally { | ||
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); | ||
gp.setPropertyValue(originalValue == null ? "" : originalValue); | ||
as.saveGlobalProperty(gp); | ||
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES); | ||
Context.flushSession(); | ||
} | ||
} | ||
|
||
private static class PrintingChangeSetExecutorCallback implements ChangeSetExecutorCallback { | ||
|
||
private int i = 1; | ||
|
||
private String message; | ||
|
||
public PrintingChangeSetExecutorCallback(String message) { | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* @see ChangeSetExecutorCallback#executing(liquibase.changelog.ChangeSet, int) | ||
*/ | ||
@Override | ||
public void executing(ChangeSet changeSet, int numChangeSetsToRun) { | ||
log.info(message + " (" + i++ + "/" + numChangeSetsToRun + "): Author: " + changeSet.getAuthor() + " Comments: " | ||
+ changeSet.getComments() + " Description: " + changeSet.getDescription()); | ||
} | ||
} | ||
|
||
} |
This file contains 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 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 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 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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
This Source Code Form is subject to the terms of the Mozilla Public License, | ||
v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
|
||
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
graphic logo is a trademark of OpenMRS Inc. | ||
|
||
--> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | ||
|
||
<!-- | ||
From applicationContext-service.xml in openmrs-api | ||
Needed to override and add hibernate mappings to the classpath since omod is not packaged yet | ||
--> | ||
<bean id="sessionFactory" class="org.openmrs.api.db.hibernate.HibernateSessionFactoryBean"> | ||
<property name="configLocations"> | ||
<list> | ||
<value>classpath:hibernate.cfg.xml</value> | ||
</list> | ||
</property> | ||
<property name="mappingJarLocations"> | ||
<ref bean="mappingJarResources"/> | ||
</property> | ||
<!-- default properties must be set in the hibernate.default.properties --> | ||
<property name="packagesToScan"> | ||
<list> | ||
<value>org.openmrs</value> | ||
</list> | ||
</property> | ||
</bean> | ||
|
||
</beans> |
This file contains 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
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.
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.
To keep this simple, I think conversion of the tests to use testcontainers should be done separately as part of another ticket, just run H2 in mysql mode for now
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.
Hi @wluyima, I've tried the option you are talking about and the error never occurs. It's as if the option
MODE=MYSQL
is never considered altogether.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.
How are you doing it? Do you mind sharing the code snippet?
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.
I always make tests fail on H2 that pass on MySQL by setting the mode and it is why I ended up adding this line and this, if you comment them out and set mode to mysql, some tests will fail and the same should happen for you, unless you are missing something.
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.
@wluyima, I'm doing the above to reproduce the exact runtime error. Otherwise a different non-specific error occurs. See snippet in draft here.
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.
And which is the other error you get in tests?
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.
I shared the other one above.
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.
Please share full stacktrace, that is just a portion
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.
Runtime/test failure with actual MySQL env
Error with H2 in MODE=MYSQL
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.
Thanks! The stacktraces might not look 100% identical but if you closely analyze both of them, they are reporting the same issue i.e. the
NOT IN
clause having empty braces which is the real issue, H2 seems not to have an issue with it but MySQL does, so using MODE=MySQL should be sufficient to reproduce the bug in tests.Feel free to create a separate ticket to update the module to use testcontainers