mongo_mapper-unstable 2010.2.8 → 2010.2.9
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/VERSION +1 -1
- data/lib/mongo_mapper/document.rb +1 -1
- data/lib/mongo_mapper.rb +40 -30
- data/test/functional/test_document.rb +22 -42
- data/test/test_helper.rb +6 -0
- data/test/unit/test_mongo_mapper.rb +69 -9
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2010.02.
|
1
|
+
2010.02.09
|
data/lib/mongo_mapper.rb
CHANGED
@@ -17,83 +17,93 @@ require 'validatable'
|
|
17
17
|
module MongoMapper
|
18
18
|
# generic MM error
|
19
19
|
class MongoMapperError < StandardError; end
|
20
|
-
|
20
|
+
|
21
21
|
# raised when key expected to exist but not found
|
22
22
|
class KeyNotFound < MongoMapperError; end
|
23
|
-
|
23
|
+
|
24
24
|
# raised when document expected but not found
|
25
25
|
class DocumentNotFound < MongoMapperError; end
|
26
|
-
|
26
|
+
|
27
27
|
# raised when document not valid and using !
|
28
28
|
class DocumentNotValid < MongoMapperError
|
29
29
|
def initialize(document)
|
30
30
|
super("Validation failed: #{document.errors.full_messages.join(", ")}")
|
31
31
|
end
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
# @api public
|
35
35
|
def self.connection
|
36
36
|
@@connection ||= Mongo::Connection.new
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
# @api public
|
40
40
|
def self.connection=(new_connection)
|
41
41
|
@@connection = new_connection
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
# @api public
|
45
45
|
def self.logger
|
46
46
|
connection.logger
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
# @api public
|
50
50
|
def self.database=(name)
|
51
51
|
@@database = nil
|
52
52
|
@@database_name = name
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
# @api public
|
56
56
|
def self.database
|
57
57
|
if @@database_name.blank?
|
58
58
|
raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
@@database ||= MongoMapper.connection.db(@@database_name)
|
62
62
|
end
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@@ensured_indexes ||= []
|
63
|
+
|
64
|
+
def self.config=(hash)
|
65
|
+
@@config = hash
|
67
66
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
@@
|
67
|
+
|
68
|
+
def self.config
|
69
|
+
raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
|
70
|
+
@@config
|
72
71
|
end
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
72
|
+
|
73
|
+
def self.connect(environment, options={})
|
74
|
+
raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
|
75
|
+
MongoMapper.connection = Mongo::Connection.new(config[environment]['host'], config[environment]['port'], options)
|
76
|
+
MongoMapper.database = config[environment]['database']
|
77
|
+
if config[environment]['username'].present? && config[environment]['password'].present?
|
78
|
+
MongoMapper.database.authenticate(config[environment]['username'], config[environment]['password'])
|
79
|
+
end
|
77
80
|
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
|
82
|
+
def self.setup(config, environment, options={})
|
83
|
+
using_passenger = options.delete(:passenger)
|
84
|
+
handle_passenger_forking if using_passenger
|
85
|
+
self.config = config
|
86
|
+
connect(environment, options)
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.handle_passenger_forking
|
90
|
+
if defined?(PhusionPassenger)
|
91
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
92
|
+
connection.connect_to_master if forked
|
93
|
+
end
|
84
94
|
end
|
85
95
|
end
|
86
|
-
|
96
|
+
|
87
97
|
# @api private
|
88
98
|
def self.use_time_zone?
|
89
99
|
Time.respond_to?(:zone) && Time.zone ? true : false
|
90
100
|
end
|
91
|
-
|
101
|
+
|
92
102
|
# @api private
|
93
103
|
def self.time_class
|
94
104
|
use_time_zone? ? Time.zone : Time
|
95
105
|
end
|
96
|
-
|
106
|
+
|
97
107
|
# @api private
|
98
108
|
def self.normalize_object_id(value)
|
99
109
|
value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
|
@@ -778,68 +778,55 @@ class DocumentTest < Test::Unit::TestCase
|
|
778
778
|
|
779
779
|
context "#save (with options)" do
|
780
780
|
setup do
|
781
|
-
MongoMapper.ensured_indexes = []
|
782
|
-
|
783
781
|
@document = Doc do
|
784
782
|
key :name, String
|
785
783
|
set_collection_name 'test_indexes'
|
786
|
-
ensure_index :name, :unique => true
|
787
|
-
end
|
788
|
-
if @document.database.collection_names.include?(@document.collection.name)
|
789
|
-
@document.collection.drop_indexes
|
790
784
|
end
|
791
|
-
|
792
|
-
|
785
|
+
drop_indexes(@document)
|
786
|
+
@document.ensure_index :name, :unique => true
|
793
787
|
end
|
794
|
-
|
788
|
+
|
795
789
|
should "allow passing safe" do
|
796
|
-
|
797
|
-
doc.save
|
798
|
-
|
790
|
+
@document.create(:name => 'John')
|
799
791
|
assert_raises(Mongo::OperationFailure) do
|
800
792
|
@document.new(:name => 'John').save(:safe => true)
|
801
793
|
end
|
802
794
|
end
|
803
|
-
|
795
|
+
|
804
796
|
should "raise argument error if options has unsupported key" do
|
805
|
-
|
806
|
-
|
797
|
+
assert_raises(ArgumentError) do
|
798
|
+
@document.new.save(:foo => true)
|
799
|
+
end
|
807
800
|
end
|
808
801
|
end
|
809
802
|
|
810
803
|
context "#save! (with options)" do
|
811
804
|
setup do
|
812
|
-
MongoMapper.ensured_indexes = []
|
813
|
-
|
814
805
|
@document = Doc do
|
815
806
|
key :name, String
|
816
807
|
set_collection_name 'test_indexes'
|
817
|
-
ensure_index :name, :unique => true
|
818
808
|
end
|
819
|
-
|
820
|
-
|
821
|
-
@document.collection.drop_indexes
|
822
|
-
end
|
823
|
-
|
824
|
-
MongoMapper.ensure_indexes!
|
809
|
+
drop_indexes(@document)
|
810
|
+
@document.ensure_index :name, :unique => true
|
825
811
|
end
|
826
|
-
|
812
|
+
|
827
813
|
should "allow passing safe" do
|
828
|
-
|
829
|
-
|
814
|
+
@document.create(:name => 'John')
|
830
815
|
assert_raises(Mongo::OperationFailure) do
|
831
816
|
@document.new(:name => 'John').save!(:safe => true)
|
832
817
|
end
|
833
818
|
end
|
834
|
-
|
819
|
+
|
835
820
|
should "raise argument error if options has unsupported key" do
|
836
|
-
|
837
|
-
|
821
|
+
assert_raises(ArgumentError) do
|
822
|
+
@document.new.save!(:foo => true)
|
823
|
+
end
|
838
824
|
end
|
839
|
-
|
825
|
+
|
840
826
|
should "raise argument error if using validate as that would be pointless with save!" do
|
841
|
-
|
842
|
-
|
827
|
+
assert_raises(ArgumentError) do
|
828
|
+
@document.new.save!(:validate => false)
|
829
|
+
end
|
843
830
|
end
|
844
831
|
end
|
845
832
|
|
@@ -1224,28 +1211,22 @@ class DocumentTest < Test::Unit::TestCase
|
|
1224
1211
|
|
1225
1212
|
context "Indexing" do
|
1226
1213
|
setup do
|
1227
|
-
|
1228
|
-
@document.collection.drop_indexes
|
1214
|
+
drop_indexes(@document)
|
1229
1215
|
end
|
1230
1216
|
|
1231
1217
|
should "allow creating index for a key" do
|
1232
1218
|
@document.ensure_index :first_name
|
1233
|
-
MongoMapper.ensure_indexes!
|
1234
|
-
|
1235
1219
|
@document.should have_index('first_name_1')
|
1236
1220
|
end
|
1237
1221
|
|
1238
1222
|
should "allow creating unique index for a key" do
|
1239
1223
|
@document.ensure_index :first_name, :unique => true
|
1240
|
-
MongoMapper.ensure_indexes!
|
1241
|
-
|
1242
1224
|
@document.should have_index('first_name_1')
|
1243
1225
|
end
|
1244
1226
|
|
1245
1227
|
should "allow creating index on multiple keys" do
|
1246
1228
|
@document.ensure_index [[:first_name, 1], [:last_name, -1]]
|
1247
|
-
|
1248
|
-
|
1229
|
+
|
1249
1230
|
# order is different for different versions of ruby so instead of
|
1250
1231
|
# just checking have_index('first_name_1_last_name_-1') I'm checking
|
1251
1232
|
# the values of the indexes to make sure the index creation was successful
|
@@ -1257,7 +1238,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
1257
1238
|
|
1258
1239
|
should "work with :index shortcut when defining key" do
|
1259
1240
|
@document.key :father, String, :index => true
|
1260
|
-
MongoMapper.ensure_indexes!
|
1261
1241
|
@document.should have_index('father_1')
|
1262
1242
|
end
|
1263
1243
|
end
|
data/test/test_helper.rb
CHANGED
@@ -46,6 +46,12 @@ class Test::Unit::TestCase
|
|
46
46
|
klass.class_eval(&block) if block_given?
|
47
47
|
klass
|
48
48
|
end
|
49
|
+
|
50
|
+
def drop_indexes(klass)
|
51
|
+
if klass.database.collection_names.include?(klass.collection.name)
|
52
|
+
klass.collection.drop_indexes
|
53
|
+
end
|
54
|
+
end
|
49
55
|
end
|
50
56
|
|
51
57
|
test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
|
@@ -8,58 +8,118 @@ class MongoMapperTest < Test::Unit::TestCase
|
|
8
8
|
MongoMapper.connection = conn
|
9
9
|
MongoMapper.connection.should == conn
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
should "default connection to new mongo ruby driver" do
|
13
13
|
MongoMapper.connection = nil
|
14
14
|
MongoMapper.connection.should be_instance_of(Mongo::Connection)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
should "be able to write and read default database" do
|
18
18
|
MongoMapper.database = 'test'
|
19
19
|
MongoMapper.database.should be_instance_of(Mongo::DB)
|
20
20
|
MongoMapper.database.name.should == 'test'
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
should "have document not found error" do
|
24
24
|
lambda {
|
25
25
|
MongoMapper::DocumentNotFound
|
26
26
|
}.should_not raise_error
|
27
27
|
end
|
28
|
+
|
29
|
+
should "be able to read/write config" do
|
30
|
+
config = {
|
31
|
+
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'},
|
32
|
+
'production' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test-prod'}
|
33
|
+
}
|
34
|
+
|
35
|
+
MongoMapper.config = config
|
36
|
+
MongoMapper.config.should == config
|
37
|
+
end
|
38
|
+
|
39
|
+
context "connecting to environment from config" do
|
40
|
+
should "work without authentication" do
|
41
|
+
MongoMapper.config = {
|
42
|
+
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
|
43
|
+
}
|
44
|
+
|
45
|
+
Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
|
46
|
+
MongoMapper.expects(:database=).with('test')
|
47
|
+
Mongo::DB.any_instance.expects(:authenticate).never
|
48
|
+
MongoMapper.connect('development')
|
49
|
+
end
|
50
|
+
|
51
|
+
should "work with options" do
|
52
|
+
MongoMapper.config = {
|
53
|
+
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
|
54
|
+
}
|
55
|
+
|
56
|
+
connection, logger = mock('connection'), mock('logger')
|
57
|
+
Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
|
58
|
+
MongoMapper.connect('development', :logger => logger)
|
59
|
+
end
|
60
|
+
|
61
|
+
should "work with authentication" do
|
62
|
+
MongoMapper.config = {
|
63
|
+
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
|
64
|
+
}
|
65
|
+
|
66
|
+
Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
|
67
|
+
MongoMapper.connect('development')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "setup" do
|
72
|
+
should "work as shortcut for setting config, environment and options" do
|
73
|
+
config, logger = mock('config'), mock('logger')
|
74
|
+
MongoMapper.expects(:config=).with(config)
|
75
|
+
MongoMapper.expects(:connect).with('development', :logger => logger)
|
76
|
+
MongoMapper.expects(:handle_passenger_forking).never
|
77
|
+
MongoMapper.setup(config, 'development', :logger => logger)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "handle passenger if option present" do
|
81
|
+
config, logger = mock('config'), mock('logger')
|
82
|
+
MongoMapper.expects(:config=).with(config)
|
83
|
+
MongoMapper.expects(:connect).with('development', :logger => logger)
|
84
|
+
MongoMapper.expects(:handle_passenger_forking)
|
85
|
+
MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
|
86
|
+
end
|
87
|
+
end
|
28
88
|
|
89
|
+
|
29
90
|
context "use_time_zone?" do
|
30
91
|
should "be true if Time.zone set" do
|
31
92
|
Time.zone = 'Hawaii'
|
32
93
|
MongoMapper.use_time_zone?.should be_true
|
33
94
|
Time.zone = nil
|
34
95
|
end
|
35
|
-
|
96
|
+
|
36
97
|
should "be false if Time.zone not set" do
|
37
98
|
MongoMapper.use_time_zone?.should be_false
|
38
99
|
end
|
39
100
|
end
|
40
|
-
|
101
|
+
|
41
102
|
context "time_class" do
|
42
103
|
should "be Time.zone if using time zones" do
|
43
104
|
Time.zone = 'Hawaii'
|
44
105
|
MongoMapper.time_class.should == Time.zone
|
45
106
|
Time.zone = nil
|
46
107
|
end
|
47
|
-
|
108
|
+
|
48
109
|
should "be Time if not using time zones" do
|
49
110
|
MongoMapper.time_class.should == Time
|
50
111
|
end
|
51
112
|
end
|
52
|
-
|
113
|
+
|
53
114
|
context "normalize_object_id" do
|
54
115
|
should "turn string into object id" do
|
55
116
|
id = Mongo::ObjectID.new
|
56
117
|
MongoMapper.normalize_object_id(id.to_s).should == id
|
57
118
|
end
|
58
|
-
|
119
|
+
|
59
120
|
should "leave object id alone" do
|
60
121
|
id = Mongo::ObjectID.new
|
61
122
|
MongoMapper.normalize_object_id(id).should == id
|
62
123
|
end
|
63
124
|
end
|
64
|
-
|
65
125
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper-unstable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2010.2.
|
4
|
+
version: 2010.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-09 00:00:00 -05:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|