mutations 0.5.12 → 0.6.0
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/{.travis → .travis.yml} +3 -0
- data/CHANGELOG.md +10 -2
- data/Gemfile +5 -2
- data/README.md +1 -0
- data/TODO +1 -3
- data/lib/mutations.rb +14 -1
- data/lib/mutations/additional_filter.rb +13 -0
- data/lib/mutations/array_filter.rb +12 -30
- data/lib/mutations/boolean_filter.rb +3 -3
- data/lib/mutations/command.rb +5 -4
- data/lib/mutations/date_filter.rb +52 -0
- data/lib/mutations/duck_filter.rb +4 -4
- data/lib/mutations/errors.rb +24 -20
- data/lib/mutations/exception.rb +1 -1
- data/lib/mutations/file_filter.rb +7 -7
- data/lib/mutations/float_filter.rb +5 -5
- data/lib/mutations/hash_filter.rb +12 -29
- data/lib/mutations/integer_filter.rb +5 -5
- data/lib/mutations/model_filter.rb +13 -8
- data/lib/mutations/outcome.rb +1 -1
- data/lib/mutations/string_filter.rb +10 -10
- data/lib/mutations/version.rb +2 -2
- data/mutations.gemspec +0 -2
- data/spec/additional_filter_spec.rb +76 -0
- data/spec/array_filter_spec.rb +25 -17
- data/spec/boolean_filter_spec.rb +5 -5
- data/spec/command_spec.rb +30 -30
- data/spec/date_filter_spec.rb +146 -0
- data/spec/default_spec.rb +10 -10
- data/spec/duck_filter_spec.rb +6 -6
- data/spec/errors_spec.rb +8 -9
- data/spec/file_filter_spec.rb +26 -22
- data/spec/float_filter_spec.rb +8 -8
- data/spec/hash_filter_spec.rb +31 -23
- data/spec/inheritance_spec.rb +9 -9
- data/spec/integer_filter_spec.rb +9 -9
- data/spec/model_filter_spec.rb +26 -6
- data/spec/mutations_spec.rb +1 -1
- data/spec/simple_command.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/string_filter_spec.rb +17 -17
- metadata +8 -4
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'stringio'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
describe "Mutations::DateFilter" do
|
6
|
+
it "takes a date object" do
|
7
|
+
date = Date.new
|
8
|
+
f = Mutations::DateFilter.new
|
9
|
+
filtered, errors = f.filter(date)
|
10
|
+
assert_equal date, filtered
|
11
|
+
assert_equal nil, errors
|
12
|
+
end
|
13
|
+
|
14
|
+
it "takes a DateTime object" do
|
15
|
+
date = DateTime.new
|
16
|
+
f = Mutations::DateFilter.new
|
17
|
+
filtered, errors = f.filter(date)
|
18
|
+
assert_equal date, filtered
|
19
|
+
assert_equal nil, errors
|
20
|
+
end
|
21
|
+
|
22
|
+
it "takes a Time object and coverts it to a date" do
|
23
|
+
time = Time.now
|
24
|
+
f = Mutations::DateFilter.new
|
25
|
+
filtered, errors = f.filter(time)
|
26
|
+
if time.respond_to?(:to_date) # 1.8.7 doesn't support to_date
|
27
|
+
assert_equal time.to_date, filtered
|
28
|
+
assert_equal nil, errors
|
29
|
+
else
|
30
|
+
assert_equal :date, errors
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "checks if the given date is after a certain date" do
|
35
|
+
date = Date.new(2005, 1, 1)
|
36
|
+
after_date = Date.new(2000, 1, 1)
|
37
|
+
f = Mutations::DateFilter.new(:after => after_date)
|
38
|
+
filtered, errors = f.filter(date)
|
39
|
+
|
40
|
+
assert_equal date, filtered
|
41
|
+
assert_equal nil, errors
|
42
|
+
end
|
43
|
+
|
44
|
+
it "gives errors when the given date is before the after date" do
|
45
|
+
date = Date.new(1995, 1, 1)
|
46
|
+
after_date = Date.new(2000, 1, 1)
|
47
|
+
f = Mutations::DateFilter.new(:after => after_date)
|
48
|
+
filtered, errors = f.filter(date)
|
49
|
+
|
50
|
+
assert_equal nil, filtered
|
51
|
+
assert_equal :after, errors
|
52
|
+
end
|
53
|
+
|
54
|
+
it "checks if the given date is before a certain date" do
|
55
|
+
date = Date.new(1995, 1, 1)
|
56
|
+
after_date = Date.new(2000, 1, 1)
|
57
|
+
f = Mutations::DateFilter.new(:before => after_date)
|
58
|
+
filtered, errors = f.filter(date)
|
59
|
+
|
60
|
+
assert_equal date, filtered
|
61
|
+
assert_equal nil, errors
|
62
|
+
end
|
63
|
+
|
64
|
+
it "gives errors when the given date is after the after before" do
|
65
|
+
date = Date.new(2005, 1, 1)
|
66
|
+
before_date = Date.new(2000, 1, 1)
|
67
|
+
f = Mutations::DateFilter.new(:before => before_date)
|
68
|
+
filtered, errors = f.filter(date)
|
69
|
+
|
70
|
+
assert_equal nil, filtered
|
71
|
+
assert_equal :before, errors
|
72
|
+
end
|
73
|
+
|
74
|
+
it "checks if the given date is in the given range" do
|
75
|
+
date = Date.new(2005, 1, 1)
|
76
|
+
after_date = Date.new(2000, 1, 1)
|
77
|
+
before_date = Date.new(2010, 1, 1)
|
78
|
+
f = Mutations::DateFilter.new(:after => after_date, :before => before_date)
|
79
|
+
filtered, errors = f.filter(date)
|
80
|
+
|
81
|
+
assert_equal date, filtered
|
82
|
+
assert_equal nil, errors
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should be able to parse a D-M-Y string to a date" do
|
86
|
+
date_string = "2-1-2000"
|
87
|
+
date = Date.new(2000, 1, 2)
|
88
|
+
f = Mutations::DateFilter.new
|
89
|
+
filtered, errors = f.filter(date_string)
|
90
|
+
|
91
|
+
assert_equal date, filtered
|
92
|
+
assert_equal nil, errors
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should be able to parse a Y-M-D string to a date" do
|
96
|
+
date_string = "2000-1-2"
|
97
|
+
date = Date.new(2000, 1, 2)
|
98
|
+
f = Mutations::DateFilter.new
|
99
|
+
filtered, errors = f.filter(date_string)
|
100
|
+
|
101
|
+
assert_equal date, filtered
|
102
|
+
assert_equal nil, errors
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should be able to handle date formatting" do
|
106
|
+
date_string = "2000-1-2"
|
107
|
+
date = Date.new(2000, 1, 2)
|
108
|
+
f = Mutations::DateFilter.new(:format => '%Y-%m-%d')
|
109
|
+
filtered, errors = f.filter(date_string)
|
110
|
+
|
111
|
+
assert_equal date, filtered
|
112
|
+
assert_equal nil, errors
|
113
|
+
|
114
|
+
date_string = "1, 2, 2000"
|
115
|
+
f = Mutations::DateFilter.new(:format => '%m, %d, %Y')
|
116
|
+
filtered, errors = f.filter(date_string)
|
117
|
+
|
118
|
+
assert_equal date, filtered
|
119
|
+
assert_equal nil, errors
|
120
|
+
end
|
121
|
+
|
122
|
+
it "considers nil to be invalid" do
|
123
|
+
f = Mutations::DateFilter.new
|
124
|
+
filtered, errors = f.filter(nil)
|
125
|
+
|
126
|
+
assert_equal nil, filtered
|
127
|
+
assert_equal :nils, errors
|
128
|
+
end
|
129
|
+
|
130
|
+
it "allows the use of nil when specified" do
|
131
|
+
f = Mutations::DateFilter.new(:nils => true)
|
132
|
+
filtered, errors = f.filter(nil)
|
133
|
+
|
134
|
+
assert_equal nil, filtered
|
135
|
+
assert_equal nil, errors
|
136
|
+
end
|
137
|
+
|
138
|
+
it "doesn't allow non-existing dates" do
|
139
|
+
date_string = "1, 20, 2013"
|
140
|
+
f = Mutations::DateFilter.new
|
141
|
+
filtered, errors = f.filter(date_string)
|
142
|
+
|
143
|
+
assert_equal nil, filtered
|
144
|
+
assert_equal :date, errors
|
145
|
+
end
|
146
|
+
end
|
data/spec/default_spec.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'simple_command'
|
3
3
|
|
4
4
|
describe 'Mutations - defaults' do
|
5
5
|
|
6
6
|
class DefaultCommand < Mutations::Command
|
7
7
|
required do
|
8
|
-
string :name, default
|
8
|
+
string :name, :default => "Bob Jones"
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def execute
|
12
12
|
inputs
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "should have a default if no value is passed" do
|
17
17
|
outcome = DefaultCommand.run
|
18
18
|
assert_equal true, outcome.success?
|
19
19
|
assert_equal ({"name" => "Bob Jones"}), outcome.result
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should have the passed value if a value is passed" do
|
23
|
-
outcome = DefaultCommand.run(name
|
23
|
+
outcome = DefaultCommand.run(:name => "Fred")
|
24
24
|
assert_equal true, outcome.success?
|
25
25
|
assert_equal ({"name" => "Fred"}), outcome.result
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "should be an error if nil is passed on a required field with a default" do
|
29
|
-
outcome = DefaultCommand.run(name
|
29
|
+
outcome = DefaultCommand.run(:name => nil)
|
30
30
|
assert_equal false, outcome.success?
|
31
31
|
end
|
32
|
-
|
33
|
-
end
|
32
|
+
|
33
|
+
end
|
data/spec/duck_filter_spec.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations::DuckFilter" do
|
4
4
|
|
5
5
|
it "allows objects that respond to a single specified method" do
|
6
|
-
f = Mutations::DuckFilter.new(methods
|
6
|
+
f = Mutations::DuckFilter.new(:methods => [:length])
|
7
7
|
filtered, errors = f.filter("test")
|
8
8
|
assert_equal "test", filtered
|
9
9
|
assert_equal nil, errors
|
@@ -14,7 +14,7 @@ describe "Mutations::DuckFilter" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "doesn't allow objects that respond to a single specified method" do
|
17
|
-
f = Mutations::DuckFilter.new(methods
|
17
|
+
f = Mutations::DuckFilter.new(:methods => [:length])
|
18
18
|
filtered, errors = f.filter(true)
|
19
19
|
assert_equal true, filtered
|
20
20
|
assert_equal :duck, errors
|
@@ -25,14 +25,14 @@ describe "Mutations::DuckFilter" do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "considers nil to be invalid" do
|
28
|
-
f = Mutations::DuckFilter.new(nils
|
28
|
+
f = Mutations::DuckFilter.new(:nils => false)
|
29
29
|
filtered, errors = f.filter(nil)
|
30
30
|
assert_equal nil, filtered
|
31
31
|
assert_equal :nils, errors
|
32
32
|
end
|
33
33
|
|
34
34
|
it "considers nil to be valid" do
|
35
|
-
f = Mutations::DuckFilter.new(nils
|
35
|
+
f = Mutations::DuckFilter.new(:nils => true)
|
36
36
|
filtered, errors = f.filter(nil)
|
37
37
|
assert_equal nil, filtered
|
38
38
|
assert_equal nil, errors
|
@@ -46,4 +46,4 @@ describe "Mutations::DuckFilter" do
|
|
46
46
|
assert_equal nil, errors
|
47
47
|
end
|
48
48
|
end
|
49
|
-
end
|
49
|
+
end
|
data/spec/errors_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations - errors" do
|
4
4
|
|
@@ -23,7 +23,7 @@ describe "Mutations - errors" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "returns an ErrorHash as the top level error object, and ErrorAtom's inside" do
|
26
|
-
o = GivesErrors.run(hash1
|
26
|
+
o = GivesErrors.run(:hash1 => 1, :arr1 => "bob")
|
27
27
|
|
28
28
|
assert !o.success?
|
29
29
|
assert o.errors.is_a?(Mutations::ErrorHash)
|
@@ -35,7 +35,7 @@ describe "Mutations - errors" do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it "returns an ErrorHash for nested hashes" do
|
38
|
-
o = GivesErrors.run(hash1
|
38
|
+
o = GivesErrors.run(:hash1 => {:bool1 => "poop"})
|
39
39
|
|
40
40
|
assert !o.success?
|
41
41
|
assert o.errors.is_a?(Mutations::ErrorHash)
|
@@ -45,7 +45,7 @@ describe "Mutations - errors" do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
it "returns an ErrorArray for errors in arrays" do
|
48
|
-
o = GivesErrors.run(str1
|
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)
|
@@ -62,7 +62,7 @@ describe "Mutations - errors" do
|
|
62
62
|
|
63
63
|
describe "Bunch o errors" do
|
64
64
|
before do
|
65
|
-
@outcome = GivesErrors.run(str1
|
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
|
@@ -84,10 +84,9 @@ describe "Mutations - errors" do
|
|
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
|
-
assert_equal expected, @outcome.errors.message_list
|
87
|
+
assert_equal expected.size, @outcome.errors.message_list.size
|
88
|
+
expected.each { |e| assert @outcome.errors.message_list.include?(e) }
|
88
89
|
end
|
89
90
|
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
end
|
92
|
+
end
|
data/spec/file_filter_spec.rb
CHANGED
@@ -1,21 +1,25 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'stringio'
|
3
3
|
require 'tempfile'
|
4
4
|
|
5
5
|
describe "Mutations::FileFilter" do
|
6
|
-
|
6
|
+
|
7
7
|
class UploadedStringIO < StringIO
|
8
8
|
attr_accessor :content_type, :original_filename
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
# NOTE: Ruby 1.8.7 doesn't have file.size. Mutations need file to respond to size.
|
12
|
+
# Therefore, in 1.8.7 File objects are invalid files.
|
13
|
+
if File.new("README.md").respond_to?(:size)
|
14
|
+
it "allows files - file class" do
|
15
|
+
file = File.new("README.md")
|
16
|
+
f = Mutations::FileFilter.new
|
17
|
+
filtered, errors = f.filter(file)
|
18
|
+
assert_equal file, filtered
|
19
|
+
assert_equal nil, errors
|
20
|
+
end
|
17
21
|
end
|
18
|
-
|
22
|
+
|
19
23
|
it "allows files - stringio class" do
|
20
24
|
file = StringIO.new("bob")
|
21
25
|
f = Mutations::FileFilter.new
|
@@ -23,12 +27,12 @@ describe "Mutations::FileFilter" do
|
|
23
27
|
assert_equal file, filtered
|
24
28
|
assert_equal nil, errors
|
25
29
|
end
|
26
|
-
|
30
|
+
|
27
31
|
it "allows files - tempfile" do
|
28
32
|
file = Tempfile.new("bob")
|
29
33
|
f = Mutations::FileFilter.new
|
30
34
|
filtered, errors = f.filter(file)
|
31
|
-
|
35
|
+
assert filtered.is_a?(Tempfile) # NOTE: 1.9.3 and 1.8.7 treat == and eql? differently
|
32
36
|
assert_equal nil, errors
|
33
37
|
end
|
34
38
|
|
@@ -37,21 +41,21 @@ describe "Mutations::FileFilter" do
|
|
37
41
|
filtered, errors = f.filter("string")
|
38
42
|
assert_equal "string", filtered
|
39
43
|
assert_equal :file, errors
|
40
|
-
|
44
|
+
|
41
45
|
filtered, errors = f.filter(12)
|
42
46
|
assert_equal 12, filtered
|
43
47
|
assert_equal :file, errors
|
44
48
|
end
|
45
49
|
|
46
50
|
it "considers nil to be invalid" do
|
47
|
-
f = Mutations::FileFilter.new(nils
|
51
|
+
f = Mutations::FileFilter.new(:nils => false)
|
48
52
|
filtered, errors = f.filter(nil)
|
49
53
|
assert_equal nil, filtered
|
50
54
|
assert_equal :nils, errors
|
51
55
|
end
|
52
56
|
|
53
57
|
it "considers nil to be valid" do
|
54
|
-
f = Mutations::FileFilter.new(nils
|
58
|
+
f = Mutations::FileFilter.new(:nils => true)
|
55
59
|
filtered, errors = f.filter(nil)
|
56
60
|
assert_equal nil, filtered
|
57
61
|
assert_equal nil, errors
|
@@ -59,33 +63,33 @@ describe "Mutations::FileFilter" do
|
|
59
63
|
|
60
64
|
it "should allow small files" do
|
61
65
|
file = StringIO.new("bob")
|
62
|
-
f = Mutations::FileFilter.new(size
|
66
|
+
f = Mutations::FileFilter.new(:size => 4)
|
63
67
|
filtered, errors = f.filter(file)
|
64
68
|
assert_equal file, filtered
|
65
69
|
assert_equal nil, errors
|
66
70
|
end
|
67
|
-
|
71
|
+
|
68
72
|
it "shouldn't allow big files" do
|
69
73
|
file = StringIO.new("bob")
|
70
|
-
f = Mutations::FileFilter.new(size
|
74
|
+
f = Mutations::FileFilter.new(:size => 2)
|
71
75
|
filtered, errors = f.filter(file)
|
72
76
|
assert_equal file, filtered
|
73
77
|
assert_equal :size, errors
|
74
78
|
end
|
75
|
-
|
79
|
+
|
76
80
|
it "should require extra methods if uploaded file: accept" do
|
77
81
|
file = UploadedStringIO.new("bob")
|
78
|
-
f = Mutations::FileFilter.new(upload
|
82
|
+
f = Mutations::FileFilter.new(:upload => true)
|
79
83
|
filtered, errors = f.filter(file)
|
80
84
|
assert_equal file, filtered
|
81
85
|
assert_equal nil, errors
|
82
86
|
end
|
83
|
-
|
87
|
+
|
84
88
|
it "should require extra methods if uploaded file: deny" do
|
85
89
|
file = StringIO.new("bob")
|
86
|
-
f = Mutations::FileFilter.new(upload
|
90
|
+
f = Mutations::FileFilter.new(:upload => true)
|
87
91
|
filtered, errors = f.filter(file)
|
88
92
|
assert_equal file, filtered
|
89
93
|
assert_equal :file, errors
|
90
94
|
end
|
91
|
-
end
|
95
|
+
end
|
data/spec/float_filter_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations::FloatFilter" do
|
4
4
|
|
@@ -53,45 +53,45 @@ describe "Mutations::FloatFilter" do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it "considers nil to be invalid" do
|
56
|
-
f = Mutations::FloatFilter.new(nils
|
56
|
+
f = Mutations::FloatFilter.new(:nils => false)
|
57
57
|
filtered, errors = f.filter(nil)
|
58
58
|
assert_equal nil, filtered
|
59
59
|
assert_equal :nils, errors
|
60
60
|
end
|
61
61
|
|
62
62
|
it "considers nil to be valid" do
|
63
|
-
f = Mutations::FloatFilter.new(nils
|
63
|
+
f = Mutations::FloatFilter.new(:nils => true)
|
64
64
|
filtered, errors = f.filter(nil)
|
65
65
|
assert_equal nil, filtered
|
66
66
|
assert_equal nil, errors
|
67
67
|
end
|
68
68
|
|
69
69
|
it "considers low numbers invalid" do
|
70
|
-
f = Mutations::FloatFilter.new(min
|
70
|
+
f = Mutations::FloatFilter.new(:min => 10)
|
71
71
|
filtered, errors = f.filter(3)
|
72
72
|
assert_equal 3, filtered
|
73
73
|
assert_equal :min, errors
|
74
74
|
end
|
75
75
|
|
76
76
|
it "considers low numbers valid" do
|
77
|
-
f = Mutations::FloatFilter.new(min
|
77
|
+
f = Mutations::FloatFilter.new(:min => 10)
|
78
78
|
filtered, errors = f.filter(31)
|
79
79
|
assert_equal 31, filtered
|
80
80
|
assert_equal nil, errors
|
81
81
|
end
|
82
82
|
|
83
83
|
it "considers high numbers invalid" do
|
84
|
-
f = Mutations::FloatFilter.new(max
|
84
|
+
f = Mutations::FloatFilter.new(:max => 10)
|
85
85
|
filtered, errors = f.filter(31)
|
86
86
|
assert_equal 31, filtered
|
87
87
|
assert_equal :max, errors
|
88
88
|
end
|
89
89
|
|
90
90
|
it "considers high numbers vaild" do
|
91
|
-
f = Mutations::FloatFilter.new(max
|
91
|
+
f = Mutations::FloatFilter.new(:max => 10)
|
92
92
|
filtered, errors = f.filter(3)
|
93
93
|
assert_equal 3, filtered
|
94
94
|
assert_equal nil, errors
|
95
95
|
end
|
96
96
|
|
97
|
-
end
|
97
|
+
end
|