Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Same named tests covering up mismatched exception test on no id found. #3

Closed
wants to merge 7 commits into from

2 participants

Mike Evans Sergey Avseyev
Mike Evans

Not really sure if Couchbase::Model's Couchbase::Error::MissingId or the Couchbase gem's Couchbase::Error::NotFound exception should actually be raised here. Changed it to NotFound in the test so it would pass.

Sergey Avseyev

I have fixed this issue in http://review.couchbase.org/24684, but could you extract the changes about logging into the separate commit and upload to out review system?

We have a document about how to make a contribution https://github.com/couchbase/couchbase-ruby-model/blob/master/CONTRIBUTING.markdown

Sergey Avseyev

@ingenthr I think this ticket might be closed, because original issue has been fixed

Mike Evans

I should have closed this a while ago...

Mike Evans mje113 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 7, 2012
  1. Mike Evans

    Allow for proper ActiveModel style attribute inheritance when subclas…

    mje113 authored
    …sing Couchbase::Model subclasses, when using Rails.
Commits on Dec 10, 2012
  1. Mike Evans
  2. Mike Evans
Commits on Dec 27, 2012
  1. Mike Evans

    Merge conflict

    mje113 authored
  2. Mike Evans
Commits on Dec 28, 2012
  1. Mike Evans
  2. Mike Evans
This page is out of date. Refresh to see the latest.
56 lib/couchbase/model.rb
View
@@ -21,6 +21,7 @@
require 'couchbase/model/version'
require 'couchbase/model/uuid'
require 'couchbase/model/configuration'
+require 'couchbase/model/logger'
unless Object.respond_to?(:singleton_class)
require 'couchbase/model/ext/singleton_class'
@@ -129,6 +130,15 @@ class Model
# @private Container for all view names of all subclasses
@@views = {}
+ # Establish logger for logging operations
+ def self.logger
+ @@logger ||= Couchbase::Logger.new
+ end
+
+ def self.log(operation, key, options = {}, &block)
+ logger.log(operation, key, options) { yield }
+ end
+
# Use custom connection options
#
# @since 0.0.1
@@ -400,11 +410,13 @@ def self.belongs_to(name, options = {})
# @example Find model using +id+
# post = Post.find('the-id')
def self.find(id)
- if id && (res = bucket.get(id, :quiet => false, :extended => true))
- obj, flags, cas = res
- obj = {:raw => obj} unless obj.is_a?(Hash)
- new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
- end
+ #log('find', id) do
+ if id && (res = bucket.get(id, :quiet => false, :extended => true))
+ obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
+ new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ end
+ #end
end
# Find the model using +id+ attribute
@@ -418,10 +430,12 @@ def self.find(id)
# @example Find model using +id+
# post = Post.find_by_id('the-id')
def self.find_by_id(id)
- if id && (res = bucket.get(id, :quiet => true, :extended => true))
- obj, flags, cas = res
- obj = {:raw => obj} unless obj.is_a?(Hash)
- new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ log('find', id) do
+ if id && (res = bucket.get(id, :quiet => true, :extended => true))
+ obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
+ new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ end
end
end
@@ -495,15 +509,19 @@ def create(options = {})
if respond_to?(:valid?) && !valid?
return false
end
+
options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
- unless @meta
- @meta = {}
- if @meta.respond_to?(:with_indifferent_access)
- @meta = @meta.with_indifferent_access
+
+ model.log('create', @id, options) do
+ unless @meta
+ @meta = {}
+ if @meta.respond_to?(:with_indifferent_access)
+ @meta = @meta.with_indifferent_access
+ end
end
+ @meta['cas'] = model.bucket.add(@id, value, options)
end
- @meta['cas'] = model.bucket.add(@id, value, options)
self
end
@@ -543,8 +561,10 @@ def save(options = {})
return false
end
options = model.defaults.merge(options)
- value = (options[:format] == :plain) ? @raw : attributes_with_values
- @meta['cas'] = model.bucket.replace(@id, value, options)
+ model.log('save', @id, options) do
+ value = (options[:format] == :plain) ? @raw : attributes_with_values
+ @meta['cas'] = model.bucket.replace(@id, value, options)
+ end
self
end
@@ -587,7 +607,9 @@ def update(attrs, options = {})
# p.delete
def delete(options = {})
raise Couchbase::Error::MissingId, "missing id attribute" unless @id
- model.bucket.delete(@id, options)
+ model.log('delete', @id) do
+ model.bucket.delete(@id, options)
+ end
@id = nil
@meta = nil
self
45 lib/couchbase/model/logger.rb
View
@@ -0,0 +1,45 @@
+# Author:: Couchbase <info@couchbase.com>
+# Copyright:: 2012 Couchbase, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+module Couchbase
+
+ class Logger
+
+ def initialize
+ @_logger = if defined?(::Rails)
+ Rails.logger
+ elsif defined?(::Logger)
+ ::Logger.new($stderr)
+ else
+ nil
+ end
+ end
+
+ def log(operation, key, options = {}, &block)
+ return yield unless @_logger
+
+ start = Time.now
+ result = yield
+ elapsed = Time.now - start
+ @_logger.info("Couchbase [#{operation}] (#{elapsed}ms) key=\"#{key}\" (#{options})")
+ result
+ rescue Exception => e
+ @_logger.error "Couchbase error [#{e.class.name}] [#{e.message}]"
+ raise e
+ end
+ end
+end
1  test/setup.rb
View
@@ -165,4 +165,5 @@ def with_mock(params = {})
def uniq_id(*suffixes)
[caller.first[/.*[` ](.*)'/, 1], suffixes].join("_")
end
+
end
6 test/test_model.rb
View
@@ -126,13 +126,13 @@ def test_refreshes_the_attributes_with_reload_method
end
def test_it_raises_not_found_exception
- assert_raises Couchbase::Error::MissingId do
+ assert_raises Couchbase::Error::NotFound do
Post.find("missing_key")
end
end
- def test_it_raises_not_found_exception
- refute Post.find_by_id("missing_key")
+ def test_it_returns_nil_when_key_not_found
+ assert_equal nil, Post.find_by_id("missing_key")
end
def test_doesnt_raise_if_the_attribute_redefined
Something went wrong with that request. Please try again.