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



100
101
102
# File 'lib/standard_api/test_case.rb', line 100

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

Instance Method Details

#include_filter_testsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/standard_api/test_case.rb', line 104

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, where: predicate, format: 'json'
        assert_equal model.filter(predicate).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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/standard_api/test_case.rb', line 153

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



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

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

#normalize(*attributes, &block) ⇒ Object



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

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