mongodb 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -5
- data/lib/mongodb/driver.rb +33 -0
- data/lib/mongodb/driver/collection.rb +156 -0
- data/lib/mongodb/driver/database.rb +6 -0
- data/lib/mongodb/driver/dynamic_finders.rb +41 -0
- data/lib/{mongo_db → mongodb}/driver/spec.rb +12 -12
- data/lib/mongodb/gems.rb +6 -0
- data/lib/mongodb/integration/locales.rb +4 -0
- data/lib/mongodb/integration/locales/activemodel/ru.yml +27 -0
- data/lib/mongodb/migration.rb +8 -0
- data/lib/mongodb/migration/definition.rb +19 -0
- data/lib/mongodb/migration/migration.rb +68 -0
- data/lib/mongodb/migration/tasks.rb +19 -0
- data/lib/mongodb/model.rb +26 -0
- data/lib/mongodb/model/assignment.rb +65 -0
- data/lib/mongodb/model/attribute_convertors.rb +54 -0
- data/lib/mongodb/model/callbacks.rb +36 -0
- data/lib/mongodb/model/crud.rb +57 -0
- data/lib/mongodb/model/db.rb +53 -0
- data/lib/mongodb/model/misc.rb +33 -0
- data/lib/mongodb/model/model.rb +11 -0
- data/lib/mongodb/model/query.rb +36 -0
- data/lib/mongodb/model/scope.rb +99 -0
- data/lib/mongodb/model/spec.rb +12 -0
- data/lib/mongodb/model/support/types.rb +110 -0
- data/lib/mongodb/model/validation.rb +5 -0
- data/lib/mongodb/object.rb +18 -0
- data/lib/mongodb/object/object_helper.rb +62 -0
- data/lib/mongodb/object/object_serializer.rb +273 -0
- data/readme.md +261 -6
- data/spec/driver/collection_spec.rb +83 -0
- data/spec/{mongo_model/hash → driver}/crud_spec.rb +30 -29
- data/spec/driver/database_spec.rb +9 -0
- data/spec/driver/dynamic_finders_spec.rb +50 -0
- data/spec/driver/fixes_spec.rb +12 -0
- data/spec/driver/hash_helper_spec.rb +24 -0
- data/spec/driver/spec_helper.rb +28 -0
- data/spec/integration/am_conversion_spec.rb +1 -0
- data/spec/integration/am_validation_spec.rb +34 -0
- data/spec/integration/validatable2_spec.rb +40 -0
- data/spec/migration/migration_spec.rb +60 -0
- data/spec/model/assignment_spec.rb +80 -0
- data/spec/model/attribute_convertors_spec.rb +73 -0
- data/spec/model/callbacks_spec.rb +47 -0
- data/spec/model/crud_spec.rb +151 -0
- data/spec/model/db_spec.rb +63 -0
- data/spec/model/misc_spec.rb +58 -0
- data/spec/model/query_spec.rb +47 -0
- data/spec/model/scope_spec.rb +149 -0
- data/spec/model/spec_helper.rb +4 -0
- data/spec/model/validation_spec.rb +37 -0
- data/spec/object/callbacks_spec.rb +97 -0
- data/spec/object/crud_shared.rb +53 -0
- data/spec/object/crud_spec.rb +55 -0
- data/spec/object/spec_helper.rb +14 -0
- data/spec/{mongo_model/object → object}/validation_spec.rb +38 -36
- metadata +92 -25
- data/lib/mongo_db.rb +0 -3
- data/lib/mongo_db/driver.rb +0 -5
- data/lib/mongo_db/driver/connection.rb +0 -0
- data/lib/mongo_db/driver/database.rb +0 -5
- data/lib/mongo_db/gems.rb +0 -2
- data/lib/mongo_db/model.rb +0 -0
- data/spec/mongo_ext/migration_spec.rb +0 -0
- data/spec/mongo_ext/misc_spec.rb +0 -10
- data/spec/mongo_ext/spec_helper.rb +0 -4
- data/spec/mongo_model/model/crud_spec.rb +0 -123
- data/spec/mongo_model/model/query_spec.rb +0 -0
- data/spec/mongo_model/object/callbacks_spec.rb +0 -100
- data/spec/mongo_model/object/crud_shared.rb +0 -53
- data/spec/mongo_model/object/crud_spec.rb +0 -45
- data/spec/mongo_model/spec_helper.rb +0 -1
- data/spec/query_spec.rb +0 -0
- data/spec/test_spec.rb +0 -5
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'driver/spec_helper'
|
2
|
+
|
3
|
+
describe "Dynamic Finders" do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
class FindersStub
|
8
|
+
include Mongo::DynamicFinders
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
after(:all){remove_constants :FindersStub}
|
13
|
+
|
14
|
+
it "parse_finder" do
|
15
|
+
[
|
16
|
+
[:first_by_name, 'Jim'], [:first, {name: 'Jim'}],
|
17
|
+
[:first_by_name!, 'Jim'], [:first!, {name: 'Jim'}],
|
18
|
+
|
19
|
+
[:all_by_name, 'Jim'], [:all, {name: 'Jim'}],
|
20
|
+
|
21
|
+
[:each_by_name, 'Jim'], [:each, {name: 'Jim'}],
|
22
|
+
|
23
|
+
[:by_name, 'Jim'], [:first, {name: 'Jim'}],
|
24
|
+
[:by_name!, 'Jim'], [:first!, {name: 'Jim'}],
|
25
|
+
|
26
|
+
[:first_by_id, 'id'], [:first, {_id: 'id'}],
|
27
|
+
[:first_by_id!, 'id'], [:first!, {_id: 'id'}],
|
28
|
+
[:by_id, 'id'], [:first, {_id: 'id'}],
|
29
|
+
[:by_id!, 'id'], [:first!, {_id: 'id'}],
|
30
|
+
].each_slice 2 do |check, expectation|
|
31
|
+
stub = FindersStub.new
|
32
|
+
stub.should_receive(expectation.first).with(expectation.last)
|
33
|
+
stub.send check.first, check.last
|
34
|
+
end
|
35
|
+
|
36
|
+
stub = FindersStub.new
|
37
|
+
-> {stub.invalid_finder 'a', 'b'}.should raise_error(NoMethodError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should allow to use bang version only with :first" do
|
41
|
+
stub = FindersStub.new
|
42
|
+
-> {stub.all_by_name!('Jim')}.should raise_error(/can't use bang/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'integration with collection' do
|
46
|
+
db.units.first_by_name('Jim').should be_nil
|
47
|
+
db.units.save name: 'Jim'
|
48
|
+
db.units.first_by_name('Jim')[:name].should == 'Jim'
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'driver/spec_helper'
|
2
|
+
|
3
|
+
describe "Driver fixes" do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
it "should always return array if input is array" do
|
7
|
+
db.units.insert([{name: 'Zeratul'}]).class.should == Array
|
8
|
+
|
9
|
+
db.units.insert(name: 'Zeratul').class.should == BSON::ObjectId
|
10
|
+
db.units.insert([{name: 'Zeratul'}, {name: 'Tassadar'}]).class.should == Array
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'driver/spec_helper'
|
2
|
+
|
3
|
+
describe "Collection" do
|
4
|
+
before do
|
5
|
+
@helper = Object.new
|
6
|
+
@helper.send :extend, Mongo::CollectionExt
|
7
|
+
end
|
8
|
+
|
9
|
+
it "symbolize" do
|
10
|
+
@helper.send(:symbolize_doc, {
|
11
|
+
'a' => 1,
|
12
|
+
'b' => {
|
13
|
+
'c' => 2,
|
14
|
+
'd' => [{'e' => 3}]
|
15
|
+
}
|
16
|
+
}).should == {
|
17
|
+
a: 1,
|
18
|
+
b: {
|
19
|
+
c: 2,
|
20
|
+
d: [{e: 3}]
|
21
|
+
}
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mongodb/driver'
|
2
|
+
|
3
|
+
Mongo.defaults.merge! \
|
4
|
+
symbolize: true,
|
5
|
+
convert_underscore_to_dollar: true,
|
6
|
+
batch_size: 50,
|
7
|
+
multi: true,
|
8
|
+
safe: true
|
9
|
+
|
10
|
+
require 'ruby_ext'
|
11
|
+
require 'rspec_ext'
|
12
|
+
require 'mongodb/driver/spec'
|
13
|
+
|
14
|
+
#
|
15
|
+
# Handy spec helpers
|
16
|
+
#
|
17
|
+
rspec do
|
18
|
+
def db
|
19
|
+
mongo.db
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Object.class_eval do
|
24
|
+
def mongo_id?; false end
|
25
|
+
end
|
26
|
+
BSON::ObjectId.class_eval do
|
27
|
+
def mongo_id?; true end
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# to_json, to_xml
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# require 'model/spec_helper'
|
2
|
+
# require 'active_model'
|
3
|
+
#
|
4
|
+
# describe "Validations" do
|
5
|
+
# with_mongo_model
|
6
|
+
#
|
7
|
+
# before do
|
8
|
+
# class Unit
|
9
|
+
# inherit Mongo::Model
|
10
|
+
# collection :units
|
11
|
+
#
|
12
|
+
# include ActiveModel::Validations
|
13
|
+
#
|
14
|
+
# attr_accessor :name, :status
|
15
|
+
#
|
16
|
+
# validates_presence_of :name
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# after{remove_constants :Unit}
|
21
|
+
#
|
22
|
+
# it "ActiveModel integration smoke test" do
|
23
|
+
# unit = Unit.new
|
24
|
+
# unit.should be_invalid
|
25
|
+
# unit.errors.size.should == 1
|
26
|
+
# unit.errors.first.first.should == :name
|
27
|
+
# unit.save.should be_false
|
28
|
+
#
|
29
|
+
# unit.name = 'Zeratul'
|
30
|
+
# unit.should be_valid
|
31
|
+
# unit.errors.should be_empty
|
32
|
+
# unit.save.should be_true
|
33
|
+
# end
|
34
|
+
# end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
require 'validatable'
|
3
|
+
|
4
|
+
describe "Integration with validatable2" do
|
5
|
+
with_mongo_model
|
6
|
+
|
7
|
+
before do
|
8
|
+
class Unit
|
9
|
+
inherit Mongo::Model
|
10
|
+
collection :units
|
11
|
+
|
12
|
+
include Validatable
|
13
|
+
|
14
|
+
attr_accessor :name, :status
|
15
|
+
|
16
|
+
validates_presence_of :name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
after{remove_constants :Unit}
|
21
|
+
|
22
|
+
it "ActiveModel integration smoke test" do
|
23
|
+
unit = Unit.new
|
24
|
+
unit.should_not be_valid
|
25
|
+
unit.errors.size.should == 1
|
26
|
+
unit.errors.first.first.should == :name
|
27
|
+
unit.save.should be_false
|
28
|
+
|
29
|
+
unit.name = 'Zeratul'
|
30
|
+
unit.should be_valid
|
31
|
+
unit.errors.should be_empty
|
32
|
+
unit.save.should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not save errors as instance variables" do
|
36
|
+
unit = Unit.new
|
37
|
+
unit.valid?
|
38
|
+
unit.instance_variables.select{|iv_name| iv_name !~ /^@_/}.should be_empty
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'driver/spec_helper'
|
2
|
+
require 'mongodb/migration'
|
3
|
+
|
4
|
+
describe "Migration" do
|
5
|
+
with_mongo
|
6
|
+
before{@migration = Mongo::Migration.new mongo.db}
|
7
|
+
|
8
|
+
it "shouldn't update if versions are the same" do
|
9
|
+
@migration.update(0).should be_false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "migration should provide access to database" do
|
13
|
+
@migration.add 1 do |m|
|
14
|
+
m.up do |db|
|
15
|
+
db.users.save name: 'Bob'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
@migration.update(1).should be_true
|
19
|
+
db.users.count.should == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
it "increase_db_version" do
|
23
|
+
@migration.current_version.should == 0
|
24
|
+
|
25
|
+
check = mock
|
26
|
+
@migration.add 1 do |m|
|
27
|
+
m.up{check.up}
|
28
|
+
end
|
29
|
+
|
30
|
+
check.should_receive :up
|
31
|
+
@migration.update(1).should be_true
|
32
|
+
@migration.current_version.should == 1
|
33
|
+
end
|
34
|
+
|
35
|
+
it "decrease_db_version" do
|
36
|
+
check = mock
|
37
|
+
@migration.add 1 do |m|
|
38
|
+
m.up{check.up}
|
39
|
+
m.down{check.down}
|
40
|
+
end
|
41
|
+
|
42
|
+
check.should_receive :up
|
43
|
+
@migration.update(1).should be_true
|
44
|
+
|
45
|
+
check.should_receive :down
|
46
|
+
@migration.update(0).should be_true
|
47
|
+
@migration.current_version.should == 0
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should automigrate to highest version" do
|
51
|
+
@migration.add 1 do |m|
|
52
|
+
m.up{}
|
53
|
+
end
|
54
|
+
@migration.add 2 do |m|
|
55
|
+
m.up{}
|
56
|
+
end
|
57
|
+
@migration.update.should be_true
|
58
|
+
@migration.current_version.should == 2
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Model callbacks' do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
after{remove_constants :User, :Writer}
|
7
|
+
|
8
|
+
it "should update attributes" do
|
9
|
+
class User
|
10
|
+
inherit Mongo::Model
|
11
|
+
|
12
|
+
attr_accessor :name, :has_mail, :age, :banned
|
13
|
+
end
|
14
|
+
|
15
|
+
u = User.new
|
16
|
+
u.set name: 'Alex', has_mail: '1', age: '31', banned: '0'
|
17
|
+
[u.name, u.has_mail, u.age, u.banned].should == ['Alex', '1', '31', '0']
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should update only specified attributes" do
|
21
|
+
class User
|
22
|
+
inherit Mongo::Model
|
23
|
+
|
24
|
+
attr_accessor :name, :has_mail, :age, :position, :banned
|
25
|
+
|
26
|
+
assign do
|
27
|
+
name String, true
|
28
|
+
has_mail Boolean, true
|
29
|
+
age Integer, true
|
30
|
+
position true
|
31
|
+
banned Boolean
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
u = User.new
|
36
|
+
u.set name: 'Alex', has_mail: '1', age: '31', position: [11, 34] ,banned: '0'
|
37
|
+
[u.name, u.has_mail, u.age, u.position, u.banned].should == ['Alex', true, 31, [11, 34], nil]
|
38
|
+
|
39
|
+
# should allow to forcefully cast and update any attribute
|
40
|
+
u.set! banned: '0'
|
41
|
+
u.banned.should == false
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should inherit assignment rules" do
|
45
|
+
class User
|
46
|
+
inherit Mongo::Model
|
47
|
+
|
48
|
+
attr_accessor :age
|
49
|
+
|
50
|
+
assign do
|
51
|
+
age Integer, true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Writer < User
|
56
|
+
attr_accessor :posts
|
57
|
+
|
58
|
+
assign do
|
59
|
+
posts Integer, true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
u = Writer.new
|
64
|
+
u.set age: '20', posts: '12'
|
65
|
+
[u.age, u.posts].should == [20, 12]
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'casting smoke test' do
|
69
|
+
[
|
70
|
+
Boolean, '1', true,
|
71
|
+
Date, '2011-08-23', Date.parse('2011-08-23'),
|
72
|
+
Float, '1.2', 1.2,
|
73
|
+
Integer, '10', 10,
|
74
|
+
String, 'Hi', 'Hi',
|
75
|
+
Time, '2011-08-23', Date.parse('2011-08-23').to_time
|
76
|
+
].each_slice 3 do |type, raw, expected|
|
77
|
+
type.cast(raw).should == expected
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'model/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._clear_cache
|
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,47 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Model callbacks' do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
after(:all){remove_constants :TheModel, :Player}
|
7
|
+
|
8
|
+
it "callback integration" do
|
9
|
+
class TheModel
|
10
|
+
inherit Mongo::Model
|
11
|
+
end
|
12
|
+
|
13
|
+
model = TheModel.new
|
14
|
+
|
15
|
+
model.should_receive(:run_before_callbacks).with(:save, {method: :save})
|
16
|
+
model._run_callbacks :before, :save
|
17
|
+
end
|
18
|
+
|
19
|
+
it "integration smoke test" do
|
20
|
+
class Player
|
21
|
+
inherit Mongo::Model
|
22
|
+
|
23
|
+
before_validate :before_validate_check
|
24
|
+
after_save :after_save_check
|
25
|
+
|
26
|
+
attr_accessor :missions
|
27
|
+
|
28
|
+
class Mission
|
29
|
+
inherit Mongo::Model
|
30
|
+
|
31
|
+
before_validate :before_validate_check
|
32
|
+
after_save :after_save_check
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
mission = Player::Mission.new
|
37
|
+
player = Player.new
|
38
|
+
player.missions = [mission]
|
39
|
+
|
40
|
+
player.should_receive(:before_validate_check).once.ordered.and_return(nil)
|
41
|
+
mission.should_receive(:before_validate_check).once.ordered.and_return(nil)
|
42
|
+
player.should_receive(:after_save_check).once.ordered.and_return(nil)
|
43
|
+
mission.should_receive(:after_save_check).once.ordered.and_return(nil)
|
44
|
+
|
45
|
+
db.units.save(player).should be_true
|
46
|
+
end
|
47
|
+
end
|