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



127
128
129
# File 'lib/standard_api/test_case.rb', line 127

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

Instance Method Details

#include_filter_testsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'lib/standard_api/test_case.rb', line 131

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/standard_api/test_case.rb', line 180

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



172
173
174
175
176
177
178
# File 'lib/standard_api/test_case.rb', line 172

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



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

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