Search

Friday, 5 June 2015

Intro to Unit Test Data Creation Framework continued…….





In my last blog I laid out the codebase required for the framework.

Now you have created the structure of the framework, lets run some examples.
But before we do I'd like to point out 1 thing, in the framework you have various options depending how you personally feel you would like to structure the framework. You can make the TestDataComplexData class extend the TestDataBulkData class instead of the TestDataInsertData and you may want to rename the TestDataComplexData class to something like TestDataCreation class and so it is more generic. These are just options for you to play around with.


To perform no DML and just return a new Contact using the standard json string you can add a returnContact function in the TestDataReturnData class:


The new function will look like:

    public Contact returnContact(KeyValue[] kVals){
            
        return (Contact) (super.returnAnyObject(new TestDataFramework_JsonLibrary.Standard().M.get('CONTACT'), kVals, Contact.class)[0]);
    }



An example of using this new function:


TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.returnContact(null);



To simply create a new Contact using the standard json string:


TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.insertContact(null, null);



To add some values into certain fields:


KeyValue[] kvs = new KeyValue[]{};
//this will overwrite the Email field with a new email
kvs.add(new KeyValue('Email', 'myemail@yahoo.com', 'String'));

TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.insertContact(null, kvs);



To simply create a new Account and Contact using the standard json string and to add a value into a field:

Map<System.Type, List<KeyValue>> keyMap = new Map<System.Type, List<KeyValue>>();
KeyValue[] kvs = new KeyValue[]{};
//this will overwrite the Email field with a new email
kvs.add(new KeyValue('Email', 'myemail@yahoo.com', 'String'));

kMaps.put(Contact.class, kvs);

TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.insertContactAndAccount(kMaps);



To create 1 Account and 5 Contacts linked using the standard json string#


KeyValue[] kvsA = new KeyValue[]{};
KeyValue[] kvsC = new KeyValue[]{};

Map<System.Type, KeyValueBulk> keyMap = new Map<System.Type, KeyValueBulk>();
keyMap.put(Account.class, new KeyValueBulk(1, kvsA));
keyMap.put(Contact.class, new KeyValueBulk(5, kvsC));

TestDataBulkData dataCl = new TestDataBulkData ();
dataCl.insertAccountAndContacts(keyMap);



If your framework is setup so that TestDataComplexData class extends the TestDataBulkData class instead just change the code above to


KeyValue[] kvsA = new KeyValue[]{};
KeyValue[] kvsC = new KeyValue[]{};

Map<System.Type, KeyValueBulk> keyMap = new Map<System.Type, KeyValueBulk>();
keyMap.put(Account.class, new KeyValueBulk(1, kvsA));
keyMap.put(Contact.class, new KeyValueBulk(5, kvsC));

TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.insertAccountAndContacts(keyMap);



If you now want to bypass the triggers to speed up processing time because you want to upload a lot of records to be used in your testmethod:


KeyValue[] kvsA = new KeyValue[]{};
KeyValue[] kvsC = new KeyValue[]{};

Map<System.Type, KeyValueBulk> keyMap = new Map<System.Type, KeyValueBulk>();
keyMap.put(Account.class, new KeyValueBulk(1, kvsA));
keyMap.put(Contact.class, new KeyValueBulk(5, kvsC));

TriggerController.rapidProcessing = new Map<System.Type, Boolean>{ Account.class => true, Contact.class => true};

//now rapidProcessing has been turned on for both objects the code in the triggers will be bypassed. You will need to build into your triggers the Trigger Control Framework which will be the next thing I will cover in my next post:

TestDataComplexData dataCl = new TestDataComplexData ();
dataCl.insertAccountAndContacts(keyMap);



If you want to insert an Account using the standard json string and then update that Account:


TestDataUpdateData dataCl = new TestDataUpdateData();
KeyValue[] kvsUpdate = new KeyValue[]{};
//this will overwrite the BillingPostalCode field with a new post code
kvsUpdate.add(new KeyValue('BillingPostalCode', 'EC1 2CV', 'String'));

//first argument is null because this overrides the fields for the insert part
dataCl.updateAccount(null, kvsUpdate);


If you want to insert an Account using the standard json string and then update that Account:


TestDataUpdateData dataCl = new TestDataUpdateData();

KeyValue[] kvsInsert = new KeyValue[]{};
//this will overwrite the BillingPostalCode field with a new post code
kvsInsert.add(new KeyValue('BillingPostalCode', 'SE1 2SG', 'String'));

KeyValue[] kvsUpdate = new KeyValue[]{};
//this will overwrite the BillingPostalCode field with a new post code
kvsUpdate.add(new KeyValue('BillingPostalCode', 'EC1 2CV', 'String'));

dataCl.updateAccount(kvsInsert, kvsUpdate);



If you want to create a new insert function and add this to the framework, here are the steps you will need to follow:


  1. Create a new constant in the Constant class
  2. Add a new json string to the libraryMap variable in the json library class
  3. Create a new insert function in the TestDataInsertData class similar to the insertContact() function just replacing with the new Sobject type
  4. You can now use this function in the other classes TestDataComplexData, TestDataBulkData and TestDataUpdateData to build more complex data structures if you require



No comments:

Post a Comment

Note: only a member of this blog may post a comment.