Skip to content

Commit d90ffb9

Browse files
authored
Version 3.2.0 (#117)
* updating field case to match exisitng DML field * fixing issue with Utils initialization * making TestUtils global * refactoring to use new test utils methods * Soql cleanup
1 parent 6573444 commit d90ffb9

File tree

8 files changed

+76
-138
lines changed

8 files changed

+76
-138
lines changed

source/classes/DatabaseLayer.cls

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
*/
44
@SuppressWarnings('PMD.AvoidGlobalModifier')
55
global class DatabaseLayer {
6-
public static final DatabaseLayerUtils UTILS;
7-
private static final DatabaseLayer INSTANCE;
6+
private static final DatabaseLayer INSTANCE = new DatabaseLayer();
87

98
private Dml currentDml;
109
private SoqlProvider currentSoql;
1110

12-
static {
13-
INSTANCE = new DatabaseLayer();
14-
UTILS = new DatabaseLayerUtils(INSTANCE);
15-
}
16-
1711
private DatabaseLayer() {
1812
this.currentDml = new Dml(this);
1913
this.currentSoql = new SoqlProvider(this);
@@ -41,6 +35,19 @@ global class DatabaseLayer {
4135
}
4236
}
4337

38+
/**
39+
* @description Provides access to utility methods for database operations and testing.
40+
* @return Singleton instance of the DatabaseLayerUtils class
41+
*/
42+
@SuppressWarnings('PMD.PropertyNamingConventions')
43+
public static final DatabaseLayerUtils Utils {
44+
get {
45+
DatabaseLayer.Utils = DatabaseLayer.Utils ?? new DatabaseLayerUtils(INSTANCE);
46+
return DatabaseLayer.Utils;
47+
}
48+
private set;
49+
}
50+
4451
/**
4552
* @description Switches the DML provider to use mock implementation for testing.
4653
* @return The mock DML instance that was configured

source/classes/DatabaseLayerTestUtils.cls

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* @description Test utility class providing common functionality and spy implementations for framework testing.
33
*/
4+
@SuppressWarnings('PMD.AvoidGlobalModifier')
45
@IsTest
5-
public class DatabaseLayerTestUtils {
6+
global class DatabaseLayerTestUtils {
67
// Note: These properties do not use traditional naming conventions to support a 'namespace-like' API
78
@SuppressWarnings('PMD.FieldNamingConventions')
89
public static final PreAndPostProcessorSpy DmlPluginSpy = new PreAndPostProcessorSpy();
@@ -11,14 +12,37 @@ public class DatabaseLayerTestUtils {
1112

1213
/**
1314
* @description Creates a mock DatabaseLayerSetting__mdt for testing and injects it into the metadata selector.
15+
* @param setting The custom metadata record being injected.
1416
* @return The created test settings object
1517
*/
16-
public static DatabaseLayerSetting__mdt initSettings() {
17-
DatabaseLayerSetting__mdt setting = new DatabaseLayerSetting__mdt();
18+
global static DatabaseLayerSetting__mdt initSettings(DatabaseLayerSetting__mdt setting) {
1819
DatabaseLayer.Utils.MetadataSelector.settings = new List<DatabaseLayerSetting__mdt>{ setting };
20+
DatabaseLayer.Dml.initPlugins();
21+
DatabaseLayer.Soql.initPlugins();
1922
return setting;
2023
}
2124

25+
/**
26+
* @description Creates an empty DatabaseLayerSetting__mdt for testing and injects it into the metadata selector.
27+
* @return The created test settings object
28+
*/
29+
global static DatabaseLayerSetting__mdt initSettings() {
30+
return DatabaseLayerTestUtils.initSettings(new DatabaseLayerSetting__mdt());
31+
}
32+
33+
/**
34+
* @description Creates a DatabaseLayerSetting__mdt for testing with both DML and SOQL plugins configured to use the same class.
35+
* @param className The name of the class to use for both DML and SOQL pre/post processing
36+
* @return The created test settings object with plugin configuration
37+
*/
38+
global static DatabaseLayerSetting__mdt initDmlAndSoqlPlugins(String className) {
39+
DatabaseLayerSetting__mdt setting = new DatabaseLayerSetting__mdt(
40+
DmlPreAndPostProcessor__c = className,
41+
SoqlPreAndPostProcessor__c = className
42+
);
43+
return DatabaseLayerTestUtils.initSettings(setting);
44+
}
45+
2246
// **** INNER **** //
2347
/**
2448
* @description Spy implementation for tracking plugin method invocations during testing.

source/classes/DmlTest.cls

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ private class DmlTest {
1313
// Custom metadata actually defined in the org should not interfere with this test:
1414
DatabaseLayer.Utils.MetadataSelector.settings = new List<DatabaseLayerSetting__mdt>{};
1515
// Initialize plugins
16-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
17-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
18-
DatabaseLayer.Dml.initPlugins();
16+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(DatabaseLayerTestUtils.SamplePlugin.class.getName());
1917
}
2018

2119
// **** TESTS **** //
@@ -874,10 +872,6 @@ private class DmlTest {
874872

875873
@IsTest
876874
static void shouldRunPreAndPostProcessorPluginLogic() {
877-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
878-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
879-
// Re-initialize the plugins, now that the field has been set:
880-
DatabaseLayer.Dml.initPlugins();
881875
Account account = DmlTest.initAccount();
882876

883877
Test.startTest();
@@ -896,10 +890,6 @@ private class DmlTest {
896890

897891
@IsTest
898892
static void shouldRunPluginErrorLogic() {
899-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
900-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
901-
// Re-initialize the plugins, now that the field has been set:
902-
DatabaseLayer.Dml.initPlugins();
903893
Account account = DmlTest.initAccount();
904894

905895
Test.startTest();
@@ -925,10 +915,7 @@ private class DmlTest {
925915

926916
@IsTest
927917
static void shouldNotRunProcessorLogicIfInvalidApexClassDefined() {
928-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
929-
settings.DmlPreAndPostProcessor__c = 'an obviously invalid apex class name';
930-
// Re-initialize the plugins, now that the field has been set:
931-
DatabaseLayer.Dml.initPlugins();
918+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins('An obviously invalid apex class name');
932919
Account account = DmlTest.initAccount();
933920

934921
Test.startTest();
@@ -947,9 +934,7 @@ private class DmlTest {
947934

948935
@IsTest
949936
static void shouldNotRunProcessorLogicIfNoValueDefined() {
950-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
951-
settings.DmlPreAndPostProcessor__c = null;
952-
DatabaseLayer.Dml.initPlugins();
937+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(null);
953938
Account account = DmlTest.initAccount();
954939

955940
Test.startTest();
@@ -1044,12 +1029,6 @@ private class DmlTest {
10441029
return leadToConvert;
10451030
}
10461031

1047-
private static DatabaseLayerSetting__mdt initSettings() {
1048-
DatabaseLayerSetting__mdt settings = new DatabaseLayerSetting__mdt();
1049-
DatabaseLayer.Utils.MetadataSelector.settings?.add(settings);
1050-
return settings;
1051-
}
1052-
10531032
// **** INNER **** //
10541033
private class DeleteCallback extends DataSource.AsyncDeleteCallback {
10551034
public override void processDelete(Database.DeleteResult result) {

source/classes/MockDmlTest.cls

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ private class MockDmlTest {
1111
// Use Mock DML:
1212
DatabaseLayer.useMockDml();
1313
// Initialize plugins:
14-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
15-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
16-
DatabaseLayer.Dml.initPlugins();
14+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(DatabaseLayerTestUtils.SamplePlugin.class.getName());
1715
}
1816

1917
// **** TESTS **** //
@@ -905,10 +903,6 @@ private class MockDmlTest {
905903

906904
@IsTest
907905
static void shouldRunPreAndPostProcessorPluginLogic() {
908-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
909-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
910-
// Re-initialize the plugins, now that the field has been set:
911-
DatabaseLayer.Dml.initPlugins();
912906
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);
913907

914908
Test.startTest();
@@ -927,10 +921,6 @@ private class MockDmlTest {
927921

928922
@IsTest
929923
static void shouldRunPluginErrorLogic() {
930-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
931-
settings.DmlPreAndPostProcessor__c = DatabaseLayerTestUtils.SamplePlugin.class.getName();
932-
// Re-initialize the plugins, now that the field has been set:
933-
DatabaseLayer.Dml.initPlugins();
934924
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);
935925
MockDml.shouldFail();
936926

@@ -957,10 +947,7 @@ private class MockDmlTest {
957947

958948
@IsTest
959949
static void shouldNotRunProcessorLogicIfInvalidApexClassDefined() {
960-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
961-
settings.DmlPreAndPostProcessor__c = 'an obviously invalid apex class name';
962-
// Re-initialize the plugins, now that the field has been set:
963-
DatabaseLayer.Dml.initPlugins();
950+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins('An obviously invalid apex class name');
964951
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);
965952

966953
Test.startTest();
@@ -979,9 +966,7 @@ private class MockDmlTest {
979966

980967
@IsTest
981968
static void shouldNotRunProcessorLogicIfNoValueDefined() {
982-
DatabaseLayerSetting__mdt settings = DatabaseLayerTestUtils.initSettings();
983-
settings.DmlPreAndPostProcessor__c = null;
984-
DatabaseLayer.Dml.initPlugins();
969+
DatabaseLayerTestUtils.initDmlAndSoqlPlugins(null);
985970
List<Account> accounts = MockDmlTest.initRecords(Account.SObjectType, false);
986971

987972
Test.startTest();

0 commit comments

Comments
 (0)