1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
require 'abstract_unit'
require 'active_support/core_ext/hash/conversions'
require "fixtures/person"
require "fixtures/street_address"
########################################################################
# Testing the schema of your Active Resource models
########################################################################
class SchemaTest < ActiveModel::TestCase
def setup
setup_response # find me in abstract_unit
end
def teardown
Person.schema = nil # hack to stop test bleedthrough...
end
#####################################################
# Passing in a schema directly and returning it
####
test "schema on a new model should be empty" do
assert Person.schema.blank?, "should have a blank class schema"
assert Person.new.schema.blank?, "should have a blank instance schema"
end
test "schema should only accept a hash" do
["blahblah", ['one','two'], [:age, :name], Person.new].each do |bad_schema|
assert_raises(ArgumentError,"should only accept a hash (or nil), but accepted: #{bad_schema.inspect}") do
Person.schema = bad_schema
end
end
end
test "schema should accept a simple hash" do
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema
end
test "schema should accept a hash with simple values" do
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema
end
test "schema should accept all known attribute types as values" do
# I'd prefer to use here...
ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type|
assert_nothing_raised("should have accepted #{the_type.inspect}"){ Person.schema = {'my_key' => the_type }}
end
end
test "schema should not accept unknown values" do
bad_values = [ :oogle, :blob, 'thing']
bad_values.each do |bad_value|
assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do
Person.schema = {'key' => bad_value}
end
end
end
test "schema should accept nil and remove the schema" do
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema, Person.schema # sanity check
assert_nothing_raised { Person.schema = nil }
assert_nil Person.schema, "should have nulled out the schema, but still had: #{Person.schema.inspect}"
end
test "schema should be with indifferent access" do
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
new_schema_syms = new_schema.keys
assert_nothing_raised { Person.schema = new_schema }
new_schema_syms.each do |col|
assert Person.new.respond_to?(col.to_s), "should respond to the schema's string key, but failed on: #{col.to_s}"
assert Person.new.respond_to?(col.to_sym), "should respond to the schema's symbol key, but failed on: #{col.to_sym}"
end
end
test "schema on a fetched resource should return all the attributes of that model instance" do
p = Person.find(1)
s = p.schema
assert s.present?, "should have found a non-empty schema!"
p.attributes.each do |the_attr, val|
assert s.has_key?(the_attr), "should have found attr: #{the_attr} in schema, but only had: #{s.inspect}"
end
end
test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do
matz = Person.find(1)
rick = Person.find(6)
m_attrs = matz.attributes.keys.sort
r_attrs = rick.attributes.keys.sort
assert_not_equal m_attrs, r_attrs, "should have different attributes on each model"
assert_not_equal matz.schema, rick.schema, "should have had different schemas too"
end
test "defining a schema should return it when asked" do
assert Person.schema.blank?, "should have a blank class schema"
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised {
Person.schema = new_schema
assert_equal new_schema, Person.schema, "should have saved the schema on the class"
assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance"
}
end
test "defining a schema, then fetching a model should still match the defined schema" do
# sanity checks
assert Person.schema.blank?, "should have a blank class schema"
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
matz = Person.find(1)
assert !matz.schema.blank?, "should have some sort of schema on an instance variable"
assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!"
assert_nothing_raised {
Person.schema = new_schema
assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema"
}
end
#####################################################
# Using the schema syntax
####
test "should be able to use schema" do
assert_respond_to Person, :schema, "should at least respond to the schema method"
assert_nothing_raised("Should allow the schema to take a block") do
Person.schema { }
end
end
test "schema definition should store and return attribute set" do
assert_nothing_raised do
s = nil
Person.schema do
s = self
attribute :foo, :string
end
assert_respond_to s, :attrs, "should return attributes in theory"
assert_equal({'foo' => 'string' }, s.attrs, "should return attributes in practice")
end
end
test "should be able to add attributes through schema" do
assert_nothing_raised do
s = nil
Person.schema do
s = self
attribute('foo', 'string')
end
assert s.attrs.has_key?('foo'), "should have saved the attribute name"
assert_equal 'string', s.attrs['foo'], "should have saved the attribute type"
end
end
test "should convert symbol attributes to strings" do
assert_nothing_raised do
s = nil
Person.schema do
attribute(:foo, :integer)
s = self
end
assert s.attrs.has_key?('foo'), "should have saved the attribute name as a string"
assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string"
end
end
test "should be able to add all known attribute types" do
assert_nothing_raised do
ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type|
s = nil
Person.schema do
s = self
attribute('foo', the_type)
end
assert s.attrs.has_key?('foo'), "should have saved the attribute name"
assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}"
end
end
end
test "attributes should not accept unknown values" do
bad_values = [ :oogle, :blob, 'thing']
bad_values.each do |bad_value|
s = nil
assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do
Person.schema do
s = self
attribute 'key', bad_value
end
end
assert !self.respond_to?(bad_value), "should only respond to a known attribute type, but accepted: #{bad_value.inspect}"
assert_raises(NoMethodError,"should only have methods for known attribute types, but accepted: #{bad_value.inspect}") do
Person.schema do
send bad_value, 'key'
end
end
end
end
test "should accept attribute types as the type's name as the method" do
ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type|
s = nil
Person.schema do
s = self
send(the_type,'foo')
end
assert s.attrs.has_key?('foo'), "should now have saved the attribute name"
assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}"
end
end
test "should accept multiple attribute names for an attribute method" do
names = ['foo','bar','baz']
s = nil
Person.schema do
s = self
string(*names)
end
names.each do |the_name|
assert s.attrs.has_key?(the_name), "should now have saved the attribute name: #{the_name}"
assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string"
end
end
#####################################################
# What a schema does for us
####
# respond_to?
test "should respond positively to attributes that are only in the schema" do
new_attr_name = :my_new_schema_attribute
new_attr_name_two = :another_new_schema_attribute
assert Person.schema.blank?, "sanity check - should have a blank class schema"
assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet"
assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet"
assert_nothing_raised do
Person.schema = {new_attr_name.to_s => 'string'}
Person.schema { string new_attr_name_two }
end
assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}"
assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}"
end
test "should not care about ordering of schema definitions" do
new_attr_name = :my_new_schema_attribute
new_attr_name_two = :another_new_schema_attribute
assert Person.schema.blank?, "sanity check - should have a blank class schema"
assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet"
assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet"
assert_nothing_raised do
Person.schema { string new_attr_name_two }
Person.schema = {new_attr_name.to_s => 'string'}
end
assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}"
assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}"
end
# method_missing effects
test "should not give method_missing for attribute only in schema" do
new_attr_name = :another_new_schema_attribute
new_attr_name_two = :another_new_schema_attribute
assert Person.schema.blank?, "sanity check - should have a blank class schema"
assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do
Person.new.send(new_attr_name)
end
assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name_two} as a method") do
Person.new.send(new_attr_name_two)
end
Person.schema = {new_attr_name.to_s => :float}
Person.schema { string new_attr_name_two }
assert_nothing_raised do
Person.new.send(new_attr_name)
Person.new.send(new_attr_name_two)
end
end
########
# Known attributes
#
# Attributes can be known to be attributes even if they aren't actually
# 'set' on a particular instance.
# This will only differ from 'attributes' if a schema has been set.
test "new model should have no known attributes" do
assert Person.known_attributes.blank?, "should have no known attributes"
assert Person.new.known_attributes.blank?, "should have no known attributes on a new instance"
end
test "setting schema should set known attributes on class and instance" do
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
assert_equal new_schema.keys.sort, Person.known_attributes.sort
assert_equal new_schema.keys.sort, Person.new.known_attributes.sort
end
test "known attributes on a fetched resource should return all the attributes of the instance" do
p = Person.find(1)
attrs = p.known_attributes
assert attrs.present?, "should have found some attributes!"
p.attributes.each do |the_attr, val|
assert attrs.include?(the_attr), "should have found attr: #{the_attr} in known attributes, but only had: #{attrs.inspect}"
end
end
test "with two instances, known attributes should match the attributes of the individual instances - even if they differ" do
matz = Person.find(1)
rick = Person.find(6)
m_attrs = matz.attributes.keys.sort
r_attrs = rick.attributes.keys.sort
assert_not_equal m_attrs, r_attrs, "should have different attributes on each model"
assert_not_equal matz.known_attributes, rick.known_attributes, "should have had different known attributes too"
end
test "setting schema then fetching should add schema attributes to the instance attributes" do
# an attribute in common with fetched instance and one that isn't
new_schema = {'age' => 'integer', 'name' => 'string',
'height' => 'float', 'bio' => 'text',
'weight' => 'decimal', 'photo' => 'binary',
'alive' => 'boolean', 'created_at' => 'timestamp',
'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'}
assert_nothing_raised { Person.schema = new_schema }
matz = Person.find(1)
known_attrs = matz.known_attributes
matz.attributes.keys.each do |the_attr|
assert known_attrs.include?(the_attr), "should have found instance attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}"
end
new_schema.keys.each do |the_attr|
assert known_attrs.include?(the_attr), "should have found schema attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}"
end
end
end
|