riak_json 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +24 -0
- data/LICENSE +16 -0
- data/README.md +178 -0
- data/Rakefile +40 -0
- data/lib/riak_json/client.rb +139 -0
- data/lib/riak_json/client_transport.rb +48 -0
- data/lib/riak_json/collection.rb +107 -0
- data/lib/riak_json/collection_schema.rb +61 -0
- data/lib/riak_json/document.rb +54 -0
- data/lib/riak_json/errors.rb +25 -0
- data/lib/riak_json/query.rb +34 -0
- data/lib/riak_json/query_result.rb +50 -0
- data/lib/riak_json/version.rb +23 -0
- data/lib/riak_json.rb +34 -0
- data/riak_json.gemspec +51 -0
- data/test/examples/riak.yml +13 -0
- data/test/helper.rb +31 -0
- data/test/integration/client_integration_test.rb +51 -0
- data/test/integration/collection_integration_test.rb +227 -0
- data/test/unit/client_test.rb +163 -0
- data/test/unit/client_transport_test.rb +27 -0
- data/test/unit/collection_schema_test.rb +111 -0
- data/test/unit/collection_test.rb +281 -0
- data/test/unit/document_test.rb +67 -0
- data/test/unit/query_test.rb +30 -0
- metadata +209 -0
@@ -0,0 +1,163 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a RiakJson Client" do
|
24
|
+
context "when created" do
|
25
|
+
it "has an empty collection cache" do
|
26
|
+
rj_test_client.collection_cache.must_be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has a riak host and port" do
|
30
|
+
client = rj_test_client
|
31
|
+
client.host.must_equal @rj_client.host
|
32
|
+
client.port.must_equal @rj_client.port
|
33
|
+
end
|
34
|
+
|
35
|
+
it "initializes a transport object" do
|
36
|
+
client = rj_test_client
|
37
|
+
client.transport.must_be_kind_of RiakJson::ClientTransport
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "knows the URLs to Riak and RiakJSON API endpoints" do
|
42
|
+
it "knows riak cluster and riakjson urls" do
|
43
|
+
client = rj_test_client
|
44
|
+
client.base_riak_url.wont_be_empty
|
45
|
+
client.base_riak_json_url.wont_be_empty
|
46
|
+
end
|
47
|
+
it "knows riakjson collection url" do
|
48
|
+
client = rj_test_client
|
49
|
+
client.base_collection_url.wont_be_empty
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "uses its collection cache when instantiating collections" do
|
54
|
+
client = rj_test_client
|
55
|
+
collection1 = client.collection('ruby_test_collection')
|
56
|
+
collection2 = client.collection('ruby_test_collection')
|
57
|
+
collection1.must_be_same_as collection2, "Client uses collection cache, collection1 and collection2 should be identical"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "reads JSON objects from collections" do
|
61
|
+
client = rj_test_client
|
62
|
+
collection_name = 'ruby_test_collection'
|
63
|
+
test_key = 'document-key-123'
|
64
|
+
client.transport = MiniTest::Mock.new
|
65
|
+
|
66
|
+
# Test that a client.get_json_object call results in an HTTP GET request to /collection_name/key
|
67
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/document-key-123", :get]
|
68
|
+
client.get_json_object(collection_name, test_key)
|
69
|
+
client.transport.verify
|
70
|
+
end
|
71
|
+
|
72
|
+
it "writes a new JSON object to a collection with a specified key" do
|
73
|
+
client = rj_test_client
|
74
|
+
collection_name = 'ruby_test_collection'
|
75
|
+
test_key = 'document-key-123'
|
76
|
+
test_json = { 'field_one' => '123', 'field_two' => 'abc' }.to_json
|
77
|
+
client.transport = MiniTest::Mock.new
|
78
|
+
|
79
|
+
# Test that a client.insert_json_object call results in an HTTP PUT request to /collection_name/key
|
80
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/document-key-123", :put, test_json]
|
81
|
+
client.insert_json_object(collection_name, test_key, test_json)
|
82
|
+
client.transport.verify
|
83
|
+
end
|
84
|
+
|
85
|
+
it "updates an existing JSON object in a collection" do
|
86
|
+
client = rj_test_client
|
87
|
+
collection_name = 'ruby_test_collection'
|
88
|
+
test_key = 'document-key-123'
|
89
|
+
test_json = { 'field_one' => '123', 'field_two' => 'abc' }.to_json
|
90
|
+
client.transport = MiniTest::Mock.new
|
91
|
+
|
92
|
+
# Test that a client.update_json_object call results in an HTTP PUT request to /collection_name/key
|
93
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/document-key-123", :put, test_json]
|
94
|
+
client.update_json_object(collection_name, test_key, test_json)
|
95
|
+
client.transport.verify
|
96
|
+
end
|
97
|
+
|
98
|
+
it "raises an exception if updating a JSON object with no key" do
|
99
|
+
client = rj_test_client
|
100
|
+
collection_name = 'ruby_test_collection'
|
101
|
+
nil_key = nil
|
102
|
+
test_json = { 'field_one' => '123', 'field_two' => 'abc' }.to_json
|
103
|
+
|
104
|
+
lambda { client.update_json_object(collection_name, nil, test_json) }.must_raise Exception
|
105
|
+
end
|
106
|
+
|
107
|
+
it "deletes an existing JSON object in a collection" do
|
108
|
+
client = rj_test_client
|
109
|
+
collection_name = 'ruby_test_collection'
|
110
|
+
test_key = 'document-key-123'
|
111
|
+
client.transport = MiniTest::Mock.new
|
112
|
+
|
113
|
+
# Test that a client.delete_json_object call results in an HTTP DELETE request to /collection_name/key
|
114
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/document-key-123", :delete]
|
115
|
+
client.delete_json_object(collection_name, test_key)
|
116
|
+
client.transport.verify
|
117
|
+
end
|
118
|
+
|
119
|
+
context "performs document Schema administration" do
|
120
|
+
it "sets a schema json object into a collection's schema api endpoint" do
|
121
|
+
client = rj_test_client
|
122
|
+
collection_name = 'ruby_test_collection'
|
123
|
+
client.transport = MiniTest::Mock.new
|
124
|
+
schema_json = [{
|
125
|
+
:name => "field_one",
|
126
|
+
:type => "string",
|
127
|
+
:require => true
|
128
|
+
}, {
|
129
|
+
:name => "field_two",
|
130
|
+
:type => "text",
|
131
|
+
:require => false
|
132
|
+
}].to_json
|
133
|
+
|
134
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/schema", :put, schema_json]
|
135
|
+
client.set_schema_json(collection_name, schema_json)
|
136
|
+
client.transport.verify
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "sends JSON queries to retrieve documents" do
|
141
|
+
it "sends requests to /query/one" do
|
142
|
+
client = rj_test_client
|
143
|
+
collection_name = 'ruby_test_collection'
|
144
|
+
client.transport = MiniTest::Mock.new
|
145
|
+
query_json = {:company_name => 'Basho Technologies'}.to_json
|
146
|
+
|
147
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/query/one", :put, query_json]
|
148
|
+
client.get_query_one(collection_name, query_json)
|
149
|
+
client.transport.verify
|
150
|
+
end
|
151
|
+
|
152
|
+
it "sends requests to /query/all" do
|
153
|
+
client = rj_test_client
|
154
|
+
collection_name = 'cities'
|
155
|
+
client.transport = MiniTest::Mock.new
|
156
|
+
query_json = {:country => 'USA'}.to_json
|
157
|
+
|
158
|
+
client.transport.expect :send_request, nil, ["#{client.base_collection_url}/#{collection_name}/query/all", :put, query_json]
|
159
|
+
client.get_query_all(collection_name, query_json)
|
160
|
+
client.transport.verify
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a RiakJson ClientTransport" do
|
24
|
+
it "is instantiated to enable the Client to make REST calls" do
|
25
|
+
transport = RiakJson::ClientTransport.new
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a RiakJson Collection Schema" do
|
24
|
+
context "builds a schema definition by adding fields" do
|
25
|
+
it "initializes to an empty fields list" do
|
26
|
+
schema = RiakJson::CollectionSchema.new
|
27
|
+
schema.fields.must_be_empty
|
28
|
+
schema.fields.count.must_equal 0
|
29
|
+
end
|
30
|
+
|
31
|
+
it "can add field definitions with type passed in" do
|
32
|
+
schema = RiakJson::CollectionSchema.new
|
33
|
+
schema.add_field(:text, :city, true)
|
34
|
+
schema.fields.count.must_equal 1
|
35
|
+
schema.fields[0][:name].must_equal 'city'
|
36
|
+
schema.fields[0][:type].must_equal 'text'
|
37
|
+
schema.fields[0][:require].must_equal true
|
38
|
+
|
39
|
+
lambda { schema.add_field(:invalid_type, :state) }.must_raise Exception, "Invalid field type"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can add a text field to the schema definition" do
|
43
|
+
# schema = [{
|
44
|
+
# :name => "city",
|
45
|
+
# :type => "text",
|
46
|
+
# :require => true
|
47
|
+
# }]
|
48
|
+
schema = RiakJson::CollectionSchema.new
|
49
|
+
schema.add_text_field(name='city', required=true)
|
50
|
+
schema.fields.count.must_equal 1
|
51
|
+
schema.fields[0][:name].must_equal 'city'
|
52
|
+
schema.fields[0][:type].must_equal 'text'
|
53
|
+
schema.fields[0][:require].must_equal true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "can add a string field to the schema definition" do
|
57
|
+
# schema = [{
|
58
|
+
# :name => "state",
|
59
|
+
# :type => "string",
|
60
|
+
# :require => false
|
61
|
+
# }]
|
62
|
+
schema = RiakJson::CollectionSchema.new
|
63
|
+
schema.add_string_field(name='state') # required: false by default
|
64
|
+
schema.fields.count.must_equal 1
|
65
|
+
schema.fields[0][:name].must_equal 'state'
|
66
|
+
schema.fields[0][:type].must_equal 'string'
|
67
|
+
schema.fields[0][:require].must_equal false
|
68
|
+
end
|
69
|
+
|
70
|
+
it "can add a multi_string field to the schema definition" do
|
71
|
+
# schema = [{
|
72
|
+
# :name => "zip_codes",
|
73
|
+
# :type => "multi_string",
|
74
|
+
# :require => true
|
75
|
+
# }]
|
76
|
+
schema = RiakJson::CollectionSchema.new
|
77
|
+
schema.add_multi_string_field(name='zip_codes', required=true)
|
78
|
+
schema.fields.count.must_equal 1
|
79
|
+
schema.fields[0][:name].must_equal 'zip_codes'
|
80
|
+
schema.fields[0][:type].must_equal 'multi_string'
|
81
|
+
schema.fields[0][:require].must_equal true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "can add an integer field to the schema definition" do
|
85
|
+
# schema = [{
|
86
|
+
# :name => "population",
|
87
|
+
# :type => "integer",
|
88
|
+
# :require => false
|
89
|
+
# }]
|
90
|
+
schema = RiakJson::CollectionSchema.new
|
91
|
+
schema.add_integer_field(name='population', required=false)
|
92
|
+
schema.fields.count.must_equal 1
|
93
|
+
schema.fields[0][:name].must_equal 'population'
|
94
|
+
schema.fields[0][:type].must_equal 'integer'
|
95
|
+
schema.fields[0][:require].must_equal false
|
96
|
+
end
|
97
|
+
|
98
|
+
it "builds a json object representation" do
|
99
|
+
schema = RiakJson::CollectionSchema.new
|
100
|
+
schema.add_text_field(name='city', required=true)
|
101
|
+
schema.add_string_field(name='state')
|
102
|
+
|
103
|
+
# This can be stored via collection.set_schema
|
104
|
+
schema_json = schema.build
|
105
|
+
|
106
|
+
fields_array = JSON.parse(schema_json)
|
107
|
+
fields_array.count.must_equal 2
|
108
|
+
fields_array[0]['name'].must_equal 'city'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,281 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a RiakJson Collection" do
|
24
|
+
context "when created" do
|
25
|
+
it "has a name" do
|
26
|
+
client = rj_test_client
|
27
|
+
collection_name = 'ruby_test_collection'
|
28
|
+
collection = client.collection(collection_name) # create a new collection object
|
29
|
+
collection.name.must_equal collection_name
|
30
|
+
end
|
31
|
+
|
32
|
+
it "requires a name" do
|
33
|
+
client = rj_test_client # in helper.rb
|
34
|
+
lambda { client.collection() }.must_raise ArgumentError
|
35
|
+
end
|
36
|
+
|
37
|
+
it "cannot have an empty or nil name" do
|
38
|
+
client = rj_test_client # in helper.rb
|
39
|
+
lambda { client.collection(nil) }.must_raise ArgumentError, "A collection cannot have a nil string name"
|
40
|
+
lambda { client.collection('') }.must_raise ArgumentError, "A collection cannot have an empty string name"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "has a client/connection" do
|
44
|
+
client = rj_test_client
|
45
|
+
collection_name = 'ruby_test_collection'
|
46
|
+
collection = client.collection(collection_name) # create a new collection object
|
47
|
+
collection.client.must_be_kind_of(RiakJson::Client)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "uses the Client to read and write raw JSON objects to a collection" do
|
52
|
+
it "gets a raw JSON object for a collection/key" do
|
53
|
+
client = rj_test_client
|
54
|
+
collection_name = 'ruby_test_collection'
|
55
|
+
collection = client.collection(collection_name) # create a new collection object
|
56
|
+
test_key = 'document-key-123'
|
57
|
+
|
58
|
+
# Test that a collection.insert_raw_json(doc) results in a call to client.insert_json_object
|
59
|
+
collection.client = MiniTest::Mock.new
|
60
|
+
collection.client.expect :get_json_object, nil, [collection_name, test_key]
|
61
|
+
collection.get_raw_json(test_key)
|
62
|
+
collection.client.verify
|
63
|
+
end
|
64
|
+
|
65
|
+
it "inserts a raw JSON object into a collection/key" do
|
66
|
+
client = rj_test_client
|
67
|
+
collection_name = 'ruby_test_collection'
|
68
|
+
collection = client.collection(collection_name) # create a new collection object
|
69
|
+
|
70
|
+
test_key = 'document-key-123'
|
71
|
+
json_obj = { 'field_one' => '123', 'field_two' => 'abc' }.to_json
|
72
|
+
|
73
|
+
# Test that a collection.insert_raw_json(doc) results in a call to client.insert_json_object
|
74
|
+
collection.client = MiniTest::Mock.new
|
75
|
+
collection.client.expect :insert_json_object, nil, [collection_name, test_key, json_obj]
|
76
|
+
collection.insert_raw_json(test_key, json_obj)
|
77
|
+
collection.client.verify
|
78
|
+
end
|
79
|
+
|
80
|
+
it "updates a raw JSON object into a collection/key" do
|
81
|
+
client = rj_test_client
|
82
|
+
collection_name = 'ruby_test_collection'
|
83
|
+
collection = client.collection(collection_name) # create a new collection object
|
84
|
+
|
85
|
+
test_key = 'document-key-123'
|
86
|
+
json_obj = { 'field_one' => '123', 'field_two' => 'abc' }.to_json
|
87
|
+
|
88
|
+
# Test that a collection.update_raw_json(doc) results in a call to client.update_json_object
|
89
|
+
collection.client = MiniTest::Mock.new
|
90
|
+
collection.client.expect :update_json_object, nil, [collection_name, test_key, json_obj]
|
91
|
+
collection.update_raw_json(test_key, json_obj)
|
92
|
+
collection.client.verify
|
93
|
+
end
|
94
|
+
|
95
|
+
it "deletes a raw JSON object for a collection/key" do
|
96
|
+
client = rj_test_client
|
97
|
+
collection_name = 'ruby_test_collection'
|
98
|
+
collection = client.collection(collection_name) # create a new collection object
|
99
|
+
test_key = 'document-key-123'
|
100
|
+
|
101
|
+
# Test that a collection.delete_raw_json(doc) results in a call to client.delete_json_object
|
102
|
+
collection.client = MiniTest::Mock.new
|
103
|
+
collection.client.expect :delete_json_object, nil, [collection_name, test_key]
|
104
|
+
collection.delete_raw_json(test_key)
|
105
|
+
collection.client.verify
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "administers Schemas for collections" do
|
110
|
+
it "sets a schema object for a collection" do
|
111
|
+
client = rj_test_client
|
112
|
+
collection_name = 'ruby_test_collection-new'
|
113
|
+
collection = client.collection(collection_name)
|
114
|
+
collection.has_schema?.must_equal false
|
115
|
+
schema_json = [{
|
116
|
+
:name => "field_one",
|
117
|
+
:type => "string",
|
118
|
+
:require => true
|
119
|
+
}, {
|
120
|
+
:name => "field_two",
|
121
|
+
:type => "text",
|
122
|
+
:require => false
|
123
|
+
}].to_json
|
124
|
+
|
125
|
+
collection.client = MiniTest::Mock.new
|
126
|
+
collection.client.expect :set_schema_json, nil, [collection_name, schema_json]
|
127
|
+
collection.set_schema(schema_json)
|
128
|
+
collection.client.verify
|
129
|
+
end
|
130
|
+
|
131
|
+
it "gets a schema object for a collection" do
|
132
|
+
client = rj_test_client
|
133
|
+
collection_name = 'ruby_test_collection'
|
134
|
+
collection = client.collection(collection_name)
|
135
|
+
|
136
|
+
collection.client = MiniTest::Mock.new
|
137
|
+
collection.client.expect :get_schema, nil, [collection_name]
|
138
|
+
collection.get_schema
|
139
|
+
collection.client.verify
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context "can write and delete Documents, and load them by key" do
|
144
|
+
it "can load a document by its key" do
|
145
|
+
client = rj_test_client
|
146
|
+
collection_name = 'ruby_test_collection'
|
147
|
+
collection = client.collection(collection_name)
|
148
|
+
|
149
|
+
test_key = 'key-123'
|
150
|
+
returned_json = '{"field_one": "abc"}' # Value mock-loaded from RiakJson
|
151
|
+
|
152
|
+
client = MiniTest::Mock.new
|
153
|
+
client.expect :get_json_object, returned_json, [collection_name, test_key]
|
154
|
+
collection.client = client
|
155
|
+
|
156
|
+
document = collection.find_by_key(test_key)
|
157
|
+
client.verify
|
158
|
+
document.must_be_kind_of RiakJson::Document
|
159
|
+
document.key.must_equal test_key
|
160
|
+
end
|
161
|
+
|
162
|
+
it "can insert a Document" do
|
163
|
+
client = rj_test_client
|
164
|
+
collection_name = 'ruby_test_collection'
|
165
|
+
collection = client.collection(collection_name)
|
166
|
+
|
167
|
+
# A Collection performs an insert by invoking doc.key and doc.to_json
|
168
|
+
# and then sending along the raw json object to its client
|
169
|
+
test_key = 'key-123'
|
170
|
+
test_json = { 'field_one' => 'abc' }
|
171
|
+
doc = RiakJson::Document.new(test_key, test_json)
|
172
|
+
|
173
|
+
client = MiniTest::Mock.new
|
174
|
+
client.expect :insert_json_object, nil, [collection_name, test_key, String]
|
175
|
+
collection.client = client
|
176
|
+
|
177
|
+
collection.insert(doc)
|
178
|
+
client.verify
|
179
|
+
end
|
180
|
+
|
181
|
+
it "can update a Document" do
|
182
|
+
client = rj_test_client
|
183
|
+
collection_name = 'ruby_test_collection'
|
184
|
+
collection = client.collection(collection_name)
|
185
|
+
|
186
|
+
# A Collection performs an update by invoking doc.key and doc.to_json
|
187
|
+
# and then sending along the raw json object to its client
|
188
|
+
test_key = 'key-123'
|
189
|
+
test_json = { 'field_one' => 'abc' }
|
190
|
+
doc = RiakJson::Document.new(test_key, test_json)
|
191
|
+
|
192
|
+
client = MiniTest::Mock.new
|
193
|
+
client.expect :update_json_object, nil, [collection_name, test_key, String]
|
194
|
+
collection.client = client
|
195
|
+
|
196
|
+
collection.update(doc)
|
197
|
+
client.verify
|
198
|
+
end
|
199
|
+
|
200
|
+
it "can remove a Document" do
|
201
|
+
client = rj_test_client
|
202
|
+
collection_name = 'ruby_test_collection'
|
203
|
+
collection = client.collection(collection_name)
|
204
|
+
|
205
|
+
# A Collection performs a remove by invoking doc.key
|
206
|
+
# and then sending along the key to its client
|
207
|
+
test_key = 'key-123'
|
208
|
+
doc = RiakJson::Document.new(test_key)
|
209
|
+
|
210
|
+
client = MiniTest::Mock.new
|
211
|
+
client.expect :delete_json_object, nil, [collection_name, test_key]
|
212
|
+
collection.client = client
|
213
|
+
|
214
|
+
collection.remove(doc)
|
215
|
+
client.verify
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
context "can query to find one or more documents" do
|
220
|
+
it "can query for one document via an exact field match" do
|
221
|
+
client = rj_test_client
|
222
|
+
collection_name = 'ruby_test_collection'
|
223
|
+
collection = client.collection(collection_name)
|
224
|
+
|
225
|
+
query_json = {:company_name => 'Basho Technologies'}.to_json
|
226
|
+
|
227
|
+
returned_json = '{"company_name": "Basho Technologies", "_id": "basho"}' # Value mock-loaded from RiakJson
|
228
|
+
|
229
|
+
client = MiniTest::Mock.new
|
230
|
+
client.expect :get_query_one, returned_json, [collection_name, query_json]
|
231
|
+
collection.client = client
|
232
|
+
|
233
|
+
document = collection.find_one(query_json)
|
234
|
+
collection.client.verify
|
235
|
+
|
236
|
+
document.must_be_kind_of RiakJson::Document
|
237
|
+
document.key.must_equal "basho"
|
238
|
+
document['company_name'].must_equal 'Basho Technologies'
|
239
|
+
end
|
240
|
+
|
241
|
+
it "returns an empty QueryResult if a find_all() call returns no documents" do
|
242
|
+
client = rj_test_client
|
243
|
+
collection_name = 'ruby_test_collection'
|
244
|
+
collection = client.collection(collection_name)
|
245
|
+
|
246
|
+
query = {:company_name => 'nonexistent'}.to_json
|
247
|
+
returned_json = '[]'
|
248
|
+
|
249
|
+
client = MiniTest::Mock.new
|
250
|
+
client.expect :get_query_all, returned_json, [collection_name, query]
|
251
|
+
collection.client = client
|
252
|
+
document = collection.find_all(query)
|
253
|
+
collection.client.verify
|
254
|
+
|
255
|
+
# Now verify the same behavior when the json string returned from server is nil
|
256
|
+
returned_json = nil
|
257
|
+
client = MiniTest::Mock.new
|
258
|
+
client.expect :get_query_all, returned_json, [collection_name, query]
|
259
|
+
collection.client = client
|
260
|
+
document = collection.find_all(query)
|
261
|
+
collection.client.verify
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
it "returns nil if a query_one() call finds no results" do
|
266
|
+
client = rj_test_client
|
267
|
+
collection_name = 'ruby_test_collection'
|
268
|
+
collection = client.collection(collection_name)
|
269
|
+
|
270
|
+
query_json = {:company_name => 'Basho Technologies'}.to_json
|
271
|
+
empty_results_json = [].to_json
|
272
|
+
|
273
|
+
client = MiniTest::Mock.new
|
274
|
+
client.expect :get_query_one, empty_results_json, [collection_name, query_json]
|
275
|
+
collection.client = client
|
276
|
+
|
277
|
+
doc = collection.find_one(query_json)
|
278
|
+
collection.client.verify
|
279
|
+
doc.must_be_nil
|
280
|
+
end
|
281
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a RiakJson Document" do
|
24
|
+
context "when initialized" do
|
25
|
+
it "has a key" do
|
26
|
+
test_key = 'key-123'
|
27
|
+
doc = RiakJson::Document.new(test_key)
|
28
|
+
doc.key.must_equal test_key
|
29
|
+
end
|
30
|
+
|
31
|
+
it "if created without a body parameter, initializes it to an empty hash" do
|
32
|
+
doc = RiakJson::Document.new
|
33
|
+
doc.body.must_be_kind_of Hash
|
34
|
+
doc.body.must_be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has a body" do
|
38
|
+
test_key = 'key-123'
|
39
|
+
test_body = { 'field_one' => '123', 'field_two' => 'abc' }
|
40
|
+
doc = RiakJson::Document.new(test_key, test_body)
|
41
|
+
doc.body.must_equal test_body
|
42
|
+
end
|
43
|
+
|
44
|
+
it "implements a .to_json_document method" do
|
45
|
+
test_key = 'key-123'
|
46
|
+
test_body = { 'field_one' => '123', 'field_two' => 'abc' }
|
47
|
+
doc = RiakJson::Document.new(test_key, test_body)
|
48
|
+
doc.to_json.must_be_kind_of String
|
49
|
+
json_str = doc.to_json_document
|
50
|
+
|
51
|
+
parsed_doc_body = JSON.parse(json_str)
|
52
|
+
# Note - a parsed JSON document has keys as strings, not symbols
|
53
|
+
parsed_doc_body['field_one'].must_equal '123'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "implements hash style getters and setters for its body" do
|
58
|
+
test_key = 'key-123'
|
59
|
+
test_body = { 'field_one' => '123', 'field_two' => 'abc' }
|
60
|
+
doc = RiakJson::Document.new(test_key, test_body)
|
61
|
+
|
62
|
+
doc['field_one'].must_equal '123'
|
63
|
+
|
64
|
+
doc['field_two'] = 'xyz'
|
65
|
+
doc.body['field_two'].must_equal 'xyz'
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'helper'
|
22
|
+
|
23
|
+
describe "a Query" do
|
24
|
+
it "can be created manually, from a json object" do
|
25
|
+
json = {'country' => 'USA'}.to_json
|
26
|
+
query = RiakJson::Query.from_json(json)
|
27
|
+
query.must_be_kind_of RiakJson::Query
|
28
|
+
query.body = json
|
29
|
+
end
|
30
|
+
end
|