Search in sources :

Example 1 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project ORCID-Source by ORCID.

the class DBUnitTest method cleanClientSourcedProfiles.

private static void cleanClientSourcedProfiles(IDatabaseConnection connection) throws AmbiguousTableNameException, DatabaseUnitException, SQLException {
    QueryDataSet dataSet = new QueryDataSet(connection);
    dataSet.addTable("profile", "SELECT p1.* FROM profile p1 LEFT JOIN client_details c ON c.group_orcid = p1.orcid LEFT JOIN profile p2 ON p1.source_id = p2.source_id WHERE p2.source_id IS NULL AND (c.client_details_id IS NULL OR p1.client_source_id IS NOT NULL)");
    dataSet.addTable("other_name");
    dataSet.addTable("record_name");
    dataSet.addTable("biography");
    dataSet.addTable("profile_keyword");
    dataSet.addTable("work");
    dataSet.addTable("profile_event");
    dataSet.addTable("researcher_url");
    dataSet.addTable("email");
    dataSet.addTable("email_event");
    dataSet.addTable("external_identifier");
    dataSet.addTable("org");
    dataSet.addTable("org_affiliation_relation");
    dataSet.addTable("peer_review_subject");
    dataSet.addTable("peer_review");
    dataSet.addTable("profile_funding");
    dataSet.addTable("funding_external_identifier");
    dataSet.addTable("webhook");
    dataSet.addTable("oauth2_token_detail");
    dataSet.addTable("notification");
    dataSet.addTable("notification_item");
    dataSet.addTable("given_permission_to");
    dataSet.addTable("subject");
    dataSet.addTable("shibboleth_account");
    dataSet.addTable("group_id_record");
    dataSet.addTable("address");
    dataSet.addTable("invalid_record_data_changes");
    DatabaseOperation.DELETE.execute(connection, dataSet);
    QueryDataSet theRest = new QueryDataSet(connection);
    theRest.addTable("profile", "SELECT * FROM profile WHERE source_id IS NOT NULL AND source_id != orcid ORDER BY orcid DESC");
    theRest.addTable("client_details");
    theRest.addTable("client_secret");
    theRest.addTable("custom_email");
    DatabaseOperation.DELETE.execute(connection, theRest);
}
Also used : QueryDataSet(org.dbunit.database.QueryDataSet)

Example 2 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project openmrs-core by openmrs.

the class CreateInitialDataSet method shouldCreateInitialTestDataSetXmlFile.

/**
 * This test creates an xml dbunit file from the current database connection information found
 * in the runtime properties. This method has to "skip over the base setup" because it tries to
 * do things (like initialize the database) that shouldn't be done to a standard mysql database.
 *
 * @throws Exception
 */
