redis_orm 0.5.1 → 0.8

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.
@@ -1,119 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class CutoutAggregator < RedisOrm::Base
4
- property :modified_at, Time
5
-
6
- property :revision, Integer, :default => 0
7
- end
8
-
9
- class Cutout < RedisOrm::Base
10
- property :filename, String
11
-
12
- before_create :increase_revisions
13
- before_destroy :decrease_revisions
14
-
15
- def increase_revisions
16
- ca = CutoutAggregator.last
17
- ca.update_attribute(:revision, ca.revision + 1) if ca
18
- end
19
-
20
- def decrease_revisions
21
- ca = CutoutAggregator.first
22
- if ca.revision > 0
23
- ca.update_attribute :revision, ca.revision - 1
24
- end
25
-
26
- ca.destroy if ca.revision == 0
27
- end
28
- end
29
-
30
- class Comment < RedisOrm::Base
31
- property :text, String
32
-
33
- belongs_to :user
34
-
35
- before_save :trim_whitespaces
36
- after_save :regenerate_karma
37
-
38
- def trim_whitespaces
39
- self.text = self.text.strip
40
- end
41
-
42
- def regenerate_karma
43
- if self.user
44
- self.user.update_attribute :karma, (self.user.karma - self.text.length)
45
- end
46
- end
47
- end
48
-
49
- class User < RedisOrm::Base
50
- property :first_name, String
51
- property :last_name, String
52
-
53
- property :karma, Integer, :default => 1000
54
-
55
- index :first_name
56
- index :last_name
57
- index [:first_name, :last_name]
58
-
59
- has_many :comments
60
-
61
- after_create :store_in_rating
62
- after_destroy :after_destroy_callback
63
-
64
- def store_in_rating
65
- $redis.zadd "users:sorted_by_rating", 0.0, self.id
66
- end
67
-
68
- def after_destroy_callback
69
- self.comments.map{|c| c.destroy}
70
- end
71
- end
72
-
73
- describe "check callbacks" do
74
- it "should fire after_create/after_destroy callbacks" do
75
- user = User.new :first_name => "Robert", :last_name => "Pirsig"
76
- user.save
77
-
78
- $redis.zrank("users:sorted_by_rating", user.id).should == 0
79
-
80
- comment = Comment.create :text => "First!"
81
- user.comments << comment
82
-
83
- u = User.first
84
- u.id.should == user.id
85
- u.comments.count.should == 1
86
- u.destroy
87
- u.comments.count.should == 0
88
- end
89
-
90
- it "should fire before_create/before_destroy callbacks" do
91
- CutoutAggregator.create
92
-
93
- CutoutAggregator.count.should == 1
94
- Cutout.create :filename => "1.jpg"
95
- Cutout.create :filename => "2.jpg"
96
- CutoutAggregator.last.revision.should == 2
97
- Cutout.last.destroy
98
- Cutout.last.destroy
99
- CutoutAggregator.count.should == 0
100
- end
101
-
102
- it "should fire after_save/before_save callbacks" do
103
- comment = Comment.new :text => " Trim meeee ! "
104
- comment.save
105
- Comment.first.text.should == "Trim meeee !"
106
-
107
- user = User.new :first_name => "Robert", :last_name => "Pirsig"
108
- user.save
109
- user.karma.should == 1000
110
-
111
- user.comments << comment
112
- user.comments.count == 1
113
-
114
- c = Comment.first
115
- c.update_attributes :text => "Another brick in the wall"
116
-
117
- User.first.karma.should == 975
118
- end
119
- end
@@ -1,31 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class User < RedisOrm::Base
4
- property :name, String
5
- property :age, Integer, :default => 26
6
- property :gender, RedisOrm::Boolean, :default => true
7
- end
8
-
9
- describe "check associations" do
10
- it "should return correct _changes array" do
11
- user = User.new :name => "german"
12
- user.name_changed?.should == false
13
-
14
- user.name_changes.should == ["german"]
15
- user.save
16
-
17
- user.name_changes.should == ["german"]
18
- user.name = "germaninthetown"
19
- user.name_changes.should == ["german", "germaninthetown"]
20
- user.name_changed?.should == true
21
- user.save
22
-
23
- user = User.first
24
- user.name.should == "germaninthetown"
25
- user.name_changed?.should == false
26
- user.name_changes.should == ["germaninthetown"]
27
- user.name = "german"
28
- user.name_changed?.should == true
29
- user.name_changes.should == ["germaninthetown", "german"]
30
- end
31
- end
@@ -1,68 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class User < RedisOrm::Base
4
- property :first_name, String
5
- property :last_name, String
6
-
7
- index :first_name, :unique => true
8
- index :last_name, :unique => true
9
- index [:first_name, :last_name], :unique => false
10
- end
11
-
12
- class CustomUser < RedisOrm::Base
13
- property :first_name, String
14
- property :last_name, String
15
-
16
- index :first_name, :unique => false
17
- index :last_name, :unique => false
18
- index [:first_name, :last_name], :unique => true
19
- end
20
-
21
- describe "check associations" do
22
- it "should create and use indexes to implement dynamic finders" do
23
- user1 = User.new
24
- user1.first_name = "Dmitrii"
25
- user1.last_name = "Samoilov"
26
- user1.save
27
-
28
- User.find_by_first_name("John").should == nil
29
-
30
- user = User.find_by_first_name "Dmitrii"
31
- user.id.should == user1.id
32
-
33
- User.find_all_by_first_name("Dmitrii").size.should == 1
34
-
35
- user = User.find_by_first_name_and_last_name('Dmitrii', 'Samoilov')
36
- user.should be
37
- user.id.should == user1.id
38
-
39
- User.find_all_by_first_name_and_last_name('Dmitrii', 'Samoilov').size.should == 1
40
-
41
- User.find_all_by_last_name_and_first_name('Samoilov', 'Dmitrii')[0].id.should == user1.id
42
-
43
- lambda{User.find_by_first_name_and_cast_name('Dmitrii', 'Samoilov')}.should raise_error
44
- end
45
-
46
- it "should create and use indexes to implement dynamic finders" do
47
- user1 = CustomUser.new
48
- user1.first_name = "Dmitrii"
49
- user1.last_name = "Samoilov"
50
- user1.save
51
-
52
- user2 = CustomUser.new
53
- user2.first_name = "Dmitrii"
54
- user2.last_name = "Nabaldyan"
55
- user2.save
56
-
57
- user = CustomUser.find_by_first_name "Dmitrii"
58
- user.id.should == user1.id
59
-
60
- CustomUser.find_by_last_name("Krassovkin").should == nil
61
-
62
- CustomUser.find_all_by_first_name("Dmitrii").size.should == 2
63
- end
64
-
65
- it "should properly delete indices when record was deleted" do
66
-
67
- end
68
- end
@@ -1,45 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class User < RedisOrm::Base
4
- property :name, String
5
- property :age, Integer
6
- property :created_at, Time
7
-
8
- index :age
9
-
10
- has_one :profile
11
- end
12
-
13
- class Profile < RedisOrm::Base
14
- property :title, String
15
- belongs_to :user
16
- end
17
-
18
- class Jigsaw < RedisOrm::Base
19
- property :title, String
20
- belongs_to :user
21
- end
22
-
23
- describe "exceptions test" do
24
- it "should raise an exception if association is provided with improper class" do
25
- User.count.should == 0
26
-
27
- user = User.new :name => "german", :age => 26
28
- user.save
29
-
30
- user.should be
31
- user.name.should == "german"
32
- User.count.should == 1
33
-
34
- lambda{ User.find :all, :conditions => {:name => "german"} }.should raise_error
35
- User.find(:all, :conditions => {:age => 26}).size.should == 1
36
- lambda{ User.find :all, :conditions => {:name => "german", :age => 26} }.should raise_error
37
-
38
- jigsaw = Jigsaw.new
39
- jigsaw.title = "123"
40
- jigsaw.save
41
-
42
- # RedisOrm::TypeMismatchError
43
- lambda { user.profile = jigsaw }.should raise_error
44
- end
45
- end
@@ -1,54 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class Profile < RedisOrm::Base
4
- property :name, String
5
-
6
- has_one :location
7
- end
8
-
9
- class Location < RedisOrm::Base
10
- property :coordinates, String
11
-
12
- has_many :profiles
13
- end
14
-
15
- describe "check associations" do
16
- it "should save associations properly" do
17
- @profile = Profile.new
18
- @profile.name = "my profile"
19
- @profile.save
20
-
21
- @profile.should be
22
- @profile.name.should == "my profile"
23
-
24
- @location1 = Location.new
25
- @location1.coordinates = "44.343456345 56.23341432"
26
- @location1.save
27
- @location1.should be
28
- @location1.coordinates.should == "44.343456345 56.23341432"
29
-
30
- @profile.location = @location1
31
-
32
- @profile.location.should be
33
- @profile.location.id.should == @location1.id
34
-
35
- @location1.profiles.size.should == 1
36
- @location1.profiles.first.id.should == @profile.id
37
-
38
- # check second profile
39
- @profile2 = Profile.new
40
- @profile2.name = "someone else's profile"
41
- @profile2.save
42
-
43
- @profile2.should be
44
- @profile2.name.should == "someone else's profile"
45
-
46
- @profile2.location = @location1
47
-
48
- @profile2.location.should be
49
- @profile2.location.id.should == @location1.id
50
-
51
- @location1.profiles.size.should == 2
52
- @location1.profiles.collect{|p| p.id}.sort.should == [@profile.id, @profile2.id].sort
53
- end
54
- end
data/test/indices_test.rb DELETED
@@ -1,81 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class User < RedisOrm::Base
4
- property :first_name, String
5
- property :last_name, String
6
-
7
- index :first_name
8
- index :last_name
9
- index [:first_name, :last_name]
10
- end
11
-
12
- class OmniUser < RedisOrm::Base
13
- property :email, String
14
- property :uid, Integer
15
-
16
- index :email, :case_insensitive => true
17
- index :uid
18
- index [:email, :uid], :case_insensitive => true
19
- end
20
-
21
- describe "check indices" do
22
- it "should change index accordingly to the changes in the model" do
23
- user = User.new :first_name => "Robert", :last_name => "Pirsig"
24
- user.save
25
-
26
- u = User.find_by_first_name("Robert")
27
- u.id.should == user.id
28
-
29
- u = User.find_by_first_name_and_last_name("Robert", "Pirsig")
30
- u.id.should == user.id
31
-
32
- u.first_name = "Chris"
33
- u.save
34
-
35
- User.find_by_first_name("Robert").should == nil
36
-
37
- User.find_by_first_name_and_last_name("Robert", "Pirsig").should == nil
38
-
39
- User.find_by_first_name("Chris").id.should == user.id
40
- User.find_by_last_name("Pirsig").id.should == user.id
41
- User.find_by_first_name_and_last_name("Chris", "Pirsig").id.should == user.id
42
- end
43
-
44
- it "should change index accordingly to the changes in the model (test #update_attributes method)" do
45
- user = User.new :first_name => "Robert", :last_name => "Pirsig"
46
- user.save
47
-
48
- u = User.find_by_first_name("Robert")
49
- u.id.should == user.id
50
-
51
- u = User.find_by_first_name_and_last_name("Robert", "Pirsig")
52
- u.id.should == user.id
53
-
54
- u.update_attributes :first_name => "Christofer", :last_name => "Robin"
55
-
56
- User.find_by_first_name("Robert").should == nil
57
- User.find_by_last_name("Pirsig").should == nil
58
- User.find_by_first_name_and_last_name("Robert", "Pirsig").should == nil
59
-
60
- User.find_by_first_name("Christofer").id.should == user.id
61
- User.find_by_last_name("Robin").id.should == user.id
62
- User.find_by_first_name_and_last_name("Christofer", "Robin").id.should == user.id
63
- end
64
-
65
- it "should create case insensitive indices too" do
66
- ou = OmniUser.new :email => "GERMAN@Ya.ru", :uid => 2718281828
67
- ou.save
68
-
69
- OmniUser.count.should == 1
70
- OmniUser.find_by_email("german@ya.ru").should be
71
- OmniUser.find_all_by_email("german@ya.ru").count.should == 1
72
-
73
- OmniUser.find_by_email_and_uid("german@ya.ru", 2718281828).should be
74
- OmniUser.find_all_by_email_and_uid("german@ya.ru", 2718281828).count.should == 1
75
-
76
- OmniUser.find_by_email("geRman@yA.rU").should be
77
- OmniUser.find_all_by_email_and_uid("GerMan@Ya.ru", 2718281828).count.should == 1
78
-
79
- OmniUser.find_all_by_email_and_uid("german@ya.ru", 2718281829).count.should == 0
80
- end
81
- end
data/test/options_test.rb DELETED
@@ -1,243 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- class Album < RedisOrm::Base
4
- property :title, String
5
-
6
- has_one :photo, :as => :front_photo
7
- has_many :photos, :dependent => :destroy
8
- end
9
-
10
- class Category < RedisOrm::Base
11
- property :title, String
12
-
13
- has_many :photos, :dependent => :nullify
14
- end
15
-
16
- class Photo < RedisOrm::Base
17
- property :image, String
18
- property :image_type, String
19
-
20
- property :checked, RedisOrm::Boolean, :default => false
21
- index :checked
22
-
23
- property :inverted, RedisOrm::Boolean, :default => true
24
- index :inverted
25
-
26
- index :image
27
- index [:image, :image_type]
28
-
29
- belongs_to :album
30
- belongs_to :user
31
- belongs_to :category
32
- end
33
-
34
- class User < RedisOrm::Base
35
- property :name, String
36
-
37
- has_one :photo, :dependent => :destroy
38
- end
39
-
40
- describe "test options" do
41
- before(:each) do
42
- @album = Album.new
43
- @album.title = "my 1st album"
44
- @album.save
45
-
46
- @album.should be
47
- @album.title.should == "my 1st album"
48
-
49
- @photo1 = Photo.new :image => "facepalm.jpg", :image_type => "jpg", :checked => true
50
- @photo1.save
51
- @photo1.should be
52
- @photo1.image.should == "facepalm.jpg"
53
- @photo1.image_type.should == "jpg"
54
-
55
- @photo2 = Photo.new :image => "boobs.png", :image_type => "png", :inverted => false
56
- @photo2.save
57
- @photo2.should be
58
- @photo2.image.should == "boobs.png"
59
- @photo2.image_type.should == "png"
60
- end
61
-
62
- it "should return correct array when :limit and :offset options are provided" do
63
- @album.photos.count.should == 0
64
-
65
- @album.photos.all(:limit => 2, :offset => 0).should == []
66
-
67
- @album.photos << [@photo1, @photo2]
68
-
69
- @album.photos.all(:limit => 0, :offset => 0).should == []
70
- @album.photos.all(:limit => 1, :offset => 0).size.should == 1
71
- @album.photos.all(:limit => 2, :offset => 0).size.should == 2 #[@photo1, @photo2]
72
-
73
- @album.photos.all(:limit => 0, :offset => 0).should == []
74
- @album.photos.all(:limit => 1, :offset => 1).size.should == 1 # [@photo2]
75
- @album.photos.all(:limit => 2, :offset => 2).should == []
76
-
77
- @album.photos.find(:all, :limit => 1, :offset => 1).size.should == 1
78
-
79
- Photo.find(:all).size.should == 2
80
- Photo.find(:first).id.should == @photo1.id
81
- Photo.find(:last).id.should == @photo2.id
82
-
83
- Photo.find(:all, :conditions => {:image => "facepalm.jpg"}).size.should == 1
84
- Photo.find(:all, :conditions => {:image => "boobs.png"}).size.should == 1
85
-
86
- Photo.find(:all, :conditions => {:image => "facepalm.jpg", :image_type => "jpg"}).size.should == 1
87
- Photo.find(:all, :conditions => {:image => "boobs.png", :image_type => "png"}).size.should == 1
88
-
89
- Photo.find(:first, :conditions => {:image => "facepalm.jpg"}).id.should == @photo1.id
90
- Photo.find(:first, :conditions => {:image => "boobs.png"}).id.should == @photo2.id
91
-
92
- Photo.find(:first, :conditions => {:image => "facepalm.jpg", :image_type => "jpg"}).id.should == @photo1.id
93
- Photo.find(:first, :conditions => {:image => "boobs.png", :image_type => "png"}).id.should == @photo2.id
94
-
95
- Photo.find(:last, :conditions => {:image => "facepalm.jpg"}).id.should == @photo1.id
96
- Photo.find(:last, :conditions => {:image => "boobs.png"}).id.should == @photo2.id
97
-
98
- Photo.find(:last, :conditions => {:image => "facepalm.jpg", :image_type => "jpg"}).id.should == @photo1.id
99
- Photo.find(:last, :conditions => {:image => "boobs.png", :image_type => "png"}).id.should == @photo2.id
100
- end
101
-
102
- it "should correctly save boolean values" do
103
- $redis.hgetall("photo:#{@photo1.id}")["inverted"].should == "true"
104
- $redis.hgetall("photo:#{@photo2.id}")["inverted"].should == "false"
105
-
106
- @photo1.inverted.should == true
107
- @photo2.inverted.should == false
108
-
109
- $redis.zrange("photo:inverted:true", 0, -1).should include(@photo1.id.to_s)
110
- $redis.zrange("photo:inverted:false", 0, -1).should include(@photo2.id.to_s)
111
-
112
- $redis.hgetall("photo:#{@photo1.id}")["checked"].should == "true"
113
- $redis.hgetall("photo:#{@photo2.id}")["checked"].should == "false"
114
-
115
- @photo1.checked.should == true
116
- @photo2.checked.should == false
117
-
118
- $redis.zrange("photo:checked:true", 0, -1).should include(@photo1.id.to_s)
119
- $redis.zrange("photo:checked:false", 0, -1).should include(@photo2.id.to_s)
120
- end
121
-
122
- it "should search on bool values properly" do
123
- Photo.find(:all, :conditions => {:checked => true}).size.should == 1
124
- Photo.find(:all, :conditions => {:checked => true}).first.id.should == @photo1.id
125
- Photo.find(:all, :conditions => {:checked => false}).size.should == 1
126
- Photo.find(:all, :conditions => {:checked => false}).first.id.should == @photo2.id
127
-
128
- Photo.find(:all, :conditions => {:inverted => true}).size.should == 1
129
- Photo.find(:all, :conditions => {:inverted => true}).first.id.should == @photo1.id
130
- Photo.find(:all, :conditions => {:inverted => false}).size.should == 1
131
- Photo.find(:all, :conditions => {:inverted => false}).first.id.should == @photo2.id
132
- end
133
-
134
- it "should return correct array when :order option is provided" do
135
- Photo.all(:order => "asc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
136
- Photo.all(:order => "desc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
137
-
138
- Photo.all(:order => "asc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
139
- Photo.all(:order => "desc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
140
-
141
- Photo.all(:order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
142
- Photo.all(:order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
143
-
144
- # testing #find method
145
- Photo.find(:all, :order => "asc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
146
- Photo.find(:all, :order => "desc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
147
-
148
- Photo.find(:all, :order => "asc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
149
- Photo.find(:all, :order => "desc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
150
-
151
- Photo.find(:first, :order => "asc", :limit => 1, :offset => 1).id.should == @photo2.id
152
- Photo.find(:first, :order => "desc", :limit => 1, :offset => 1).id.should == @photo1.id
153
-
154
- Photo.find(:last, :order => "asc").id.should == @photo2.id
155
- Photo.find(:last, :order => "desc").id.should == @photo1.id
156
-
157
- @album.photos.count.should == 0
158
- @album.photos.all(:limit => 2, :offset => 0).should == []
159
- @album.photos << @photo2
160
- @album.photos << @photo1
161
-
162
- @album.photos.all(:order => "asc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
163
- @album.photos.all(:order => "desc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
164
- @album.photos.all(:order => "asc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
165
- @album.photos.all(:order => "desc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
166
- @album.photos.all(:order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
167
- @album.photos.all(:order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
168
-
169
- @album.photos.find(:all, :order => "asc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
170
- @album.photos.find(:all, :order => "desc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
171
-
172
- @album.photos.find(:first, :order => "asc").id.should == @photo2.id
173
- @album.photos.find(:first, :order => "desc").id.should == @photo1.id
174
-
175
- @album.photos.find(:last, :order => "asc").id.should == @photo1.id
176
- @album.photos.find(:last, :order => "desc").id.should == @photo2.id
177
-
178
- @album.photos.find(:last, :order => "desc", :offset => 2).should == nil
179
- @album.photos.find(:first, :order => "desc", :offset => 2).should == nil
180
-
181
- @album.photos.find(:all, :order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
182
- @album.photos.find(:all, :order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
183
- end
184
-
185
- it "should delete associated records when :dependant => :destroy in *has_many* assoc" do
186
- @album.photos << [@photo1, @photo2]
187
-
188
- @album.photos.count.should == 2
189
-
190
- Photo.count.should == 2
191
- @album.destroy
192
- Photo.count.should == 0
193
- Album.count.should == 0
194
- end
195
-
196
- it "should *NOT* delete associated records when :dependant => :nullify or empty in *has_many* assoc" do
197
- Photo.count.should == 2
198
-
199
- category = Category.new
200
- category.title = "cats"
201
- category.save
202
-
203
- Category.count.should == 1
204
-
205
- category.photos << [@photo1, @photo2]
206
- category.photos.count.should == 2
207
-
208
- category.destroy
209
-
210
- Photo.count.should == 2
211
- Category.count.should == 0
212
- end
213
-
214
- it "should delete associated records when :dependant => :destroy and leave them otherwise in *has_one* assoc" do
215
- user = User.new
216
- user.name = "Dmitrii Samoilov"
217
- user.save
218
- user.should be
219
-
220
- user.photo = @photo1
221
-
222
- user.photo.id.should == @photo1.id
223
-
224
- User.count.should == 1
225
- Photo.count.should == 2
226
- user.destroy
227
- Photo.count.should == 1
228
- User.count.should == 0
229
- end
230
-
231
- it "should delete link to associated record when record was deleted" do
232
- @album.photos << [@photo1, @photo2]
233
-
234
- @album.photos.count.should == 2
235
-
236
- Photo.count.should == 2
237
- @photo1.destroy
238
- Photo.count.should == 1
239
-
240
- @album.photos.count.should == 1
241
- @album.photos.size.should == 1
242
- end
243
- end