riagent 0.0.1

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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +24 -0
  4. data/Gemfile.lock +73 -0
  5. data/LICENSE +202 -0
  6. data/README.md +230 -0
  7. data/Rakefile +47 -0
  8. data/lib/rails/generators/riagent/install/install_generator.rb +35 -0
  9. data/lib/rails/generators/riagent/install/templates/riak.yml +16 -0
  10. data/lib/riagent/active_document.rb +53 -0
  11. data/lib/riagent/associations.rb +119 -0
  12. data/lib/riagent/configuration.rb +73 -0
  13. data/lib/riagent/conversion.rb +83 -0
  14. data/lib/riagent/errors.rb +33 -0
  15. data/lib/riagent/persistence/riak_json_strategy.rb +62 -0
  16. data/lib/riagent/persistence.rb +166 -0
  17. data/lib/riagent/railtie.rb +35 -0
  18. data/lib/riagent/search_schema.rb +58 -0
  19. data/lib/riagent/version.rb +23 -0
  20. data/lib/riagent.rb +29 -0
  21. data/riagent.gemspec +51 -0
  22. data/test/config/riak.yml.example +19 -0
  23. data/test/examples/models/address_book.rb +32 -0
  24. data/test/examples/models/blog_post.rb +31 -0
  25. data/test/examples/models/contact.rb +29 -0
  26. data/test/examples/models/user.rb +41 -0
  27. data/test/integration/persistence_integration_test.rb +76 -0
  28. data/test/seeds.rb +30 -0
  29. data/test/test_helper.rb +33 -0
  30. data/test/unit/active_document_test.rb +41 -0
  31. data/test/unit/active_model_lint_test.rb +33 -0
  32. data/test/unit/associations_has_many_test.rb +32 -0
  33. data/test/unit/associations_has_one_test.rb +85 -0
  34. data/test/unit/config_test.rb +37 -0
  35. data/test/unit/embedded_test.rb +38 -0
  36. data/test/unit/persistence_riak_json_test.rb +118 -0
  37. data/test/unit/persistence_test.rb +57 -0
  38. data/test/unit/search_schema_test.rb +26 -0
  39. data/test/unit/validation_test.rb +39 -0
  40. metadata +227 -0