@Test
@SkipBaseSetup
public void shouldCreateInitialTestDataSetXmlFile() throws Exception {
    // over this whole "test"
    if (getLoadCount() != 1)
        return;
    // database connection for dbunit
    IDatabaseConnection connection = new DatabaseConnection(getConnection());
    // partial database export
    QueryDataSet initialDataSet = new QueryDataSet(connection);
    initialDataSet.addTable("concept", "SELECT * FROM concept");
    initialDataSet.addTable("concept_answer", "SELECT * FROM concept_answer");
    initialDataSet.addTable("concept_class", "SELECT * FROM concept_class");
    initialDataSet.addTable("concept_datatype", "SELECT * FROM concept_datatype");
    initialDataSet.addTable("concept_name", "SELECT * FROM concept_name");
    initialDataSet.addTable("concept_numeric", "SELECT * FROM concept_numeric");
    initialDataSet.addTable("concept_set", "SELECT * FROM concept_set");
    initialDataSet.addTable("concept_synonym", "SELECT * FROM concept_synonym");
    initialDataSet.addTable("drug", "SELECT * FROM drug");
    initialDataSet.addTable("drug_order", "SELECT * FROM drug_order");
    initialDataSet.addTable("encounter", "SELECT * FROM encounter");
    initialDataSet.addTable("encounter_type", "SELECT * FROM encounter_type");
    initialDataSet.addTable("location", "SELECT * FROM location");
    initialDataSet.addTable("obs", "SELECT * FROM obs");
    initialDataSet.addTable("order_type", "SELECT * FROM order_type");
    initialDataSet.addTable("orders", "SELECT * FROM orders");
    initialDataSet.addTable("patient", "SELECT * FROM patient");
    initialDataSet.addTable("patient_identifier", "SELECT * FROM patient_identifier");
    initialDataSet.addTable("patient_identifier_type", "SELECT * FROM patient_identifier_type");
    initialDataSet.addTable("patient_program", "SELECT * FROM patient_program");
    initialDataSet.addTable("patient_state", "SELECT * FROM patient_state");
    initialDataSet.addTable("person", "SELECT * FROM person");
    initialDataSet.addTable("person_address", "SELECT * FROM person_address");
    initialDataSet.addTable("person_attribute", "SELECT * FROM person_attribute");
    initialDataSet.addTable("person_attribute_type", "SELECT * FROM person_attribute_type");
    initialDataSet.addTable("person_name", "SELECT * FROM person_name");
    initialDataSet.addTable("privilege", "SELECT * FROM privilege");
    initialDataSet.addTable("program", "SELECT * FROM program");
    initialDataSet.addTable("program_workflow", "SELECT * FROM program_workflow");
    initialDataSet.addTable("program_workflow_state", "SELECT * FROM program_workflow_state");
    initialDataSet.addTable("relationship", "SELECT * FROM relationship");
    initialDataSet.addTable("relationship_type", "SELECT * FROM relationship_type");
    initialDataSet.addTable("role", "SELECT * FROM role");
    initialDataSet.addTable("role_privilege", "SELECT * FROM role_privilege");
    initialDataSet.addTable("role_role", "SELECT * FROM role_role");
    initialDataSet.addTable("user_role", "SELECT * FROM user_role");
    initialDataSet.addTable("users", "SELECT * FROM users");
    /*
		initialDataSet.addTable("field", "SELECT * FROM field");
		initialDataSet.addTable("field_answer", "SELECT * FROM field_answer");
		initialDataSet.addTable("field_type", "SELECT * FROM field_type");
		initialDataSet.addTable("form", "SELECT * FROM form");
		initialDataSet.addTable("form_field", "SELECT * FROM form_field");
		initialDataSet.addTable("hl7_source", "SELECT * FROM hl7_source");
		*/
    FlatXmlDataSet.write(initialDataSet, new FileOutputStream("test/api/org/openmrs/logic/include/LogicBasicTest.xml"));
// full database export
// IDataSet fullDataSet = connection.createDataSet();
// FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));
// dependent tables database export: export table X and all tables that
// have a PK which is a FK on X, in the right order for insertion
// String[] depTableNames = TablesDependencyHelper.getAllDependentTables(connection, "X");
// IDataSet depDataset = connection.createDataSet( depTableNames );
// FlatXmlDataSet.write(depDataSet, new FileOutputStream("dependents.xml"));
// TestUtil.printOutTableContents(getConnection(), "encounter_type", "encounter");
}
Also used : FileOutputStream(java.io.FileOutputStream) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Test(org.junit.Test)

Example 3 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project trainning by fernandotomasio.

the class DataExtractor method main.

public static void main(String[] args) throws ClassNotFoundException, SQLException, DataSetException, FileNotFoundException, IOException, DatabaseUnitException {
    // Class.forName("org.mysql.Driver");
    Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/trainning_development", "root", "roland");
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    QueryDataSet partialDataSet = new QueryDataSet(connection);
    // Mention all the tables here for which you want data to be extracted
    // take note of the order to prevent FK constraint violation when re-inserting
    partialDataSet.addTable("turmas");
    partialDataSet.addTable("cursos");
    // partialDataSet.addTable("user_role");
    // partialDataSet.addTable("project_assignment");
    // partialDataSet.addTable("timesheet_entry");
    // XML file into which data needs to be extracted
    FlatXmlDataSet.write(partialDataSet, new FileOutputStream("/home/fernandofot/fixture.xml"));
    System.out.println("Dataset written");
}
Also used : FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection)

Example 4 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project activityinfo by bedatadriven.

the class AdminTileRendererTest method main.

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/activityinfo", "root", "adminpwd");
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    QueryDataSet partialDataSet = new QueryDataSet(connection);
    // Mention all the tables here for which you want data to be extracted
    // take note of the order to prevent FK constraint violation when
    // re-inserting
    partialDataSet.addTable("country", "select * from country where iso2='JO'");
    partialDataSet.addTable("adminlevel", "select * from adminlevel where countryid=360");
    partialDataSet.addTable("adminentity", "select * from adminentity where adminlevelid=1360");
    // XML file into which data needs to be extracted
    FlatXmlDataSet.write(partialDataSet, new FileOutputStream("src/test/resources/dbunit/jordan.db.xml"));
    System.out.println("Dataset written");
}
Also used : FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection)

Example 5 with QueryDataSet

use of org.dbunit.database.QueryDataSet in project openmrs-module-pihcore by PIH.

the class CreateCoreMetadata method run.

@Test
public void run() throws Exception {
    // only run this test if it is being run alone and if an output directory has been specified
    if (getLoadCount() != 1 || ObjectUtil.isNull(getOutputDirectory()))
        return;
    // database connection for dbunit
    IDatabaseConnection connection = new DatabaseConnection(getConnection());
    // partial database export
    QueryDataSet initialDataSet = new QueryDataSet(connection);
    initialDataSet.addTable("users", "SELECT * FROM users");
    initialDataSet.addTable("person", "SELECT * FROM person p WHERE (select count(*) from users where person_id = p.person_id) > 0 OR (select count(*) from provider where person_id = p.person_id) > 0");
    initialDataSet.addTable("concept", "SELECT * FROM concept");
    initialDataSet.addTable("concept_answer", "SELECT * FROM concept_answer");
    initialDataSet.addTable("concept_class", "SELECT * FROM concept_class");
    initialDataSet.addTable("concept_datatype", "SELECT * FROM concept_datatype");
    initialDataSet.addTable("concept_map_type", "SELECT * FROM concept_map_type");
    initialDataSet.addTable("concept_name", "SELECT * FROM concept_name");
    initialDataSet.addTable("concept_numeric", "SELECT * FROM concept_numeric");
    initialDataSet.addTable("concept_reference_map", "SELECT * FROM concept_reference_map");
    initialDataSet.addTable("concept_reference_source", "SELECT * FROM concept_reference_source");
    initialDataSet.addTable("concept_reference_term", "SELECT * FROM concept_reference_term");
    initialDataSet.addTable("concept_reference_term_map", "SELECT * FROM concept_reference_term_map");
    initialDataSet.addTable("concept_set", "SELECT * FROM concept_set");
    initialDataSet.addTable("encounter_role", "SELECT * FROM encounter_role");
    initialDataSet.addTable("encounter_type", "SELECT * FROM encounter_type");
    initialDataSet.addTable("location", "SELECT * FROM location");
    initialDataSet.addTable("location_attribute", "SELECT * FROM location_attribute");
    initialDataSet.addTable("location_attribute_type", "SELECT * FROM location_attribute_type");
    initialDataSet.addTable("location_tag", "SELECT * FROM location_tag");
    initialDataSet.addTable("location_tag_map", "SELECT * FROM location_tag_map");
    initialDataSet.addTable("order_type", "SELECT * FROM order_type");
    initialDataSet.addTable("patient_identifier_type", "SELECT * FROM patient_identifier_type");
    initialDataSet.addTable("person_attribute_type", "SELECT * FROM person_attribute_type");
    initialDataSet.addTable("privilege", "SELECT * FROM privilege");
    initialDataSet.addTable("provider", "SELECT * FROM provider");
    initialDataSet.addTable("relationship_type", "SELECT * FROM relationship_type");
    initialDataSet.addTable("role", "SELECT * FROM role");
    initialDataSet.addTable("role_privilege", "SELECT * FROM role_privilege");
    initialDataSet.addTable("role_role", "SELECT * FROM role_role");
    initialDataSet.addTable("user_role", "SELECT * FROM user_role");
    initialDataSet.addTable("visit_type", "SELECT * FROM visit_type");
    initialDataSet.addTable("global_property", "SELECT * FROM global_property WHERE property like 'emr.%' or property like 'emrapi.%'");
    File outputFile = new File(getOutputDirectory(), "coreMetadata.xml");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FlatXmlDataSet.write(initialDataSet, baos);
    String contents = baos.toString("UTF-8");
    FileWriter writer = new FileWriter(outputFile);
    for (String line : contents.split(System.getProperty("line.separator"))) {
        if (line.contains("<concept ")) {
            line = line.replace("short_name=\"\" ", "");
            line = line.replace("description=\"\" ", "");
        }
        if (line.contains("system_id=\"admin\"")) {
            line = "  <users user_id=\"1\" person_id=\"1\" system_id=\"admin\" username=\"\" password=\"4a1750c8607d0fa237de36c6305715c223415189\" salt=\"c788c6ad82a157b712392ca695dfcf2eed193d7f\" secret_question=\"\" creator=\"1\" date_created=\"2005-01-01 00:00:00.0\" changed_by=\"1\" date_changed=\"2007-09-20 21:54:12.0\" retired=\"false\" retire_reason=\"\" uuid=\"1010d442-e134-11de-babe-001e378eb67e\"/>";
        }
        writer.write(line + System.getProperty("line.separator"));
    }
    writer.flush();
    writer.close();
}
Also used : FileWriter(java.io.FileWriter) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) QueryDataSet(org.dbunit.database.QueryDataSet) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Aggregations

QueryDataSet (org.dbunit.database.QueryDataSet)8 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)6 DatabaseConnection (org.dbunit.database.DatabaseConnection)5 FileOutputStream (java.io.FileOutputStream)4 Connection (java.sql.Connection)3 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 DatabaseConfig (org.dbunit.database.DatabaseConfig)1 HsqldbDataTypeFactory (org.dbunit.ext.hsqldb.HsqldbDataTypeFactory)1 MySqlConnection (org.dbunit.ext.mysql.MySqlConnection)1 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)1