ohm-contrib 2.0.0.alpha5 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/contrib"
3
+
4
+ test "plugins" do
5
+ assert defined?(Ohm::Callbacks)
6
+ assert defined?(Ohm::DataTypes)
7
+ assert defined?(Ohm::Locking)
8
+ assert defined?(Ohm::Scope)
9
+ assert defined?(Ohm::Slug)
10
+ assert defined?(Ohm::SoftDelete)
11
+ assert defined?(Ohm::Timestamps)
12
+ assert defined?(Ohm::Versioned)
13
+ end
@@ -0,0 +1,151 @@
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/datatypes"
3
+
4
+ class Product < Ohm::Model
5
+ include Ohm::DataTypes
6
+
7
+ attribute :stock, Type::Integer
8
+ attribute :price, Type::Decimal
9
+ attribute :rating, Type::Float
10
+ attribute :bought_at, Type::Time
11
+ attribute :date_released, Type::Date
12
+ attribute :sizes, Type::Hash
13
+ attribute :stores, Type::Array
14
+ attribute :published, Type::Boolean
15
+ attribute :state, Type::Symbol
16
+ attribute :comments, Type::Set
17
+ end
18
+
19
+ test "Type::Integer" do
20
+ product = Product.new(stock: "1")
21
+
22
+ assert_equal 1, product.stock
23
+ product.save
24
+
25
+ product = Product[product.id]
26
+ assert_equal 1, product.stock
27
+ end
28
+
29
+ test "Type::Time" do
30
+ time = Time.utc(2011, 11, 22)
31
+ product = Product.new(bought_at: time)
32
+
33
+ assert product.bought_at.kind_of?(Time)
34
+ product.save
35
+
36
+ product = Product[product.id]
37
+ assert product.bought_at.kind_of?(Time)
38
+ assert_equal time, product.bought_at
39
+
40
+ assert_equal "2011-11-22 00:00:00 UTC", product.get(:bought_at)
41
+ assert_equal "2011-11-22 00:00:00 UTC", product.bought_at.to_s
42
+ end
43
+
44
+ test "Type::Date" do
45
+ product = Product.new(date_released: Date.today)
46
+
47
+ assert product.date_released.kind_of?(Date)
48
+
49
+ product = Product.new(date_released: "2011-11-22")
50
+ assert product.date_released.kind_of?(Date)
51
+
52
+ product.save
53
+
54
+ product = Product[product.id]
55
+ assert_equal Date.new(2011, 11, 22), product.date_released
56
+ assert_equal "2011-11-22", product.get(:date_released)
57
+ assert_equal "2011-11-22", product.date_released.to_s
58
+ end
59
+
60
+ test "Type::Hash" do
61
+ sizes = { "XS" => 1, "S" => 2, "L" => 3 }
62
+
63
+ product = Product.new(sizes: sizes)
64
+ assert product.sizes.kind_of?(Hash)
65
+ product.save
66
+
67
+ product = Product[product.id]
68
+ assert_equal sizes, product.sizes
69
+ assert_equal %Q[{"XS":1,"S":2,"L":3}], product.get(:sizes)
70
+ assert_equal %Q[{"XS":1,"S":2,"L":3}], product.sizes.to_s
71
+ end
72
+
73
+ test "Type::Array" do
74
+ stores = ["walmart", "marshalls", "jcpenny"]
75
+ product = Product.new(stores: stores)
76
+ assert product.stores.kind_of?(Array)
77
+
78
+ product.save
79
+
80
+ product = Product[product.id]
81
+ assert_equal stores, product.stores
82
+ assert_equal %Q(["walmart","marshalls","jcpenny"]), product.get(:stores)
83
+ assert_equal %Q(["walmart","marshalls","jcpenny"]), product.stores.to_s
84
+ end
85
+
86
+ test "Type::Decimal" do
87
+ product = Product.new(price: 0.001)
88
+
89
+ # This fails if 0.001 is a float.
90
+ x = 0
91
+ 1000.times { x += product.price }
92
+ assert_equal 1, x
93
+
94
+ product.save
95
+
96
+ product = Product[product.id]
97
+ assert_equal 0.001, product.price
98
+ assert_equal "0.1E-2", product.get(:price)
99
+ end
100
+
101
+ test "Type::Float" do
102
+ product = Product.new(rating: 4.5)
103
+ assert product.rating.kind_of?(Float)
104
+
105
+ product.save
106
+ product = Product[product.id]
107
+ assert_equal 4.5, product.rating
108
+ assert_equal "4.5", product.get(:rating)
109
+ end
110
+
111
+ test "Type::Boolean" do
112
+ product = Product.new(published: 1)
113
+ assert_equal true, product.published
114
+
115
+ product.save
116
+
117
+ product = Product[product.id]
118
+ assert_equal "true", product.get(:published)
119
+ assert_equal true, product.published
120
+
121
+ product.published = false
122
+ product.save
123
+
124
+ product = Product[product.id]
125
+ assert_equal nil, product.get(:published)
126
+ assert_equal false, product.published
127
+ end
128
+
129
+ test "Type::Symbol" do
130
+ product = Product.new(state: 'available')
131
+
132
+ assert_equal :available, product.state
133
+ product.save
134
+
135
+ product = Product[product.id]
136
+ assert_equal :available, product.state
137
+ end
138
+
139
+ test "Type::Set" do
140
+ comments = ::Set.new(["Good product", "Awesome!", "Great."])
141
+ product = Product.new(comments: comments)
142
+ assert product.comments.kind_of?(::Set)
143
+
144
+ product.save
145
+
146
+ product = Product[product.id]
147
+
148
+ assert_equal comments, product.comments
149
+ assert_equal %Q(["Awesome!","Good product","Great."]), product.get(:comments)
150
+ assert_equal %Q(["Awesome!","Good product","Great."]), product.comments.to_s
151
+ end
@@ -1,23 +1,8 @@
1
- $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
-
3
1
  require "cutest"
