mutations 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +6 -6
- data/lib/mutations/array_filter.rb +23 -19
- data/lib/mutations/boolean_filter.rb +6 -6
- data/lib/mutations/command.rb +31 -31
- data/lib/mutations/errors.rb +10 -10
- data/lib/mutations/exception.rb +2 -2
- data/lib/mutations/hash_filter.rb +35 -31
- data/lib/mutations/input_filter.rb +8 -8
- data/lib/mutations/integer_filter.rb +5 -5
- data/lib/mutations/model_filter.rb +10 -10
- data/lib/mutations/outcome.rb +1 -1
- data/lib/mutations/string_filter.rb +10 -10
- data/lib/mutations/version.rb +1 -1
- data/mutations.gemspec +1 -1
- data/spec/array_filter_spec.rb +36 -28
- data/spec/boolean_filter_spec.rb +7 -7
- data/spec/command_spec.rb +42 -42
- data/spec/errors_spec.rb +19 -19
- data/spec/hash_filter_spec.rb +28 -19
- data/spec/inheritance_spec.rb +8 -8
- data/spec/integer_filter_spec.rb +11 -11
- data/spec/model_filter_spec.rb +21 -21
- data/spec/mutations_spec.rb +2 -2
- data/spec/simple_command.rb +3 -3
- data/spec/string_filter_spec.rb +18 -18
- metadata +1 -1
@@ -6,15 +6,15 @@ module Mutations
|
|
6
6
|
min: nil, # lowest value, inclusive
|
7
7
|
max: nil # highest value, inclusive
|
8
8
|
}
|
9
|
-
|
9
|
+
|
10
10
|
def filter(data)
|
11
|
-
|
11
|
+
|
12
12
|
# Handle nil case
|
13
13
|
if data.nil?
|
14
14
|
return [nil, nil] if options[:nils]
|
15
15
|
return [nil, :nils]
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
# Ensure it's the correct data type (Fixnum)
|
19
19
|
if !data.is_a?(Fixnum)
|
20
20
|
if data.is_a?(String) && data =~ /^-?\d/
|
@@ -23,10 +23,10 @@ module Mutations
|
|
23
23
|
return [data, :integer]
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
return [data, :min] if options[:min] && data < options[:min]
|
28
28
|
return [data, :max] if options[:max] && data > options[:max]
|
29
|
-
|
29
|
+
|
30
30
|
# We win, it's valid!
|
31
31
|
[data, nil]
|
32
32
|
end
|
@@ -6,47 +6,47 @@ module Mutations
|
|
6
6
|
builder: nil, # Could be a class or a string which will be constantized. If present, and a hash is passed, then we use that to construct a model
|
7
7
|
new_records: false, # If false, unsaved AR records are not valid. Things that don't respond to new_record? are valid. true: anything is valid
|
8
8
|
}
|
9
|
-
|
9
|
+
|
10
10
|
def initialize(name, opts = {})
|
11
11
|
super(opts)
|
12
12
|
@name = name
|
13
|
-
|
13
|
+
|
14
14
|
class_const = options[:class] || @name.to_s.camelize
|
15
15
|
class_const = class_const.constantize if class_const.is_a?(String)
|
16
16
|
self.options[:class] = class_const
|
17
|
-
|
17
|
+
|
18
18
|
if options[:builder]
|
19
19
|
options[:builder] = options[:builder].constantize if options[:builder].is_a?(String)
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def filter(data)
|
24
|
-
|
24
|
+
|
25
25
|
# Handle nil case
|
26
26
|
if data.nil?
|
27
27
|
return [nil, nil] if options[:nils]
|
28
28
|
return [nil, :nils]
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
# Passing in attributes. Let's see if we have a builder
|
32
32
|
if data.is_a?(Hash) && options[:builder]
|
33
33
|
ret = options[:builder].run(data)
|
34
|
-
|
34
|
+
|
35
35
|
if ret.success?
|
36
36
|
data = ret.result
|
37
37
|
else
|
38
38
|
return [data, ret.errors]
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
# We have a winner, someone passed in the correct data type!
|
43
43
|
if data.is_a?(options[:class])
|
44
44
|
return [data, :new_records] if !options[:new_records] && (data.respond_to?(:new_record?) && data.new_record?)
|
45
45
|
return [data, nil]
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
return [data, :model]
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
end
|
52
52
|
end
|
data/lib/mutations/outcome.rb
CHANGED
@@ -11,24 +11,24 @@ module Mutations
|
|
11
11
|
in: nil, # Can be an array like %w(red blue green)
|
12
12
|
discard_empty: false # If the param is optional, discard_empty: true drops empty fields.
|
13
13
|
}
|
14
|
-
|
14
|
+
|
15
15
|
def filter(data)
|
16
|
-
|
16
|
+
|
17
17
|
# Handle nil case
|
18
18
|
if data.nil?
|
19
19
|
return [nil, nil] if options[:nils]
|
20
20
|
return [nil, :nils]
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
# At this point, data is not nil. If it's not a string, convert it to a string for some standard classes
|
24
24
|
data = data.to_s if !options[:strict] && [TrueClass, FalseClass, Fixnum, Symbol].include?(data.class)
|
25
|
-
|
25
|
+
|
26
26
|
# Now ensure it's a string:
|
27
27
|
return [data, :string] unless data.is_a?(String)
|
28
|
-
|
28
|
+
|
29
29
|
# At this point, data is a string. Now transform it using strip:
|
30
30
|
data = data.strip if options[:strip]
|
31
|
-
|
31
|
+
|
32
32
|
# Now check if it's blank:
|
33
33
|
if data == ""
|
34
34
|
if options[:empty]
|
@@ -37,17 +37,17 @@ module Mutations
|
|
37
37
|
return [data, :empty]
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
# Now check to see if it's the correct size:
|
42
42
|
return [data, :min_length] if options[:min_length] && data.length < options[:min_length]
|
43
43
|
return [data, :max_length] if options[:max_length] && data.length > options[:max_length]
|
44
|
-
|
44
|
+
|
45
45
|
# Ensure it match
|
46
46
|
return [data, :in] if options[:in] && !options[:in].include?(data)
|
47
|
-
|
47
|
+
|
48
48
|
# Ensure it matches the regexp
|
49
49
|
return [data, :matches] if options[:matches] && (options[:matches] !~ data)
|
50
|
-
|
50
|
+
|
51
51
|
# We win, it's valid!
|
52
52
|
[data, nil]
|
53
53
|
end
|
data/lib/mutations/version.rb
CHANGED
data/mutations.gemspec
CHANGED
data/spec/array_filter_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations::ArrayFilter" do
|
4
|
-
|
4
|
+
|
5
5
|
it "allows arrays" do
|
6
6
|
f = Mutations::ArrayFilter.new(:arr)
|
7
7
|
filtered, errors = f.filter([1])
|
8
8
|
assert_equal [1], filtered
|
9
9
|
assert_equal nil, errors
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "considers non-arrays to be invalid" do
|
13
13
|
f = Mutations::ArrayFilter.new(:arr)
|
14
14
|
['hi', true, 1, {a: "1"}, Object.new].each do |thing|
|
@@ -16,90 +16,98 @@ describe "Mutations::ArrayFilter" do
|
|
16
16
|
assert_equal :array, errors
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "considers nil to be invalid" do
|
21
21
|
f = Mutations::ArrayFilter.new(:arr, nils: false)
|
22
22
|
filtered, errors = f.filter(nil)
|
23
23
|
assert_equal nil, filtered
|
24
24
|
assert_equal :nils, errors
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
it "considers nil to be valid" do
|
28
28
|
f = Mutations::ArrayFilter.new(:arr, nils: true)
|
29
29
|
filtered, errors = f.filter(nil)
|
30
30
|
filtered, errors = f.filter(nil)
|
31
31
|
assert_equal nil, errors
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "lets you specify a class, and has valid elements" do
|
35
35
|
f = Mutations::ArrayFilter.new(:arr, class: Fixnum)
|
36
36
|
filtered, errors = f.filter([1,2,3])
|
37
37
|
assert_equal nil, errors
|
38
38
|
assert_equal [1,2,3], filtered
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
it "lets you specify a class as a string, and has valid elements" do
|
42
42
|
f = Mutations::ArrayFilter.new(:arr, class: 'Fixnum')
|
43
43
|
filtered, errors = f.filter([1,2,3])
|
44
44
|
assert_equal nil, errors
|
45
45
|
assert_equal [1,2,3], filtered
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
it "lets you specify a class, and has invalid elements" do
|
49
49
|
f = Mutations::ArrayFilter.new(:arr, class: Fixnum)
|
50
50
|
filtered, errors = f.filter([1, "bob"])
|
51
51
|
assert_equal [nil, :class], errors.symbolic
|
52
52
|
assert_equal [1,"bob"], filtered
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
it "lets you use a block to supply an element filter" do
|
56
56
|
f = Mutations::ArrayFilter.new(:arr) { string }
|
57
|
-
|
57
|
+
|
58
58
|
filtered, errors = f.filter(["hi", {stuff: "ok"}])
|
59
59
|
assert_nil errors[0]
|
60
60
|
assert_equal :string, errors[1].symbolic
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
it "lets you array-ize everything" do
|
64
64
|
f = Mutations::ArrayFilter.new(:arr, arrayize: true) { string }
|
65
|
-
|
65
|
+
|
66
66
|
filtered, errors = f.filter("foo")
|
67
67
|
assert_equal ["foo"], filtered
|
68
68
|
assert_nil errors
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
it "lets you array-ize an empty string" do
|
72
72
|
f = Mutations::ArrayFilter.new(:arr, arrayize: true) { string }
|
73
|
-
|
73
|
+
|
74
74
|
filtered, errors = f.filter("")
|
75
75
|
assert_equal [], filtered
|
76
76
|
assert_nil errors
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
it "lets you pass integers in arrays" do
|
80
80
|
f = Mutations::ArrayFilter.new(:arr) { integer min: 4 }
|
81
|
-
|
81
|
+
|
82
82
|
filtered, errors = f.filter([5,6,1,"bob"])
|
83
83
|
assert_equal [5,6,1,"bob"], filtered
|
84
84
|
assert_equal [nil, nil, :min, :integer], errors.symbolic
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
|
+
it "lets you pass floats in arrays" do
|
88
|
+
f = Mutations::ArrayFilter.new(:float) { float min: 4.0 }
|
89
|
+
|
90
|
+
filtered, errors = f.filter([5.0,6.0,1.0,"bob"])
|
91
|
+
assert_equal [5.0,6.0,1.0,"bob"], filtered
|
92
|
+
assert_equal [nil, nil, :min, :float], errors.symbolic
|
93
|
+
end
|
94
|
+
|
87
95
|
it "lets you pass booleans in arrays" do
|
88
96
|
f = Mutations::ArrayFilter.new(:arr) { boolean }
|
89
|
-
|
97
|
+
|
90
98
|
filtered, errors = f.filter([true, false, "1"])
|
91
99
|
assert_equal [true, false, true], filtered
|
92
100
|
assert_equal nil, errors
|
93
101
|
end
|
94
|
-
|
102
|
+
|
95
103
|
it "lets you pass model in arrays" do
|
96
104
|
f = Mutations::ArrayFilter.new(:arr) { model :string }
|
97
|
-
|
105
|
+
|
98
106
|
filtered, errors = f.filter(["hey"])
|
99
107
|
assert_equal ["hey"], filtered
|
100
108
|
assert_equal nil, errors
|
101
109
|
end
|
102
|
-
|
110
|
+
|
103
111
|
it "lets you pass hashes in arrays" do
|
104
112
|
f = Mutations::ArrayFilter.new(:arr) do
|
105
113
|
hash do
|
@@ -107,44 +115,44 @@ describe "Mutations::ArrayFilter" do
|
|
107
115
|
string :foo
|
108
116
|
integer :bar
|
109
117
|
end
|
110
|
-
|
118
|
+
|
111
119
|
optional do
|
112
120
|
boolean :baz
|
113
121
|
end
|
114
122
|
end
|
115
123
|
end
|
116
|
-
|
124
|
+
|
117
125
|
filtered, errors = f.filter([{foo: "f", bar: 3, baz: true}, {foo: "f", bar: 3}, {foo: "f"}])
|
118
126
|
assert_equal [{:foo=>"f", :bar=>3, :baz=>true}, {:foo=>"f", :bar=>3}, {:foo=>"f"}], filtered
|
119
|
-
|
127
|
+
|
120
128
|
assert_equal nil, errors[0]
|
121
129
|
assert_equal nil, errors[1]
|
122
130
|
assert_equal ({"bar"=>:required}), errors[2].symbolic
|
123
131
|
end
|
124
|
-
|
132
|
+
|
125
133
|
it "lets you pass arrays of arrays" do
|
126
134
|
f = Mutations::ArrayFilter.new(:arr) do
|
127
135
|
array do
|
128
136
|
string
|
129
137
|
end
|
130
138
|
end
|
131
|
-
|
139
|
+
|
132
140
|
filtered, errors = f.filter([["h", "e"], ["l"], [], ["lo"]])
|
133
141
|
assert_equal filtered, [["h", "e"], ["l"], [], ["lo"]]
|
134
142
|
assert_equal nil, errors
|
135
143
|
end
|
136
|
-
|
144
|
+
|
137
145
|
it "handles errors for arrays of arrays" do
|
138
146
|
f = Mutations::ArrayFilter.new(:arr) do
|
139
147
|
array do
|
140
148
|
string
|
141
149
|
end
|
142
150
|
end
|
143
|
-
|
151
|
+
|
144
152
|
filtered, errors = f.filter([["h", "e", {}], ["l"], [], [""]])
|
145
153
|
assert_equal [[nil, nil, :string], nil, nil, [:empty]], errors.symbolic
|
146
154
|
assert_equal [[nil, nil, "Array[2] isn't a string"], nil, nil, ["Array[0] can't be blank"]], errors.message
|
147
155
|
assert_equal ["Array[2] isn't a string", "Array[0] can't be blank"], errors.message_list
|
148
156
|
end
|
149
|
-
|
157
|
+
|
150
158
|
end
|
data/spec/boolean_filter_spec.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations::BooleanFilter" do
|
4
|
-
|
4
|
+
|
5
5
|
it "allows booleans" do
|
6
6
|
f = Mutations::BooleanFilter.new
|
7
7
|
filtered, errors = f.filter(true)
|
8
8
|
assert_equal true, filtered
|
9
9
|
assert_equal nil, errors
|
10
|
-
|
10
|
+
|
11
11
|
filtered, errors = f.filter(false)
|
12
12
|
assert_equal false, filtered
|
13
13
|
assert_equal nil, errors
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "considers non-booleans to be invalid" do
|
17
17
|
f = Mutations::BooleanFilter.new
|
18
18
|
[[true], {a: "1"}, Object.new].each do |thing|
|
@@ -20,21 +20,21 @@ describe "Mutations::BooleanFilter" do
|
|
20
20
|
assert_equal :boolean, errors
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "considers nil to be invalid" do
|
25
25
|
f = Mutations::BooleanFilter.new(nils: false)
|
26
26
|
filtered, errors = f.filter(nil)
|
27
27
|
assert_equal nil, filtered
|
28
28
|
assert_equal :nils, errors
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "considers nil to be valid" do
|
32
32
|
f = Mutations::BooleanFilter.new(nils: true)
|
33
33
|
filtered, errors = f.filter(nil)
|
34
34
|
assert_equal nil, filtered
|
35
35
|
assert_equal nil, errors
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
it "considers certain strings to be valid booleans" do
|
39
39
|
f = Mutations::BooleanFilter.new
|
40
40
|
[["true", true], ["TRUE", true], ["TrUe", true], ["1", true], ["false", false], ["FALSE", false], ["FalSe", false], ["0", false], [0, false], [1, true]].each do |(str, v)|
|
@@ -43,7 +43,7 @@ describe "Mutations::BooleanFilter" do
|
|
43
43
|
assert_equal nil, errors
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it "considers other string to be invalid" do
|
48
48
|
f = Mutations::BooleanFilter.new
|
49
49
|
["", "truely", "2"].each do |str|
|
data/spec/command_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require_relative 'spec_helper'
|
|
2
2
|
require 'simple_command'
|
3
3
|
|
4
4
|
describe "Command" do
|
5
|
-
|
5
|
+
|
6
6
|
describe "SimpleCommand" do
|
7
7
|
it "should allow valid in put in" do
|
8
8
|
outcome = SimpleCommand.run(name: "John", email: "john@gmail.com", amount: 5)
|
@@ -11,166 +11,166 @@ describe "Command" do
|
|
11
11
|
assert_equal ({name: "John", email: "john@gmail.com", amount: 5}).stringify_keys, outcome.result
|
12
12
|
assert_equal nil, outcome.errors
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "should filter out spurious params" do
|
16
16
|
outcome = SimpleCommand.run(name: "John", email: "john@gmail.com", amount: 5, buggers: true)
|
17
|
-
|
17
|
+
|
18
18
|
assert outcome.success?
|
19
19
|
assert_equal ({name: "John", email: "john@gmail.com", amount: 5}).stringify_keys, outcome.result
|
20
20
|
assert_equal nil, outcome.errors
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
it "should discover errors in inputs" do
|
24
24
|
outcome = SimpleCommand.run(name: "JohnTooLong", email: "john@gmail.com")
|
25
|
-
|
25
|
+
|
26
26
|
assert !outcome.success?
|
27
27
|
assert_equal :max_length, outcome.errors.symbolic[:name]
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "shouldn't throw an exception with run!" do
|
31
31
|
result = SimpleCommand.run!(name: "John", email: "john@gmail.com", amount: 5)
|
32
32
|
assert_equal ({name: "John", email: "john@gmail.com", amount: 5}).stringify_keys, result
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should throw an exception with run!" do
|
36
36
|
assert_raises Mutations::ValidationException do
|
37
37
|
result = SimpleCommand.run!(name: "John", email: "john@gmail.com", amount: "bob")
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
it "should do standalone validation" do
|
42
42
|
outcome = SimpleCommand.validate(name: "JohnLong", email: "john@gmail.com")
|
43
43
|
assert outcome.success?
|
44
44
|
assert_nil outcome.result
|
45
45
|
assert_nil outcome.errors
|
46
|
-
|
46
|
+
|
47
47
|
outcome = SimpleCommand.validate(name: "JohnTooLong", email: "john@gmail.com")
|
48
48
|
assert !outcome.success?
|
49
49
|
assert_nil outcome.result
|
50
50
|
assert_equal :max_length, outcome.errors.symbolic[:name]
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
it "should merge multiple hashes" do
|
54
54
|
outcome = SimpleCommand.run({name: "John", email: "john@gmail.com"}, {email: "bob@jones.com", amount: 5})
|
55
|
-
|
55
|
+
|
56
56
|
assert outcome.success?
|
57
57
|
assert_equal ({name: "John", email: "bob@jones.com", amount: 5}).stringify_keys, outcome.result
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
it "should merge hashes indifferently" do
|
61
61
|
outcome = SimpleCommand.run({name: "John", email: "john@gmail.com"}, {"email" => "bob@jones.com", "amount" => 5})
|
62
|
-
|
62
|
+
|
63
63
|
assert outcome.success?
|
64
64
|
assert_equal ({name: "John", email: "bob@jones.com", amount: 5}).stringify_keys, outcome.result
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
it "shouldn't accept non-hashes" do
|
68
68
|
assert_raises ArgumentError do
|
69
69
|
outcome = SimpleCommand.run(nil)
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
assert_raises ArgumentError do
|
73
73
|
outcome = SimpleCommand.run(1)
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "should accept nothing at all" do
|
78
78
|
SimpleCommand.run # make sure nothing is raised
|
79
79
|
end
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
describe "EigenCommand" do
|
83
83
|
class EigenCommand < Mutations::Command
|
84
|
-
|
84
|
+
|
85
85
|
required { string :name }
|
86
86
|
optional { string :email }
|
87
|
-
|
87
|
+
|
88
88
|
def execute
|
89
89
|
{name: name, email: email}
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
it "should define getter methods on params" do
|
94
94
|
mutation = EigenCommand.run(name: "John", email: "john@gmail.com")
|
95
95
|
assert_equal ({name: "John", email: "john@gmail.com"}), mutation.result
|
96
96
|
end
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
99
|
describe "MutatatedCommand" do
|
100
100
|
class MutatatedCommand < Mutations::Command
|
101
|
-
|
101
|
+
|
102
102
|
required { string :name }
|
103
103
|
optional { string :email }
|
104
|
-
|
104
|
+
|
105
105
|
def execute
|
106
106
|
self.name, self.email = "bob", "bob@jones.com"
|
107
107
|
{name: inputs[:name], email: inputs[:email]}
|
108
108
|
end
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
it "should define setter methods on params" do
|
112
112
|
mutation = MutatatedCommand.run(name: "John", email: "john@gmail.com")
|
113
113
|
assert_equal ({name: "bob", email: "bob@jones.com"}), mutation.result
|
114
114
|
end
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
describe "ErrorfulCommand" do
|
118
118
|
class ErrorfulCommand < Mutations::Command
|
119
|
-
|
119
|
+
|
120
120
|
required { string :name }
|
121
121
|
optional { string :email }
|
122
|
-
|
122
|
+
|
123
123
|
def execute
|
124
124
|
add_error("bob", :is_a_bob)
|
125
|
-
|
125
|
+
|
126
126
|
1
|
127
127
|
end
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
it "should let you add errors" do
|
131
131
|
outcome = ErrorfulCommand.run(name: "John", email: "john@gmail.com")
|
132
|
-
|
132
|
+
|
133
133
|
assert !outcome.success?
|
134
134
|
assert_nil outcome.result
|
135
135
|
assert :is_a_bob, outcome.errors.symbolic[:bob]
|
136
136
|
end
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
describe "MultiErrorCommand" do
|
140
140
|
class ErrorfulCommand < Mutations::Command
|
141
|
-
|
141
|
+
|
142
142
|
required { string :name }
|
143
143
|
optional { string :email }
|
144
|
-
|
144
|
+
|
145
145
|
def execute
|
146
146
|
moar_errors = Mutations::ErrorHash.new
|
147
147
|
moar_errors[:bob] = Mutations::ErrorAtom.new(:bob, :is_short)
|
148
148
|
moar_errors[:sally] = Mutations::ErrorAtom.new(:sally, :is_fat)
|
149
|
-
|
149
|
+
|
150
150
|
merge_errors(moar_errors)
|
151
|
-
|
151
|
+
|
152
152
|
1
|
153
153
|
end
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
it "should let you merge errors" do
|
157
157
|
outcome = ErrorfulCommand.run(name: "John", email: "john@gmail.com")
|
158
|
-
|
158
|
+
|
159
159
|
assert !outcome.success?
|
160
160
|
assert_nil outcome.result
|
161
161
|
assert :is_short, outcome.errors.symbolic[:bob]
|
162
162
|
assert :is_fat, outcome.errors.symbolic[:sally]
|
163
163
|
end
|
164
164
|
end
|
165
|
-
|
165
|
+
|
166
166
|
describe "PresentCommand" do
|
167
167
|
class PresentCommand < Mutations::Command
|
168
|
-
|
168
|
+
|
169
169
|
optional do
|
170
170
|
string :email
|
171
171
|
string :name
|
172
172
|
end
|
173
|
-
|
173
|
+
|
174
174
|
def execute
|
175
175
|
if name_present? && email_present?
|
176
176
|
1
|
@@ -183,7 +183,7 @@ describe "Command" do
|
|
183
183
|
end
|
184
184
|
end
|
185
185
|
end
|
186
|
-
|
186
|
+
|
187
187
|
it "should handle *_present? methods" do
|
188
188
|
assert_equal 1, PresentCommand.run!(name: "John", email: "john@gmail.com")
|
189
189
|
assert_equal 2, PresentCommand.run!(email: "john@gmail.com")
|
@@ -191,5 +191,5 @@ describe "Command" do
|
|
191
191
|
assert_equal 4, PresentCommand.run!
|
192
192
|
end
|
193
193
|
end
|
194
|
-
|
194
|
+
|
195
195
|
end
|