Skip to content

Commit 891b966

Browse files
committed
refactoring + additional tests
Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent 4093b6c commit 891b966

File tree

2 files changed

+104
-32
lines changed

2 files changed

+104
-32
lines changed

force-app/main/default/classes/main/standard-soql/SOQL.cls

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ public virtual inherited sharing class SOQL implements Queryable {
171171
SubQuery whereAre(Filter filter);
172172
//ORDER BY
173173
SubQuery orderBy(SObjectField field);
174-
SubQuery orderBy(String relationshipName, SObjectField field);
174+
SubQuery orderBy(String field);
175175
SubQuery orderBy(String field, String direction);
176+
SubQuery orderBy(String relationshipName, SObjectField field);
176177
SubQuery sortDesc();
177178
SubQuery nullsLast();
178179
// LIMIT
@@ -188,9 +189,9 @@ public virtual inherited sharing class SOQL implements Queryable {
188189
// ADD CONDITION
189190
FilterGroup add(FilterGroup filterGroup);
190191
FilterGroup add(Filter filter);
191-
FilterGroup addAll(List<Filter> filters);
192192
FilterGroup add(String dynamicCondition);
193-
FilterGroup addAll(List<String> dynamicConditions);
193+
FilterGroup add(List<Filter> filters);
194+
FilterGroup add(List<String> dynamicConditions);
194195
// ORDER
195196
FilterGroup anyConditionMatching();
196197
FilterGroup conditionLogic(String order);
@@ -1243,6 +1244,11 @@ public virtual inherited sharing class SOQL implements Queryable {
12431244
return this;
12441245
}
12451246

1247+
public SubQuery orderBy(String field) {
1248+
builder.orderBys.newOrderBy().with(field);
1249+
return this;
1250+
}
1251+
12461252
public SubQuery orderBy(String relationshipName, SObjectField field) {
12471253
builder.orderBys.newOrderBy().with(relationshipName, field);
12481254
return this;
@@ -1368,27 +1374,27 @@ public virtual inherited sharing class SOQL implements Queryable {
13681374
return add(new FilterAdapter(filter));
13691375
}
13701376

1371-
public FilterGroup addAll(List<Filter> filters) {
1372-
for (Filter filter: filters) {
1373-
add(filter);
1374-
}
1375-
return this;
1376-
}
1377-
13781377
public FilterGroup add(String dynamicCondition) {
13791378
return add(new StringConditionAdapter(dynamicCondition));
13801379
}
13811380

1382-
public FilterGroup addAll(List<String> dynamicConditions) {
1383-
for (String dynamicCondition: dynamicConditions) {
1384-
add(dynamicCondition);
1381+
public FilterGroup add(FilterClause condition) {
1382+
if (condition.hasValue()) {
1383+
queryConditions.add(condition);
13851384
}
13861385
return this;
13871386
}
13881387

1389-
public FilterGroup add(FilterClause condition) {
1390-
if (condition.hasValue()) {
1391-
queryConditions.add(condition);
1388+
public FilterGroup add(List<Filter> filters) {
1389+
for (Filter filter: filters) {
1390+
add(filter);
1391+
}
1392+
return this;
1393+
}
1394+
1395+
public FilterGroup add(List<String> dynamicConditions) {
1396+
for (String dynamicCondition: dynamicConditions) {
1397+
add(dynamicCondition);
13921398
}
13931399
return this;
13941400
}

force-app/main/default/classes/main/standard-soql/SOQL_Test.cls

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ private class SOQL_Test {
566566
.toString();
567567

568568
// Verify
569-
Assert.areEqual('SELECT Id, toLabel(Status), Subject FROM Case', soql);
569+
Assert.areEqual('SELECT Id, toLabel(Status), Subject FROM Case', soql, 'The generated SOQL should match the expected one.');
570570
}
571571

572572
@IsTest
@@ -764,6 +764,19 @@ private class SOQL_Test {
764764
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts) FROM Account', soql, 'The generated SOQL should match the expected one.');
765765
}
766766

767+
@IsTest
768+
static void subQueryStringFields() {
769+
// Test
770+
String soql = SOQL.of(Account.SObjectType)
771+
.with(Account.Name)
772+
.with(SOQL.SubQuery.of('Contacts')
773+
.with('Id, Name')
774+
).toString();
775+
776+
// Verify
777+
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts) FROM Account', soql, 'The generated SOQL should match the expected one.');
778+
}
779+
767780
@IsTest
768781
static void subQueryRelatedFields() {
769782
// Test
@@ -826,7 +839,7 @@ private class SOQL_Test {
826839
}
827840

828841
@IsTest
829-
static void subQueryOrderBy() {
842+
static void subQueryOrderBySObjectField() {
830843
// Test
831844
String soql = SOQL.of(Account.SObjectType)
832845
.with(Account.Name)
@@ -841,6 +854,22 @@ private class SOQL_Test {
841854
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts ORDER BY Name DESC NULLS LAST) FROM Account', soql, 'The generated SOQL should match the expected one.');
842855
}
843856

857+
@IsTest
858+
static void subQueryOrderByStringField() {
859+
// Test
860+
String soql = SOQL.of(Account.SObjectType)
861+
.with(Account.Name)
862+
.with(SOQL.SubQuery.of('Contacts')
863+
.with(Contact.Id, Contact.Name)
864+
.orderBy('Name')
865+
.sortDesc()
866+
.nullsLast()
867+
).toString();
868+
869+
// Verify
870+
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts ORDER BY Name DESC NULLS LAST) FROM Account', soql, 'The generated SOQL should match the expected one.');
871+
}
872+
844873
@IsTest
845874
static void subQueryOrderByDynamic() {
846875
// Test
@@ -853,7 +882,7 @@ private class SOQL_Test {
853882
).toString();
854883

855884
// Verify
856-
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts ORDER BY Name ASC NULLS LAST) FROM Account', soql);
885+
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts ORDER BY Name ASC NULLS LAST) FROM Account', soql, 'The generated SOQL should match the expected one.');
857886
}
858887

859888
@IsTest
@@ -1571,7 +1600,7 @@ private class SOQL_Test {
15711600
// Setup
15721601
SOQL.FilterGroup filterGroup = SOQL.FilterGroup;
15731602

1574-
filterGroup.addAll(new List<SOQL.Filter> {
1603+
filterGroup.add(new List<SOQL.Filter> {
15751604
SOQL.Filter.with(Account.Name).equal('Test'),
15761605
SOQL.Filter.with(Account.BillingCity).equal('Krakow')
15771606
});
@@ -1582,11 +1611,11 @@ private class SOQL_Test {
15821611

15831612
// Verify
15841613
String soql = builder.toString();
1585-
Assert.areEqual('SELECT Id FROM Account WHERE (Name = :v1 AND BillingCity = :v2)', soql);
1614+
Assert.areEqual('SELECT Id FROM Account WHERE (Name = :v1 AND BillingCity = :v2)', soql, 'The generated SOQL should match the expected one.');
15861615

15871616
Map<String, Object> binding = builder.binding();
1588-
Assert.areEqual('Test', binding.get('v1'));
1589-
Assert.areEqual('Krakow', binding.get('v2'));
1617+
Assert.areEqual('Test', binding.get('v1'), 'The binding variable should match the expected value.');
1618+
Assert.areEqual('Krakow', binding.get('v2'), 'The binding variable should match the expected value.');
15901619
}
15911620

15921621
@IsTest
@@ -1598,31 +1627,68 @@ private class SOQL_Test {
15981627
filterGroup.add('BillingCity = \'Krakow\'');
15991628

16001629
// Test
1601-
SOQL builder = SOQL.of(Account.SObjectType)
1602-
.whereAre(filterGroup);
1630+
String soql = SOQL.of(Account.SObjectType)
1631+
.whereAre(filterGroup)
1632+
.toString();
16031633

16041634
// Verify
1605-
String soql = builder.toString();
1606-
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' AND BillingCity = \'Krakow\')', soql);
1635+
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' AND BillingCity = \'Krakow\')', soql, 'The generated SOQL should match the expected one.');
1636+
}
1637+
1638+
@IsTest
1639+
static void dynamicStringFiltersGroupWithAnyConditionMatching() {
1640+
// Setup
1641+
SOQL.FilterGroup filterGroup = SOQL.FilterGroup;
1642+
1643+
filterGroup.add('Name = \'Test\'');
1644+
filterGroup.add('BillingCity = \'Krakow\'');
1645+
filterGroup.anyConditionMatching();
1646+
1647+
// Test
1648+
String soql = SOQL.of(Account.SObjectType)
1649+
.whereAre(filterGroup)
1650+
.toString();
1651+
1652+
// Verify
1653+
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' OR BillingCity = \'Krakow\')', soql, 'The generated SOQL should match the expected one.');
16071654
}
16081655

16091656
@IsTest
16101657
static void dynamicStringFiltersListGroup() {
1658+
// Test
1659+
String soql = SOQL.of(Account.SObjectType)
1660+
.whereAre(SOQL.FilterGroup
1661+
.add(new List<String> {
1662+
'Name = \'Test\'',
1663+
'BillingCity = \'Krakow\''
1664+
})
1665+
).toString();
1666+
1667+
// Verify
1668+
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' AND BillingCity = \'Krakow\')', soql, 'The generated SOQL should match the expected one.');
1669+
}
1670+
1671+
@IsTest
1672+
static void dynamicStringFiltersListGroupWithAnyConditionMatching() {
16111673
// Setup
16121674
SOQL.FilterGroup filterGroup = SOQL.FilterGroup;
16131675

1614-
filterGroup.addAll(new List<String> {
1676+
filterGroup.add(new List<String> {
16151677
'Name = \'Test\'',
16161678
'BillingCity = \'Krakow\''
16171679
});
16181680

16191681
// Test
1620-
SOQL builder = SOQL.of(Account.SObjectType)
1621-
.whereAre(filterGroup);
1682+
String soql = SOQL.of(Account.SObjectType)
1683+
.whereAre(SOQL.FilterGroup
1684+
.add(new List<String> {
1685+
'Name = \'Test\'',
1686+
'BillingCity = \'Krakow\''
1687+
}).anyConditionMatching()
1688+
).toString();
16221689

16231690
// Verify
1624-
String soql = builder.toString();
1625-
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' AND BillingCity = \'Krakow\')', soql);
1691+
Assert.areEqual('SELECT Id FROM Account WHERE (Name = \'Test\' OR BillingCity = \'Krakow\')', soql, 'The generated SOQL should match the expected one.');
16261692
}
16271693

16281694
@IsTest

0 commit comments

Comments
 (0)