4
-
5
- if ENV["SCRIPTED"]
6
- require "ohm/scripted"
7
- else
8
- require "ohm"
9
- end
10
-
11
- require "ohm/contrib"
12
- require "override"
13
-
14
- NOW = Time.utc(2010, 5, 12)
15
-
16
- include Override
2
+ require "ohm"
17
3
 
18
4
  prepare do
19
5
  Ohm.flush
20
- override(Time, :now => NOW)
21
6
  end
22
7
 
23
8
  def assert_nothing_raised(*exceptions)
@@ -0,0 +1,10 @@
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/locking"
3
+
4
+ class Server < Ohm::Model
5
+ include Ohm::Locking
6
+ end
7
+
8
+ test "mutex method is added at instance and class level" do
9
+ assert Server.new.respond_to?(:mutex)
10
+ end
@@ -1,6 +1,5 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("../helper", __FILE__)
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/scope"
4
3
 
5
4
  class Post < Ohm::Model
6
5
  set :comments, :Comment
@@ -8,7 +7,7 @@ end
8
7
 
9
8
  module Finders
10
9
  def rejected
11
- find(:status => "rejected")
10
+ find(status: "rejected")
12
11
  end
13
12
  end
14
13
 
@@ -20,7 +19,7 @@ class Comment < Ohm::Model
20
19
 
21
20
  scope do
22
21
  def approved
23
- find(:status => "approved")
22
+ find(status: "approved")
24
23
  end
25
24
  end
26
25
 
@@ -33,8 +32,8 @@ end
33
32
 
34
33
  test "allows custom methods for the defined scopes" do
35
34
  post = Post.create
36
- comment = Comment.create(:status => "approved")
37
- Post.redis.call("SADD", post.comments.key, comment.id)
35
+ comment = Comment.create(status: "approved")
36
+ post.comments.add(comment)
38
37
 
39
38
  assert post.comments.approved.is_a?(Ohm::MultiSet)
40
39
  assert post.comments.approved.include?(comment)
@@ -42,17 +41,16 @@ end
42
41
 
43
42
  test "allows custom methods to be included from a module" do
44
43
  post = Post.create
45
- comment = Comment.create(:status => "rejected")
46
- Post.redis.call("SADD", post.comments.key, comment.id)
44
+ comment = Comment.create(status: "rejected")
45
+ post.comments.add(comment)
47
46
 
48
47
  assert post.comments.rejected.is_a?(Ohm::MultiSet)
49
48
  assert post.comments.rejected.include?(comment)
50
49
  end
51
50
 
52
51
  test "works with the main Comment.all collection as well" do
53
- approved = Comment.create(:status => "approved")
54
- rejected = Comment.create(:status => "rejected")
55
-
52
+ approved = Comment.create(status: "approved")
53
+ rejected = Comment.create(status: "rejected")
56
54
 
57
55
  assert Comment.all.approved.include?(approved)
58
56
  assert Comment.all.rejected.include?(rejected)
@@ -1,9 +1,9 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("../helper", __FILE__)
1
+ require_relative "../lib/ohm/slug"
2
+ require_relative "helper"
4
3
 
5
4
  class Article < Ohm::Model
6
5
  include Ohm::Slug
6
+
7
7
  attribute :title
8
8
 
9
9
  def to_s
@@ -12,13 +12,19 @@ class Article < Ohm::Model
12
12
  end
13
13
 
14
14
  test "to_param" do
15
- article = Article.create(:title => "A very Unique and Interesting String?'")
15
+ article = Article.create(title: "A very Unique and Interesting String?'")
16
16
 
17
- assert '1-a-very-unique-and-interesting-string' == article.to_param
17
+ assert_equal '1-a-very-unique-and-interesting-string', article.to_param
18
18
  end
19
19
 
20
20
  test "finding" do
21
- article = Article.create(:title => "A very Unique and Interesting String?'")
21
+ article = Article.create(title: "A very Unique and Interesting String?'")
22
+
23
+ assert_equal 1, Article[article.to_param].id
24
+ end
25
+
26
+ test "transliterate non-ascii characters" do
27
+ article = Article.create(title: "Décor")
22
28
 
23
- assert 1 == Article["1-a-very-unique-and-interesting-string"].id
29
+ assert_equal article.to_param, "1-decor"
24
30
  end
@@ -1,6 +1,5 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/softdelete"
4
3
 
5
4
  class Person < Ohm::Model
6
5
  include Ohm::SoftDelete
@@ -23,9 +22,9 @@ test "deleted?" do
23
22
  end
24
23
 
25
24
  test "all excludes deleted records" do
26
- person = Person.create(:name => "matz")
25
+ person = Person.create(name: "matz")
27
26
 
28
- assert Person.all.first == person
27
+ assert_equal person, Person.all.first
29
28
 
30
29
  person.delete
31
30
 
@@ -33,48 +32,44 @@ test "all excludes deleted records" do
33
32
  end
34
33
 
35
34
  test "find does not exclude deleted records" do
36
- person = Person.create(:name => "matz")
35
+ person = Person.create(name: "matz")
37
36
 
38
- assert Person.find(:name => "matz").first == person
37
+ assert_equal person, Person.find(name: "matz").first
39
38
 
40
39
  person.delete
41
40
 
42
- assert Person.find(:name => "matz").include?(person)
41
+ assert Person.find(name: "matz").include?(person)
43
42
  end
44
43
 
45
44
  test "find with many criteria doesn't exclude deleted records" do
46
- person = Person.create(:name => "matz", :age => 38)
45
+ person = Person.create(name: "matz", age: 38)
47
46
 
48
- assert Person.find(:name => "matz", :age => 38).first == person
47
+ assert_equal person, Person.find(name: "matz", age: 38).first
49
48
 
50
49
  person.delete
51
50
 
