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/spec/errors_spec.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe "Mutations - errors" do
4
-
4
+
5
5
  class GivesErrors < Mutations::Command
6
6
  required do
7
7
  string :str1
8
8
  string :str2, :in => %w(opt1 opt2 opt3)
9
9
  end
10
-
10
+
11
11
  optional do
12
12
  integer :int1
13
13
  hash :hash1 do
@@ -16,15 +16,15 @@ describe "Mutations - errors" do
16
16
  end
17
17
  array :arr1 do integer end
18
18
  end
19
-
19
+
20
20
  def execute
21
21
  inputs
22
22
  end
23
23
  end
24
-
24
+
25
25
  it "returns an ErrorHash as the top level error object, and ErrorAtom's inside" do
26
26
  o = GivesErrors.run(hash1: 1, arr1: "bob")
27
-
27
+
28
28
  assert !o.success?
29
29
  assert o.errors.is_a?(Mutations::ErrorHash)
30
30
  assert o.errors[:str1].is_a?(Mutations::ErrorAtom)
@@ -33,20 +33,20 @@ describe "Mutations - errors" do
33
33
  assert o.errors[:hash1].is_a?(Mutations::ErrorAtom)
34
34
  assert o.errors[:arr1].is_a?(Mutations::ErrorAtom)
35
35
  end
36
-
36
+
37
37
  it "returns an ErrorHash for nested hashes" do
38
38
  o = GivesErrors.run(hash1: {bool1: "poop"})
39
-
39
+
40
40
  assert !o.success?
41
41
  assert o.errors.is_a?(Mutations::ErrorHash)
42
42
  assert o.errors[:hash1].is_a?(Mutations::ErrorHash)
43
43
  assert o.errors[:hash1][:bool1].is_a?(Mutations::ErrorAtom)
44
44
  assert o.errors[:hash1][:bool2].is_a?(Mutations::ErrorAtom)
45
45
  end
46
-
46
+
47
47
  it "returns an ErrorArray for errors in arrays" do
48
48
  o = GivesErrors.run(str1: "a", str2: "opt1", arr1: ["bob", 1, "sally"])
49
-
49
+
50
50
  assert !o.success?
51
51
  assert o.errors.is_a?(Mutations::ErrorHash)
52
52
  assert o.errors[:arr1].is_a?(Mutations::ErrorArray)
@@ -54,17 +54,17 @@ describe "Mutations - errors" do
54
54
  assert_nil o.errors[:arr1][1]
55
55
  assert o.errors[:arr1][2].is_a?(Mutations::ErrorAtom)
56
56
  end
57
-
57
+
58
58
  it "titleizes keys" do
59
59
  atom = Mutations::ErrorAtom.new(:newsletter_subscription, :boolean)
60
60
  assert_equal "Newsletter Subscription isn't a boolean", atom.message
61
61
  end
62
-
62
+
63
63
  describe "Bunch o errors" do
64
- before do
64
+ before do
65
65
  @outcome = GivesErrors.run(str1: "", str2: "opt9", int1: "zero", hash1: {bool1: "bob"}, arr1: ["bob", 1, "sally"])
66
66
  end
67
-
67
+
68
68
  it "gives symbolic errors" do
