Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Forked from anonymous/BasicUnitTest.cls
Last active December 9, 2015 21:39
Show Gist options
  • Save joshbirk/4332411 to your computer and use it in GitHub Desktop.
Save joshbirk/4332411 to your computer and use it in GitHub Desktop.
static public Boolean createBaselineData() {
Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500);
insert m;
Invoice__c i = new Invoice__c();
insert i;
Line_Item__c li = new Line_Item__c(Name='1',Quantity__c=10,Merchandise__c=m.Id,Invoice__c=i.Id);
insert li;
return true;
}
static public Boolean enterDataFromText(String dataString) {
List<String> data = dataString.split('\n');
List<Merchandise__c> merch= new List<Merchandise__c>();
for(String s : data) {
List<String> objectData = s.split(',');
Merchandise__c m = new Merchandise__c(Name=objectData[0].replaceAll('"',''),Quantity__c=Decimal.valueOf(objectData[1]),Price__c=Decimal.valueOf(objectData[2]));
merch.add(m);
}
try {
insert merch;
return true;
} catch(DMLException d) {
return false;
}
//CreatedDate cannot be written, but it can be imported
List<Merchandise__c> merchandise = Test.loadData(Merchandise__.sObjectType, 'MerchData');
update merchandise;
@isTest
global class MockWarehousePullData implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{ "Items": [ { "Name": "Super Laptop", "Price": 2999.99, "Quantity": 4 }, { "Name": "Super Duper Laptop", "Price": 3999.99, "Quantity": 4 } ] }');
res.setStatusCode(200);
return res;
}
}
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new MockWarehousePullData());
// Call class that utilizes this endpoint
WarehouseDataPull.pullData();
@isTest
static public void testRequiredFields() {
//assertion: Merchandise requires Price and Quantity
}
System.debug('REQUIRED FIELDS:');
//Detect all the required fields and debug out the result. This will help if a required field is added in the future.
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Merchandise__c.fields.getMap();
Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult dr = describeFields.get(field).getDescribe();
if(dr.isCreateable() && !dr.isNillable() && !dr.isDefaultedOnCreate()) {
System.debug('REQUIRED FIELD:' +field);
}
}
@isTest
static public void testRequiredFields() {
Merchandise__c m = new Merchandise__c(Name = 'Test');
//assertion: Merchandise requires Price and Quantity
try {
insert m;
} catch (DMLException e) {
System.assert(e.getMessage().length() > 0);
}
m.Quantity__c = 0;
m.Price__c = 9.99;
//assertion: Merchandise requires Price and Quantity
try {
insert m;
System.assert(String.valueOf(m.Id).length() > 0);
} catch (DMLException e) {
System.debug(e.getMessage());
}
System.debug('REQUIRED FIELDS:');
//Detect all the required fields and debug out the result. This will help if a required field is added in the future.
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Merchandise__c.fields.getMap();
Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult dr = describeFields.get(field).getDescribe();
if(dr.isCreateable() && !dr.isNillable() && !dr.isDefaultedOnCreate()) {
System.debug('REQUIRED FIELD:' +field);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment