dm-persevere-adapter 0.18.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,25 +2,28 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
  gem 'rspec'
3
3
  require 'spec'
4
4
 
5
- require 'ruby-debug'
6
5
  require DataMapper.root / 'lib' / 'dm-core' / 'spec' / 'adapter_shared_spec'
6
+ agg_dir = path_to("dm-aggregates", "0.10.2")[0]
7
+ require agg_dir / 'spec' / 'public' / 'shared' / 'aggregate_shared_spec'
8
+
7
9
  require Pathname(__FILE__).dirname.expand_path.parent + 'lib/persevere_adapter'
8
10
 
9
11
  describe DataMapper::Adapters::PersevereAdapter do
10
12
  before :all do
11
13
  # This needs to point to a valid persevere server
12
14
  @adapter = DataMapper.setup(:default, { :adapter => 'persevere', :host => 'localhost', :port => '8080' })
15
+ @repository = DataMapper.repository(@adapter.name)
13
16
 
14
- class Bozon
17
+ class ::Bozon
15
18
  include DataMapper::Resource
16
-
19
+
17
20
  # Persevere only does id's as strings.
18
21
  property :id, String, :serial => true
19
22
  property :author, String
20
23
  property :created_at, DateTime
21
24
  property :title, String
22
25
  end
23
-
26
+
24
27
  @test_schema_hash = {
25
28
  'id' => 'Vanilla',
26
29
  'properties' => {
@@ -29,7 +32,7 @@ describe DataMapper::Adapters::PersevereAdapter do
29
32
  'data' => { 'type' => 'string'}
30
33
  }
31
34
  }
32
-
35
+
33
36
  @test_schema_hash_alt = {
34
37
  'id' => 'test1/Vanilla',
35
38
  'properties' => {
@@ -45,7 +48,7 @@ describe DataMapper::Adapters::PersevereAdapter do
45
48
  'newdata' => { 'type' => 'any'}
46
49
  }
47
50
  }
48
-
51
+
49
52
  @test_schema_hash_alt_mod = {
50
53
  'id' => 'test1/Vanilla',
51
54
  'properties' => {
@@ -54,95 +57,140 @@ describe DataMapper::Adapters::PersevereAdapter do
54
57
  }
55
58
  }
56
59
  end
57
-
58
- describe 'migrations' do
59
- it 'should create the Bozon storage' do
60
- Bozon.auto_migrate!
61
- Bozon.auto_migrate_down!
62
- end
63
-
64
- it "should destroy Create then Remove the Bozon Storage" do
65
- @adapter.get_schema(Bozon.storage_name).should == false
66
- Bozon.auto_migrate_up!
67
- @adapter.get_schema(Bozon.storage_name).should_not == false
68
- Bozon.auto_migrate_down!
69
- @adapter.get_schema(Bozon.storage_name).should == false
70
- end
71
- end
72
-
73
- it_should_behave_like 'An Adapter Foo'
74
-
75
- describe '#put_schema' do
76
- it 'should create the json schema for the hash' do
77
- @adapter.put_schema(@test_schema_hash).should_not == false
78
- end
79
60
 
80
- it 'should create the json schema for the hash under the specified project' do
81
- @adapter.put_schema(@test_schema_hash, "test").should_not == false
61
+ it_should_behave_like 'An Adapter'
62
+
63
+ describe 'migrations' do
64
+ it 'should create the Bozon storage' do
65
+ Bozon.auto_migrate!
66
+ Bozon.auto_migrate_down!
67
+ end
68
+
69
+ it "should destroy Create then Remove the Bozon Storage" do
70
+ @adapter.get_schema(Bozon.storage_name).should == false
71
+ Bozon.auto_migrate_up!
72
+ @adapter.get_schema(Bozon.storage_name).should_not == false
73
+ Bozon.auto_migrate_down!
74
+ @adapter.get_schema(Bozon.storage_name).should == false
75
+ end
76
+
77
+ describe '#put_schema' do
78
+ it 'should create the json schema for the hash' do
79
+ @adapter.put_schema(@test_schema_hash).should_not == false
80
+ end
81
+
82
+ it 'should create the json schema for the hash under the specified project' do
83
+ @adapter.put_schema(@test_schema_hash, "test").should_not == false
84
+ end
85
+
86
+ it 'should create the json schema for the hash under the specified project' do
87
+ @adapter.put_schema(@test_schema_hash_alt).should_not == false
88
+ end
89
+ end
90
+
91
+ describe '#get_schema' do
92
+ it 'should return all of the schemas (in json) if no name is provided' do
93
+ result = @adapter.get_schema()
94
+ result.should_not == false
95
+ JSON.parse(result).class.should == Array
96
+ end
97
+
98
+ it 'should return the json schema of the class specified' do
99
+ result = @adapter.get_schema("Object")
100
+ result.should_not == false
101
+ JSON.parse(result)["id"].should == "Object"
102
+ end
103
+
104
+ # I don't think we need these tests.
105
+ # it 'should return all of the schemas (in json) for a project if no name is provided' do
106
+ # result = @adapter.get_schema(nil, "Class")
107
+ # debugger
108
+ # result
109
+ # end
110
+ #
111
+ # it 'should return all of the schemas (in json) if no name is provided' do
112
+ # @adapter.get_schema("Object", "Class")
113
+ # end
114
+ end
115
+
116
+ describe '#update_schema' do
117
+ it 'should update a previously existing schema' do
118
+ result = @adapter.update_schema(@test_schema_hash_mod)
119
+ result.should_not == false
120
+
121
+ @test_schema_hash_mod['id'].should match(JSON.parse(result)['id'])
122
+ end
123
+
124
+ it 'should update a previously created schema under the specified project' do
125
+ result = @adapter.update_schema(@test_schema_hash_mod, "test")
126
+ result.should_not == false
127
+ @test_schema_hash_mod['id'].should match(JSON.parse(result)['id'])
128
+ end
129
+
130
+ it 'should update a previously created schema under the specified project' do
131
+ result = @adapter.update_schema(@test_schema_hash_alt_mod)
132
+ result.should_not == false
133
+ @test_schema_hash_alt_mod['id'].should match(JSON.parse(result)['id'])
134
+ end
135
+ end
136
+
137
+ describe '#delete_schema' do
138
+ it 'should delete the specified schema' do
139
+ @adapter.delete_schema(@test_schema_hash).should == true
140
+ end
141
+
142
+ it 'should delete the specified schema in the specified project' do
143
+ @adapter.delete_schema(@test_schema_hash, "test").should == true
144
+ end
145
+
146
+ it 'should delete the specified schema in the specified project' do
147
+ @adapter.delete_schema(@test_schema_hash_alt).should == true
148
+ end
82
149
  end
