Skip to content

Commit f6c03c4

Browse files
committed
#162: fixed methods and tessts according to code review recommedations
1 parent 6836c55 commit f6c03c4

File tree

2 files changed

+71
-65
lines changed

2 files changed

+71
-65
lines changed

fflib/src/classes/fflib_QueryFactory.cls

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -514,23 +514,6 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
514514
);
515515
return this;
516516
}
517-
/**
518-
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
519-
* related through an object lookup or master-detail relationship.
520-
* Use the set to store unique field names, since we only want to sort
521-
* by the same field one time. The sort expressions are stored in a list
522-
* so that they are applied to the SOQL in the same order that they
523-
* were added in.
524-
* @param fieldName The string value of the field to be sorted on
525-
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
526-
* @param nullsLast whether to sort null values last (NULLS LAST keyword included).
527-
**/
528-
public fflib_QueryFactory setOrdering(String fieldName, SortOrder direction, Boolean nullsLast){
529-
order = new List<Ordering> {
530-
new Ordering(getFieldToken(fieldName), direction, nullsLast)
531-
};
532-
return this;
533-
}
534517

535518
/**
536519
* Add a field to be sorted on. This may be a direct field or a field
@@ -550,25 +533,26 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
550533
return this;
551534
}
552535

553-
/**
554-
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
536+
/**
537+
* Add a field to be sorted on. This may be a direct field or a field
555538
* related through an object lookup or master-detail relationship.
556539
* Use the set to store unique field names, since we only want to sort
557540
* by the same field one time. The sort expressions are stored in a list
558541
* so that they are applied to the SOQL in the same order that they
559542
* were added in.
560-
* @param field The SObjectfield to sort. This can only be a direct reference.
543+
* The "NULLS FIRST" keywords will be included by default. If "NULLS LAST"
544+
* is required, use one of the overloaded addOrdering methods which include this parameter.
545+
* @param fieldName The string value of the field to be sorted on
561546
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
562-
* @param nullsLast whether to sort null values last (NULLS LAST keyword included).
563-
**/
564-
public fflib_QueryFactory setOrdering(SObjectField field, SortOrder direction, Boolean nullsLast){
565-
order = new List<Ordering> {
566-
new Ordering(new QueryField(field), direction, nullsLast)
567-
};
547+
**/
548+
public fflib_QueryFactory addOrdering(String fieldName, SortOrder direction){
549+
order.add(
550+
new Ordering(getFieldToken(fieldName), direction)
551+
);
568552
return this;
569553
}
570554

571-
/**
555+
/**
572556
* Add a field to be sorted on. This may be a direct field or a field
573557
* related through an object lookup or master-detail relationship.
574558
* Use the set to store unique field names, since we only want to sort
@@ -577,46 +561,43 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
577561
* were added in.
578562
* The "NULLS FIRST" keywords will be included by default. If "NULLS LAST"
579563
* is required, use one of the overloaded addOrdering methods which include this parameter.
580-
* @param fieldName The string value of the field to be sorted on
564+
* @param field The SObjectfield to sort. This can only be a direct reference.
581565
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
582-
**/
583-
public fflib_QueryFactory addOrdering(String fieldName, SortOrder direction){
566+
**/
567+
public fflib_QueryFactory addOrdering(SObjectField field, SortOrder direction){
584568
order.add(
585569
new Ordering(getFieldPath(fieldName), direction)
586570
);
587571
return this;
588572
}
589-
590-
/**
573+
/**
591574
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
592575
* related through an object lookup or master-detail relationship.
593576
* Use the set to store unique field names, since we only want to sort
594577
* by the same field one time. The sort expressions are stored in a list
595578
* so that they are applied to the SOQL in the same order that they
596579
* were added in.
597-
* The "NULLS FIRST" keywords will be included by default. If "NULLS LAST"
598-
* is required, use one of the overloaded addOrdering methods which include this parameter.
599580
* @param fieldName The string value of the field to be sorted on
600581
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
582+
* @param nullsLast whether to sort null values last (NULLS LAST keyword included).
601583
**/
602-
public fflib_QueryFactory setOrdering(String fieldName, SortOrder direction){
603-
order = new List<Ordering> {
604-
new Ordering(getFieldToken(fieldName), direction)
605-
};
584+
public fflib_QueryFactory setOrdering(String fieldName, SortOrder direction, Boolean nullsLast){
585+
setOrdering(
586+
new Ordering(getFieldToken(fieldName), direction, nullsLast)
587+
);
606588
return this;
607589
}
608590

609591
/**
610-
* Add a field to be sorted on. This may be a direct field or a field
592+
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
611593
* related through an object lookup or master-detail relationship.
612594
* Use the set to store unique field names, since we only want to sort
613595
* by the same field one time. The sort expressions are stored in a list
614596
* so that they are applied to the SOQL in the same order that they
615597
* were added in.
616-
* The "NULLS FIRST" keywords will be included by default. If "NULLS LAST"
617-
* is required, use one of the overloaded addOrdering methods which include this parameter.
618598
* @param field The SObjectfield to sort. This can only be a direct reference.
619599
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
600+
* @param nullsLast whether to sort null values last (NULLS LAST keyword included).
620601
**/
621602
public fflib_QueryFactory addOrdering(SObjectField field, SortOrder direction){
622603
order.add(
@@ -625,6 +606,25 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
625606
return this;
626607
}
627608

609+
/**
610+
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
611+
* related through an object lookup or master-detail relationship.
612+
* Use the set to store unique field names, since we only want to sort
613+
* by the same field one time. The sort expressions are stored in a list
614+
* so that they are applied to the SOQL in the same order that they
615+
* were added in.
616+
* The "NULLS FIRST" keywords will be included by default. If "NULLS LAST"
617+
* is required, use one of the overloaded addOrdering methods which include this parameter.
618+
* @param fieldName The string value of the field to be sorted on
619+
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
620+
**/
621+
public fflib_QueryFactory setOrdering(String fieldName, SortOrder direction){
622+
setOrdering(
623+
new Ordering(getFieldToken(fieldName), direction)
624+
);
625+
return this;
626+
}
627+
628628
/**
629629
* Remove existing ordering and set a field to be sorted on. This may be a direct field or a field
630630
* related through an object lookup or master-detail relationship.
@@ -638,9 +638,9 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
638638
* @param SortOrder the direction to be sorted on (ASCENDING or DESCENDING)
639639
**/
640640
public fflib_QueryFactory setOrdering(SObjectField field, SortOrder direction){
641-
order = new List<Ordering> {
642-
new Ordering(new QueryField(field), direction)
643-
};
641+
setOrdering(
642+
new Ordering(field, direction)
643+
);
644644
return this;
645645
}
646646

fflib/src/classes/fflib_QueryFactoryTest.cls

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,40 +162,46 @@ private class fflib_QueryFactoryTest {
162162
}
163163

164164
@isTest
165-
static void orderingSet(){
165+
static void setOrdering_ReplacesPreviousOrderingsWithExpectedOrdering(){
166166
fflib_QueryFactory qf = new fflib_QueryFactory(Contact.SObjectType);
167167
qf.selectField('name');
168168
qf.selectField('email');
169169
qf.setCondition( 'name = \'test\'' );
170-
qf.setOrdering( new fflib_QueryFactory.Ordering('Contact','LastModifiedDate',fflib_QueryFactory.SortOrder.DESCENDING) );
171170

172-
System.assertEquals(1,qf.getOrderings().size());
173-
System.assertEquals(Contact.LastModifiedDate,qf.getOrderings()[0].getField() );
174-
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING,qf.getOrderings()[0].getDirection() );
171+
//test base method with ordeting by OwnerId Descending
172+
qf.setOrdering( new fflib_QueryFactory.Ordering('Contact','OwnerId',fflib_QueryFactory.SortOrder.DESCENDING) );
173+
174+
System.assertEquals(1, qf.getOrderings().size(), 'Unexpected order size - setOrder should replace default Orderings');
175+
System.assertEquals(Contact.OwnerId, qf.getOrderings()[0].getField(), 'Unexpected order field - should have been resolved from the field OwnerId');
176+
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING, qf.getOrderings()[0].getDirection(), 'Unexpected order direction.');
175177

176-
qf.setOrdering('LastModifiedDate', fflib_QueryFactory.SortOrder.DESCENDING, true);
178+
//test method overload with ordering by LastModifiedDate Ascending
179+
qf.setOrdering('LastModifiedDate', fflib_QueryFactory.SortOrder.ASCENDING, true);
177180

178-
System.assertEquals(1,qf.getOrderings().size());
179-
System.assertEquals(Contact.LastModifiedDate,qf.getOrderings()[0].getField() );
180-
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING,qf.getOrderings()[0].getDirection() );
181+
System.assertEquals(1, qf.getOrderings().size(), 'Unexpected order size - setOrder should replace previous Orderings');
182+
System.assertEquals(Contact.LastModifiedDate, qf.getOrderings()[0].getField(), 'Unexpected order field - should have been resolved from the field LastModifiedDate');
183+
System.assertEquals(fflib_QueryFactory.SortOrder.ASCENDING, qf.getOrderings()[0].getDirection(), 'Unexpected order direction.');
181184

182-
qf.setOrdering(Contact.LastModifiedDate, fflib_QueryFactory.SortOrder.DESCENDING, true);
185+
//test method overload with ordering by CreatedDate Descending
186+
qf.setOrdering(Contact.CreatedDate, fflib_QueryFactory.SortOrder.DESCENDING, true);
183187

184-
System.assertEquals(1,qf.getOrderings().size());
185-
System.assertEquals(Contact.LastModifiedDate,qf.getOrderings()[0].getField() );
186-
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING,qf.getOrderings()[0].getDirection() );
188+
System.assertEquals(1, qf.getOrderings().size(), 'Unexpected order size - setOrder should replace previous Orderings');
189+
System.assertEquals(Contact.CreatedDate, qf.getOrderings()[0].getField(), 'Unexpected order field - should have been resolved from the field CreatedDate');
190+
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING, qf.getOrderings()[0].getDirection(), 'Unexpected order direction.');
187191

188-
qf.setOrdering('LastModifiedDate', fflib_QueryFactory.SortOrder.DESCENDING);
192+
//test method overload with ordering by CreatedBy.Name Descending
193+
qf.setOrdering('CreatedBy.Name', fflib_QueryFactory.SortOrder.DESCENDING);
189194

190-
System.assertEquals(1,qf.getOrderings().size());
191-
System.assertEquals(Contact.LastModifiedDate,qf.getOrderings()[0].getField() );
192-
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING,qf.getOrderings()[0].getDirection() );
195+
System.assertEquals(1, qf.getOrderings().size(), 'Unexpected order size - setOrder should replace previous Orderings');
196+
System.assertEquals(Contact.CreatedById, qf.getOrderings()[0].getField(), 'Unexpected order field - should have been resolved from the field CreatedBy.Name');
197+
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING, qf.getOrderings()[0].getDirection(), 'Unexpected order direction.');
193198

194-
qf.setOrdering(Contact.LastModifiedDate, fflib_QueryFactory.SortOrder.DESCENDING);
199+
//test method overload with ordering by Birthdate Ascending
200+
qf.setOrdering(Contact.Birthdate, fflib_QueryFactory.SortOrder.ASCENDING);
195201

196-
System.assertEquals(1,qf.getOrderings().size());
197-
System.assertEquals(Contact.LastModifiedDate,qf.getOrderings()[0].getField() );
198-
System.assertEquals(fflib_QueryFactory.SortOrder.DESCENDING,qf.getOrderings()[0].getDirection() );
202+
System.assertEquals(1, qf.getOrderings().size(), 'Unexpected order size - setOrder should replace previous Orderings');
203+
System.assertEquals(Contact.Birthdate, qf.getOrderings()[0].getField(), 'Unexpected order field - should have been resolved from the field Birthdate');
204+
System.assertEquals(fflib_QueryFactory.SortOrder.ASCENDING, qf.getOrderings()[0].getDirection(), 'Unexpected order direction.');
199205

200206
String query = qf.toSOQL();
201207
}

0 commit comments

Comments
 (0)