tl;dr How can I test the viability of an update
or insert
using the simple-salesforce python package?
I am writing a test script for a simple-salesforce based python class I have created. The class involves sf.bulk.Object_Name__c.insert(data)
and sf.bulk.Object_Name__c.update(data)
code, but in the test script I would like to confirm that the insert/update would insert successfully or not, without actually committing the new record or change.
The only item I saw that suggested this might be possible was a page in the Simple-Salesforce documentation noting the existence of the simple_salesforce.tests.test_util
module. Unfortunately, I was not able figure out how to access this sub-module, or subsequently try out the test_util
class, and the document was quite sparse on instructions on how to do so.
I don't believe this is possible. The simple-salesforce package is, essentially, syntactic sugar for building code that will hook into Salesforce's native Rest APIs.
The subpackage simple_salesforce.tests is not a hook into the Salesforce Test functionality, but rather the test code for simple_salesforce itself.
The type of testing that you would like to complete is limited to Salesforce's native Database.Test class. These are built in Apex and run within Salesforce itself. There isn't a good way to complete the test you would like to do.
You could write test classes to ensure that the requests that your code generates are properly formatted, but testing that it could actually generate records within Salesforce would require simple-salesforce to be aware of all of your SFDC Organizations metadata, validations, permissions, triggers, processes, etc. This would require simple-salesforce to all but completely replicate salesforce - making it much less simple.
My recommendation is to test your code in a sandbox environment and ensure it works there before moving directly to production.
Alternatively, (and this is purely hypothetical and pretty hacky) you could attempt to build your own hooks into the Salesforce APIs to utilize the Database.setSavePoint() and Database.rollBack() functions. You would set a save point, initialize your bulk job, wait for the bulk job to finish, complete your tests to confirm things were committed, and then rollback to the save point.