Forum Discussion

endorium79's avatar
endorium79
New Contributor
2 months ago
Solved

DB connection fails when run in a test

I have the below function. When I run the function on its own, it passes everytime. When I run this function in a scripted test, it fails everytime with the error 'Cannot open database "BDS.SynergyPlusWfm.Working". I change nothing at all. Why would it pass run on its own but fail when run from a scripted test? Ive ruled out environment issues as it passes fine when run on its own. Its driving me mad so any help is appreciated. Test complete is on windows server 2022. The SQL instance in in a podman container on a windows server 2025 box.

.

 

function verifyDBTable(sqlQuery, csvFilePath, noResults) {
  try {
    logFolderCreation("Verify DB Table");

    // Build connection string
    var connectionString =
      "Provider=MSOLEDBSQL;" +
      "Data Source=" + Project.Variables.SQLServer + ";" +
      "Initial Catalog=BDS.SynergyPlusWfm.Working;" +
      "User ID=" + Project.Variables.SQLUser + ";" +
      "Password=" + Project.Variables.SQLPassword + ";" +
      "Encrypt=true;" +
      "TrustServerCertificate=true;" +
      "Persist Security Info=True;";

    // File paths
    var actualFilePath = checkVariable("<_projectPath>\\Files\\TestData\\Actual\\" + csvFilePath);
    var expectedFilePath = checkVariable("<_projectPath>\\Files\\TestData\\Expected\\dataBase\\" + csvFilePath);

    // Reset output file
    if (aqFile.Exists(actualFilePath)) {
      aqFile.Delete(actualFilePath);
      aqFile["Create"](actualFilePath)
    }
    aqFile["Create"](actualFilePath)
    var file = aqFile.OpenTextFile(actualFilePath, aqFile.faWrite, aqFile.ctUTF8);

    // Open DB connection
    var dbConn = ADO.CreateADOConnection();
    dbConn.ConnectionString = connectionString;
    dbConn.LoginPrompt = false;
    dbConn.Open();

    if (!dbConn.Connected) {
      Log.Error("Database connection failed.");
      return;
    }

    Log.Message("Connected to database successfully.");

    // Execute SQL and fetch results
    var recSet = dbConn.Execute_(sqlQuery);;

    if (recSet.EOF) {
      if (noResults != null) {
        Log.Checkpoint("No results were returned as expected.");
      } else {
        Log.Error("Query returned no results.");
      }
      recSet.Close();
      dbConn.Close();
      file.Close();
      return;
    }

    // Write header
    var header = [];
    for (var i = 0; i < recSet.Fields.Count; i++) {
      header.push(recSet.Fields.Item(i).Name);
    }
    file.WriteLine(header.join(","));

    // Write each row
    while (!recSet.EOF) {
      var row = [];
      for (var j = 0; j < recSet.Fields.Count; j++) {
        var field = recSet.Fields.Item(j);
        var val = "";

        try {
          var fieldType = field.Type;
          if (field.Value === null || field.Value === undefined) {
            val = "";
          } else if (fieldType === 135 || fieldType === 7 || fieldType === 146) {
            val = aqConvert.DateTimeToFormatStr(field.Value, "%Y-%m-%d %H:%M:%S");
          } else {
            val = String(field.Value);
          }

          // Escape for CSV format
          val = val.replace(/"/g, '""');
          if (val.indexOf(",") > -1 || val.indexOf('"') > -1) {
            val = '"' + val + '"';
          }
        } catch (ex) {
          val = "<IGNORE>";
        }

        row.push(val);
      }

      file.WriteLine(row.join(","));
      recSet.MoveNext();
    }

    // Clean up
    file.Close();
    recSet.Close();
    dbConn.Close();

    compareCSVFiles(actualFilePath, expectedFilePath);
  } catch (e) {
    Log.Error("Error exporting DB results to CSV: " + e.message);
  }
}

  • I solved it in the end. It was the WS2 IP. It wasn't resolving properly

3 Replies

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    How are you running the function on it's own?

    How are you running the function in a scripted test?

    On what line is the error Cannot open database "BDS.SynergyPlusWfm.Working" occurring, on dbConn.Open()?

    How many times is this function called when running in your scripted tests?

    What language are you using JavaScript, C# Script?

    I'm assuming Podman is similar to Docker - what version of SQL are you using?

     

  • scot1967's avatar
    scot1967
    Icon for Champion Level 2 rankChampion Level 2


    Good morning from Missouri,  🤠  LOL

    Could you verify/send a copy of the connectionString value as well?  I am curious about the project variables when run in each circumstance.    (Just **** out the user and password) but do verify they are correct.  😉 

     var connectionString =
          "Provider=MSOLEDBSQL;" +
          "Data Source=" + Project.Variables.SQLServer + ";" +
          "Initial Catalog=BDS.SynergyPlusWfm.Working;" +
          "User ID=" + Project.Variables.SQLUser + ";" +
          "Password=" + Project.Variables.SQLPassword + ";" +
          "Encrypt=true;" +
          "TrustServerCertificate=true;" +
          "Persist Security Info=True;";

    ... If you find my posts helpful drop me a like! 👍 Be sure to mark or post the solution to help others out and/or to credit the one who helped you. 😎

  • endorium79's avatar
    endorium79
    New Contributor

    I solved it in the end. It was the WS2 IP. It wasn't resolving properly