Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

WIP: Code cleanup before next release #46

Merged
merged 10 commits into from
Jun 2, 2017
10 changes: 0 additions & 10 deletions src/classes/CollectionUtils.cls

This file was deleted.

5 changes: 0 additions & 5 deletions src/classes/CollectionUtils.cls-meta.xml

This file was deleted.

6 changes: 3 additions & 3 deletions src/classes/DML.cls
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public virtual class DML implements IDML {
}

public virtual SObject doUpsert(SObject record) {
//Salesforce will only allow upsert calls for SObjects if a declared-type list is passed in.
//This is fine for the bulk method, where we can assume the caller is passing in an explicit list, but for a single record,
//the only way to successfully perform the upsert is to dynamically spin up a list of the SObject's type
// Salesforce will only allow upsert calls for SObjects if a declared-type list is passed in.
// This is fine for the bulk method, where we can assume the caller is passing in an explicit list, but for a single record,
// the only way to successfully perform the upsert is to dynamically spin up a list of the SObject's type
String listType = 'List<' + record.getSObjectType() + '>';
List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
castRecords.add(record);
Expand Down
7 changes: 3 additions & 4 deletions src/classes/DMLMock.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
public virtual class DMLMock implements IDML {
// DML

public SObject doInsert(SObject record) { return this.doInsert(new List<SObject>{record})[0]; }
public List<SObject> doInsert(List<SObject> recordList) {
TestingUtils.generateIds(recordList);
Expand Down Expand Up @@ -32,11 +32,10 @@ public virtual class DMLMock implements IDML {

public void doDelete(SObject record) { this.doDelete(new List<SObject>{record}); }
public void doDelete(List<SObject> recordList) {
if(!CollectionUtils.IsNullOrEmpty(recordList)) {
TestingUtils.deletedRecords.addAll(recordList);
}
if(recordList != null) TestingUtils.deletedRecords.addAll(recordList);
}

public void doHardDelete(SObject record) { this.doHardDelete(new List<SObject>{record}); }
public void doHardDelete(List<SObject> recordList) { this.doDelete(recordList); }

}
6 changes: 6 additions & 0 deletions src/classes/DMLMock_Tests.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
@isTest
private class DMLMock_Tests {

static IDML dmlRepo = new DMLMock();
static Schema.Contact con = createContact();

Expand Down Expand Up @@ -80,4 +85,5 @@ private class DMLMock_Tests {

return con;
}

}
6 changes: 6 additions & 0 deletions src/classes/DML_Tests.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
@isTest
private class DML_Tests {

static IDML dml = new DML();
static Contact contact = createContact();

Expand Down Expand Up @@ -85,4 +90,5 @@ private class DML_Tests {

return contact;
}

}
1 change: 0 additions & 1 deletion src/classes/Exceptions.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*************************************************************************************************/
public without sharing class Exceptions {

public class DescribeException extends Exception {}
public class InvalidOperationException extends Exception {}
public class RecordTypeException extends Exception {}
public class SObjectTriggerHandlerException extends Exception {}
Expand Down
4 changes: 4 additions & 0 deletions src/classes/IQueryFilter.cls
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
public interface IQueryFilter {

String getValue();
Expand Down
14 changes: 7 additions & 7 deletions src/classes/QueryBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
private final Map<String, Schema.SObjectField> sobjectTypeFieldMap;

public QueryBuilder(Schema.SObjectType sobjectType, Schema.FieldSet fieldSet, List<Schema.SObjectField> sobjectFieldList) {
this.sobjectType = sobjectType;
this.sobjectFieldList = sobjectFieldList;
this.fieldSet = fieldSet;
this.sobjectType = sobjectType;
this.sobjectFieldList = sobjectFieldList;
this.fieldSet = fieldSet;
this.sobjectTypeFieldMap = this.sobjectType.getDescribe().fields.getMap();

this.forUpdate = false;
this.queryFields = new Set<String>();
this.orderByList = new List<String>();
this.sobjectTypeFieldMap = this.sobjectType.getDescribe().fields.getMap();
this.whereClauseList = new List<String>();
this.childRelationshipQueries = new List<String>();
this.whereClauseList = new List<String>();
this.orderByList = new List<String>();
this.forUpdate = false;

this.addCommonQueryFields();
this.addFieldSetMembers();
Expand Down
2 changes: 1 addition & 1 deletion src/classes/QueryDateLiteral_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@isTest
public class QueryDateLiteral_Tests {

private static integer n_number = 5;
private static Integer n_number = 5;

@isTest
static void it_should_return_yesterday_string() {
Expand Down
1 change: 0 additions & 1 deletion src/classes/SObjectFieldDescriber_Tests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/

@isTest
private class SObjectFieldDescriber_Tests {

Expand Down
30 changes: 6 additions & 24 deletions src/classes/SObjectRepositoryMocks.cls
Original file line number Diff line number Diff line change
Expand Up @@ -89,41 +89,23 @@ public class SObjectRepositoryMocks {
}

private void createMockObjects() {
//We would expect that for the Ids passed in, there will be a corresponding number of records returned of the exact same
//SObjectType as their Ids.
// We would expect that for the Ids passed in, there will be a corresponding number of records returned of the exact same
// SObjectType as their Ids.
this.records = new List<SObject>();
for(Id thing : this.recordIdList) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, thank you for cleaning that up

SObjectType objType = thing.getSObjectType();
for(Id recordId : this.recordIdList) {
SObjectType objType = recordId.getSObjectType();
SObject record = (SObject)Type.forName(String.valueOf(objType)).newInstance();
record.put('Id', thing);
record.put('Id', recordId);

this.records.add(record);
}
}

private List<SObject> returnListOfSObjects() {
if(CollectionUtils.isNullOrEmpty(this.records)) return new List<SObject>();
if(this.records == null) return new List<SObject>();

return this.records;
}

private SObjectType getSObjectType(Schema.SObjectField field) {
//There is supposed to be a built in Salesforce getDescribe() result for Schema.SObjectField, getReferenceTo()
//that should return a list of objects that field is associated with, but it has been broken for several years.
//This is a solution that was proposed on the Salesforce stack exchange and is the only work-around to a native lookup
//that I have been able to find.
Integer fieldHash = ((Object)field).hashCode();

// Build a map of hashcodes for each fieldDescribe taken from Schema Global Describe
Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
Map<Integer,Schema.SObjectType> fieldHashCodeToSObjectTypeMap = new Map<Integer,Schema.SObjectType>();
for (String sobjname: globalDescribe.keySet()) {
for (Schema.SObjectField sobjField : globalDescribe.get(sObjName).getDescribe().fields.getMap().values())
fieldHashCodeToSObjectTypeMap.put(((Object) sObjField).hashCode(), globalDescribe.get(sobjName));
}

return fieldHashCodeToSObjectTypeMap.get(fieldHash);
}
}

}
12 changes: 4 additions & 8 deletions src/classes/SObjectTriggerHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ public abstract class SObjectTriggerHandler extends NebulaCore implements ISObje

private static Map<Integer, Set<TriggerContext>> hashCodesForProcessedRecords = new Map<Integer, Set<TriggerContext>>();

@testVisible
private enum TriggerContext {
@testVisible private enum TriggerContext {
BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,
AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, AFTER_UNDELETE
}
@testVisible
private TriggerContext context; // The current context of the trigger
@testVisible private TriggerContext context; // The current context of the trigger

private Integer hashCode; // The hash code for the current records
private Boolean isTestMode;
private Boolean isTriggerExecuting; // Checks if the code was called by a trigger

@testVisible
private List<SObject> recordList, oldRecordList;
@testVisible
private Map<Id, SObject> recordMap, oldRecordMap;
@testVisible private List<SObject> recordList, oldRecordList;
@testVisible private Map<Id, SObject> recordMap, oldRecordMap;

public SObjectTriggerHandler() {
this(false);
Expand Down
7 changes: 7 additions & 0 deletions src/classes/SObjectTriggerHandler_Tests.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
@isTest
private class SObjectTriggerHandler_Tests {

private class LeadTriggerHandlerTest extends SObjectTriggerHandler {
public LeadTriggerHandlerTest() {
super(true);
Expand All @@ -19,6 +24,7 @@ private class SObjectTriggerHandler_Tests {
}

static List<Lead> leadList = new List<Lead>();

static void setupData() {
for(Integer i = 0; i < 5; i++) {
Lead lead = new Lead(
Expand Down Expand Up @@ -92,4 +98,5 @@ private class SObjectTriggerHandler_Tests {

// Test.stopTest();
// }

}
4 changes: 4 additions & 0 deletions src/classes/SObjectTypeDescriber.cls
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
public without sharing class SObjectTypeDescriber {

public Schema.SObjectType SObjectType {get; private set;}
Expand Down
10 changes: 4 additions & 6 deletions src/classes/SObjectTypeDescriber_Tests.cls
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
@isTest
private class SObjectTypeDescriber_Tests {

Expand Down Expand Up @@ -42,10 +46,8 @@ private class SObjectTypeDescriber_Tests {
Schema.SObjectType expectedSObjectType = Schema.Lead.SObjectType;

Test.startTest();

SObjectTypeDescriber sobjectTypeDescriber = new SObjectTypeDescriber(expectedSObjectType);
System.assertEquals(expectedSObjectType, sobjectTypeDescriber.SObjectType);

Test.stopTest();
}

Expand All @@ -54,9 +56,7 @@ private class SObjectTypeDescriber_Tests {
SObjectTypeDescriber sobjectTypeDescriber = new SObjectTypeDescriber(Schema.Lead.SObjectType);

Test.startTest();

System.assertEquals(true, sobjectTypeDescriber.validateSObjectFieldExists(Schema.Lead.Id));

Test.stopTest();
}

Expand All @@ -65,9 +65,7 @@ private class SObjectTypeDescriber_Tests {
SObjectTypeDescriber sobjectTypeDescriber = new SObjectTypeDescriber(Schema.Lead.SObjectType);

Test.startTest();

System.assertEquals(false, sobjectTypeDescriber.validateSObjectFieldExists(Schema.Task.Id));

Test.stopTest();
}

Expand Down
3 changes: 2 additions & 1 deletion src/classes/TestingUtils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestingUtils {
}

public static SObject setReadOnlyField(SObject sobj, SObjectField fieldName, String value) {
return setReadOnlyField(sobj,new Map<SObjectField,String>{fieldName => value});
return setReadOnlyField(sobj, new Map<SObjectField,String>{fieldName => value});
}

public static SObject setReadOnlyField(SObject sobj, Map<SObjectField,String> changesToFields) {
Expand All @@ -44,4 +44,5 @@ public class TestingUtils {

return (SObject)JSON.deserialize(serializedRecord, SObject.class);
}

}