mongo_mapper-unstable 2010.1.27 → 2010.1.28

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.
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ class ProtectedTest < Test::Unit::TestCase
4
+ context 'A document with protected attributes' do
5
+ setup do
6
+ @doc_class = Doc do
7
+ key :name, String
8
+ key :admin, Boolean, :default => false
9
+
10
+ attr_protected :admin
11
+ end
12
+
13
+ @doc = @doc_class.create(:name => 'Steve Sloan')
14
+ end
15
+
16
+ should 'have protected attributes class method' do
17
+ @doc_class.protected_attributes.should == [:admin].to_set
18
+ end
19
+
20
+ should "default protected attributes to nil" do
21
+ Doc().protected_attributes.should be_nil
22
+ end
23
+
24
+ should "have protected attributes instance method" do
25
+ @doc.protected_attributes.should equal(@doc_class.protected_attributes)
26
+ end
27
+
28
+ should "work with :protected shortcut when defining key" do
29
+ Doc() do
30
+ key :user_id, ObjectId, :protected => true
31
+ end.protected_attributes.should == [:user_id].to_set
32
+ end
33
+
34
+ should 'assign protected attribute through accessor' do
35
+ @doc.admin = true
36
+ @doc.admin.should be_true
37
+ end
38
+
39
+ should 'ignore protected attribute on #update_attributes' do
40
+ @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
41
+ @doc.name.should == 'Ren Hoek'
42
+ @doc.admin.should be_false
43
+ end
44
+
45
+ should 'ignore protected attribute on #update_attributes!' do
46
+ @doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
47
+ @doc.name.should == 'Stimpson J. Cat'
48
+ @doc.admin.should be_false
49
+ end
50
+ end
51
+
52
+ context "Single collection inherited protected attributes" do
53
+ setup do
54
+ class ::GrandParent
55
+ include MongoMapper::Document
56
+
57
+ key :_type, String
58
+ key :site_id, ObjectId
59
+
60
+ attr_protected :site_id
61
+ end
62
+ GrandParent.collection.remove
63
+
64
+ class ::Child < ::GrandParent
65
+ key :position, Integer
66
+
67
+ attr_protected :position
68
+ end
69
+
70
+ class ::GrandChild < ::Child; end
71
+
72
+ class ::OtherChild < ::GrandParent
73
+ key :blog_id, ObjectId
74
+
75
+ attr_protected :blog_id
76
+ end
77
+ end
78
+
79
+ teardown do
80
+ Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
81
+ Object.send :remove_const, 'Child' if defined?(::Child)
82
+ Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
83
+ Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
84
+ end
85
+
86
+ should "share keys down the inheritance trail" do
87
+ GrandParent.protected_attributes.should == [:site_id].to_set
88
+ Child.protected_attributes.should == [:site_id, :position].to_set
89
+ GrandChild.protected_attributes.should == [:site_id, :position].to_set
90
+ OtherChild.protected_attributes.should == [:site_id, :blog_id].to_set
91
+ end
92
+ end
93
+
94
+ context 'An embedded document with protected attributes' do
95
+ setup do
96
+ @doc_class = Doc('Project')
97
+ @edoc_class = EDoc('Person') do
98
+ key :name, String
99
+ key :admin, Boolean, :default => false
100
+
101
+ attr_protected :admin
102
+ end
103
+ @doc_class.many :people, :class => @edoc_class
104
+
105
+ @doc = @doc_class.create(:title => 'MongoMapper')
106
+ @edoc = @edoc_class.new(:name => 'Steve Sloan')
107
+ @doc.people << @edoc
108
+ end
109
+
110
+ should 'have protected attributes class method' do
111
+ @edoc_class.protected_attributes.should == [:admin].to_set
112
+ end
113
+
114
+ should "default protected attributes to nil" do
115
+ EDoc().protected_attributes.should be_nil
116
+ end
117
+
118
+ should "have protected attributes instance method" do
119
+ @edoc.protected_attributes.should equal(@edoc_class.protected_attributes)
120
+ end
121
+
122
+ should 'assign protected attribute through accessor' do
123
+ @edoc.admin = true
124
+ @edoc.admin.should be_true
125
+ end
126
+
127
+ should 'ignore protected attribute on #update_attributes' do
128
+ @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
129
+ @edoc.name.should == 'Ren Hoek'
130
+ @edoc.admin.should be_false
131
+ end
132
+
133
+ should 'ignore protected attribute on #update_attributes!' do
134
+ @edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
135
+ @edoc.name.should == 'Stimpson J. Cat'
136
+ @edoc.admin.should be_false
137
+ end
138
+ end
139
+ end
data/test/test_helper.rb CHANGED
@@ -16,46 +16,40 @@ require 'support/timing'
16
16
 
17
17
  class Test::Unit::TestCase
18
18
  include CustomMatchers
19
-
20
- cattr_accessor :mm_document_count
21
- self.mm_document_count = 0
22
-
19
+
23
20
  def Doc(name=nil, &block)
24
- Test::Unit::TestCase.mm_document_count += 1
25
-
26
21
  klass = Class.new do
27
22
  include MongoMapper::Document
28
23
  set_collection_name "test#{rand(20)}"
29
-
24
+
30
25
  if name
31
26
  class_eval "def self.name; '#{name}' end"
32
27
  class_eval "def self.to_s; '#{name}' end"
33
28
  end
34
-
35
- class_eval(&block) if block_given?
36
29
  end
30
+
31
+ klass.class_eval(&block) if block_given?
37
32
  klass.collection.remove
38
33
  klass
39
34
  end
40
-
35
+
41
36
  def EDoc(name=nil, &block)
42
- Class.new do
37
+ klass = Class.new do
43
38
  include MongoMapper::EmbeddedDocument
44
-
39
+
45
40
  if name
46
41
  class_eval "def self.name; '#{name}' end"
47
42
  class_eval "def self.to_s; '#{name}' end"
48
43
  end
49
-
50
- class_eval(&block) if block_given?
51
44
  end
45
+
46
+ klass.class_eval(&block) if block_given?
47
+ klass
52
48
  end
53
49
  end
54
50
 
55
51
  test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
56
52
  FileUtils.mkdir_p(test_dir) unless File.exist?(test_dir)
57
53
 
58
- MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {
59
- :logger => Logger.new(test_dir + '/test.log')
60
- })
54
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {:logger => Logger.new(test_dir + '/test.log')})
61
55
  MongoMapper.database = 'test'