File: schema_validator_tests.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (104 lines) | stat: -rw-r--r-- 3,748 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

Shindo.tests('Fog::Schema::DataValidator', 'meta') do

  validator = Fog::Schema::DataValidator.new

  tests('#validate') do
    tests('returns true') do
      returns(true, 'when value matches schema expectation') do
        validator.validate({ 'key' => 'Value' }, 'key' => String)
      end

      returns(true, 'when values within an array all match schema expectation') do
        validator.validate({ 'key' => [1, 2] }, 'key' => [Integer])
      end

      returns(true, 'when nested values match schema expectation') do
        validator.validate({ 'key' => { nested_key: 'Value' } }, 'key' => { nested_key: String })
      end

      returns(true, 'when collection of values all match schema expectation') do
        validator.validate([{ 'key' => 'Value' }, 'key' => 'Value'], [{ 'key' => String }])
      end

      returns(true, 'when collection is empty although schema covers optional members') do
        validator.validate([], [{ 'key' => String }])
      end

      returns(true, 'when additional keys are passed and not strict') do
        validator.validate({ 'key' => 'Value', extra: 'Bonus' }, { 'key' => String }, allow_extra_keys: true)
      end

      returns(true, 'when value is nil and schema expects NilClass') do
        validator.validate({ 'key' => nil }, 'key' => NilClass)
      end

      returns(true, 'when value and schema match as hashes') do
        validator.validate({}, {})
      end

      returns(true, 'when value and schema match as arrays') do
        validator.validate([], [])
      end

      returns(true, 'when value is a Time') do
        validator.validate({ 'time' => Time.now }, 'time' => Time)
      end

      returns(true, 'when key is missing but value should be NilClass (#1477)') do
        validator.validate({}, { 'key' => NilClass }, allow_optional_rules: true)
      end

      returns(true, 'when key is missing but value is nullable (#1477)') do
        validator.validate({}, { 'key' => Fog::Nullable::String }, allow_optional_rules: true)
      end
    end

    tests('returns false') do
      returns(false, 'when value does not match schema expectation') do
        validator.validate({ 'key' => nil }, 'key' => String)
      end

      returns(false, 'when key formats do not match') do
        validator.validate({ 'key' => 'Value' }, key: String)
      end

      returns(false, 'when additional keys are passed and strict') do
        validator.validate({ 'key' => 'Missing' }, {})
      end

      returns(false, 'when some keys do not appear') do
        validator.validate({}, 'key' => String)
      end

      returns(false, 'when collection contains a member that does not match schema') do
        validator.validate([{ 'key' => 'Value' }, 'key' => 5], ['key' => String])
      end

      returns(false, 'when collection has multiple schema patterns') do
        validator.validate([{ 'key' => 'Value' }], [{ 'key' => Integer }, { 'key' => String }])
      end

      returns(false, 'when hash and array are compared') do
        validator.validate({}, [])
      end

      returns(false, 'when array and hash are compared') do
        validator.validate([], {})
      end

      returns(false, 'when a hash is expected but another data type is found') do
        validator.validate({ 'key' => { nested_key: [] } }, 'key' => { nested_key: {} })
      end

      returns(false, 'when key is missing but value should be NilClass (#1477)') do
        validator.validate({}, { 'key' => NilClass }, allow_optional_rules: false)
      end

      returns(false, 'when key is missing but value is nullable (#1477)') do
        validator.validate({}, { 'key' => Fog::Nullable::String }, allow_optional_rules: false)
      end
    end
  end
end