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



135
136
137
# File 'lib/standard_api/test_case.rb', line 135

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

Instance Method Details

#controller_classObject



139
140
141
142
143
144
# File 'lib/standard_api/test_case.rb', line 139

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



146
147
148
149
150
151
152
153
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
# File 'lib/standard_api/test_case.rb', line 146

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

    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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/standard_api/test_case.rb', line 195

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



187
188
189
190
191
192
193
# File 'lib/standard_api/test_case.rb', line 187

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



213
214
215
216
217
218
# File 'lib/standard_api/test_case.rb', line 213

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