Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mocking c #5

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 68 additions & 7 deletions force-app/main/default/classes/SOSL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public inherited sharing class SOSL implements ISearchable {
// RESULT
List<List<SObject>> toSearchList();
Search.SearchResults toSearchResult();
// MOCKING
ISearchable mockId(String queryIdentifier);
}

public interface IReturning {
Expand Down Expand Up @@ -120,6 +122,7 @@ public inherited sharing class SOSL implements ISearchable {
IFilter isNotNull();
IFilter isTrue();
IFilter isFalse();
IFilter equal(String value);
IFilter equal(Object value);
IFilter notEqual(Object value);
IFilter lessThan(Object value);
Expand Down Expand Up @@ -151,6 +154,7 @@ public inherited sharing class SOSL implements ISearchable {

private SearchableBuilder builder;
private Executor executor;
private static Mock mock = new Mock();

public static SOSL find(String searchText) {
return new SOSL(searchText);
Expand Down Expand Up @@ -252,6 +256,11 @@ public inherited sharing class SOSL implements ISearchable {
return this;
}

public SOSL mockId(String mockId) {
executor.mockId = mockId;
return this;
}

public ISearchable systemMode() {
executor.systemMode();
return this;
Expand All @@ -267,6 +276,16 @@ public inherited sharing class SOSL implements ISearchable {
return this;
}

@TestVisible
private static void setMock(String mockId, List<List<SObject>> results) {
mock.setMock(mockId, results);
}

@TestVisible
private static void setMock(String mockId, List<Id> results) {
mock.setMock(mockId, results);
}

public override String toString() {
return builder.toString();
}
Expand Down Expand Up @@ -373,23 +392,23 @@ public inherited sharing class SOSL implements ISearchable {
private class SoslSearchClause implements QueryClause {
private String searchGroup;

public void inAllFields(){
public void inAllFields() {
this.searchGroup = 'ALL';
}

public void inEmailFields(){
public void inEmailFields() {
this.searchGroup = 'EMAIL';
}

public void inNameFields(){
public void inNameFields() {
this.searchGroup = 'NAME';
}

public void inPhoneFields(){
public void inPhoneFields() {
this.searchGroup = 'PHONE';
}

public void inSidebarFields(){
public void inSidebarFields() {
this.searchGroup = 'SIDEBAR';
}

Expand Down Expand Up @@ -732,7 +751,7 @@ public inherited sharing class SOSL implements ISearchable {
}

public void withNetworkIn(Iterable<Id> networkIds) {
add('NETWORK IN (\'' + String.join(networkIds, '\', \'') + '\')');
add('NETWORK IN (\'' + String.join(networkIds, '\', \'') + '\')');
}

public void withPriceBookId(Id priceBookId) {
Expand Down Expand Up @@ -763,7 +782,6 @@ public inherited sharing class SOSL implements ISearchable {
public class SoslUpdateClauses implements QueryClause {
private List<String> updateClauses = new List<String>();


public void updateViewStat() {
updateClauses.add('VIEWSTAT');
}
Expand Down Expand Up @@ -961,6 +979,10 @@ public inherited sharing class SOSL implements ISearchable {
return set('=', value);
}

public IFilter equal(String value) {
return set('=', quote(value));
}

public IFilter notEqual(Object value) {
return set('!=', value);
}
Expand Down Expand Up @@ -1099,10 +1121,20 @@ public inherited sharing class SOSL implements ISearchable {
}

public List<List<SObject>> search(String query, List<Object> bindedValues) {
if (mock.hasFixedMock(mockId)) {
Test.setFixedSearchResults(mock.getFixedMock(mockId));
} else if (mock.hasMock(mockId)) {
return mock.getMock(mockId);
}

return sharingExecutor.setBinding(bindedValues).search(query, accessMode);
}

public Search.SearchResults find(String query, List<Object> bindedValues) {
if (mock.hasFixedMock(mockId)) {
Test.setFixedSearchResults(mock.getFixedMock(mockId));
}

return sharingExecutor.setBinding(bindedValues).find(query, accessMode);
}
}
Expand Down Expand Up @@ -1165,6 +1197,35 @@ public inherited sharing class SOSL implements ISearchable {
}
}

private class Mock {
private final Map<String, List<List<SObject>>> sObjectsMocks = new Map<String, List<List<SObject>>>();
private final Map<String, List<Id>> fixedResultsMocks = new Map<String, List<Id>>();

public void setMock(String mockId, List<List<SObject>> results) {
sObjectsMocks.put(mockId, results);
}

public void setMock(String mockId, List<Id> results) {
fixedResultsMocks.put(mockId, results);
}

public Boolean hasMock(String mockId) {
return sObjectsMocks.containsKey(mockId);
}

public Boolean hasFixedMock(String mockId) {
return fixedResultsMocks.containsKey(mockId);
}

public List<List<SObject>> getMock(String mockId) {
return sObjectsMocks.get(mockId);
}

public List<Id> getFixedMock(String mockId) {
return fixedResultsMocks.get(mockId);
}
}

private static String quote(String value) {
return '\'' + value + '\'';
}
Expand Down
101 changes: 86 additions & 15 deletions force-app/main/default/classes/SOSL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE RecordType.DeveloperName = :binded0)', sosl.toString());
Assert.areEqual('Partner', sosl.binding()[0]);
Assert.areEqual('\'Partner\'', sosl.binding()[0]);
}

@IsTest
Expand All @@ -235,7 +235,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE Name = :binded0)', sosl.toString());
Assert.areEqual('Test', sosl.binding()[0]);
Assert.areEqual('\'Test\'', sosl.binding()[0]);
}

@IsTest
Expand All @@ -251,7 +251,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE Industry = :binded0)', sosl.toString());
Assert.areEqual('IT', sosl.binding()[0]);
Assert.areEqual('\'IT\'', sosl.binding()[0]);
}

@IsTest
Expand All @@ -267,7 +267,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Contact(Id, Name WHERE Account.Industry = :binded0)', sosl.toString());
Assert.areEqual('IT', sosl.binding()[0]);
Assert.areEqual('\'IT\'', sosl.binding()[0]);
}

@IsTest
Expand All @@ -283,7 +283,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE Industry = :binded0)', sosl.toString());
Assert.areEqual('IT', sosl.binding()[0]);
Assert.areEqual('\'IT\'', sosl.binding()[0]);
}

@IsTest
Expand Down Expand Up @@ -379,7 +379,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE Name = :binded0)', sosl.toString());
Assert.areEqual('My Account', sosl.binding()[0]);
Assert.areEqual('\'My Account\'', sosl.binding()[0]);
}

@IsTest
Expand Down Expand Up @@ -700,7 +700,7 @@ private class SOSL_Test {

// Verify
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE (Industry = :binded0))', sosl.toString());
Assert.areEqual('IT', sosl.binding()[0]);
Assert.areEqual('\'IT\'', sosl.binding()[0]);
}

@IsTest
Expand All @@ -721,8 +721,8 @@ private class SOSL_Test {
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE (Name = :binded0 AND Industry = :binded1))', sosl.toString());

List<Object> bindedValues = sosl.binding();
Assert.areEqual('MyAccount', bindedValues[0]);
Assert.areEqual('IT', bindedValues[1]);
Assert.areEqual('\'MyAccount\'', bindedValues[0]);
Assert.areEqual('\'IT\'', bindedValues[1]);
}

