From 76280a7d3c03fd5341c1c8e4a76f6fea12be3224 Mon Sep 17 00:00:00 2001 From: Cristian N Date: Fri, 6 Sep 2024 14:11:42 +0200 Subject: [PATCH 1/3] test: add tests to uncover the bug --- .../test/classes/fflib_SObjectsTest.cls | 80 +++++++++++++++++-- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index dd5bd13f0a0..92bd0efa46a 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -102,6 +102,13 @@ private class fflib_SObjectsTest System.assertEquals(3, domain.selectWithoutShippingCountry().size()); } + @IsTest + static void itShouldReturnRecordsWithoutFieldValuesForNonStringFields() + { + DomainAccounts domain = generateDomain(); + + System.assertEquals(4, domain.selectWithoutNumberOfEmployees().size()); + } @IsTest static void itShouldReturnRecordsWithoutAllFieldValues() @@ -118,13 +125,20 @@ private class fflib_SObjectsTest System.assert(domain.selectWithShippingCountry().size() == 4); } + @IsTest + static void itShouldReturnRecordsWithNumberOfEmployees() + { + DomainAccounts domain = generateDomain(); + + System.assert(domain.selectWithNumberOfEmployees().size() == 3); + } @IsTest static void itShouldReturnRecordsWithAllFieldValues() { DomainAccounts domain = generateDomain(); - System.assert(domain.selectPopulatedRecords().size() == 4); + System.assert(domain.selectPopulatedRecords().size() == 2); } @IsTest @@ -151,6 +165,44 @@ private class fflib_SObjectsTest ); } + @IsTest + static void itShouldReturnFieldValuesForNonStringFields() + { + DomainAccounts domain = generateDomain(); + + final Set expectedOriginalType = new Set + { + null, + 10, + 20, + 30 + }; + + System.assert( + domain.getFieldValues(Schema.Account.NumberOfEmployees) + .equals(expectedOriginalType) + ); + } + + @IsTest + static void itShouldReturnStringFieldValuesForNonStringFields() + { + DomainAccounts domain = generateDomain(); + + final Set expectedStrings = new Set + { + null, + '10', + '20', + '30' + }; + + System.assert( + domain.getStringFieldValues(Schema.Account.NumberOfEmployees) + .equals(expectedStrings) + ); + } + @IsTest static void itShouldSetFieldValue() { @@ -212,11 +264,11 @@ private class fflib_SObjectsTest { new Account(Name = 'A', ShippingCountry = 'USA'), new Account(Name = 'B', ShippingCountry = 'Ireland'), - new Account(Name = 'C', ShippingCountry = 'UK'), + new Account(Name = 'C', ShippingCountry = 'UK', NumberOfEmployees = 10), new Account(Name = 'D', ShippingCountry = ''), - new Account(Name = 'E'), + new Account(Name = 'E', NumberOfEmployees = 20), new Account(), - new Account(Name = 'G', ShippingCountry = 'Canada') + new Account(Name = 'G', ShippingCountry = 'Canada', NumberOfEmployees = 30) } ); return domain; @@ -260,13 +312,28 @@ private class fflib_SObjectsTest ); } + public List selectWithoutNumberOfEmployees() + { + return (List) getRecordsWithBlankFieldValues( + Schema.Account.NumberOfEmployees + ); + } + + public List selectWithNumberOfEmployees() + { + return (List) getRecordsWithNotBlankFieldValues( + Schema.Account.NumberOfEmployees + ); + } + public List selectWithEmptyRecord() { return (List) getRecordsWithAllBlankFieldValues( new Set { Schema.Account.Name, - Schema.Account.ShippingCountry + Schema.Account.ShippingCountry, + Schema.Account.NumberOfEmployees } ); } @@ -277,7 +344,8 @@ private class fflib_SObjectsTest new Set { Schema.Account.Name, - Schema.Account.ShippingCountry + Schema.Account.ShippingCountry, + Schema.Account.NumberOfEmployees } ); } From 455fbd307bd165360d8a2b4c18a09ed6e6af03cf Mon Sep 17 00:00:00 2001 From: Cristian N Date: Fri, 6 Sep 2024 14:11:58 +0200 Subject: [PATCH 2/3] fix: replace type casting with method --- .../apex-common/main/classes/fflib_SObjects.cls | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 95c48ccf017..8de2bdbd1f3 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -182,7 +182,7 @@ public virtual class fflib_SObjects Set result = new Set(); for (SObject record : getRecords()) { - result.add((String) record.get(field)); + result.add(String.valueOf(record.get(field))); } return result; } @@ -257,7 +257,7 @@ public virtual class fflib_SObjects { for (SObjectField field : fields) { - if (String.isNotBlank((String) record.get(field))) continue; + if (String.isNotBlank(String.valueOf(record.get(field)))) continue; result.add(record); break; @@ -279,7 +279,7 @@ public virtual class fflib_SObjects Boolean allBlank = true; for (SObjectField field : fields) { - if (String.isNotBlank((String) record.get(field))) + if (String.isNotBlank(String.valueOf(record.get(field)))) { allBlank = false; break; @@ -314,7 +314,7 @@ public virtual class fflib_SObjects { for (SObjectField field : fields) { - if (String.isNotBlank((String) record.get(field))) + if (String.isNotBlank(String.valueOf(record.get(field)))) { result.add(record); break; @@ -337,7 +337,7 @@ public virtual class fflib_SObjects Boolean allNonBlank = true; for (SObjectField field : fields) { - if (String.isBlank((String) record.get(field))) + if (String.isBlank(String.valueOf(record.get(field)))) { allNonBlank = false; break; From f493b7236c32770d5dea30704091652b5c772356 Mon Sep 17 00:00:00 2001 From: Cristian N Date: Tue, 17 Dec 2024 22:10:33 +0100 Subject: [PATCH 3/3] fix: revert casting for specific method --- sfdx-source/apex-common/main/classes/fflib_SObjects.cls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 8de2bdbd1f3..b1849b88724 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -182,7 +182,7 @@ public virtual class fflib_SObjects Set result = new Set(); for (SObject record : getRecords()) { - result.add(String.valueOf(record.get(field))); + result.add((String) record.get(field)); } return result; }