Class: RSpec::Rails::Api::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/api/utils.rb

Overview

Helper methods

Class Method Summary collapse

Class Method Details

.check_attribute_type(type, except: []) ⇒ Object



81
82
83
84
# File 'lib/rspec/rails/api/utils.rb', line 81

def self.check_attribute_type(type, except: [])
  keys = PARAM_TYPES.keys.reject { |key| except.include? key }
  keys.include?(type)
end

.check_value_type(type, value) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



30
31
32
33
34
35
36
37
# File 'lib/rspec/rails/api/utils.rb', line 30

def self.check_value_type(type, value) # rubocop:disable Metrics/CyclomaticComplexity
  return true if type == :boolean && (value.is_a?(TrueClass) || value.is_a?(FalseClass))
  return true if type == :array && value.is_a?(Array)

  raise "Unknown type #{type}" unless PARAM_TYPES.key? type

  value.is_a? PARAM_TYPES[type][:class]
end

.deep_get(hash, path) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rspec/rails/api/utils.rb', line 10

def self.deep_get(hash, path)
  path.split('.').inject(hash) do |sub_hash, key|
    return nil unless sub_hash.is_a?(Hash) && sub_hash.key?(key.to_sym)

    sub_hash[key.to_sym]
  end
end

.deep_set(hash, path, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec/rails/api/utils.rb', line 18

def self.deep_set(hash, path, value)
  path = path.split('.') unless path.is_a? Array

  return value if path.count.zero?

  current_key       = path.shift.to_sym
  hash[current_key] = {} unless hash[current_key].is_a?(Hash)
  hash[current_key] = deep_set(hash[current_key], path, value)

  hash
end

.same_keys?(actual, expected) ⇒ Boolean

rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/rspec/rails/api/utils.rb', line 76

def self.same_keys?(actual, expected)
  optional = expected.reject { |_key, value| value[:required] }.keys
  actual.symbolize_keys.keys.sort - optional == expected.keys.sort - optional
end

.validate_deep_object(expected_type, expected_attributes, actual) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rspec/rails/api/utils.rb', line 60

def self.validate_deep_object(expected_type, expected_attributes, actual)
  if %i[object array].include?(expected_type) && expected_attributes.is_a?(Hash)
    case expected_type
    when :object
      return false unless validate_object_structure actual, expected_attributes
    when :array
      actual.each do |array_entry|
        return false unless validate_object_structure array_entry, expected_attributes
      end
    end
  end

  true
end

.validate_object_structure(actual, expected) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/rails/api/utils.rb', line 39

def self.validate_object_structure(actual, expected)
  # Check keys
  return false unless same_keys? actual, expected

  expected.each_key do |key|
    next unless expected[key][:required]

    expected_type       = expected[key][:type]
    expected_attributes = expected[key][:attributes]

    # Type
    return false unless check_value_type expected_type, actual[key.to_s]

    # Deep object ?
    return false unless validate_deep_object expected_type, expected_attributes, actual[key.to_s]
  end

  true
end