@@ -0,0 +1,23 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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
+ module Riagent
22
+ VERSION = "0.0.1"
23
+ end
data/lib/riagent.rb ADDED
@@ -0,0 +1,29 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 "riagent/version"
22
+ require "riagent/errors"
23
+ require "riagent/configuration"
24
+ require "riagent/active_document"
25
+ require "riagent/railtie" if defined?(Rails)
26
+
27
+ module Riagent
28
+ extend Riagent::Configuration
29
+ end
data/riagent.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+ ## -------------------------------------------------------------------
3
+ ##
4
+ ## Copyright (c) "2014" Dmitri Zagidulin and Basho Technologies, Inc.
5
+ ##
6
+ ## This file is provided to you under the Apache License,
7
+ ## Version 2.0 (the "License"); you may not use this file
8
+ ## except in compliance with the License. You may obtain
9
+ ## a copy of the License at
10
+ ##
11
+ ## http://www.apache.org/licenses/LICENSE-2.0
12
+ ##
13
+ ## Unless required by applicable law or agreed to in writing,
14
+ ## software distributed under the License is distributed on an
15
+ ## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ ## KIND, either express or implied. See the License for the
17
+ ## specific language governing permissions and limitations
18
+ ## under the License.
19
+ ##
20
+ ## -------------------------------------------------------------------
21
+
22
+ lib = File.expand_path('../lib', __FILE__)
23
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
24
+ require 'riagent/version'
25
+
26
+ Gem::Specification.new do |spec|
27
+ spec.name = "riagent"
28
+ spec.version = Riagent::VERSION
29
+ spec.authors = ["Dmitri Zagidulin"]
30
+ spec.email = ["dzagidulin@gmail.com"]
31
+ spec.summary = %q{Rails integration for RiakJson document store}
32
+ spec.description = %q{Provides Ruby on Rails integration for RiakJson (Riak + Solr document store API)}
33
+ spec.homepage = "https://github.com/dmitrizagidulin/riagent"
34
+ spec.license = "Apache 2.0"
35
+
36
+ spec.files = `git ls-files -z`.split("\x0")
37
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
38
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
39
+ spec.require_paths = ["lib"]
40
+
41
+ spec.add_runtime_dependency "riak_json"
42
+ spec.add_runtime_dependency "riak-client"
43
+ spec.add_runtime_dependency "riagent-document"
44
+ spec.add_runtime_dependency "activemodel", "~> 4.0"
45
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
46
+
47
+ spec.add_development_dependency "bundler"
48
+ spec.add_development_dependency "rake"
49
+ spec.add_development_dependency "minitest", "~> 4.2"
50
+ spec.add_development_dependency "minitest-spec-context"
51
+ end
@@ -0,0 +1,19 @@
1
+ # Sample riak.yml config file for running integration tests
2
+ # Be sure to 'cp riak.yml.example riak.yml' before testing
3
+
4
+ # Configure Riak and RiakJson connections for the client, by environment
5
+ development:
6
+ http_port: 8098
7
+ pb_port: 8097 # Protocol Buffer port
8
+ host: 127.0.0.1
9
+
10
+ # You should spin up another Riak instance to use as a test server
11
+ test:
12
+ http_port: 8098
13
+ pb_port: 8097
14
+ host: 127.0.0.1
15
+
16
+ production:
17
+ http_port: 8098
18
+ pb_port: 8097
19
+ host: 127.0.0.1
@@ -0,0 +1,32 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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_relative 'contact'
22
+
23
+ # Sample model demonstrating embedding of other ActiveDocuments
24
+ # See test/examples/models/contact.rb (the model that's being embedded)
25
+ # See also test/unit/embedded_test.rb
26
+ class AddressBook
27
+ include Riagent::ActiveDocument
28
+
29
+ collection_type :riak_json # Persist to a RiakJson::Collection
30
+
31
+ attribute :contacts, Set[Contact]
32
+ end
@@ -0,0 +1,31 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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
+ class BlogPost
22
+ include Riagent::ActiveDocument
23
+
24
+ collection_type :riak_json # Persist to a RiakJson::Collection
25
+
26
+ # Explicit attributes
27
+ # key is an implied attribute, present in all ActiveDocument instances
28
+ attribute :title, String, search_index: { as: :text }
29
+ attribute :description, String, search_index: { as: :text }
30
+ attribute :body, String, search_index: { as: :text }
31
+ end
@@ -0,0 +1,29 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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
+ # Sample model to be embedded in another ActiveDocument
22
+ # See test/examples/models/address_book.rb
23
+ # See also test/unit/embedded_test.rb
24
+ class Contact
25
+ include Riagent::ActiveDocument
26
+
27
+ attribute :contact_name, String
28
+ attribute :contact_email, String
29
+ end
@@ -0,0 +1,41 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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_relative 'address_book'
22
+ require_relative 'blog_post'
23
+
24
+ class User
25
+ include Riagent::ActiveDocument
26
+
27
+ collection_type :riak_json # Persist to a RiakJson::Collection
28
+
29
+ # Explicit attributes
30
+ # key is an implied attribute, present in all ActiveDocument instances
31
+ attribute :username, String, search_index: { as: :text }
32
+ attribute :email, String, search_index: { as: :string }, default: ''
33
+ attribute :language, String, default: 'en'
34
+
35
+ # Associations
36
+ has_one :address_book, :class => AddressBook
37
+ has_many :posts, :class => BlogPost, :using => :solr
38
+
39
+ # Validations
40
+ validates_presence_of :username
41
+ end
@@ -0,0 +1,76 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ TEST_USERNAME = 'earl'
24
+ TEST_USERNAME_2 = 'count'
25
+
26
+ def test_user(test_key='earl-123')
27
+ user = User.new username: TEST_USERNAME, email: 'earl@desandwich.com'
28
+ user.key = test_key
29
+ user
30
+ end
31
+
32
+ describe "a Riagent::ActiveDocument's Persistence Layer" do
33
+ it "can save a document and generate a key" do
34
+ user = User.new username: TEST_USERNAME
35
+ generated_key = user.save
36
+
37
+ generated_key.wont_be_nil "Key not generated from document.save"
38
+ generated_key.wont_be_empty "Key not generated from document.save"
39
+ generated_key.must_be_kind_of String
40
+ user.key.must_equal generated_key
41
+
42
+ refute user.new_record?, "Document should not be marked as new after saving"
43
+ assert user.persisted?, "Document should be marked as persisted after saving"
44
+
45
+ # Clean up
46
+ user.destroy
47
+ end
48
+
49
+ it "can read, update and delete a document" do
50
+ test_key = 'george-123'
51
+ new_user = User.new username: 'george', email: 'george@washington.com'
52
+ new_user.key = test_key
53
+ new_user.save
54
+
55
+ found_user = User.find(test_key) # Load User by key
56
+ found_user.must_be_kind_of User
57
+ found_user.key.must_equal test_key
58
+ refute found_user.new_record?, "A loaded by key user object is not new"
59
+ assert found_user.persisted?, "A loaded by key user object should be markes as persisted"
60
+
61
+ new_attributes = {username: 'henry', email: 'henry@gmail.com' }
62
+ found_user.update(new_attributes) # Also saves
63
+
64
+ updated_user = User.find(test_key)
65
+ updated_user.username.must_equal 'henry'
66
+
67
+ found_user.destroy
68
+ assert found_user.destroyed?
69
+ end
70
+
71
+ it "returns an empty array for queries that return no results" do
72
+ query = { username: 'nonexistent' }
73
+ result = User.where(query)
74
+ result.must_be_empty
75
+ end
76
+ end
data/test/seeds.rb ADDED
@@ -0,0 +1,30 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'riagent'
22
+ require './test/examples/models/user'
23
+
24
+ puts "Seeding..."
25
+ # Load config file and set up the relevant clients for seeding test data
26
+ Riagent.load_config_file('test/config/riak.yml')
27
+ Riagent.init_clients(:test) # Set up the client for the test environment
28
+
29
+ # Store the Solr indexing schema for the User model
30
+ User.save_solr_schema()
@@ -0,0 +1,33 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'minitest/autorun'
22
+ require 'minitest-spec-context'
23
+ require 'riagent'
24
+ require 'examples/models/address_book'
25
+ require 'examples/models/contact'
26
+ require 'examples/models/user'
27
+
28
+ # Set this to silence "[deprecated] I18n.enforce_available_locales will default to true in the future." warnings
29
+ I18n.config.enforce_available_locales = true
30
+
31
+ # Load config file and set up the relevant clients for integration testing
32
+ Riagent.load_config_file('test/config/riak.yml')
33
+ Riagent.init_clients(:test) # Set up the client for the test environment
@@ -0,0 +1,41 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ describe "a Riagent::ActiveDocument" do
24
+ # an instance of Riagent::ActiveDocument
25
+ # see test/models/user.rb
26
+ let(:user_model) { User.new }
27
+
28
+ it "extends Riagent::Document" do
29
+ user_model.must_be_kind_of Riagent::Document
30
+ end
31
+
32
+ it "should know its collection name" do
33
+ # a document's collection name is used in ActiveModel::Conversion compatibility
34
+ User.collection_name.must_equal 'users'
35
+ end
36
+
37
+ it "uses its collection name to help form URLs" do
38
+ user_model.key = 'test-user-123'
39
+ user_model.to_partial_path.must_equal 'users/test-user-123'
40
+ end
41
+ end
@@ -0,0 +1,33 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+ require 'active_model/lint'
23
+
24
+ # Runs the ActiveModel::Lint test suite, to make sure a model fits the ActiveModel API
25
+ # See http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
26
+ class ActiveModelLintTest < MiniTest::Test
27
+ include ActiveModel::Lint::Tests
28
+
29
+ def setup
30
+ # Sample Riagent::ActiveDocument instance, implementing the ActiveModel API. See test/examples/models/user.rb
31
+ @model = User.new
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ describe "Riagent::Document has_many association" do
24
+ # If User has_many :posts,
25
+ # Then user is source, posts is a target collection
26
+
27
+ it "adds getter and setter methods for the target collection" do
28
+ user = User.new
29
+ user.must_respond_to :posts
30
+ user.must_respond_to :posts=
31
+ end
32
+ end
@@ -0,0 +1,85 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ describe "Riagent::Document has_one association" do
24
+ # If User has_one :address_book,
25
+ # Then user is source, and address_book is target
26
+
27
+ it "adds a <target>_key attribute" do
28
+ # a User model includes a 'has_one :address_book' association
29
+ User.attribute_set[:address_book_key].wont_be_nil
30
+ user = User.new
31
+ user.must_respond_to :address_book_key
32
+ end
33
+
34
+ it "adds getter and setter methods for the target" do
35
+ user = User.new
36
+ user.must_respond_to :address_book
37
+ user.must_respond_to :address_book=
38
+ end
39
+
40
+ it "assigning a target object sets corresponding <target>_key and <target> attribute values" do
41
+ user = User.new
42
+ address_book = AddressBook.new
43
+ address_book.key = 'test-book-123'
44
+
45
+ user.address_book = address_book
46
+
47
+ user.address_book_key.must_equal 'test-book-123'
48
+
49
+ user.address_book.must_be_kind_of AddressBook
50
+ end
51
+
52
+ it "should check type when assigning to a target attribute" do
53
+ user = User.new
54
+ lambda { user.address_book = User.new }.must_raise ArgumentError
55
+ end
56
+
57
+ it "lazy loads the target object, if key is present" do
58
+ user = User.new
59
+ user.address_book_key = 'test-book-123' # Set the target key manually
60
+ user.address_book_cache.must_be_nil "Setting the target key should not load the target object instance"
61
+
62
+ # Create a mock object that'll be loaded from db
63
+ mock_loaded_address_book = AddressBook.new
64
+ mock_loaded_address_book.key = 'test-book-123'
65
+
66
+ AddressBook.collection = MiniTest::Mock.new
67
+ AddressBook.collection.expect :find_by_key, mock_loaded_address_book, ['test-book-123']
68
+
69
+ # Calling user.address_book should lazy-load via the target collection.find_by_key()
70
+ user.address_book
71
+ AddressBook.collection.verify
72
+
73
+ # Reset
74
+ AddressBook.collection = nil
75
+ end
76
+
77
+ it "adds a build_<target> method" do
78
+ user = User.new
79
+ user.key = 'test-user-123'
80
+
81
+ user.build_address_book(attributes={})
82
+ # Target object gets source object's key by default, via build_<target>()
83
+ user.address_book.key.must_equal 'test-user-123'
84
+ end
85
+ end
@@ -0,0 +1,37 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ describe "Riagent" do
24
+ it "provides a configuration hash" do
25
+ Riagent.config.must_be_kind_of Hash
26
+ Riagent.config.wont_be_empty # The config file was initialized by test_helper.rb
27
+ Riagent.must_respond_to :load_config_file
28
+ Riagent.must_respond_to :init_clients
29
+ Riagent.must_respond_to :init_riak_json_client
30
+ Riagent.must_respond_to :riak_json_client
31
+ end
32
+
33
+ it "initializes a RiakJson client" do
34
+ # This should have been initialized from config file in test_helper.rb
35
+ Riagent.riak_json_client.must_be_kind_of RiakJson::Client
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) "2014" Dmitri Zagidulin and 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 'test_helper'
22
+
23
+ describe "a Riagent::ActiveDocument" do
24
+ it "can be composed of other embedded ActiveDocuments" do
25
+ address_book = AddressBook.new user_key: 'earl-123'
26
+ c1 = Contact.new contact_name: 'Joe', contact_email: 'joe@test.net'
27
+ c2 = Contact.new contact_name: 'Jane', contact_email: 'jane@test.net'
28
+ address_book.contacts << c1
29
+ address_book.contacts << c2
30
+ json_str = address_book.to_json_document
31
+
32
+ new_book = AddressBook.from_json json_str
33
+ new_book.contacts.count.must_equal 2
34
+ contact = new_book.contacts.first
35
+ contact.contact_name.must_equal 'Joe'
36
+ contact.must_be_kind_of Contact
37
+ end
38
+ end