Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 7 commits into from
Closed
56 changes: 39 additions & 17 deletions lib/couchbase/model.rb
Expand Up @@ -21,6 +21,7 @@
require 'couchbase/model/version' require 'couchbase/model/version'
require 'couchbase/model/uuid' require 'couchbase/model/uuid'
require 'couchbase/model/configuration' require 'couchbase/model/configuration'
require 'couchbase/model/logger'


unless Object.respond_to?(:singleton_class) unless Object.respond_to?(:singleton_class)
require 'couchbase/model/ext/singleton_class' require 'couchbase/model/ext/singleton_class'
Expand Down Expand Up @@ -129,6 +130,15 @@ class Model
# @private Container for all view names of all subclasses # @private Container for all view names of all subclasses
@@views = {} @@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 # Use custom connection options
# #
# @since 0.0.1 # @since 0.0.1
Expand Down Expand Up @@ -400,11 +410,13 @@ def self.belongs_to(name, options = {})
# @example Find model using +id+ # @example Find model using +id+
# post = Post.find('the-id') # post = Post.find('the-id')
def self.find(id) def self.find(id)
if id && (res = bucket.get(id, :quiet => false, :extended => true)) #log('find', id) do
obj, flags, cas = res if id && (res = bucket.get(id, :quiet => false, :extended => true))
obj = {:raw => obj} unless obj.is_a?(Hash) obj, flags, cas = res
new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj)) obj = {:raw => obj} unless obj.is_a?(Hash)
end new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
end
#end
end end


# Find the model using +id+ attribute # Find the model using +id+ attribute
Expand All @@ -418,10 +430,12 @@ def self.find(id)
# @example Find model using +id+ # @example Find model using +id+
# post = Post.find_by_id('the-id') # post = Post.find_by_id('the-id')
def self.find_by_id(id) def self.find_by_id(id)
if id && (res = bucket.get(id, :quiet => true, :extended => true)) log('find', id) do
obj, flags, cas = res if id && (res = bucket.get(id, :quiet => true, :extended => true))
obj = {:raw => obj} unless obj.is_a?(Hash) obj, flags, cas = res
new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj)) obj = {:raw => obj} unless obj.is_a?(Hash)
new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
end
end end
end end


Expand Down Expand Up @@ -495,15 +509,19 @@ def create(options = {})
if respond_to?(:valid?) && !valid? if respond_to?(:valid?) && !valid?
return false return false
end end

options = model.defaults.merge(options) options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values value = (options[:format] == :plain) ? @raw : attributes_with_values
unless @meta
@meta = {} model.log('create', @id, options) do
if @meta.respond_to?(:with_indifferent_access) unless @meta
@meta = @meta.with_indifferent_access @meta = {}
if @meta.respond_to?(:with_indifferent_access)
@meta = @meta.with_indifferent_access
end
end end
@meta['cas'] = model.bucket.add(@id, value, options)
end end
@meta['cas'] = model.bucket.add(@id, value, options)
self self
end end


Expand Down Expand Up @@ -543,8 +561,10 @@ def save(options = {})
return false return false
end end
options = model.defaults.merge(options) options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values model.log('save', @id, options) do
@meta['cas'] = model.bucket.replace(@id, value, options) value = (options[:format] == :plain) ? @raw : attributes_with_values
@meta['cas'] = model.bucket.replace(@id, value, options)
end
self self
end end


Expand Down Expand Up @@ -587,7 +607,9 @@ def update(attrs, options = {})
# p.delete # p.delete
def delete(options = {}) def delete(options = {})
raise Couchbase::Error::MissingId, "missing id attribute" unless @id 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 @id = nil
@meta = nil @meta = nil
self self
Expand Down
45 changes: 45 additions & 0 deletions lib/couchbase/model/logger.rb
@@ -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 change: 1 addition & 0 deletions test/setup.rb
Expand Up @@ -165,4 +165,5 @@ def with_mock(params = {})
def uniq_id(*suffixes) def uniq_id(*suffixes)
[caller.first[/.*[` ](.*)'/, 1], suffixes].join("_") [caller.first[/.*[` ](.*)'/, 1], suffixes].join("_")
end end

end end
6 changes: 3 additions & 3 deletions test/test_model.rb
Expand Up @@ -126,13 +126,13 @@ def test_refreshes_the_attributes_with_reload_method
end end


def test_it_raises_not_found_exception def test_it_raises_not_found_exception
assert_raises Couchbase::Error::MissingId do assert_raises Couchbase::Error::NotFound do
Post.find("missing_key") Post.find("missing_key")
end end
end end


def test_it_raises_not_found_exception def test_it_returns_nil_when_key_not_found
refute Post.find_by_id("missing_key") assert_equal nil, Post.find_by_id("missing_key")
end end


def test_doesnt_raise_if_the_attribute_redefined def test_doesnt_raise_if_the_attribute_redefined
Expand Down