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

Query builder with named queries and stubs #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 70 additions & 2 deletions src/classes/QueryBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*/
public virtual inherited sharing class QueryBuilder {

private static Map<String, QueryBuilder> stubRegistry;
static {
stubRegistry = new Map<String, QueryBuilder>();
}

public enum FilterScope {
Delegated,
Everything,
Expand Down Expand Up @@ -1374,6 +1379,36 @@ public virtual inherited sharing class QueryBuilder {
return this.setCheckFLS(true);
}

/**
* Register named query to stub it from test method.
*
* <br/><br/>
* <p><b>Example</b></p>
* <pre>
* <code>
* //real class
* new QueryBuilder(Account.class)
* .namedQueryRegister('unique_query_name')
* .toString();
*
* <br/>
* //test class
* new QueryBuilder(Account.class)
* .buildStub()
* .addStubToString('fake query string')
* .namedQueryStub('unique_query_name')
* .applyStub();
* </code>
* </pre>
*
* @param name
*
* @return
*/
public QueryBuilder namedQueryRegister(String name) {
return stubRegistry.containsKey(name) ? stubRegistry.get(name) : this;
}

/**
* Returns an instance of StubbedQueryBuilder, which can be used in Unit Tests.
*
Expand Down Expand Up @@ -2444,6 +2479,37 @@ public virtual inherited sharing class QueryBuilder {
return (QueryBuilder) this.parent;
}

/**
* Stub registered named query. Use only in test methods.
*
* <br/><br/>
* <p><b>Example</b></p>
* <pre>
* <code>
* //real class
* new QueryBuilder(Account.class)
* .namedQueryRegister('unique_query_name')
* .toString();
*
* <br/>
* //test class
* new QueryBuilder(Account.class)
* .buildStub()
* .addStubToString('fake query string')
* .namedQueryStub('unique_query_name')
* .applyStub();
* </code>
* </pre>
*
* @param queryName
*
* @return current instance of StubbedQueryBuilder
*/
public StubbedQueryBuilder namedQueryStub(String queryName) {
stubRegistry.put(queryName, this.stubbedQueryBuilder);
return this;
}

//stub building methods
public StubbedQueryBuilder addStubToString(String queryString) {
this.queryString = queryString;
Expand Down Expand Up @@ -2512,8 +2578,10 @@ public virtual inherited sharing class QueryBuilder {
}

private Map<Id, SObject> stubToMap(Map<Id, SObject> mapToFill) {
mapToFill = this.toMapFillResult;
return this.toMapFillResult;
for (Id sobjId : this.toMapResult.keySet()) {
mapToFill.put(sobjId, this.toMapResult.get(sobjId));
}
return mapToFill;
}

private SObject stubToSObject() {
Expand Down