52
- assert Person.find(:name => "matz", :age => 38).include?(person)
51
+ assert Person.find(name: "matz", age: 38).include?(person)
53
52
  end
54
53
 
55
54
  test "exists? returns true for deleted records" do
56
- person = Person.create(:name => "matz")
57
-
58
- person.delete
55
+ person = Person.create(name: "matz").delete
59
56
 
60
57
  assert Person.exists?(person.id)
61
58
  end
62
59
 
63
60
  test "Model[n] can be used to retrieve deleted records" do
64
- person = Person.create(:name => "matz")
65
-
66
- person.delete
61
+ person = Person.create(name: "matz").delete
67
62
 
68
63
  assert Person[person.id].deleted?
69
- assert Person[person.id].name == "matz"
64
+ assert_equal "matz", Person[person.id].name
70
65
  end
71
66
 
72
67
  test "restoring" do
73
- person = Person.create(:name => "matz")
74
- person.delete
68
+ person = Person.create(name: "matz").delete
75
69
 
76
70
  assert Person.all.empty?
77
71
 
78
72
  person.restore
73
+
79
74
  assert Person.all.include?(person)
80
75
  end
@@ -1,31 +1,40 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
1
+ require_relative "helper"
2
+ require "override"
3
+ require_relative "../lib/ohm/timestamps"
4
4
 
5
5
  class Person < Ohm::Model
6
6
  include Ohm::Timestamps
7
7
  end
8
8
 
9
+ NOW = Time.utc(2010, 5, 12)
10
+
11
+ include Override
12
+
13
+ prepare do
14
+ override(Time, now: NOW)
15
+ end
16
+
9
17
  test "a new? record" do
10
- assert nil == Person.new.created_at
11
- assert nil == Person.new.updated_at
18
+ assert_equal nil, Person.new.created_at
19
+ assert_equal nil, Person.new.updated_at
12
20
  end
13
21
 
14
22
  test "on create" do
15
23
  person = Person.create
16
24
  person = Person[person.id]
17
25
 
18
- assert NOW == person.created_at
19
- assert NOW == person.updated_at
26
+ assert_equal NOW, person.created_at
27
+ assert_equal NOW, person.updated_at
20
28
  end
21
29
 
22
30
  test "on update" do
23
31
  person = Person.create
24
32
 
25
- override(Time, :now => Time.utc(2010, 5, 13))
33
+ override(Time, now: Time.utc(2010, 5, 13))
26
34
  person.save
35
+
27
36
  person = Person[person.id]
28
37
 
29
- assert NOW == person.created_at
30
- assert Time.utc(2010, 5, 13) == person.updated_at
38
+ assert_equal NOW, person.created_at
39
+ assert_equal Time.utc(2010, 5, 13), person.updated_at
31
40
  end
@@ -1,4 +1,5 @@
1
- require File.expand_path("../helper", __FILE__)
1
+ require_relative "helper"
2
+ require_relative "../lib/ohm/versioned"
2
3
 
3
4
  class Article < Ohm::Model
4
5
  include Ohm::Versioned
@@ -8,17 +9,17 @@ class Article < Ohm::Model
8
9
  end
9
10
 
10
11
  test do
11
- a1 = Article.create(:title => "Foo Bar", :content => "Lorem ipsum")
12
+ a1 = Article.create(title: "Foo Bar", content: "Lorem ipsum")
12
13
  a2 = Article[a1.id]
13
14
 
14
15
  a1.update({})
15
16
 
17
+ expected = { title: "Bar Baz", _version: "1", content: "Lorem ipsum" }
18
+
16
19
  begin
17
- a2.update(:title => "Bar Baz")
20
+ a2.update(title: "Bar Baz")
18
21
  rescue Ohm::VersionConflict => ex
19
22
  end
20
23
 
21
- expected = { :title => "Bar Baz", :_version => "1", :content => "Lorem ipsum" }
22
-
23
24
  assert_equal expected, ex.attributes
24
25
  end