@IsTest
Expand All @@ -744,8 +744,8 @@ private class SOSL_Test {
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE (Name = :binded0 OR Industry = :binded1))', sosl.toString());

List<Object> bindedValues = sosl.binding();
Assert.areEqual('MyAccount', bindedValues[0]);
Assert.areEqual('IT', bindedValues[1]);
Assert.areEqual('\'MyAccount\'', bindedValues[0]);
Assert.areEqual('\'IT\'', bindedValues[1]);
}

@IsTest
Expand All @@ -771,8 +771,8 @@ private class SOSL_Test {
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE (Name = :binded0 AND Industry = :binded1 AND (NumberOfEmployees > :binded2 OR IsDeleted = :binded3)))', sosl.toString());

List<Object> bindedValues = sosl.binding();
Assert.areEqual('MyAccount', bindedValues[0]);
Assert.areEqual('IT', bindedValues[1]);
Assert.areEqual('\'MyAccount\'', bindedValues[0]);
Assert.areEqual('\'IT\'', bindedValues[1]);
Assert.areEqual(5, bindedValues[2]);
Assert.areEqual(false, bindedValues[3]);
}
Expand Down Expand Up @@ -813,8 +813,8 @@ private class SOSL_Test {
Assert.areEqual('FIND \'SearchText\' IN ALL FIELDS RETURNING Account(Id, Name WHERE (Name = :binded0 OR Industry = :binded1))', sosl.toString());

List<Object> bindedValues = sosl.binding();
Assert.areEqual('MyAccount', bindedValues[0]);
Assert.areEqual('IT', bindedValues[1]);
Assert.areEqual('\'MyAccount\'', bindedValues[0]);
Assert.areEqual('\'IT\'', bindedValues[1]);
}

