Module: StandardAPI::TestCase::ClassMethods

Defined in:
lib/standard_api/test_case.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



143
144
145
# File 'lib/standard_api/test_case.rb', line 143

def self.extended(klass)
  klass.instance_variable_set('@normalizers', {})
end

Instance Method Details

#controller_classObject



147
148
149
150
151
152
# File 'lib/standard_api/test_case.rb', line 147

def controller_class
  controller_class_name = self.name.gsub(/Test$/, '')
  controller_class_name.constantize 
rescue NameError => e
  raise e if e.message != "uninitialized constant #{controller_class_name}"
end

#include_filter_testsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/standard_api/test_case.rb', line 154

def include_filter_tests
  model.instance_variable_get('@filters').each do |filter|
    next if filter[1].is_a?(Proc) # Custom filter
    next if model.reflect_on_association(filter[0]) # TODO: Relation Filter Tests
    next if !model.respond_to?(filter[0])

    define_method("test_model_filter_#{filter[0]}") do
      m = create_model
      value = m.send(filter[0])

      assert_predicate = -> (predicate) {
        get :index, params: {where: predicate}, format: 'json'
        assert_equal model.filter(predicate).order('id ASC').to_sql, assigns(plural_name).to_sql
      }

      # TODO: Test array
      case model.columns_hash[filter[0].to_s].type
      when :jsonb, :json # JSON
        assert_predicate.call({ filter[0] => value })
      else
        case value
        when Array
          assert_predicate.call({ filter[0] => value }) # Overlaps
          assert_predicate.call({ filter[0] => value[0] }) # Contains
        else
          assert_predicate.call({ filter[0] => value }) # Equality
          assert_predicate.call({ filter[0] => { gt: value } }) # Greater Than
          assert_predicate.call({ filter[0] => { greater_than: value } })
          assert_predicate.call({ filter[0] => { lt: value } }) # Less Than
          assert_predicate.call({ filter[0] => { less_than: value } })
          assert_predicate.call({ filter[0] => { gte: value } }) # Greater Than or Equal To
          assert_predicate.call({ filter[0] => { gteq: value } })
          assert_predicate.call({ filter[0] => { greater_than_or_equal_to: value } })
          assert_predicate.call({ filter[0] => { lte: value } }) # Less Than or Equal To
          assert_predicate.call({ filter[0] => { lteq: value } })
          assert_predicate.call({ filter[0] => { less_than_or_equal_to: value } })
        end
      end
    end
  end
end

#modelObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/standard_api/test_case.rb', line 204

def model
  return @model if defined?(@model) && @model

  klass_name = controller_class.name.gsub(/Controller$/, '').singularize
    
  begin
    @model = klass_name.constantize
  rescue NameError
    raise e unless e.message =~ /uninitialized constant #{klass_name}/
  end

  if @model.nil?
    raise "@model is nil: make sure you set it in your test using `self.model = ModelClass`."
  else
    @model
  end
end

#model=(val) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/standard_api/test_case.rb', line 196

def model=(val)
  @model = val
  self.filters = val.attribute_names
  self.orders = val.attribute_names
  self.includes = val.reflect_on_all_associations.map(&:name)
  @model
end

#normalize(*attributes, &block) ⇒ Object



222
223
224
225
226
227
# File 'lib/standard_api/test_case.rb', line 222

def normalize(*attributes, &block)
  attributes.each do |attribute|
    @normalizers[model] ||= {}
    @normalizers[model][attribute] = block
  end
end