Module: OpenStax::Salesforce::SpecHelpers
- Defined in:
- lib/openstax/salesforce/spec_helpers.rb
Defined Under Namespace
Classes: SalesforceProxy
Instance Method Summary collapse
-
#limit_salesforce_queries(remote_class, **conditions) ⇒ Object
Often, when dealing with Salesforce sandboxes, there are a bunch of other records in the sandbox that you’d rather your specs ignore.
-
#limit_salesforce_queries_by_token(remote_class, token) ⇒ Object
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`.
Instance Method Details
#limit_salesforce_queries(remote_class, **conditions) ⇒ Object
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`).
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
#limit_salesforce_queries_by_token(remote_class, token) ⇒ Object
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`.
6 7 8 9 10 11 12 13 |
# 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 |