83
-
84
- it 'should create the json schema for the hash under the specified project' do
85
- @adapter.put_schema(@test_schema_hash_alt).should_not == false
86
- end
87
150
  end
88
151
 
89
- describe '#get_schema' do
90
- it 'should return all of the schemas (in json) if no name is provided' do
91
- result = @adapter.get_schema()
92
- result.should_not == false
93
- JSON.parse(result).class.should == Array
94
- end
95
-
96
- it 'should return the json schema of the class specified' do
97
- result = @adapter.get_schema("Object")
98
- result.should_not == false
99
- JSON.parse(result)["id"].should == "Object"
152
+ describe 'aggregates' do
153
+ it_should_behave_like 'It Has Setup Resources'
154
+ before :all do
155
+ @dragons = Dragon.all
156
+ @countries = Country.all
100
157
  end
101
-
102
- # I don't think we need these tests.
103
- # it 'should return all of the schemas (in json) for a project if no name is provided' do
104
- # result = @adapter.get_schema(nil, "Class")
105
- # debugger
106
- # result
107
- # end
108
- #
109
- # it 'should return all of the schemas (in json) if no name is provided' do
110
- # @adapter.get_schema("Object", "Class")
111
- # end
158
+ it_should_behave_like 'An Aggregatable Class'
112
159
  end
113
160
 
114
- describe '#update_schema' do
115
- it 'should update a previously existing schema' do
116
- result = @adapter.update_schema(@test_schema_hash_mod)
117
- result.should_not == false
118
-
119
- @test_schema_hash_mod['id'].should match(JSON.parse(result)['id'])
161
+ describe 'limiting and offsets' do
162
+ before(:all) do
163
+ Bozon.auto_migrate!
164
+ (0..99).each{|i| Bozon.create!(:author => i, :title => i)}
120
165
  end
121
-
122
- it 'should update a previously created schema under the specified project' do
123
- result = @adapter.update_schema(@test_schema_hash_mod, "test")
124
- result.should_not == false
125
- @test_schema_hash_mod['id'].should match(JSON.parse(result)['id'])
166
+
167
+ it "should limit" do
168
+ result = Bozon.all(:limit => 2)
169
+ result.length.should == 2
126
170
  end
127
-
128
- it 'should update a previously created schema under the specified project' do
129
- result = @adapter.update_schema(@test_schema_hash_alt_mod)
130
- result.should_not == false
131
- @test_schema_hash_alt_mod['id'].should match(JSON.parse(result)['id'])
171
+
172
+ after(:all) do
173
+ Bozon.auto_migrate_down!
132
174
  end
133
175
  end
134
-
135
- describe '#delete_schema' do
136
- it 'should delete the specified schema' do
137
- @adapter.delete_schema(@test_schema_hash).should == true
138
- end
139
-
140
- it 'should delete the specified schema in the specified project' do
141
- @adapter.delete_schema(@test_schema_hash, "test").should == true
142
- end
143
176
 
144
- it 'should delete the specified schema in the specified project' do
145
- @adapter.delete_schema(@test_schema_hash_alt).should == true
146
- end
177
+ describe "is reflective" do
178
+ it "should be able to fetch json model descriptions" # do
179
+ # @return_hash = @test_schema_hash.clone
180
+ # @return_hash['prototype'] = {}
181
+ # @adapter.fetch_models.should_not include(@return_hash)
182
+ # @adapter.put_schema(@test_schema_hash)
183
+ # models = @adapter.fetch_models
184
+ # models.should include(@return_hash)
185
+ # @adapter.delete_schema(@test_schema_hash)
186
+ # end
187
+
188
+ it "should reflect existing schemas into DM models with reflect!" # do
189
+ # @adapter.put_schema(@test_schema_hash)
190
+ # @adapter.reflect!
191
+ # Vanilla.should_not be_nil
192
+ # end
147
193
  end
