-
-
Save joshbirk/4332411 to your computer and use it in GitHub Desktop.
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
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; | |
} |
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
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; | |
} | |
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
//CreatedDate cannot be written, but it can be imported | |
List<Merchandise__c> merchandise = Test.loadData(Merchandise__.sObjectType, 'MerchData'); | |
update merchandise; | |
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 | |
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; | |
} | |
} |
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
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new MockWarehousePullData()); | |
// Call class that utilizes this endpoint | |
WarehouseDataPull.pullData(); |
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 | |
static public void testRequiredFields() { | |
//assertion: Merchandise requires Price and Quantity | |
} |
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
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); | |
} | |
} |
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 | |
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