Forum Discussion

Semirxbih's avatar
Semirxbih
Contributor
2 years ago

Drop Database using JScript in TestComplete w/ MongoDB?

So I am working on a script in TestComplete where I have a scenario where I need to drop a database on MongoDB, but I am having a lot of trouble and wanted to get others opinion.

 

So let's say that I have the following "dropDatabase" function using JScript:

function dropDatabase(hostName, port, userName, password, databaseName) {
var conn = null;
var cmd = null;

try {
conn = ADO.CreateADOConnection();
var connectionString = "Provider=SQLOLEDB.1;Data Source=" + hostName + "," + port + ";User ID=" + userName + ";Password=" + password;
conn.Open(connectionString);
cmd = ADO.CreateCommand();
cmd.CommandText = "DROP DATABASE " + databaseName;
cmd.Execute();
Log.Message("Database dropped successfully.");
} catch (e) {
Log.Error("An error occurred while dropping the database: " + e.message);
} finally {
if (cmd != null) {
cmd = null;
}
if (conn != null) {
conn.Close();
conn = null;
}
}
}

I then have a "Then" Cucumber step definition as the following:

Then("I drop the database if \"dbEmpty\" is false", function() {
if (globalDbEmptyValue === undefined) {
Log.Error("The value of \"dbEmpty\" was not fetched.");
} else if (globalDbEmptyValue === false) {
Log.Message("The value of \"dbEmpty\" is false.");
dropDatabase("test.com", "12345", "mongo", "mongo", "Example")
} else {
Log.Message("The database was not dropped because \"dbEmpty\" is not false.");
}
});

But anytime I run the scenario, it crashes due to a "Login window" popping up for credentials, which is giving me indications that it's having a hard time creating a proper MongoDB connection.

 

All I need to do it to be able to make a proper connection so that I can drop the "Example" database.

 

Could someone give me some pointers? When I use the credentials from the example "dropDatabase" function, I am able to create a successful MongoDB connection using MongoDBCompass.

 

Thanks all!

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm not familiar with MongoDB at all, but can you use the following code and fill in the connection string and command text (without passing parameters), ensure you are using the correct ODBC driver.

    // Create database connection object
    var conn = ADO.CreateConnection();
    conn.ConnectionString = "";  // Fill-in
    conn.Open();
    
    // Create command object
    var cmd = ADO.CreateCommand();
    cmd.ActiveConnection = conn;
    cmd.CommandType = adCmdText;
    
    // Execute commands
    cmd.CommandText = ""; // Fill-in
    cmd.Execute();
    
    conn.Close();

     First verify that you can query a table, then try to drop the database.

    • Semirxbih's avatar
      Semirxbih
      Contributor

      Thanks for the response, I attempted all types of connection strings and it's not possible, I just keep getting the "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 8:45:53 Normal 0.96" error.

    • Semirxbih's avatar
      Semirxbih
      Contributor

      Almost seems like it's not possible, since I couldn't find much information online, I asked ChatGPT with a response "As it stands, it's unlikely that you'll be able to drop a MongoDB database directly from a TestComplete script using ADO and JScript.".

      I've looked around and "mongodb://" connection string was not supported for ADOConnection.

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

         I asked ChatGPT [...]

        It lies pretty often...

         

        https://community.smartbear.com/t5/TestComplete-Questions/Can-anyone-help-me-how-to-access-Mongo-database/td-p/86442

        and

        https://community.smartbear.com/t5/TestComplete-Questions/How-to-connect-the-mongo-DB-with-testComplete-smartbear/td-p/192040

        might help.

         

        In a nutshell:

        -- ADO does not access data itself. ADO is a higher level interface that uses either native ADO driver or ODBC data source name (DSN) connection to access data;

        -- It seem that native ADO driver for MongoDB does not exist. ODBC access is your only option if the above is correct;

        -- In order to use ODBC, you need to: a) (find and) install MongoDB ODBC driver of some bitness (32- or 64-bit); b) create ODBC DSN connection using odbcad32 tool, tool must be started from either <windows>\system32\ (for 64-bit) or <windows>\SysWOW64\ folder (for 32-bit) depending on the bitness of installed MongoDB ODBC driver; c) set up ODBC DSN connection so that it can access data;

        -- Craft ADO connection string using ODBC provider and the name of ODBC DSN like it is described in the referenced threads;

        -- Use created ADO connection string to create ADO connection from your test code. Important requirement is that you must use TestComplete of the same bitness as installed MongoDB ODBC driver.