module OpenStax::Salesforce::SpecHelpers
Public Instance Methods
Often, when dealing with Salesforce
sandboxes, there are a bunch of other records in the sandbox that you'd rather your specs ignore. This method makes SF queries in your specs have additional `where` clauses to limit what is returned. If you are careful how you create the records you are testing against, you can use this method to only consider those records. E.g. when creating a contact, if you make each of the last names end with “_foobar”, then by saying:
limit_salesforce_queries(OpenStax::Salesforce::Remote::Contact, last_name: "%_foobar")
your Contact queries will only return the one you made with the last name ending in “_foobar”. The methods in SalesforceProxy
that help you make SF records use the `unique_token` in this way (set via `reset_unique_token`).
# File lib/openstax/salesforce/spec_helpers.rb, line 28 def limit_salesforce_queries(remote_class, **conditions) allow(remote_class).to receive(:query) do like_conditions = {} other_conditions = {} conditions.each_pair do |key, value| if value.is_a?(String) && value.include?("%") like_conditions[key] = value else other_conditions[key] = value end end like_queries = like_conditions.map do |key, value| attribute = remote_class.mapping.mappings[key] quoted_value = "'#{value.gsub(/\\/, '\&\&').gsub(/'/, "''")}'" "#{attribute} LIKE #{quoted_value}" end result = remote_class.original_query like_queries.each do |like_query| result = result.where(like_query) end result = result.where(other_conditions) if other_conditions.any? result end end
Uses knowledge of how `SalesforceProxy` methods create new SF records using its `unique_token` to create the conditions you'd otherwise have to manually set when calling `limit_salesforce_queries`.
# File lib/openstax/salesforce/spec_helpers.rb, line 6 def limit_salesforce_queries_by_token(remote_class, token) case remote_class.new when OpenStax::Salesforce::Remote::Contact, OpenStax::Salesforce::Remote::Lead limit_salesforce_queries(remote_class, last_name: "%#{token}") else raise "Don't know how to apply to #{remote_class}" end end