Created
September 2, 2012 23:42
-
-
Save sbob-sfdc/3605669 to your computer and use it in GitHub Desktop.
Workshop 201, Tutorial 6, Step 1, Apex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class TestDeleteRestrictInvoice { | |
// Invoice generator, with or without a Line Item | |
static Invoice__c createNewInvoice(Boolean withLineItem) { | |
// Create test Invoice and insert it into the database | |
Invoice__c invoice = new Invoice__c(); | |
insert invoice; | |
// Create test Line Item and insert it into the database, if withLineItem == true | |
if (withLineItem) { | |
Line_Item__c item = new Line_Item__c( | |
name = '1', | |
Quantity__c = 1, | |
Invoice__c = invoice.Id | |
); | |
insert item; | |
} | |
return invoice; | |
} | |
// Single row Invoice with no Line Items => delete | |
static testMethod void verifyInvoiceNoLineItemsDelete(){ | |
// Create test Invoice and insert it | |
Invoice__c invoice = createNewInvoice(false); | |
// Delete the Invoice, capture the result | |
Database.DeleteResult result = Database.delete(invoice, false); | |
// Assert success, because target Invoice doesn't have Line Items | |
System.assert(result.isSuccess()); | |
} | |
// Single row Invoice with Line Items => delete restrict | |
static testMethod void verifyInvoiceLineItemsRestrict(){ | |
// Create test Invoice and Line Item and insert them | |
Invoice__c invoice = createNewInvoice(true); | |
// Delete the Invoice, capture the result | |
Database.DeleteResult result = Database.delete(invoice, false); | |
// Assert failure-not success, because target Invoice has tracks | |
System.assert(!result.isSuccess()); | |
} | |
// Bulk delete of Invoice, one without Line Items, another with | |
static testMethod void verifyBulkInvoiceDeleteRestrict(){ | |
// Create two test Invoices, one with and without a Line Item | |
Invoice__c[] invoices = new List<Invoice__c>(); | |
invoices.add(createNewInvoice(false)); | |
invoices.add(createNewInvoice(true)); | |
// Delete the Invoices, opt_allOrNone = false, capture the results. | |
Database.DeleteResult[] results = Database.delete(invoices, false); | |
// Assert success for first Invoice | |
System.assert(results[0].isSuccess()); | |
// Assert not success for second Invoice | |
System.assert(!results[1].isSuccess()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came to this page; while referring Force.com Workbook, page numbers 124-125. The above code is incomplete and missing steps. I have successfully tried the code below. ( For some reason the formatting below from " @istest to insert merchandise; " is getting messed up. While in the writing/ editing mode at gist.github.com; the formatting shows correct; but after posted it is automatically removed. Please adjust the spacing)
@istest
private class TestDeleteRestrictInvoice {
// Invoice generator, with or without a Line Item3
static Invoice3__c createNewInvoice3(Boolean withLineItem) {
// Create test Merchandise3
Merchandise3__c merchandise = new Merchandise3__c(
Name = 'Test Laptop',
Quantity__c = 1000,
Price__c = 500
);
insert merchandise;
}