module StandardAPI::TestCase

Public Class Methods

included(klass) click to toggle source
# File lib/standard_api/test_case.rb, line 22
def self.included(klass)
  [:filters, :orders, :includes].each do |attribute|
    klass.send(:class_attribute, attribute)
  end

  begin
    model_class = klass.name.gsub(/Test$/, '').constantize.model

    klass.send(:filters=, model_class.attribute_names)
    klass.send(:orders=, model_class.attribute_names)
    klass.send(:includes=, model_class.reflect_on_all_associations.map(&:name))
  rescue NameError => e
    raise e if e.message != "uninitialized constant #{model_class_name}"
  end

  klass.extend(ClassMethods)

  routes = Rails.application.routes.set.routes.inject({}) do |acc, r|
    acc[r.defaults[:controller]] ||= {}
    acc[r.defaults[:controller]][r.defaults[:action]] = true
    acc
  end

  test_cases = Dir.entries(File.expand_path(File.join(__FILE__, '..', 'test_case')))
  test_cases.select! {|fn| fn.ends_with?('_tests.rb') }
  test_cases.map! {|fn| fn.sub('_tests.rb', '') }
  (klass.controller_class.action_methods & test_cases).each do |action|
    if const_defined?("StandardAPI::TestCase::#{action.capitalize}Tests") && routes[klass.controller_class.controller_path][action]
      klass.include("StandardAPI::TestCase::#{action.capitalize}Tests".constantize)
    end
  end
end

Public Instance Methods

assert_equal_or_nil(expected, *args) click to toggle source
# File lib/standard_api/test_case.rb, line 14
def assert_equal_or_nil(expected, *args)
  if expected.nil?
    assert_nil(*args)
  else
    assert_equal(expected, *args)
  end
end
controller_class() click to toggle source
# File lib/standard_api/test_case.rb, line 89
def controller_class
  self.class.controller_class
end
create_attributes(record)
Alias for: update_attributes
create_model(attrs={}) click to toggle source
# File lib/standard_api/test_case.rb, line 93
def create_model(attrs={})
  create(model.name.underscore, attrs.merge(mask))
end
create_webmocks(attributes) click to toggle source
# File lib/standard_api/test_case.rb, line 105
def create_webmocks(attributes)
  attributes.each do |attribute, value|
    self.class.model.validators_on(attribute)
  end
end
default_limit() click to toggle source
# File lib/standard_api/test_case.rb, line 71
def default_limit
  controller_class.new.send(:default_limit)
end
default_orders() click to toggle source
# File lib/standard_api/test_case.rb, line 63
def default_orders
  controller_class.new.send(:default_orders)
end
mask() click to toggle source
# File lib/standard_api/test_case.rb, line 79
def mask
  {}
end
model() click to toggle source
# File lib/standard_api/test_case.rb, line 75
def model
  self.class.model
end
normalize_attribute(record, attribute, value) click to toggle source
# File lib/standard_api/test_case.rb, line 115
def normalize_attribute(record, attribute, value)
  if normalizers[self.class.model] && normalizers[self.class.model][attribute]
    b = normalizers[self.class.model][attribute]
    b.arity == 2 ? b.call(record, value) : b.call(value)
  else
    # validators = self.class.model.validators_on(attribute)
    value
  end
end
normalize_to_json(record, attribute, value) click to toggle source
# File lib/standard_api/test_case.rb, line 125
def normalize_to_json(record, attribute, value)
  value = normalize_attribute(record, attribute, value)
  return nil if value.nil?

  if model.columns_hash[attribute].is_a?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal)
    "#{value.to_f}"
  elsif value.is_a?(DateTime) #model.columns_hash[attribute].is_a?(ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter)
    value.in_time_zone.as_json
  else
    value.as_json
  end
end
normalizers() click to toggle source
# File lib/standard_api/test_case.rb, line 111
def normalizers
  self.class.instance_variable_get('@normalizers')
end
plural_name() click to toggle source
# File lib/standard_api/test_case.rb, line 101
def plural_name
  model.model_name.plural
end
resource_limit() click to toggle source
# File lib/standard_api/test_case.rb, line 67
def resource_limit
  controller_class.new.send(:resource_limit)
end
resource_path(action, options={}) click to toggle source
# File lib/standard_api/test_case.rb, line 83
def resource_path(action, options={})
  url_for({
    controller: controller_class.controller_path, action: action
  }.merge(options))
end
singular_name() click to toggle source
# File lib/standard_api/test_case.rb, line 97
def singular_name
  model.model_name.singular
end
supports_format(format, action=nil) click to toggle source
# File lib/standard_api/test_case.rb, line 55
def supports_format(format, action=nil)
  count = controller_class.view_paths.count do |path|
    !Dir.glob("#{path.instance_variable_get(:@path)}/{#{model.name.underscore},application}/**/#{action || '*'}.#{format}*").empty?
  end

  count > 0
end
update_attributes(record) click to toggle source
# File lib/standard_api/test_case.rb, line 145
def update_attributes(record)
  return [] if record.nil?
  record.attributes.select do |x|
    !record.class.readonly_attributes.include?(x.to_s) &&
    !@controller.send(:excludes_for, record.class).include?(x.to_sym)
  end
end
Also aliased as: create_attributes
view_attributes(record) click to toggle source
# File lib/standard_api/test_case.rb, line 138
def view_attributes(record)
  return [] if record.nil?
  record.attributes.select do |x|
    !@controller.send(:excludes_for, record.class).include?(x.to_sym)
  end
end