194
+
195
+
148
196
  end
@@ -11,9 +11,9 @@ describe Persevere do
11
11
  @blobObj = {
12
12
  'id' => 'Yogo',
13
13
  'properties' => {
14
- 'cid' => {'type' => 'string' },
15
- 'parent' => { 'type' => 'string'},
16
- 'data' => { 'type' => 'string'}
14
+ 'cid' => {'type' => 'string', 'optional' => true },
15
+ 'parent' => { 'type' => 'string', 'optional' => true},
16
+ 'data' => { 'type' => 'string', 'optional' => true}
17
17
  }
18
18
  }
19
19
 
@@ -29,90 +29,164 @@ describe Persevere do
29
29
  @mockObj = {
30
30
  'id' => 'Yogo',
31
31
  'properties' => {
32
- 'cid' => {'type' => 'string' },
33
- 'parent' => { 'type' => 'string'},
34
- 'data' => { 'type' => 'string'}
32
+ 'cid' => {'type' => 'string', 'optional' => true },
33
+ 'parent' => { 'type' => 'string', 'optional' => true},
34
+ 'data' => { 'type' => 'string', 'optional' => true}
35
35
  },
36
36
  'prototype' => {}
37
37
  }
38
+
39
+ @object = {
40
+ 'cid' => '123',
41
+ 'parent' => 'none',
42
+ 'data' => 'A Chunk Of Data'
43
+ }
44
+
38
45
  end
39
46
 
40
- #
47
+
41
48
  # Test POST to create a new class
42
- #
49
+
43
50
  describe '#post' do
44
- it 'should create a new object in persevere' do
45
- result = @p.create('/Class/', @blobObj)
46
- result.code.should == "201"
47
- JSON.parse(result.body).should == @mockObj
48
- end
49
-
50
- it 'should not allow posting with a bad object' do
51
- result = @p.create('/Class/', @corruptObj)
52
- result.code.should == "500"
53
- result.body.should == "\"Can not modify queries\""
54
- end
55
-
56
- it 'should not allow posting to an existing object/id/path' do
57
- result = @p.create('/Class/', @blobObj)
58
- result.code.should == "201"
59
- JSON.parse(result.body).should == @blobObj
60
- # This shouldn't be a 201, it should say something mean.
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
61
150
  end
62
- end
63
151
 
64
- #
65
- # Test GET to retrieve the list of classes from Persvr
66
- #
67
- describe '#get' do
68
- it 'should retrieve the previously created object from persevere' do
69
- result = @p.retrieve('/Class/Yogo')
152
+ it "should only retrieve all objects" do
153
+ result = @p.retrieve('/Yogo/')
70
154
  result.code.should == "200"
71
- JSON.parse(result.body).should == @blobObj
155
+ JSON.parse(result.body).length.should == 100
72
156
  end
73
157
 
74
- it 'should 404 on a non-existent object' do
75
- result = @p.retrieve('/Class/GetNotThere')
76
- result.code.should == "404"
77
- result.message.should == "Not Found"
78
- end
79
- end
80
-
81
- #
82
- # Test PUT to modify an existing class
83
- #
84
- describe '#put' do
85
- it 'should modify the previously created object in persevere' do
86
- @blobObj['properties']['tstAttribute'] = { 'type' => 'string' }
87
- result = @p.update('/Class/Yogo', @blobObj)
158
+ it "should retrieve the first objects" do
159
+ result = @p.retrieve('/Yogo/1')
88
160
  result.code.should == "200"
89
- JSON.parse(result.body).should == @blobObj
161
+ JSON.parse(result.body)['id'].should == '1'
90
162
  end
91
163
 
92
- it 'should fail to modify a non-existent item' do
93
- result = @p.update('/Class/NonExistent', @blobObj)
94
- result.code.should == "500"
95
- result.body.should == "\"id does not match location\""
96
- @p.delete('/Class/NonExistent') # A bug(?) in Persevere makes a broken NonExistent class.
97
- # This should be a 404 and not throw a persevere server exception
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
98
168
  end
99
- end
100
-
101
- #
102
- # Test DELETE to remove the previously created and modified class
103
- #
104
- describe '#delete' do
105
- it 'should remove the previously created and modified object from persevere' do
106
- result = @p.delete('/Class/Yogo')
107
- result.code.should == "204"
108
- @p.retrieve('/Class/Yogo').code.should == "404"
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'
109
186
  end
110
187
 
111
- it 'should fail to delete a non-existent item' do
112
- result = @p.delete('/Class/NotThere')
113
- result.code.should == "404"
114
- result.message.should == "Not Found"
115
- result.body.should == "\"Class/NotThere not found\""
188
+ after(:all) do
189
+ @p.delete('/Class/Yogo')
116
190
  end
117
191
  end
118
192
  end