ohm-contrib 0.0.31 → 0.0.33
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/LICENSE +2 -1
- data/README.markdown +2 -1
- data/Rakefile +4 -53
- data/lib/ohm/contrib.rb +2 -2
- data/lib/ohm/contrib/boundaries.rb +1 -0
- data/lib/ohm/contrib/callbacks.rb +5 -4
- data/lib/ohm/contrib/date_validations.rb +2 -1
- data/lib/ohm/contrib/extra_validations.rb +1 -0
- data/lib/ohm/contrib/length_validations.rb +1 -0
- data/lib/ohm/contrib/locking.rb +2 -1
- data/lib/ohm/contrib/lunar_macros.rb +5 -4
- data/lib/ohm/contrib/number_validations.rb +1 -0
- data/lib/ohm/contrib/slug.rb +1 -0
- data/lib/ohm/contrib/timestamping.rb +1 -0
- data/lib/ohm/contrib/typecast.rb +1 -0
- data/lib/ohm/contrib/web_validations.rb +2 -1
- data/test/autoload_test.rb +19 -0
- data/test/boundaries_test.rb +45 -0
- data/test/callbacks_lint.rb +105 -0
- data/test/date_validations_test.rb +29 -0
- data/test/helper.rb +19 -10
- data/test/instance_callbacks_test.rb +45 -0
- data/test/length_validations_test.rb +31 -0
- data/test/lunar_macros_test.rb +146 -0
- data/test/macro_callbacks_test.rb +57 -0
- data/test/membership_validation_test.rb +31 -0
- data/test/number_validations_test.rb +37 -0
- data/test/slug_test.rb +30 -0
- data/test/timestamping_test.rb +32 -0
- data/test/typecast_array_test.rb +154 -0
- data/test/typecast_boolean_test.rb +43 -0
- data/test/typecast_date_test.rb +77 -0
- data/test/typecast_decimal_test.rb +77 -0
- data/test/typecast_float_test.rb +52 -0
- data/test/typecast_hash_test.rb +117 -0
- data/test/typecast_integer_test.rb +66 -0
- data/test/typecast_string_test.rb +38 -0
- data/test/typecast_time_test.rb +67 -0
- data/test/typecast_timezone_test.rb +37 -0
- data/test/web_validations_test.rb +79 -0
- metadata +47 -56
- data/.document +0 -5
- data/.gitignore +0 -25
- data/VERSION +0 -1
- data/lib/ohm/contrib/to_hash.rb +0 -39
- data/ohm-contrib.gemspec +0 -103
- data/test/test_ohm_boundaries.rb +0 -74
- data/test/test_ohm_contrib.rb +0 -70
- data/test/test_ohm_contrib_callbacks.rb +0 -375
- data/test/test_ohm_date_validations.rb +0 -30
- data/test/test_ohm_extra_validations.rb +0 -32
- data/test/test_ohm_length_validations.rb +0 -32
- data/test/test_ohm_lunar_macros.rb +0 -165
- data/test/test_ohm_number_validations.rb +0 -59
- data/test/test_ohm_slug.rb +0 -29
- data/test/test_ohm_timestamping.rb +0 -64
- data/test/test_ohm_to_hash.rb +0 -67
- data/test/test_ohm_typecast.rb +0 -707
- data/test/test_ohm_web_validations.rb +0 -114
@@ -1,30 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class DateValidationsTest < Test::Unit::TestCase
|
4
|
-
describe "date validation with a normal string column" do
|
5
|
-
class Person < Ohm::Model
|
6
|
-
include Ohm::DateValidations
|
7
|
-
|
8
|
-
attribute :birthday
|
9
|
-
|
10
|
-
def validate
|
11
|
-
assert_date :birthday
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
test "accepts all canonical dates" do
|
16
|
-
assert Person.new(:birthday => "2010-05-05").valid?
|
17
|
-
assert Person.new(:birthday => "2010-5-5").valid?
|
18
|
-
assert Person.new(:birthday => "2010-05-5").valid?
|
19
|
-
assert Person.new(:birthday => "2010-5-05").valid?
|
20
|
-
end
|
21
|
-
|
22
|
-
test "also catches invalid dates" do
|
23
|
-
assert ! Person.new(:birthday => "2010-02-29").valid?
|
24
|
-
end
|
25
|
-
|
26
|
-
test "invalid when empty" do
|
27
|
-
assert ! Person.new(:birthday => "").valid?
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class OhmExtraValidationsTest < Test::Unit::TestCase
|
4
|
-
context "membership assertion" do
|
5
|
-
class Order < Ohm::Model
|
6
|
-
include Ohm::ExtraValidations
|
7
|
-
|
8
|
-
attribute :state
|
9
|
-
|
10
|
-
def validate
|
11
|
-
assert_member :state, %w(pending authorized declined)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
should "be successful given all the members" do
|
16
|
-
order = Order.new(:state => 'pending')
|
17
|
-
assert order.valid?
|
18
|
-
|
19
|
-
order = Order.new(:state => 'authorized')
|
20
|
-
assert order.valid?
|
21
|
-
|
22
|
-
order = Order.new(:state => 'declined')
|
23
|
-
assert order.valid?
|
24
|
-
end
|
25
|
-
|
26
|
-
should "fail on a non-member" do
|
27
|
-
order = Order.new(:state => 'foobar')
|
28
|
-
assert ! order.valid?
|
29
|
-
assert_equal [[:state, :not_member]], order.errors
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class LengthValidationsTest < Test::Unit::TestCase
|
4
|
-
describe "length validation with a normal string column" do
|
5
|
-
class Person < Ohm::Model
|
6
|
-
include Ohm::LengthValidations
|
7
|
-
|
8
|
-
attribute :name
|
9
|
-
|
10
|
-
def validate
|
11
|
-
assert_min_length :name, 5
|
12
|
-
assert_max_length :name, 10
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
test "valid length name" do
|
17
|
-
assert Person.new(:name => "bob smith").valid?
|
18
|
-
end
|
19
|
-
|
20
|
-
test "also catches short strings" do
|
21
|
-
assert ! Person.new(:name => "bob").valid?
|
22
|
-
end
|
23
|
-
|
24
|
-
test "also catches long strings" do
|
25
|
-
assert ! Person.new(:name => "robert smithson").valid?
|
26
|
-
end
|
27
|
-
|
28
|
-
test "invalid when empty" do
|
29
|
-
assert ! Person.new(:name => "").valid?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,165 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class LunarMacrosTest < Test::Unit::TestCase
|
4
|
-
setup do
|
5
|
-
Ohm.flush
|
6
|
-
end
|
7
|
-
|
8
|
-
class Post < Ohm::Model
|
9
|
-
include Ohm::LunarMacros
|
10
|
-
end
|
11
|
-
|
12
|
-
test "fuzzy / text / number / sortable" do
|
13
|
-
Post.fuzzy :name
|
14
|
-
assert_equal [:name], Post.fuzzy
|
15
|
-
|
16
|
-
Post.text :name
|
17
|
-
assert_equal [:name], Post.text
|
18
|
-
|
19
|
-
Post.number :name
|
20
|
-
assert_equal [:name], Post.number
|
21
|
-
|
22
|
-
Post.sortable :name
|
23
|
-
assert_equal [:name], Post.sortable
|
24
|
-
end
|
25
|
-
|
26
|
-
test "fuzzy / text / number / sortable done twice" do
|
27
|
-
Post.fuzzy :name
|
28
|
-
Post.fuzzy :name
|
29
|
-
assert_equal [:name], Post.fuzzy
|
30
|
-
|
31
|
-
Post.text :name
|
32
|
-
Post.text :name
|
33
|
-
assert_equal [:name], Post.text
|
34
|
-
|
35
|
-
Post.number :name
|
36
|
-
Post.number :name
|
37
|
-
assert_equal [:name], Post.number
|
38
|
-
|
39
|
-
Post.sortable :name
|
40
|
-
Post.sortable :name
|
41
|
-
assert_equal [:name], Post.sortable
|
42
|
-
end
|
43
|
-
|
44
|
-
context "on create" do
|
45
|
-
class Document < Ohm::Model
|
46
|
-
include Ohm::LunarMacros
|
47
|
-
|
48
|
-
fuzzy :filename
|
49
|
-
text :content
|
50
|
-
number :author_id
|
51
|
-
sortable :views
|
52
|
-
|
53
|
-
def filename() "Filename" end
|
54
|
-
def content() "Content" end
|
55
|
-
def author_id() 100 end
|
56
|
-
def views() 15 end
|
57
|
-
end
|
58
|
-
|
59
|
-
test "indexes filename" do
|
60
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
61
|
-
Lunar.expects(:index).with(Document).yields(doc)
|
62
|
-
|
63
|
-
doc.expects(:id).with('1')
|
64
|
-
doc.expects(:fuzzy).with(:filename, 'Filename')
|
65
|
-
doc = Document.create
|
66
|
-
end
|
67
|
-
|
68
|
-
test "indexes content" do
|
69
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
70
|
-
Lunar.expects(:index).with(Document).yields(doc)
|
71
|
-
|
72
|
-
doc.expects(:id).with('1')
|
73
|
-
doc.expects(:text).with(:content, 'Content')
|
74
|
-
doc = Document.create
|
75
|
-
end
|
76
|
-
|
77
|
-
test "indexes number" do
|
78
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
79
|
-
Lunar.expects(:index).with(Document).yields(doc)
|
80
|
-
|
81
|
-
doc.expects(:id).with('1')
|
82
|
-
doc.expects(:number).with(:author_id, 100)
|
83
|
-
doc = Document.create
|
84
|
-
end
|
85
|
-
|
86
|
-
test "indexes sortable" do
|
87
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
88
|
-
Lunar.expects(:index).with(Document).yields(doc)
|
89
|
-
|
90
|
-
doc.expects(:id).with('1')
|
91
|
-
doc.expects(:sortable).with(:views, 15)
|
92
|
-
doc = Document.create
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "on update" do
|
97
|
-
class Document < Ohm::Model
|
98
|
-
include Ohm::LunarMacros
|
99
|
-
|
100
|
-
fuzzy :filename
|
101
|
-
text :content
|
102
|
-
number :author_id
|
103
|
-
sortable :views
|
104
|
-
|
105
|
-
def filename() "Filename" end
|
106
|
-
def content() "Content" end
|
107
|
-
def author_id() 100 end
|
108
|
-
def views() 15 end
|
109
|
-
end
|
110
|
-
|
111
|
-
test "indexes filename" do
|
112
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
113
|
-
Lunar.expects(:index).times(2).with(Document).yields(doc)
|
114
|
-
|
115
|
-
doc.expects(:id).times(2).with('1')
|
116
|
-
doc.expects(:fuzzy).times(2).with(:filename, 'Filename')
|
117
|
-
doc = Document.create
|
118
|
-
doc.save
|
119
|
-
end
|
120
|
-
|
121
|
-
test "indexes content" do
|
122
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
123
|
-
Lunar.expects(:index).times(2).with(Document).yields(doc)
|
124
|
-
|
125
|
-
doc.expects(:id).times(2).with('1')
|
126
|
-
doc.expects(:text).times(2).with(:content, 'Content')
|
127
|
-
doc = Document.create
|
128
|
-
doc.save
|
129
|
-
end
|
130
|
-
|
131
|
-
test "indexes number" do
|
132
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
133
|
-
Lunar.expects(:index).times(2).with(Document).yields(doc)
|
134
|
-
|
135
|
-
doc.expects(:id).times(2).with('1')
|
136
|
-
doc.expects(:number).times(2).with(:author_id, 100)
|
137
|
-
doc = Document.create
|
138
|
-
doc.save
|
139
|
-
end
|
140
|
-
|
141
|
-
test "indexes sortable" do
|
142
|
-
doc = stub("Doc", fuzzy: nil, text: nil, number: nil, sortable: nil)
|
143
|
-
Lunar.expects(:index).times(2).with(Document).yields(doc)
|
144
|
-
|
145
|
-
doc.expects(:id).times(2).with('1')
|
146
|
-
doc.expects(:sortable).times(2).with(:views, 15)
|
147
|
-
doc = Document.create
|
148
|
-
doc.save
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
context "on delete" do
|
153
|
-
test "executes Lunar.delete" do
|
154
|
-
doc = stub("Doc", id: nil, fuzzy: nil, text: nil, number: nil,
|
155
|
-
sortable: nil)
|
156
|
-
|
157
|
-
Lunar.expects(:index).with(Document).yields(doc)
|
158
|
-
|
159
|
-
doc = Document.create
|
160
|
-
|
161
|
-
Lunar.expects(:delete).with(Document, '1')
|
162
|
-
doc.delete
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestOhmNumberValidations < Test::Unit::TestCase
|
4
|
-
class Product < Ohm::Model
|
5
|
-
include Ohm::NumberValidations
|
6
|
-
|
7
|
-
attribute :price
|
8
|
-
attribute :optional_price
|
9
|
-
|
10
|
-
def validate
|
11
|
-
assert_decimal :price
|
12
|
-
assert_decimal :optional_price unless optional_price.to_s.empty?
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
context "given no price" do
|
17
|
-
should "still validate as :not_decimal" do
|
18
|
-
product = Product.new(:price => nil)
|
19
|
-
product.valid?
|
20
|
-
|
21
|
-
assert_equal [[:price, :not_decimal]], product.errors
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "given a 0.10 value" do
|
26
|
-
should "validate as a decimal" do
|
27
|
-
product = Product.new(:price => 0.10)
|
28
|
-
|
29
|
-
assert product.valid?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "given 1 as a value" do
|
34
|
-
should "validate as a decimal" do
|
35
|
-
product = Product.new(:price => 1)
|
36
|
-
|
37
|
-
assert product.valid?
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context "given 1 dot as a value" do
|
42
|
-
should "not validate as a decimal" do
|
43
|
-
product = Product.new(:price => "1.")
|
44
|
-
|
45
|
-
assert ! product.valid?
|
46
|
-
assert_equal [[:price, :not_decimal]], product.errors
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context "given no value for optional price" do
|
51
|
-
should "have no validation errors" do
|
52
|
-
product = Product.new(:price => 10.1, :optional_price => nil)
|
53
|
-
assert product.valid?
|
54
|
-
|
55
|
-
product = Product.new(:price => 10.1, :optional_price => '')
|
56
|
-
assert product.valid?
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
data/test/test_ohm_slug.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class OhmSlugTest < Test::Unit::TestCase
|
4
|
-
module Finder
|
5
|
-
def [](id)
|
6
|
-
new(id)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class BaseWithoutToParam < Struct.new(:id)
|
11
|
-
extend Finder
|
12
|
-
|
13
|
-
include Ohm::Slug
|
14
|
-
|
15
|
-
def to_s
|
16
|
-
"A very Unique and Interesting String?'"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
test "to_param" do
|
21
|
-
obj = BaseWithoutToParam.new(1)
|
22
|
-
|
23
|
-
assert_equal '1-a-very-unique-and-interesting-string', obj.to_param
|
24
|
-
end
|
25
|
-
|
26
|
-
test "finding" do
|
27
|
-
assert_equal 1, BaseWithoutToParam['1-a-very-unique'].id
|
28
|
-
end
|
29
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestOhmTimestamping < Test::Unit::TestCase
|
4
|
-
setup do
|
5
|
-
Ohm.flush
|
6
|
-
end
|
7
|
-
|
8
|
-
class Person < Ohm::Model
|
9
|
-
include Ohm::Timestamping
|
10
|
-
end
|
11
|
-
|
12
|
-
context "a new? record" do
|
13
|
-
should "have no created_at" do
|
14
|
-
assert_nil Person.new.created_at
|
15
|
-
end
|
16
|
-
|
17
|
-
should "have no updated_at" do
|
18
|
-
assert_nil Person.new.updated_at
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "on create" do
|
23
|
-
setup do
|
24
|
-
@now = Time.utc(2010, 5, 12)
|
25
|
-
Timecop.freeze(@now)
|
26
|
-
@person = Person.create
|
27
|
-
@person = Person[@person.id]
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
should "set the created_at equal to the current time" do
|
32
|
-
assert_equal @now.to_s, @person.created_at
|
33
|
-
end
|
34
|
-
|
35
|
-
should "also set the updated_at equal to the current time" do
|
36
|
-
assert_equal @now.to_s, @person.updated_at
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "on update" do
|
41
|
-
setup do
|
42
|
-
Timecop.freeze(Time.utc(2010, 10, 30))
|
43
|
-
|
44
|
-
@person = Person.create
|
45
|
-
@old_created_at = @person.created_at.to_s
|
46
|
-
@old_updated_at = @person.updated_at.to_s
|
47
|
-
|
48
|
-
@now = Time.utc(2010, 10, 31)
|
49
|
-
Timecop.freeze(@now)
|
50
|
-
|
51
|
-
@person.save
|
52
|
-
@person = Person[@person.id]
|
53
|
-
end
|
54
|
-
|
55
|
-
should "leave created_at unchanged" do
|
56
|
-
assert_equal @old_created_at, @person.created_at
|
57
|
-
end
|
58
|
-
|
59
|
-
should "set updated_at to the current Time" do
|
60
|
-
assert_not_equal @old_updated_at, @person.updated_at
|
61
|
-
assert_equal @now.to_s, @person.updated_at
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
data/test/test_ohm_to_hash.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestOhmToHash < Test::Unit::TestCase
|
4
|
-
setup do
|
5
|
-
Ohm.flush
|
6
|
-
end
|
7
|
-
|
8
|
-
context "a Person with name: matz, age: nil, skills: 10" do
|
9
|
-
class Person < Ohm::Model
|
10
|
-
include Ohm::ToHash
|
11
|
-
|
12
|
-
attribute :name
|
13
|
-
attribute :age
|
14
|
-
attribute :skills
|
15
|
-
end
|
16
|
-
|
17
|
-
setup do
|
18
|
-
@person = Person.create(:name => 'matz', :skills => 10)
|
19
|
-
@person = Person[@person.id]
|
20
|
-
end
|
21
|
-
|
22
|
-
should "have a to_hash of { id: 1, name: 'matz', age: nil, skills: 10 }" do
|
23
|
-
assert_equal(
|
24
|
-
{ :id => '1', :name => "matz", :age => nil, :skills => '10' },
|
25
|
-
@person.to_hash
|
26
|
-
)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context "when a Post has a votes counter" do
|
31
|
-
class Post < Ohm::Model
|
32
|
-
include Ohm::ToHash
|
33
|
-
|
34
|
-
counter :votes
|
35
|
-
end
|
36
|
-
|
37
|
-
setup do
|
38
|
-
@post = Post.create
|
39
|
-
@post.incr :votes
|
40
|
-
@post.incr :votes
|
41
|
-
@post.incr :votes
|
42
|
-
end
|
43
|
-
|
44
|
-
should "include the votes in the hash" do
|
45
|
-
assert_equal({ :id => '1', :votes => 3 }, @post.to_hash)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context "when a comment has a reference to a person" do
|
50
|
-
self::Person = Class.new(Ohm::Model)
|
51
|
-
|
52
|
-
class Comment < Ohm::Model
|
53
|
-
include Ohm::ToHash
|
54
|
-
|
55
|
-
reference :person, Person
|
56
|
-
end
|
57
|
-
|
58
|
-
setup do
|
59
|
-
@person = Person.create
|
60
|
-
@comment = Comment.create(:person => @person)
|
61
|
-
end
|
62
|
-
|
63
|
-
should "have the person_id in the hash" do
|
64
|
-
assert_equal({ :id => '1', :person_id => '1' }, @comment.to_hash)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|