Skip to content

Instantly share code, notes, and snippets.

@dcarroll
Created March 17, 2014 22:24
Show Gist options
  • Save dcarroll/9609640 to your computer and use it in GitHub Desktop.
Save dcarroll/9609640 to your computer and use it in GitHub Desktop.
@isTest
private class TestInvoiceStatementDeletion {
static testmethod void TestDeleteInvoiceWithLineItem() {
// Create an invoice with a line item then try to delete it
Invoice__c inv = TestDataFactory.createOneInvoiceStatement(true);
Test.startTest();
Database.DeleteResult result = Database.delete(inv, false);
Test.stopTest();
// Verify invoice with a line item didn't get deleted.
System.assert(!result.isSuccess());
}
static testmethod void TestDeleteInvoiceWithoutLineItems() {
// Create an invoice without a line item and try to delete it
Invoice__c inv = TestDataFactory.createOneInvoiceStatement(false);
Test.startTest();
Database.DeleteResult result = Database.delete(inv, false);
Test.stopTest();
// Verify invoice without line items got deleted.
System.assert(result.isSuccess());
}
static testmethod void TestBulkDeleteInvoices() {
// Create two invoices, one with and one with out line items
// Then try to delete them both in a bulk operation, as might happen
// when a trigger fires.
List<Invoice__c> invList = new List<Invoice__c>();
invList.add(TestDataFactory.createOneInvoiceStatement(true));
invList.add(TestDataFactory.createOneInvoiceStatement(false));
Test.startTest();
Database.DeleteResult[] results = Database.delete(invList, false);
Test.stopTest();
// Verify the invoice with the line item didn't get deleted
System.assert(!results[0].isSuccess());
// Verity the invoice without line items did get deleted.
System.assert(results[1].isSuccess());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment