dm-persevere-adapter 0.71.4 → 0.72.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,102 +0,0 @@
1
- module DataMapper
2
- module Resource
3
-
4
- def get_new_objects
5
- new_parents = parent_resources.select{|p| p.new? }
6
- new_children = child_collections.collect{ |collection| collection.select{|c| c.new? }}.flatten
7
- new_children_of_new_parents = new_parents.map{ |np| np.__send__(:child_collections).collect{ |n| select{ |p| p.new? }}}.flatten
8
- new_parents_of_new_children = new_children.map{ |nc| nc.__send__(:parent_resources).select{|p| p.new? }}.flatten
9
- [ new_parents, new_children, new_children_of_new_parents, new_parents_of_new_children, self.new? ? self : [] ].flatten.uniq
10
- end
11
-
12
- def create_hook
13
- op = original_attributes.dup
14
- _create
15
- @_original_attributes = op.dup
16
- end
17
-
18
- alias _old_update _update
19
- def _update
20
- if repository.adapter.is_a?(DataMapper::Adapters::PersevereAdapter)
21
- # remove from the identity map
22
- remove_from_identity_map
23
-
24
- repository.update(dirty_attributes, collection_for_self)
25
-
26
- # remove the cached key in case it is updated
27
- remove_instance_variable(:@_key)
28
-
29
- add_to_identity_map
30
-
31
- true
32
- else
33
- _old_update
34
- end
35
- end
36
-
37
- alias _old_save _save
38
- def _save(safe)
39
- objects = get_new_objects
40
- objects.each do |obj|
41
- obj.__send__(:save_self, safe)
42
- end
43
- _old_save(safe)
44
- end
45
-
46
- ##
47
- # Convert a DataMapper Resource to a JSON.
48
- #
49
- # @param [Query] query
50
- # The DataMapper query object passed in
51
- #
52
- # @api semipublic
53
- def to_json_hash(include_relationships=true)
54
- json_rsrc = Hash.new
55
- relations = self.model.relationships.keys
56
-
57
- if include_relationships
58
- self.model.relationships.each do |nom, relation|
59
- value = relation.get!(self)
60
- parent = relation.parent_model
61
- child = relation.child_model
62
-
63
- unless value.nil?
64
- json_rsrc[nom] = case relation
65
- # KEEP THIS CASE AT THE TOP BECAUSE IT IS_A OneToMany ARGH
66
- when DataMapper::Associations::ManyToMany::Relationship then
67
- [value].flatten.map{ |v| { "$ref" => "../#{v.model.storage_name}/#{v.key.first}" } }
68
- when DataMapper::Associations::ManyToOne::Relationship then
69
- { "$ref" => "../#{value.model.storage_name}/#{value.key.first}" }
70
- when DataMapper::Associations::OneToMany::Relationship then
71
- value.map{ |v| { "$ref" => "../#{v.model.storage_name}/#{v.key.first}" } }
72
- when DataMapper::Associations::OneToOne::Relationship then
73
- { "$ref" => "../#{value.model.storage_name}/#{value.first.key.first}" }
74
- end
75
- end
76
- end
77
- end
78
-
79
- attributes(:property).each do |property, value|
80
- # debugger if model.name == 'Yogo::Setting' && property.name == :value
81
- next if value.nil? || (value.is_a?(Array) && value.empty?) || relations.include?(property.name.to_s)
82
-
83
- if property.type.respond_to?(:dump)
84
- json_rsrc[property.field] = property.type.dump(value, nil)
85
- else
86
- json_rsrc[property.field] = case value
87
- when DateTime then value.new_offset(0).strftime("%Y-%m-%dT%H:%M:%SZ")
88
- when Date then value.to_s
89
- when Time then value.strftime("%H:%M:%S")
90
- when Float then value.to_f
91
- when BigDecimal then value.to_f
92
- when Integer then value.to_i
93
- else # when String, TrueClass, FalseClass then
94
- self[property.name]
95
- end
96
- end
97
- end
98
-
99
- json_rsrc
100
- end
101
- end
102
- end
@@ -1,201 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'lib/persevere'
3
-
4
- describe Persevere do
5
- #
6
- # Create an object to interact with Persevere
7
- #
8
- before :all do
9
- @p = Persevere.new('http://localhost:8080')
10
-
11
- @blobObj = {
12
- 'id' => 'Yogo',
13
- 'properties' => {
14
- 'cid' => {'type' => 'string', 'optional' => true },
15
- 'parent' => { 'type' => 'string', 'optional' => true},
16
- 'data' => { 'type' => 'string', 'optional' => true}
17
- }
18
- }
19
-
20
- @corruptObj = {
21
- 'id' => 'Corrupt',
22
- 'properties' => {
23
- 'id' => 1234,
24
- 'parent' => { 'type' => 'string'},
25
- 'data' => { 'type' => 'string'}
26
- }
27
- }
28
-
29
- @mockObj = {
30
- 'id' => 'Yogo',
31
- 'properties' => {
32
- 'cid' => {'type' => 'string', 'optional' => true },
33
- 'parent' => { 'type' => 'string', 'optional' => true},
34
- 'data' => { 'type' => 'string', 'optional' => true}
35
- },
36
- 'prototype' => {}
37
- }
38
-
39
- @object = {
40
- 'cid' => '123',
41
- 'parent' => 'none',
42
- 'data' => 'A Chunk Of Data'
43
- }
44
-
45
- end
46
-
47
-
48
- # Test POST to create a new class
49
-
50
- describe '#post' do
51
- it 'should create a new object in persevere' do
52
- result = @p.create('/Class/', @blobObj)
53
- result.code.should == "201"
54
- JSON.parse(result.body).should == @mockObj
55
- end
56
-
57
- # it 'should not allow posting with a bad object' do
58
- # result = @p.create('/Class/', @corruptObj)
59
- # result.code.should == "500"
60
- # result.body.should == "\"Can not modify queries\""
61
- # end
62
-
63
- it 'should not allow posting to an existing object/id/path' do
64
- result = @p.create('/Class/', @blobObj)
65
- result.code.should == "201"
66
- JSON.parse(result.body).should == @blobObj
67
- # This shouldn't be a 201, it should say something mean.
68
- end
69
- end
70
-
71
- #
72
- # Test GET to retrieve the list of classes from Persvr
73
- #
74
- describe '#get' do
75
- it 'should retrieve the previously created object from persevere' do
76
- result = @p.retrieve('/Class/Yogo')
77
- result.code.should == "200"
78
- JSON.parse(result.body).should == @blobObj
79
- end
80
-
81
- it 'should 404 on a non-existent object' do
82
- result = @p.retrieve('/Class/GetNotThere')
83
- result.code.should == "404"
84
- result.message.should == "Not Found"
85
- end
86
- end
87
-
88
- #
89
- # Test PUT to modify an existing class
90
- #
91
- describe '#put' do
92
- it 'should modify the previously created object in persevere' do
93
- @blobObj['properties']['tstAttribute'] = { 'type' => 'string' }
94
- result = @p.update('/Class/Yogo', @blobObj)
95
- result.code.should == "200"
96
- JSON.parse(result.body).should == @blobObj
97
- end
98
-
99
- it 'should fail to modify a non-existent item' do
100
- result = @p.update('/Class/NonExistent', @blobObj)
101
- result.code.should == "500"
102
- result.body.should == "\"id does not match location\""
103
- @p.delete('/Class/NonExistent') # A bug(?) in Persevere makes a broken NonExistent class.
104
- # This should be a 404 and not throw a persevere server exception
105
- end
106
- end
107
-
108
- #
109
- # Test DELETE to remove the previously created and modified class
110
- #
111
- describe '#delete' do
112
- it 'should remove the previously created and modified object from persevere' do
113
- result = @p.delete('/Class/Yogo')
114
- result.code.should == "204"
115
- @p.retrieve('/Class/Yogo').code.should == "404"
116
- end
117
-
118
- it 'should fail to delete a non-existent item' do
119
- result = @p.delete('/Class/NotThere')
120
- result.code.should == "404"
121
- result.message.should == "Not Found"
122
- result.body.should == "\"Class/NotThere not found\""
123
- end
124
- end
125
-
126
- describe "POSTing objects" do
127
- before(:all) do
128
- @p.create('/Class/', @blobObj)
129
- end
130
-
131
- it "should not allow nil fields to be posted" do
132
- obj_with_nil = @object.merge({'cid' => nil})
133
- result = @p.create('/Yogo', obj_with_nil)
134
- result.code.should == "201"
135
- JSON.parse(result.body).reject{|key,value| key == 'id' }.should ==
136
- obj_with_nil.reject{|key,value| value.nil?}
137
- end
138
-
139
- after(:all) do
140
- @p.delete('/Class/Yogo')
141
- end
142
- end
143
-
144
- describe "GETting limits and offsets" do
145
- before(:all) do
146
- @p.create('/Class/', @blobObj)
147
- (0..99).each do |i|
148
- @p.create('/Yogo/', @object.merge({'cid' => "#{i}"}))
149
- end
150
- end
151
-
152
- it "should only retrieve all objects" do
153
- result = @p.retrieve('/Yogo/')
154
- result.code.should == "200"
155
- JSON.parse(result.body).length.should == 100
156
- end
157
-
158
- it "should retrieve the first objects" do
159
- result = @p.retrieve('/Yogo/1')
160
- result.code.should == "200"
161
- JSON.parse(result.body)['id'].should == '1'
162
- end
163
-
164
- it "should retrieve a 10 of the objects" do
165
- result = @p.retrieve('/Yogo/', {'Range' => "items=1-10"})
166
- result.code.should == '206'
167
- JSON.parse(result.body).length.should == 10
168
- end
169
-
170
- it "should return the first 2 objects" do
171
- result = @p.retrieve('/Yogo/', {'Range' => "items=0-1"})
172
- result.code.should == '206'
173
- json = JSON.parse(result.body)
174
- json.length.should == 2
175
- json[0]['id'].should == '1'
176
- json[1]['id'].should == '2'
177
- end
178
-
179
- it "should return 21 and up objects" do
180
- result = @p.retrieve('/Yogo/', {'Range' => 'items=20-'})
181
- result.code.should == '206'
182
- json = JSON.parse(result.body)
183
- json.length.should == 80
184
- json[0]['id'].should == '21'
185
- json[-1]['id'].should == '100'
186
- end
187
-
188
- it "should return objects with id's 11 - 15" do
189
- result = @p.retrieve('/Yogo/', {'Range' => 'items=10-14'})
190
- result.code.should == '206'
191
- json = JSON.parse(result.body)
192
- json.length.should == 5
193
- json[0]['id'].should == '11'
194
- json[-1]['id'].should == '15'
195
- end
196
-
197
- after(:all) do
198
- @p.delete('/Class/Yogo')
199
- end
200
- end
201
- end