@isTest
Expand Down Expand Up @@ -1270,4 +1270,75 @@ private class SOSL_Test {
)
.preview();
}

@IsTest
static void mock() {
// Test
List<SObject> testAccounts = new List<Account>{ new Account(Name = 'Mock') };
SOSL.setMock('MockingExample', new List<List<SObject>>{ testAccounts });

List<List<SObject>> results = SOSL.find(SEARCH_TEXT)
.inAllFields()
.returning(SOSL.returning(Account.SObjectType))
.mockId('MockingExample')
.preview()
.toSearchList();

// Verify
Assert.isFalse(results.isEmpty(), 'Should return results for at least one SObject');
Assert.isFalse(results.get(0).isEmpty(), 'Accounts list shouldnt be empty');
Assert.isNotNull(results.get(0).get(0), 'Account should be returned');
Assert.areEqual(((Account) testAccounts.get(0)).Name, ((Account) results.get(0).get(0)).Name, 'Accounts name should be equal');
}

@IsTest
static void mockFixedIds() {
// Test
List<Account> testAccounts = new List<Account>{ new Account(Name = SEARCH_TEXT) };
insert testAccounts;

SOSL.setMock('MockingExample', new List<Id>{ testAccounts.get(0).Id });

List<List<SObject>> results;
Test.startTest();
results = SOSL.find(SEARCH_TEXT)
.inAllFields()
.returning(SOSL.returning(Account.SObjectType).with(Account.Name))
.mockId('MockingExample')
.preview()
.toSearchList();
Test.stopTest();

// Verify
List<Account> fixedAccounts = [SELECT Name FROM Account];
Assert.isFalse(results.isEmpty(), 'Should return results for at least one SObject');
Assert.isFalse(results.get(0).isEmpty(), 'Accounts list shouldnt be empty');
Assert.isNotNull(results.get(0).get(0), 'Account should be returned');
Assert.areEqual(((Account) fixedAccounts.get(0)).Name, ((Account) results.get(0).get(0)).Name, 'Accounts name should be equal');
}

@IsTest
static void mockFindFixedIds() {
// Test
List<Account> testAccounts = new List<Account>{ new Account(Name = SEARCH_TEXT) };
insert testAccounts;

SOSL.setMock('MockingExample', new List<Id>{ testAccounts.get(0).Id });

Search.SearchResults results;
Test.startTest();
results = SOSL.find(SEARCH_TEXT)
.inAllFields()
.returning(SOSL.returning(Account.SObjectType).with(Account.Name))
.mockId('MockingExample')
.preview()
.toSearchResult();
Test.stopTest();

// Verify
List<Account> fixedAccounts = [SELECT Name FROM Account];
Assert.isFalse(results.get('Account').isEmpty(), 'Accounts list shouldnt be empty');
Assert.isNotNull(results.get('Account').get(0), 'Account should be returned');
Assert.areEqual(((Account) fixedAccounts.get(0)).Name, ((Account) results.get('Account').get(0).getSObject()).Name, 'Accounts name should be equal');
}
}
25 changes: 11 additions & 14 deletions website/docs/api/sosl.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ SOSL.find('MySearch')

### mockId

TBD
Query needs unique id that allows for mocking.

**Signature**

Expand All @@ -600,20 +600,17 @@ SOSL mockId(String queryIdentifier)
**Example**

```apex
TBD
```
### list mock

**Signature**

```apex
SOSL setMock(String mockId, List<SObject> records)
```

**Example**
SOSL.find(SEARCH_TEXT)
.inAllFields()
.returning(
SOSL.returning(Account.SObjectType)
)
.mockId('MockingExample')
.toSearchList();

```apex
TBD
// In Unit Test
List<SObject> testAccounts = new List<Account>{ new Account(Name = 'Mock') };
SOSL.setMock('MockingExample', new List<List<SObject>>{ testAccounts });
```

## DEBUGGING
Expand Down
Loading