69
69
  expected = {"str1"=>:empty,
70
70
  "str2"=>:in,
@@ -74,20 +74,20 @@ describe "Mutations - errors" do
74
74
 
75
75
  assert_equal expected, @outcome.errors.symbolic
76
76
  end
77
-
77
+
78
78
  it "gives messages" do
79
79
  expected = {"str1"=>"Str1 can't be blank", "str2"=>"Str2 isn't an option", "int1"=>"Int1 isn't an integer", "hash1"=>{"bool1"=>"Bool1 isn't a boolean", "bool2"=>"Bool2 is required"}, "arr1"=>["Arr1[0] isn't an integer", nil, "Arr1[2] isn't an integer"]}
80
80
 
81
81
  assert_equal expected, @outcome.errors.message
82
82
  end
83
-
83
+
84
84
  it "can flatten those messages" do
85
85
  expected = ["Str1 can't be blank", "Str2 isn't an option", "Int1 isn't an integer", "Bool1 isn't a boolean", "Bool2 is required", "Arr1[0] isn't an integer", "Arr1[2] isn't an integer"]
86
-
86
+
87
87
  assert_equal expected, @outcome.errors.message_list
88
88
  end
89
89
  end
90
-
91
-
92
-
90
+
91
+
92
+
93
93
  end
@@ -10,7 +10,7 @@ describe "Mutations::HashFilter" do
10
10
  assert_equal ({"foo" => "bar"}), filtered
11
11
  assert_equal nil, errors
12
12
  end
13
-
13
+
14
14
  it 'disallows non-hashes' do
15
15
  hf = Mutations::HashFilter.new do
16
16
  string :foo
@@ -18,7 +18,7 @@ describe "Mutations::HashFilter" do
18
18
  filtered, errors = hf.filter("bar")
19
19
  assert_equal :hash, errors
20
20
  end
21
-
21
+
22
22
  it "allows wildcards in hashes" do
23
23
  hf = Mutations::HashFilter.new do
24
24
  string :*
@@ -27,7 +27,16 @@ describe "Mutations::HashFilter" do
27
27
  assert_equal ({"foo" => "bar", "baz" => "ban"}), filtered
28
28
  assert_equal nil, errors
29
29
  end
30
-
30
+
31
+ it "allow floats in hashes" do
32
+ hf = Mutations::HashFilter.new do
33
+ float :foo
34
+ end
35
+ filtered, errors = hf.filter(foo: 3.14)
36
+ assert_equal ({"foo" => 3.14}), filtered
37
+ assert_equal nil, errors
38
+ end
39
+
31
40
  it "doesn't allow wildcards in hashes" do
32
41
  hf = Mutations::HashFilter.new do
33
42
  string :*
@@ -35,7 +44,7 @@ describe "Mutations::HashFilter" do
35
44
  filtered, errors = hf.filter(foo: [])
36
45
  assert_equal ({"foo" => :string}), errors.symbolic
37
46
  end
38
-
47
+
39
48
  it "allows a mix of specific keys and then wildcards" do
40
49
  hf = Mutations::HashFilter.new do
41
50
  string :foo
@@ -45,7 +54,7 @@ describe "Mutations::HashFilter" do
45
54
  assert_equal ({"foo" => "bar", "baz" => 4}), filtered
46
55
  assert_equal nil, errors
47
56
  end
48
-
57
+
49
58
  it "doesn't allow a mix of specific keys and then wildcards -- should raise errors appropriately" do
50
59
  hf = Mutations::HashFilter.new do
51
60
  string :foo
@@ -54,7 +63,7 @@ describe "Mutations::HashFilter" do
54
63
  filtered, errors = hf.filter(foo: "bar", baz: "poopin")
55
64
  assert_equal ({"baz" => :integer}), errors.symbolic
56
65
  end
57
-
66
+
58
67
  describe "optional params and nils" do
59
68
  it "bar is optional -- it works if not passed" do
60
69
  hf = Mutations::HashFilter.new do
@@ -65,12 +74,12 @@ describe "Mutations::HashFilter" do
65
74
  string :bar
66
75
  end
67
76
  end
68
-
77
+
69
78
  filtered, errors = hf.filter(foo: "bar")
70
79
  assert_equal ({"foo" => "bar"}), filtered
71
80
  assert_equal nil, errors
72
81
  end
73
-
82
+
74
83
  it "bar is optional -- it works if nil is passed" do
75
84
  hf = Mutations::HashFilter.new do
76
85
  required do
@@ -80,12 +89,12 @@ describe "Mutations::HashFilter" do
80
89
  string :bar
81
90
  end
82
91
  end
83
-
92
+
84
93
  filtered, errors = hf.filter(foo: "bar", bar: nil)
85
94
  assert_equal ({"foo" => "bar"}), filtered
86
95
  assert_equal nil, errors
87
96
  end
88
-
97
+
89
98
  it "bar is optional -- it works if nil is passed and nils are allowed" do
90
99
  hf = Mutations::HashFilter.new do
91
100
  required do
@@ -95,13 +104,13 @@ describe "Mutations::HashFilter" do
95
104
  string :bar, nils: true
96
105
  end
97
106
  end
98
-
107
+
99
108
  filtered, errors = hf.filter(foo: "bar", bar: nil)
100
109
  assert_equal ({"foo" => "bar", "bar" => nil}), filtered
101
110
  assert_equal nil, errors
102
111
  end
103
112
  end
104
-
113
+
105
114
  describe "optional params and empty values" do
106
115
  it "bar is optional -- discards empty" do
107
116
  hf = Mutations::HashFilter.new do
@@ -112,12 +121,12 @@ describe "Mutations::HashFilter" do
112
121
  string :bar, discard_empty: true
113
122
  end
114
123
  end
115
-
124
+
116
125
  filtered, errors = hf.filter(foo: "bar", bar: "")
117
126
  assert_equal ({"foo" => "bar"}), filtered
118
127
  assert_equal nil, errors
119
128
  end
120
-
129
+
121
130
  it "bar is optional -- errors if discard_empty is false and value is blank" do
122
131
  hf = Mutations::HashFilter.new do
123
132
  required do
@@ -127,11 +136,11 @@ describe "Mutations::HashFilter" do
127
136
  string :bar, discard_empty: false
128
137
  end
129
138
  end
130
-
139
+
131
140
  filtered, errors = hf.filter(foo: "bar", bar: "")
132
141
  assert_equal ({"bar" => :empty}), errors.symbolic
133
142
  end
134
-
143
+
135
144
  it "bar is optional -- discards empty -- now with wildcards" do
136
145
  hf = Mutations::HashFilter.new do
137
146
  required do
@@ -141,12 +150,12 @@ describe "Mutations::HashFilter" do
141
150
  string :*, discard_empty: true
142
151
  end
143
152
  end
144
-
153
+
145
154
  filtered, errors = hf.filter(foo: "bar", bar: "")
146
155
  assert_equal ({"foo" => "bar"}), filtered
147
156
  assert_equal nil, errors
148
157
  end
149
158
  end
150
-
151
-
159
+
160
+
152
161
  end
@@ -2,38 +2,38 @@ require_relative 'spec_helper'
2
2
  require 'simple_command'
3
3
 
4
4
  describe 'Mutations - inheritance' do
5
-
5
+
6
6
  class SimpleInherited < SimpleCommand
7
-
7
+
8
8
  required do
9
9
  integer :age
10
10
  end
11
-
11
+
12
12
  def execute
13
13
  @filtered_input
14
14
  end
15
15
  end
16
-
16
+
17
17
  it 'should filter with inherited command' do
18
18
  mutation = SimpleInherited.run(name: "bob", email: "jon@jones.com", age: 10, amount: 22)
19
19
  assert mutation.success?
20
20
  assert_equal HashWithIndifferentAccess.new(name: "bob", email: "jon@jones.com", age: 10, amount: 22), mutation.result
21
21
  end
22
-
22
+
23
23
  it 'should filter with original command' do
24
24
  mutation = SimpleCommand.run(name: "bob", email: "jon@jones.com", age: 10, amount: 22)
25
25
  assert mutation.success?
26
26
  assert_equal HashWithIndifferentAccess.new(name: "bob", email: "jon@jones.com", amount: 22), mutation.result
27
27
  end
28
-
28
+
29
29
  it 'shouldnt collide' do
30
30
  mutation = SimpleInherited.run(name: "bob", email: "jon@jones.com", age: 10, amount: 22)
31
31
  assert mutation.success?
32
32
  assert_equal HashWithIndifferentAccess.new(name: "bob", email: "jon@jones.com", age: 10, amount: 22), mutation.result
33
-
33
+
34
34
  mutation = SimpleCommand.run(name: "bob", email: "jon@jones.com", age: 10, amount: 22)
35
35
  assert mutation.success?
36
36
  assert_equal HashWithIndifferentAccess.new(name: "bob", email: "jon@jones.com", amount: 22), mutation.result
37
37
  end
38
-
38
+
39
39
  end
@@ -1,28 +1,28 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe "Mutations::IntegerFilter" do
4
-
4
+
5
5
  it "allows integers" do
6
6
  f = Mutations::IntegerFilter.new
7
7
  filtered, errors = f.filter(3)
8
8
  assert_equal 3, filtered
9
9
  assert_equal nil, errors
10
10
  end
11
-
11
+
12
12
  it "allows strings that start with a digit" do
13
13
  f = Mutations::IntegerFilter.new
14
14
  filtered, errors = f.filter("3")
15
15
  assert_equal 3, filtered
16
16
  assert_equal nil, errors
17
17
  end
18
-
18
+
19
19
  it "allows negative strings" do
20
20
  f = Mutations::IntegerFilter.new
21
21
  filtered, errors = f.filter("-3")
22
22
  assert_equal -3, filtered
23
23
  assert_equal nil, errors
24
24
  end
25
-
25
+
26
26
  it "doesnt't allow other strings, nor does it allow random objects or symbols" do
27
27
  f = Mutations::IntegerFilter.new
28
28
  ["zero","a1", {}, [], Object.new, :d].each do |thing|
@@ -30,47 +30,47 @@ describe "Mutations::IntegerFilter" do
30
30
  assert_equal :integer, errors
31
31
  end
32
32
  end
33
-
33
+
34
34
  it "considers nil to be invalid" do
35
35
  f = Mutations::IntegerFilter.new(nils: false)
36
36
  filtered, errors = f.filter(nil)
37
37
  assert_equal nil, filtered
38
38
  assert_equal :nils, errors
39
39
  end
40
-
40
+
41
41
  it "considers nil to be valid" do
42
42
  f = Mutations::IntegerFilter.new(nils: true)
43
43
  filtered, errors = f.filter(nil)
44
44
  assert_equal nil, filtered
45
45
  assert_equal nil, errors
46
46
  end
47
-
47
+
48
48
  it "considers low numbers invalid" do
49
49
  f = Mutations::IntegerFilter.new(min: 10)
50
50
  filtered, errors = f.filter(3)
51
51
  assert_equal 3, filtered
52
52
  assert_equal :min, errors
53
53
  end
54
-
54
+
55
55
  it "considers low numbers valid" do
56
56
  f = Mutations::IntegerFilter.new(min: 10)
57
57
  filtered, errors = f.filter(31)
58
58
  assert_equal 31, filtered
59
59
  assert_equal nil, errors
60
60
  end
61
-
61
+
62
62
  it "considers high numbers invalid" do
63
63
  f = Mutations::IntegerFilter.new(max: 10)
64
64
  filtered, errors = f.filter(31)
65
65
  assert_equal 31, filtered
66
66
  assert_equal :max, errors
67
67
  end
68
-
68
+
69
69
  it "considers high numbers vaild" do
70
70
  f = Mutations::IntegerFilter.new(max: 10)
71
71
  filtered, errors = f.filter(3)
72
72
  assert_equal 3, filtered
73
73
  assert_equal nil, errors
74
74
  end
75
-
75
+
76
76
  end
@@ -1,21 +1,21 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe "Mutations::ModelFilter" do
4
-
4
+
5
5
  class SimpleModel; end
6
6
  class AlwaysNew
7
7
  def new_record?
8
8
  true
9
9
  end
10
10
  end
11
-
11
+
12
12
  class AlwaysSaved
13
13
  def new_record?
14
14
  false
15
15
  end
16
16
  end
17
-
18
-
17
+
18
+
19
19
  it "allows models" do
20
20
  f = Mutations::ModelFilter.new(:simple_model)
21
21
  m = SimpleModel.new
@@ -23,70 +23,70 @@ describe "Mutations::ModelFilter" do
23
23
  assert_equal m, filtered
24
24
  assert_equal nil, errors
25
25
  end
26
-
26
+
27
27
  # it "disallows different types of models" do
28
28
  # end
29
-
29
+
30
30
  it "raises an exception during initialization if constantization fails" do
31
31
  assert_raises NameError do
32
32
  Mutations::ModelFilter.new(:complex_model)
33
33
  end
34
34
  end
35
-
35
+
36
36
  it "raises an exception during initialization if constantization of class fails" do
37
37
  assert_raises NameError do
38
38
  Mutations::ModelFilter.new(:simple_model, class: "ComplexModel")
39
39
  end
40
40
  end
41
-
41
+
42
42
  it "raises an exception during initialization if constantization of builder fails" do
43
43
  assert_raises NameError do
44
44
  Mutations::ModelFilter.new(:simple_model, builder: "ComplexModel")
45
45
  end
46
46
  end
47
-
47
+
48
48
  it "considers nil to be invalid" do
49
49
  f = Mutations::ModelFilter.new(:simple_model, nils: false)
50
50
  filtered, errors = f.filter(nil)
51
51
  assert_equal nil, filtered
52
52
  assert_equal :nils, errors
53
53
  end
54
-
54
+
55
55
  it "considers nil to be valid" do
56
56
  f = Mutations::ModelFilter.new(:simple_model, nils: true)
57
57
  filtered, errors = f.filter(nil)
58
58
  assert_equal nil, filtered
59
59
  assert_equal nil, errors
60
60
  end
61
-
61
+
62
62
  # it "allows you to override class with a constant and succeed" do
63
63
  # end
64
- #
64
+ #
65
65
  # it "allows you to override class with a string and succeed" do
66
66
  # end
67
- #
67
+ #
68
68
  # it "allows you to override class and fail" do
69
69
  # end
70
- #
70
+ #
71
71
  # it "allows anything if new_record is true" do
72
72
  # end
73
- #
73
+ #
74
74
  # it "disallows new_records if new_record is false" do
75
75
  # end
76
- #
76
+ #
77
77
  # it "allows saved records if new_record is false" do
78
78
  # end
79
- #
79
+ #
80
80
  # it "allows other records if new_record is false" do
81
81
  # end
82
- #
82
+ #
83
83
  # it "allows you to build a record from a hash, and succeed" do
84
84
  # end
85
- #
85
+ #
86
86
  # it "allows you to build a record from a hash, and fail" do
87
87
  # end
88
- #
88
+ #
89
89
  # it "makes sure that if you build a record from a hash, it still has to be of the right class" do
90
90
  # end
91
-
91
+
92
92
  end
@@ -1,9 +1,9 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe 'Mutations' do
4
-
4
+
5
5
  it 'should have a version' do
6
6
  assert Mutations::VERSION.is_a?(String)
7
7
  end
8
-
8
+
9
9
  end
@@ -1,14 +1,14 @@
1
1
  class SimpleCommand < Mutations::Command
2
-
2
+
3
3
  required do
4
4
  string :name, max_length: 10
5
5
  string :email
6
6
  end
7
-
7
+
8
8
  optional do
9
9
  integer :amount
10
10
  end
11
-
11
+
12
12
  def execute
13
13
  inputs
14
14
  end
@@ -8,21 +8,21 @@ describe "Mutations::StringFilter" do
8
8
  assert_equal "hello", filtered
9
9
  assert_equal nil, errors
10
10
  end
11
-
11
+
12
12
  it "allows symbols" do
13
13
  sf = Mutations::StringFilter.new
14
14
  filtered, errors = sf.filter(:hello)
15
15
  assert_equal "hello", filtered
16
16
  assert_equal nil, errors
17
17
  end
18
-
18
+
19
19
  it "allows fixnums" do
20
20
  sf = Mutations::StringFilter.new
21
21
  filtered, errors = sf.filter(1)
22
22
  assert_equal "1", filtered
23
23
  assert_equal nil, errors
24
24
  end
25
-
25
+
26
26
  it "disallows non-string" do
27
27
  sf = Mutations::StringFilter.new
28
28
  [["foo"], {a: "1"}, Object.new].each do |thing|
@@ -30,105 +30,105 @@ describe "Mutations::StringFilter" do
30
30
  assert_equal :string, errors
31
31
  end
32
32
  end
33
-
33
+
34
34
  it "strips" do
35
35
  sf = Mutations::StringFilter.new(strip: true)
36
36
  filtered, errors = sf.filter(" hello ")
37
37
  assert_equal "hello", filtered
38
38
  assert_equal nil, errors
39
39
  end
40
-
40
+
41
41
  it "doesn't strip" do
42
42
  sf = Mutations::StringFilter.new(strip: false)
43
43
  filtered, errors = sf.filter(" hello ")
44
44
  assert_equal " hello ", filtered
45
45
  assert_equal nil, errors
46
46
  end
47
-
47
+
48
48
  it "considers nil to be invalid" do
49
49
  sf = Mutations::StringFilter.new(nils: false)
50
50
  filtered, errors = sf.filter(nil)
51
51
  assert_equal nil, filtered
52
52
  assert_equal :nils, errors
53
53
  end
54
-
54
+
55
55
  it "considers nil to be valid" do
56
56
  sf = Mutations::StringFilter.new(nils: true)
57
57
  filtered, errors = sf.filter(nil)
58
58
  assert_equal nil, filtered
59
59
  assert_equal nil, errors
60
60
  end
61
-
61
+
62
62
  it "considers empty strings to be invalid" do
63
63
  sf = Mutations::StringFilter.new(empty: false)
64
64
  filtered, errors = sf.filter("")
65
65
  assert_equal "", filtered
66
66
  assert_equal :empty, errors
67
67
  end
68
-
68
+
69
69
  it "considers empty strings to be valid" do
70
70
  sf = Mutations::StringFilter.new(empty: true)
71
71
  filtered, errors = sf.filter("")
72
72
  assert_equal "", filtered
73
73
  assert_equal nil, errors
74
74
  end
75
-
75
+
76
76
  it "considers stripped strings that are empty to be invalid" do
77
77
  sf = Mutations::StringFilter.new(empty: false)
78
78
  filtered, errors = sf.filter(" ")
79
79
  assert_equal "", filtered
80
80
  assert_equal :empty, errors
81
81
  end
82
-
82
+
83
83
  it "considers long strings to be invalid" do
84
84
  sf = Mutations::StringFilter.new(max_length: 5)
85
85
  filtered, errors = sf.filter("123456")
86
86
  assert_equal "123456", filtered
87
87
  assert_equal :max_length, errors
88
88
  end
89
-
89
+
90
90
  it "considers long strings to be valid" do
91
91
  sf = Mutations::StringFilter.new(max_length: 5)
92
92
  filtered, errors = sf.filter("12345")
93
93
  assert_equal "12345", filtered
94
94
  assert_equal nil, errors
95
95
  end
96
-
96
+
97
97
  it "considers short strings to be invalid" do
98
98
  sf = Mutations::StringFilter.new(min_length: 5)
99
99
  filtered, errors = sf.filter("1234")
100
100
  assert_equal "1234", filtered
101
101
  assert_equal :min_length, errors
102
102
  end
103
-
103
+
104
104
  it "considers short strings to be valid" do
105
105
  sf = Mutations::StringFilter.new(min_length: 5)
106
106
  filtered, errors = sf.filter("12345")
107
107
  assert_equal "12345", filtered
108
108
  assert_equal nil, errors
109
109
  end
110
-
110
+
111
111
  it "considers bad matches to be invalid" do
112
112
  sf = Mutations::StringFilter.new(matches: /aaa/)
113
113
  filtered, errors = sf.filter("aab")
114
114
  assert_equal "aab", filtered
115
115
  assert_equal :matches, errors
116
116
  end
117
-
117
+
118
118
  it "considers good matches to be valid" do
119
119
  sf = Mutations::StringFilter.new(matches: /aaa/)
120
120
  filtered, errors = sf.filter("baaab")
121
121
  assert_equal "baaab", filtered
122
122
  assert_equal nil, errors
123
123
  end
124
-
124
+
125
125
  it "considers non-inclusion to be invalid" do
126
126
  sf = Mutations::StringFilter.new(in: %w(red blue green))
127
127
  filtered, errors = sf.filter("orange")
128
128
  assert_equal "orange", filtered
129
129
  assert_equal :in, errors
130
130
  end
131
-
131
+
132
132
  it "considers inclusion to be valid" do
133
133
  sf = Mutations::StringFilter.new(in: %w(red blue green))
134
134
  filtered, errors = sf.filter("red")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.5.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: