veneer 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +12 -1
- data/README.md +55 -87
- data/Rakefile +1 -1
- data/lib/veneer.rb +6 -1
- data/lib/veneer/adapters/activerecord.rb +2 -0
- data/lib/veneer/adapters/activerecord/class_wrapper.rb +99 -5
- data/lib/veneer/adapters/activerecord/instance_wrapper.rb +4 -2
- data/lib/veneer/adapters/activerecord/pre_3.0_class_wrapper.rb +62 -1
- data/lib/veneer/adapters/activerecord/property.rb +22 -0
- data/lib/veneer/adapters/datamapper.rb +1 -0
- data/lib/veneer/adapters/datamapper/class_wrapper.rb +90 -6
- data/lib/veneer/adapters/datamapper/property.rb +25 -0
- data/lib/veneer/adapters/mongomapper.rb +1 -0
- data/lib/veneer/adapters/mongomapper/class_wrapper.rb +68 -0
- data/lib/veneer/adapters/mongomapper/property.rb +16 -0
- data/lib/veneer/base/class_wrapper.rb +110 -7
- data/lib/veneer/base/property.rb +27 -0
- data/lib/veneer/lint.rb +4 -180
- data/lib/veneer/lint/adapter.rb +282 -0
- data/lib/veneer/lint/base.rb +10 -0
- data/lib/veneer/lint/properties.rb +78 -0
- data/lib/veneer/version.rb +1 -1
- data/test/support/helpers.rb +10 -1
- data/test/test_helper.rb +3 -3
- data/test/veneer/activerecord/test_adapter.rb +80 -0
- data/test/veneer/activerecord/test_properties.rb +73 -0
- data/test/veneer/datamapper/test_adapter.rb +71 -0
- data/test/veneer/datamapper/test_properties.rb +59 -0
- data/test/veneer/{adapters/test_mongomapper.rb → mongomapper/test_adapter.rb} +35 -11
- data/test/veneer/mongomapper/test_properties.rb +56 -0
- data/test/veneer_test.rb +35 -0
- data/veneer.gemspec +2 -0
- metadata +57 -51
- data/test/veneer/adapters/test_active_record.rb +0 -56
- data/test/veneer/adapters/test_datamapper.rb +0 -56
@@ -0,0 +1,27 @@
|
|
1
|
+
module Veneer
|
2
|
+
module Base
|
3
|
+
class Property < Hashie::Dash
|
4
|
+
attr_reader :parent
|
5
|
+
|
6
|
+
property :constraints
|
7
|
+
property :name
|
8
|
+
property :type
|
9
|
+
property :primary
|
10
|
+
|
11
|
+
alias :primary? :primary
|
12
|
+
|
13
|
+
def initialize(parent, properties)
|
14
|
+
@parent = parent
|
15
|
+
super properties.merge(:type => normalize(properties[:type]))
|
16
|
+
end
|
17
|
+
|
18
|
+
def normalize(properties)
|
19
|
+
::Kernel.raise ::Veneer::Errors::NotImplemented
|
20
|
+
end
|
21
|
+
|
22
|
+
def validators
|
23
|
+
parent.validators_on(name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/veneer/lint.rb
CHANGED
@@ -1,183 +1,7 @@
|
|
1
1
|
module Veneer
|
2
2
|
module Lint
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
include ::Veneer::Lint::InstanceWrapperLint
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def _veneer_teardown
|
11
|
-
Veneer(@klass).destroy_all
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassWrapperLint
|
15
|
-
def test_should_have_the_correct_inner_constants_for_veneer
|
16
|
-
assert_nothing_raised do
|
17
|
-
@klass::VeneerInterface
|
18
|
-
@klass::VeneerInterface::ClassWrapper
|
19
|
-
@klass::VeneerInterface::InstanceWrapper
|
20
|
-
end
|
21
|
-
assert @klass::VeneerInterface::ClassWrapper.ancestors.include?(::Veneer::Base::ClassWrapper)
|
22
|
-
assert @klass::VeneerInterface::InstanceWrapper.ancestors.include?(::Veneer::Base::InstanceWrapper)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_should_setup_the_veneer_lint_class_wrapper_with_a_klass
|
26
|
-
assert_not_nil @klass, "@klass should provide a class to test Veneer with"
|
27
|
-
assert_kind_of Class, @klass
|
28
|
-
ensure
|
29
|
-
_veneer_teardown
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_should_provide_valid_attributes
|
33
|
-
assert_not_nil @valid_attributes
|
34
|
-
assert_kind_of Hash, @valid_attributes
|
35
|
-
ensure
|
36
|
-
_veneer_teardown
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_should_provide_invalid_attributes
|
40
|
-
assert_not_nil @invalid_attributes
|
41
|
-
assert_kind_of Hash, @invalid_attributes
|
42
|
-
ensure
|
43
|
-
_veneer_teardown
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_should_create_an_object_from_a_hash
|
47
|
-
r = Veneer(@klass).create(@valid_attributes)
|
48
|
-
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
49
|
-
ensure
|
50
|
-
_veneer_teardown
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_should_create_the_object_from_the_hash_with_create!
|
54
|
-
r = Veneer(@klass).create!(@valid_attributes)
|
55
|
-
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
56
|
-
ensure
|
57
|
-
_veneer_teardown
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_should_instantiate_a_new_instance_of_the_object
|
61
|
-
r = Veneer(@klass).new(@valid_attributes)
|
62
|
-
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
63
|
-
ensure
|
64
|
-
_veneer_teardown
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_implementst_create_with_invalid_attributes
|
68
|
-
assert_raise Veneer::Errors::NotSaved do
|
69
|
-
Veneer(@klass).create!(@invalid_attributes)
|
70
|
-
end
|
71
|
-
ensure
|
72
|
-
_veneer_teardown
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_should_not_raise_when_create_can_save
|
76
|
-
assert_nothing_raised do
|
77
|
-
Veneer(@klass).create(@invalid_attributes)
|
78
|
-
end
|
79
|
-
ensure
|
80
|
-
_veneer_teardown
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_should_destory_all
|
84
|
-
Veneer(@klass).create(@valid_attributes)
|
85
|
-
assert Veneer(@klass).all.size > 0
|
86
|
-
Veneer(@klass).destroy_all
|
87
|
-
assert_equal 0, Veneer(@klass).all.size
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_should_find_all
|
91
|
-
create_valid_items(4)
|
92
|
-
result = Veneer(@klass).all
|
93
|
-
assert_kind_of Array, result
|
94
|
-
assert_equal 4, result.size
|
95
|
-
ensure
|
96
|
-
_veneer_teardown
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_should_find_first
|
100
|
-
create_valid_items(3)
|
101
|
-
result = Veneer(@klass).all
|
102
|
-
assert_not_nil result
|
103
|
-
ensure
|
104
|
-
_veneer_teardown
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_should_implement_limit
|
108
|
-
create_valid_items(4)
|
109
|
-
result = Veneer(@klass).all(:limit => 2)
|
110
|
-
assert_equal 2, result.size
|
111
|
-
ensure
|
112
|
-
_veneer_teardown
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_should_implement_order
|
116
|
-
create_valid_items(4)
|
117
|
-
key = @valid_attributes.keys.first
|
118
|
-
ordered_result = Veneer(@klass).all(:order => key)
|
119
|
-
result = Veneer(@klass).all
|
120
|
-
result = result.sort_by{ |i| i.send(key) }
|
121
|
-
assert_equal ordered_result, result
|
122
|
-
ensure
|
123
|
-
_veneer_teardown
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_should_implement_order_with_desc
|
127
|
-
create_valid_items(4)
|
128
|
-
key = @valid_attributes.keys.first
|
129
|
-
ordered_result = Veneer(@klass).all(:order => "#{key} desc")
|
130
|
-
result = Veneer(@klass).all
|
131
|
-
result = result.sort_by{ |i| i.send(key) }.reverse
|
132
|
-
assert_equal ordered_result, result
|
133
|
-
ensure
|
134
|
-
_veneer_teardown
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_should_implement_offset
|
138
|
-
create_valid_items(4)
|
139
|
-
result = Veneer(@klass).all
|
140
|
-
offset_result = Veneer(@klass).all(:offset => 2, :limit => 2)
|
141
|
-
assert_equal result[2..4], offset_result
|
142
|
-
ensure
|
143
|
-
_veneer_teardown
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_conditions
|
147
|
-
create_valid_items(3)
|
148
|
-
Veneer(@klass).create(@valid_attributes)
|
149
|
-
result = Veneer(@klass).all(:conditions => @valid_attributes)
|
150
|
-
assert_equal 1, result.size
|
151
|
-
ensure
|
152
|
-
_veneer_teardown
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
module InstanceWrapperLint
|
157
|
-
def test_should_implement_destroy
|
158
|
-
item = Veneer(@klass).create(@valid_attributes)
|
159
|
-
assert Veneer(@klass).all.size > 0
|
160
|
-
assert item.kind_of?(Veneer::Base::InstanceWrapper)
|
161
|
-
item.destroy
|
162
|
-
assert Veneer(@klass).all.size == 0
|
163
|
-
ensure
|
164
|
-
_veneer_teardown
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_should_implement_new_record?
|
168
|
-
item = Veneer(@klass).new(@valid_attributes)
|
169
|
-
assert item.new_record?
|
170
|
-
assert item.save
|
171
|
-
assert !item.new_record?
|
172
|
-
ensure
|
173
|
-
_veneer_teardown
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_should_implement_save!
|
177
|
-
assert_raises Veneer::Errors::NotSaved do
|
178
|
-
Veneer(@klass).create!(@invalid_attributes)
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
3
|
+
autoload :Adapter, 'veneer/lint/adapter'
|
4
|
+
autoload :Properties, 'veneer/lint/properties'
|
5
|
+
autoload :Base, 'veneer/lint/base'
|
182
6
|
end
|
183
|
-
end
|
7
|
+
end
|
@@ -0,0 +1,282 @@
|
|
1
|
+
module Veneer
|
2
|
+
module Lint
|
3
|
+
module Adapter
|
4
|
+
include Veneer::Lint::Base
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
include ::Veneer::Lint::Adapter::ClassWrapperLint
|
9
|
+
include ::Veneer::Lint::Adapter::InstanceWrapperLint
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def _veneer_teardown
|
14
|
+
Veneer(@klass).destroy_all
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassWrapperLint
|
18
|
+
def test_should_have_the_correct_inner_constants_for_veneer
|
19
|
+
assert_nothing_raised do
|
20
|
+
@klass::VeneerInterface
|
21
|
+
@klass::VeneerInterface::ClassWrapper
|
22
|
+
@klass::VeneerInterface::InstanceWrapper
|
23
|
+
end
|
24
|
+
assert @klass::VeneerInterface::ClassWrapper.ancestors.include?(::Veneer::Base::ClassWrapper)
|
25
|
+
assert @klass::VeneerInterface::InstanceWrapper.ancestors.include?(::Veneer::Base::InstanceWrapper)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_setup_the_veneer_lint_class_wrapper_with_a_klass
|
29
|
+
super
|
30
|
+
ensure
|
31
|
+
_veneer_teardown
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_provide_valid_attributes
|
35
|
+
assert_not_nil @valid_attributes
|
36
|
+
assert_kind_of Hash, @valid_attributes
|
37
|
+
ensure
|
38
|
+
_veneer_teardown
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_provide_invalid_attributes
|
42
|
+
assert_not_nil @invalid_attributes
|
43
|
+
assert_kind_of Hash, @invalid_attributes
|
44
|
+
ensure
|
45
|
+
_veneer_teardown
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_create_an_object_from_a_hash
|
49
|
+
r = Veneer(@klass).create(@valid_attributes)
|
50
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
51
|
+
ensure
|
52
|
+
_veneer_teardown
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_create_the_object_from_the_hash_with_create!
|
56
|
+
r = Veneer(@klass).create!(@valid_attributes)
|
57
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
58
|
+
ensure
|
59
|
+
_veneer_teardown
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_instantiate_a_new_instance_of_the_object
|
63
|
+
r = Veneer(@klass).new(@valid_attributes)
|
64
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
65
|
+
ensure
|
66
|
+
_veneer_teardown
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_implementst_create_with_invalid_attributes
|
70
|
+
assert_raise Veneer::Errors::NotSaved do
|
71
|
+
Veneer(@klass).create!(@invalid_attributes)
|
72
|
+
end
|
73
|
+
ensure
|
74
|
+
_veneer_teardown
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_should_not_raise_when_create_can_save
|
78
|
+
assert_nothing_raised do
|
79
|
+
Veneer(@klass).create(@invalid_attributes)
|
80
|
+
end
|
81
|
+
ensure
|
82
|
+
_veneer_teardown
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_should_destory_all
|
86
|
+
Veneer(@klass).create(@valid_attributes)
|
87
|
+
assert Veneer(@klass).all.size > 0
|
88
|
+
Veneer(@klass).destroy_all
|
89
|
+
assert_equal 0, Veneer(@klass).all.size
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_should_find_all
|
93
|
+
create_valid_items(4)
|
94
|
+
result = Veneer(@klass).all
|
95
|
+
assert_kind_of Array, result
|
96
|
+
assert_equal 4, result.size
|
97
|
+
ensure
|
98
|
+
_veneer_teardown
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_should_find_first
|
102
|
+
create_valid_items(3)
|
103
|
+
result = Veneer(@klass).all
|
104
|
+
assert_not_nil result
|
105
|
+
ensure
|
106
|
+
_veneer_teardown
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_should_implement_limit
|
110
|
+
create_valid_items(4)
|
111
|
+
result = Veneer(@klass).all(:limit => 2)
|
112
|
+
assert_equal 2, result.size
|
113
|
+
ensure
|
114
|
+
_veneer_teardown
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_should_implement_order
|
118
|
+
create_valid_items(4)
|
119
|
+
key = @valid_attributes.keys.first
|
120
|
+
ordered_result = Veneer(@klass).all(:order => key)
|
121
|
+
result = Veneer(@klass).all
|
122
|
+
result = result.sort_by{ |i| i.send(key) }
|
123
|
+
assert_equal ordered_result, result
|
124
|
+
ensure
|
125
|
+
_veneer_teardown
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_should_implement_order_with_desc
|
129
|
+
create_valid_items(4)
|
130
|
+
ordered_result = Veneer(@klass).all(:order => "name desc")
|
131
|
+
result = Veneer(@klass).all
|
132
|
+
result = result.sort_by{ |i| i.name }.reverse
|
133
|
+
assert_equal ordered_result, result
|
134
|
+
ensure
|
135
|
+
_veneer_teardown
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_should_implement_offset
|
139
|
+
create_valid_items(4)
|
140
|
+
result = Veneer(@klass).all
|
141
|
+
offset_result = Veneer(@klass).all(:offset => 2, :limit => 2)
|
142
|
+
assert_equal result[2..4], offset_result
|
143
|
+
ensure
|
144
|
+
_veneer_teardown
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_conditions
|
148
|
+
create_valid_items(3)
|
149
|
+
Veneer(@klass).create(@valid_attributes)
|
150
|
+
result = Veneer(@klass).all(:conditions => @valid_attributes)
|
151
|
+
assert_equal 1, result.size
|
152
|
+
ensure
|
153
|
+
_veneer_teardown
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_collection_associations
|
157
|
+
result = Veneer(@klass).collection_associations
|
158
|
+
assert result.kind_of?(Array)
|
159
|
+
assert result.size > 0
|
160
|
+
hash = result.first
|
161
|
+
assert hash.kind_of?(Hash)
|
162
|
+
assert_not_nil hash[:name]
|
163
|
+
assert_not_nil hash[:class]
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_member_associations
|
167
|
+
result = Veneer(@klass).member_associations
|
168
|
+
assert result.kind_of?(Array)
|
169
|
+
assert result.size > 0
|
170
|
+
hash = result.first
|
171
|
+
assert hash.kind_of?(Hash)
|
172
|
+
assert_not_nil hash[:name]
|
173
|
+
assert_not_nil hash[:class]
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
module InstanceWrapperLint
|
178
|
+
def test_should_implement_destroy
|
179
|
+
item = Veneer(@klass).create(@valid_attributes)
|
180
|
+
assert Veneer(@klass).all.size > 0
|
181
|
+
assert item.kind_of?(Veneer::Base::InstanceWrapper)
|
182
|
+
item.destroy
|
183
|
+
assert Veneer(@klass).all.size == 0
|
184
|
+
ensure
|
185
|
+
_veneer_teardown
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_should_implement_new_record?
|
189
|
+
item = Veneer(@klass).new(@valid_attributes)
|
190
|
+
assert item.new_record?
|
191
|
+
assert item.save
|
192
|
+
assert !item.new_record?
|
193
|
+
ensure
|
194
|
+
_veneer_teardown
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_should_implement_save!
|
198
|
+
assert_raises Veneer::Errors::NotSaved do
|
199
|
+
Veneer(@klass).create!(@invalid_attributes)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_should_list_model_classes
|
204
|
+
assert Veneer.model_classes.size > 0
|
205
|
+
end
|
206
|
+
|
207
|
+
#### Aggregations
|
208
|
+
def test_should_implement_count_without_arguments
|
209
|
+
create_valid_items(3)
|
210
|
+
assert_equal 3, Veneer(@klass).count
|
211
|
+
ensure
|
212
|
+
_veneer_teardown
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_should_implement_count_with_arguments
|
216
|
+
create_valid_items(3)
|
217
|
+
Veneer(@klass).create(@valid_attributes)
|
218
|
+
assert_equal 1, Veneer(@klass).count(:conditions => @valid_attributes)
|
219
|
+
ensure
|
220
|
+
_veneer_teardown
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_should_implement_sum_without_arguments
|
224
|
+
create_valid_items(3)
|
225
|
+
results = Veneer(@klass).all
|
226
|
+
results.inject(0){ |sum, i| (i.send(:integer_field) ||0) + sum }
|
227
|
+
assert_equal 3, Veneer(@klass).sum(:integer_field)
|
228
|
+
ensure
|
229
|
+
_veneer_teardown
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_should_implement_sum_with_arguments
|
233
|
+
create_valid_items(3)
|
234
|
+
Veneer(@klass).create(@valid_attributes)
|
235
|
+
results = Veneer(@klass).all(:conditions => @valid_attributes)
|
236
|
+
results.inject(0){ |sum, i| (i.send(:integer_field) ||0) + sum }
|
237
|
+
assert_equal 1, Veneer(@klass).sum(:integer_field, :conditions => @valid_attributes)
|
238
|
+
ensure
|
239
|
+
_veneer_teardown
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_should_implement_min_without_arguments
|
243
|
+
create_valid_items(3)
|
244
|
+
Veneer(@klass).create(@valid_attributes.merge(:integer_field => -5))
|
245
|
+
results = Veneer(@klass).all
|
246
|
+
assert_equal(-5, Veneer(@klass).min(:integer_field))
|
247
|
+
ensure
|
248
|
+
_veneer_teardown
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_should_implement_min_with_arguments
|
252
|
+
create_valid_items(3)
|
253
|
+
attrs = @valid_attributes.merge(:integer_field => -5)
|
254
|
+
Veneer(@klass).create(attrs)
|
255
|
+
results = Veneer(@klass).all(:conditions => attrs)
|
256
|
+
assert_equal(-5, Veneer(@klass).min(:integer_field))
|
257
|
+
ensure
|
258
|
+
_veneer_teardown
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_should_implement_max_without_arguments
|
262
|
+
create_valid_items(3)
|
263
|
+
Veneer(@klass).create(@valid_attributes.merge(:integer_field => 99999))
|
264
|
+
results = Veneer(@klass).all
|
265
|
+
assert_equal 99999, Veneer(@klass).max(:integer_field)
|
266
|
+
ensure
|
267
|
+
_veneer_teardown
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_should_implement_max_with_arguments
|
271
|
+
create_valid_items(3)
|
272
|
+
attrs = @valid_attributes.merge(:integer_field => 99999)
|
273
|
+
Veneer(@klass).create(attrs)
|
274
|
+
results = Veneer(@klass).all(:conditions => attrs)
|
275
|
+
assert_equal 99999, Veneer(@klass).max(:integer_field)
|
276
|
+
ensure
|
277
|
+
_veneer_teardown
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|