mongodbmodel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Associations' do
4
+ with_mongo_model
5
+
6
+ after{remove_constants :Post, :Comment}
7
+
8
+ it "basic" do
9
+ class Post
10
+ inherit Mongo::Model
11
+ collection :posts
12
+
13
+ attr_accessor :text
14
+
15
+ def comments
16
+ Comment.query({post_id: _id}, {sort: [[:created_at, -1]]})
17
+ end
18
+ end
19
+
20
+ class Comment
21
+ inherit Mongo::Model
22
+ collection :comments
23
+
24
+ attr_accessor :text, :post_id
25
+
26
+ def == o
27
+ [self.class, text, post_id] == [o.class, o.text, o.post_id]
28
+ end
29
+
30
+ timestamps!
31
+ end
32
+
33
+ post1 = Post.create! text: 'Post 1'
34
+ comment1 = post1.comments.create! text: 'Comment 1'
35
+ comment2 = post1.comments.create! text: 'Comment 2'
36
+
37
+ post2 = Post.create! text: 'Post 2'
38
+ comment3 = post2.comments.create! text: 'Comment 3'
39
+
40
+ post1.comments.count.should == 2
41
+ post1.comments.all.should == [comment2, comment1]
42
+ end
43
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Attribute Convertors" do
4
+ with_mongo_model
5
+
6
+ after(:all){remove_constants :TheSample}
7
+
8
+ convertors = Mongo::Model::AttributeConvertors::CONVERTORS
9
+
10
+ it ":line convertor" do
11
+ v = ['a', 'b']
12
+ str_v = 'a, b'
13
+ convertors[:line][:from_string].call(str_v).should == v
14
+ convertors[:line][:to_string].call(v).should == str_v
15
+ end
16
+
17
+ it ":yaml convertor" do
18
+ v = {'a' => 'b'}
19
+ str_v = v.to_yaml.strip
20
+
21
+ convertors[:yaml][:from_string].call(str_v).should == v
22
+ convertors[:yaml][:to_string].call(v).should == str_v
23
+ end
24
+
25
+ it ":json convertor" do
26
+ v = {'a' => 'b'}
27
+ str_v = v.to_json.strip
28
+ convertors[:json][:from_string].call(str_v).should == v
29
+ convertors[:json][:to_string].call(v).should == str_v
30
+ end
31
+
32
+ it ":field should generate helper methods if :as_string option provided" do
33
+ class ::TheSample
34
+ inherit Mongo::Model
35
+
36
+ attr_accessor :tags, :protected_tags
37
+ available_as_string :tags, :line
38
+ available_as_string :protected_tags, :line
39
+
40
+ def initialize
41
+ @tags, @protected_tags = [], []
42
+ end
43
+
44
+ assign do
45
+ tags_as_string true
46
+ end
47
+ end
48
+
49
+ o = TheSample.new
50
+
51
+ # get
52
+ o.tags_as_string.should == ''
53
+ o.tags = %w(Java Ruby)
54
+ o._cache.clear
55
+ o.tags_as_string.should == 'Java, Ruby'
56
+
57
+ # set
58
+ o.tags_as_string = ''
59
+ o.tags.should == []
60
+ o.tags_as_string = 'Java, Ruby'
61
+ o.tags.should == %w(Java Ruby)
62
+
63
+ # mass assignment
64
+ o.tags = []
65
+ o.set tags_as_string: 'Java, Ruby'
66
+ o.tags.should == %w(Java Ruby)
67
+
68
+ # # protection
69
+ o.protected_tags = []
70
+ o.set protected_tags_as_string: 'Java, Ruby'
71
+ o.protected_tags.should == []
72
+ end
73
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Model callbacks' do
4
+ with_mongo_model
5
+
6
+ after(:all){remove_constants :TheModel, :Player}
7
+
8
+ it "integration smoke test" do
9
+ class Player
10
+ inherit Mongo::Model
11
+
12
+ before_validate :before_validate_check
13
+ after_save :after_save_check
14
+
15
+ attr_accessor :missions
16
+
17
+ class Mission
18
+ inherit Mongo::Model
19
+
20
+ before_validate :before_validate_check
21
+ after_save :after_save_check
22
+ end
23
+ end
24
+
25
+ mission = Player::Mission.new
26
+ player = Player.new
27
+ player.missions = [mission]
28
+
29
+ player.should_receive(:before_validate_check).once.ordered.and_return(nil)
30
+ mission.should_receive(:before_validate_check).once.ordered.and_return(nil)
31
+ player.should_receive(:after_save_check).once.ordered.and_return(nil)
32
+ mission.should_receive(:after_save_check).once.ordered.and_return(nil)
33
+
34
+ db.units.save(player).should be_true
35
+ end
36
+ end
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+ require 'mongo/object/spec/crud_shared'
3
+
4
+ describe "Model CRUD" do
5
+ with_mongo_model
6
+
7
+ describe 'simple' do
8
+ before :all do
9
+ class Unit
10
+ inherit Mongo::Model
11
+ collection :units
12
+
13
+ attr_accessor :name, :info
14
+ def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
15
+ end
16
+ end
17
+ after(:all){remove_constants :Unit}
18
+
19
+ before do
20
+ @zeratul = Unit.build name: 'Zeratul', info: 'Dark Templar'
21
+ end
22
+
23
+ it_should_behave_like "object CRUD"
24
+
25
+ it 'model crud' do
26
+ # read
27
+ Unit.count.should == 0
28
+ Unit.all.should == []
29
+ Unit.first.should == nil
30
+
31
+ # create
32
+ @zeratul.save.should be_true
33
+ @zeratul._id.should_not be_nil
34
+
35
+ # read
36
+ Unit.count.should == 1
37
+ Unit.all.should == [@zeratul]
38
+ Unit.first.should == @zeratul
39
+ Unit.first.object_id.should_not == @zeratul.object_id
40
+
41
+ # update
42
+ @zeratul.info = 'Killer of Cerebrates'
43
+ @zeratul.save.should be_true
44
+ Unit.count.should == 1
45
+ Unit.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
46
+
47
+ # destroy
48
+ @zeratul.destroy.should be_true
49
+ Unit.count.should == 0
50
+ end
51
+
52
+ it 'should be able to save to another collection' do
53
+ # create
54
+ @zeratul.save(collection: db.heroes).should be_true
55
+ @zeratul._id.should_not be_nil
56
+
57
+ # read
58
+ Unit.count.should == 0
59
+ db.heroes.count.should == 1
60
+ db.heroes.first.should == @zeratul
61
+ db.heroes.first.object_id.should_not == @zeratul.object_id
62
+
63
+ # update
64
+ @zeratul.info = 'Killer of Cerebrates'
65
+ @zeratul.save(collection: db.heroes).should be_true
66
+ Unit.count.should == 0
67
+ db.heroes.count.should == 1
68
+ db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
69
+
70
+ # destroy
71
+ @zeratul.destroy(collection: db.heroes).should be_true
72
+ db.heroes.count.should == 0
73
+ end
74
+
75
+ it 'build' do
76
+ u = Unit.build name: 'Zeratul'
77
+ u.name.should == 'Zeratul'
78
+ end
79
+
80
+ it 'create' do
81
+ u = Unit.create(name: 'Zeratul')
82
+ u.new_record?.should be_false
83
+
84
+ u = Unit.create!(name: 'Zeratul')
85
+ u.new_record?.should be_false
86
+ end
87
+
88
+ it 'destroy_all' do
89
+ Unit.create(name: 'Zeratul')
90
+ Unit.count.should == 1
91
+ Unit.destroy_all
92
+ Unit.count.should == 0
93
+
94
+ Unit.destroy_all!
95
+ end
96
+ end
97
+
98
+ describe 'embedded' do
99
+ before :all do
100
+ class Player
101
+ inherit Mongo::Model
102
+ collection :players
103
+
104
+ attr_accessor :missions
105
+ def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
106
+
107
+ class Mission
108
+ inherit Mongo::Model
109
+
110
+ attr_accessor :name, :stats
111
+ def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
112
+ end
113
+ end
114
+ end
115
+ after(:all){remove_constants :Player}
116
+
117
+ before do
118
+ @mission_class = Player::Mission
119
+ @player = Player.new
120
+ @player.missions = [
121
+ Player::Mission.build(name: 'Wasteland', stats: {buildings: 5, units: 10}),
122
+ Player::Mission.build(name: 'Backwater Station', stats: {buildings: 8, units: 25}),
123
+ ]
124
+ end
125
+
126
+ it_should_behave_like 'embedded object CRUD'
127
+
128
+ it 'crud' do
129
+ # create
130
+ @player.save.should be_true
131
+ @player._id.should_not be_nil
132
+
133
+ # read
134
+ Player.count.should == 1
135
+ Player.first.should == @player
136
+ Player.first.object_id.should_not == @players.object_id
137
+
138
+ # update
139
+ @player.missions.first.stats[:units] = 9
140
+ @player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {buildings: 11, units: 40})
141
+ @player.save.should be_true
142
+ Player.count.should == 1
143
+ Player.first.should == @player
144
+ Player.first.object_id.should_not == @player.object_id
145
+
146
+ # destroy
147
+ @player.destroy.should be_true
148
+ Player.count.should == 0
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Collection & Database' do
4
+ with_mongo_model
5
+
6
+ before :all do
7
+ class TheModel
8
+ inherit Mongo::Model
9
+ end
10
+ end
11
+ after(:all){remove_constants :TheModel}
12
+
13
+ after do
14
+ TheModel.db = nil
15
+ TheModel.collection = nil
16
+ Mongo::Model.connection, Mongo::Model.db = nil, nil
17
+ end
18
+
19
+ it "global setting" do
20
+ Mongo::Model.connection = db.connection
21
+ Mongo::Model.db = db
22
+
23
+ Mongo::Model.connection.should == db.connection
24
+ Mongo::Model.db.should == db
25
+ end
26
+
27
+ it "should allow set database per model" do
28
+ Mongo::Model.connection = db.connection
29
+ Mongo::Model.db = db
30
+
31
+ TheModel.db.should == db
32
+
33
+ TheModel.db :test
34
+ TheModel.db.name.should == 'test'
35
+
36
+ TheModel.db = nil
37
+ TheModel.db db
38
+ TheModel.db.should == db
39
+
40
+ TheModel.db = nil
41
+ TheModel.db{db}
42
+ TheModel.db.should == db
43
+ end
44
+
45
+ it "should allow set collection per model" do
46
+ Mongo::Model.db = db
47
+
48
+ # TheModel.default_collection_name.should == :the_model
49
+ # TheModel.collection.name.should == 'the_model'
50
+
51
+ TheModel.collection :units
52
+ TheModel.collection.name.should == 'units'
53
+
54
+ TheModel.collection = nil
55
+ units = db.units
56
+ TheModel.collection units
57
+ TheModel.collection.should == units
58
+
59
+ TheModel.collection = nil
60
+ TheModel.collection{units}
61
+ TheModel.collection.should == units
62
+ end
63
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'file_model/spec/shared_crud'
3
+
4
+ describe 'File Model' do
5
+ with_mongo_model
6
+ it_should_behave_like "file model crud"
7
+
8
+ before :all do
9
+ class ImageFile; end
10
+
11
+ class Unit
12
+ inherit Mongo::Model
13
+ collection :units
14
+
15
+ attr_accessor :name
16
+
17
+ mount_file :image, ImageFile
18
+ end
19
+ end
20
+ after(:all){remove_constants :Unit}
21
+
22
+ before{@model_class = Unit}
23
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Model Miscellaneous' do
4
+ with_mongo_model
5
+
6
+ before do
7
+ class User
8
+ inherit Mongo::Model
9
+ collection :users
10
+
11
+ attr_accessor :name
12
+ end
13
+ end
14
+ after{remove_constants :Unit3, :User}
15
+
16
+ it "timestamps" do
17
+ class Unit3
18
+ inherit Mongo::Model
19
+ collection :units
20
+
21
+ attr_accessor :name
22
+
23
+ timestamps!
24
+ end
25
+
26
+ unit = Unit3.build name: 'Zeratul'
27
+ unit.save!
28
+
29
+ unit = Unit3.first
30
+ unit.created_at.should_not be_nil
31
+ unit.updated_at.should_not be_nil
32
+ created_at,updated_at = unit.created_at, unit.updated_at
33
+
34
+ unit.save!
35
+ unit.created_at.should == created_at
36
+ unit.updated_at.should > updated_at
37
+ end
38
+
39
+ it 'cache' do
40
+ class Unit3
41
+ inherit Mongo::Model
42
+ end
43
+ u = Unit3.new
44
+ u._cache.should == {}
45
+ end
46
+
47
+ it "to_param" do
48
+ u = User.new
49
+ u.to_param.should == ''
50
+ u.save!
51
+ u.to_param.should_not be_empty
52
+ end
53
+
54
+ it "dom_id" do
55
+ u = User.new
56
+ u.dom_id.should == ''
57
+ u.save!
58
+ u.dom_id.should_not be_empty
59
+ end
60
+
61
+ it 'reload' do
62
+ u = User.create! name: 'Zeratul'
63
+ u.name = 'Jim'
64
+ u.reload
65
+ u.name.should == 'Zeratul'
66
+ end
67
+ end