Refactoring tests and Validation callbacks
This commit is contained in:
parent
ea4325f5bf
commit
3579e0e334
47 changed files with 142 additions and 142 deletions
|
@ -7,7 +7,6 @@ module CouchRest
|
|||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::Connection
|
||||
include CouchRest::Model::Persistence
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::DocumentQueries
|
||||
include CouchRest::Model::Views
|
||||
include CouchRest::Model::DesignDoc
|
||||
|
@ -18,6 +17,7 @@ module CouchRest
|
|||
include CouchRest::Model::PropertyProtection
|
||||
include CouchRest::Model::Associations
|
||||
include CouchRest::Model::Validations
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::Designs
|
||||
include CouchRest::Model::CastedBy
|
||||
include CouchRest::Model::Dirty
|
||||
|
|
|
@ -5,21 +5,23 @@ module CouchRest #:nodoc:
|
|||
|
||||
module Callbacks
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
CALLBACKS = [
|
||||
:before_validation, :after_validation,
|
||||
:after_initialize,
|
||||
:before_create, :around_create, :after_create,
|
||||
:before_destroy, :around_destroy, :after_destroy,
|
||||
:before_save, :around_save, :after_save,
|
||||
:before_update, :around_update, :after_update,
|
||||
]
|
||||
|
||||
included do
|
||||
extend ActiveModel::Callbacks
|
||||
include ActiveModel::Validations::Callbacks
|
||||
|
||||
define_model_callbacks \
|
||||
:create,
|
||||
:destroy,
|
||||
:save,
|
||||
:update
|
||||
|
||||
define_model_callbacks :initialize, :only => :after
|
||||
define_model_callbacks :create, :destroy, :save, :update
|
||||
end
|
||||
|
||||
def valid?(*) #nodoc
|
||||
_run_validation_callbacks { super }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -5,11 +5,11 @@ module CouchRest::Model
|
|||
|
||||
included do
|
||||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::Properties
|
||||
include CouchRest::Model::PropertyProtection
|
||||
include CouchRest::Model::Associations
|
||||
include CouchRest::Model::Validations
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::CastedBy
|
||||
include CouchRest::Model::Dirty
|
||||
class_eval do
|
||||
|
|
|
@ -13,22 +13,33 @@ module CouchRest
|
|||
# Validations may be applied to both Model::Base and Model::CastedModel
|
||||
module Validations
|
||||
extend ActiveSupport::Concern
|
||||
included do
|
||||
include ActiveModel::Validations
|
||||
include ActiveModel::Validations::Callbacks
|
||||
include ActiveModel::Validations
|
||||
|
||||
# Determine if the document is valid.
|
||||
#
|
||||
# @example Is the document valid?
|
||||
# person.valid?
|
||||
#
|
||||
# @example Is the document valid in a context?
|
||||
# person.valid?(:create)
|
||||
#
|
||||
# @param [ Symbol ] context The optional validation context.
|
||||
#
|
||||
# @return [ true, false ] True if valid, false if not.
|
||||
#
|
||||
def valid?(context = nil)
|
||||
super context ? context : (new? ? :create : :update)
|
||||
end
|
||||
|
||||
|
||||
module ClassMethods
|
||||
|
||||
# Validates the associated casted model. This method should not be
|
||||
|
||||
# Validates the associated casted model. This method should not be
|
||||
# used within your code as it is automatically included when a CastedModel
|
||||
# is used inside the model.
|
||||
#
|
||||
def validates_casted_model(*args)
|
||||
validates_with(CastedModelValidator, _merge_attributes(args))
|
||||
end
|
||||
|
||||
|
||||
# Validates if the field is unique for this type of document. Automatically creates
|
||||
# a view if one does not already exist and performs a search for all matching
|
||||
# documents.
|
||||
|
|
|
@ -60,8 +60,8 @@ require "couchrest/model/core_extensions/time_parsing"
|
|||
# Base libraries
|
||||
require "couchrest/model/casted_model"
|
||||
require "couchrest/model/base"
|
||||
# Add rails support *after* everything has loaded
|
||||
|
||||
# Add rails support *after* everything has loaded
|
||||
if defined?(Rails)
|
||||
require "couchrest/railtie"
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'person'
|
||||
|
||||
class Card < CouchRest::Model::Base
|
||||
# Set the default database to use
|
||||
use_database DB
|
|
@ -1,5 +1,5 @@
|
|||
require File.join(FIXTURE_PATH, 'more', 'question')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require 'question'
|
||||
require 'person'
|
||||
|
||||
class Course < CouchRest::Model::Base
|
||||
use_database TEST_SERVER.default_database
|
|
@ -6,9 +6,9 @@ class Invoice < CouchRest::Model::Base
|
|||
property :client_name
|
||||
property :employee_name
|
||||
property :location
|
||||
|
||||
|
||||
# Validation
|
||||
validates_presence_of :client_name, :employee_name
|
||||
validates_presence_of :location, :message => "Hey stupid!, you forgot the location"
|
||||
|
||||
|
||||
end
|
4
spec/fixtures/models/membership.rb
vendored
Normal file
4
spec/fixtures/models/membership.rb
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
class Membership < Hash
|
||||
include CouchRest::Model::CastedModel
|
||||
|
||||
end
|
|
@ -1,3 +1,5 @@
|
|||
require 'cat'
|
||||
|
||||
class Person < Hash
|
||||
include ::CouchRest::Model::CastedModel
|
||||
property :pet, Cat
|
|
@ -1,5 +1,6 @@
|
|||
require File.join(FIXTURE_PATH, 'more', 'client')
|
||||
require File.join(FIXTURE_PATH, 'more', 'sale_entry')
|
||||
require 'client'
|
||||
require 'sale_entry'
|
||||
|
||||
class SaleInvoice < CouchRest::Model::Base
|
||||
use_database DB
|
||||
|
||||
|
@ -10,4 +11,4 @@ class SaleInvoice < CouchRest::Model::Base
|
|||
|
||||
property :date, Date
|
||||
property :price, Integer
|
||||
end
|
||||
end
|
8
spec/functional/validations_spec.rb
Normal file
8
spec/functional/validations_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
|
||||
describe CouchRest::Model::Validations do
|
||||
|
||||
let(:invoice) do
|
||||
Invoice.new()
|
||||
end
|
||||
end
|
|
@ -1,9 +1,14 @@
|
|||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
||||
|
||||
MODELS = File.join(File.dirname(__FILE__), "fixtures", "models")
|
||||
$LOAD_PATH.unshift(MODELS)
|
||||
|
||||
require "bundler/setup"
|
||||
require "rubygems"
|
||||
require "rspec" # Satisfies Autotest and anyone else not using the Rake tasks
|
||||
require "rspec"
|
||||
|
||||
require File.join(File.dirname(__FILE__), '..','lib','couchrest_model')
|
||||
# check the following file to see how to use the spec'd features.
|
||||
require 'couchrest_model'
|
||||
|
||||
unless defined?(FIXTURE_PATH)
|
||||
FIXTURE_PATH = File.join(File.dirname(__FILE__), '/fixtures')
|
||||
|
@ -16,6 +21,21 @@ unless defined?(FIXTURE_PATH)
|
|||
DB = TEST_SERVER.database(TESTDB)
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.before(:all) { reset_test_db! }
|
||||
|
||||
config.after(:all) do
|
||||
cr = TEST_SERVER
|
||||
test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
|
||||
test_dbs.each do |db|
|
||||
cr.database(db).delete! rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Require each of the fixture models
|
||||
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
|
||||
|
||||
class Basic < CouchRest::Model::Base
|
||||
use_database TEST_SERVER.default_database
|
||||
end
|
||||
|
@ -27,17 +47,6 @@ def reset_test_db!
|
|||
DB
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.before(:all) { reset_test_db! }
|
||||
|
||||
config.after(:all) do
|
||||
cr = TEST_SERVER
|
||||
test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
|
||||
test_dbs.each do |db|
|
||||
cr.database(db).delete! rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def couchdb_lucene_available?
|
||||
lucene_path = "http://localhost:5985/"
|
||||
|
|
30
spec/unit/active_model_lint_spec.rb
Normal file
30
spec/unit/active_model_lint_spec.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
# encoding: utf-8
|
||||
require 'spec_helper'
|
||||
require 'test/unit/assertions'
|
||||
require 'active_model/lint'
|
||||
|
||||
class CompliantModel < CouchRest::Model::Base
|
||||
end
|
||||
|
||||
|
||||
describe CouchRest::Model::Base do
|
||||
include Test::Unit::Assertions
|
||||
include ActiveModel::Lint::Tests
|
||||
|
||||
before :each do
|
||||
@model = CompliantModel.new
|
||||
end
|
||||
|
||||
describe "active model lint tests" do
|
||||
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
|
||||
example m.gsub('_',' ') do
|
||||
send m
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def model
|
||||
@model
|
||||
end
|
||||
|
||||
end
|
|
@ -1,7 +1,5 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'sale_invoice')
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Assocations" do
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Model attachments" do
|
||||
|
|
@ -1,11 +1,5 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'base')
|
||||
require "spec_helper"
|
||||
|
||||
describe "Model Base" do
|
||||
|
|
@ -1,12 +1,5 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'more', 'question')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
class WithCastedModelMixin < Hash
|
||||
include CouchRest::Model::CastedModel
|
|
@ -1,7 +1,4 @@
|
|||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require "spec_helper"
|
||||
|
||||
class Driver < CouchRest::Model::Base
|
||||
use_database TEST_SERVER.default_database
|
|
@ -1,4 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require "spec_helper"
|
||||
|
||||
class UnattachedDoc < CouchRest::Model::Base
|
||||
# Note: no use_database here
|
|
@ -1,5 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require "spec_helper"
|
||||
|
||||
describe "Collections" do
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require "spec_helper"
|
||||
|
||||
describe CouchRest::Model::Base do
|
||||
describe CouchRest::Model::Configuration do
|
||||
|
||||
before do
|
||||
@class = Class.new(CouchRest::Model::Base)
|
|
@ -1,7 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require 'spec_helper'
|
||||
|
||||
describe CouchRest::Model::Base do
|
||||
describe CouchRest::Model::Connection do
|
||||
|
||||
before do
|
||||
@class = Class.new(CouchRest::Model::Base)
|
|
@ -1,10 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require 'spec_helper'
|
||||
|
||||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'base')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
|
||||
describe "Design Documents" do
|
||||
describe CouchRest::Model::DesignDoc do
|
||||
|
||||
before :all do
|
||||
reset_test_db!
|
|
@ -1,10 +1,9 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require "spec_helper"
|
||||
|
||||
class DesignModel < CouchRest::Model::Base
|
||||
|
||||
end
|
||||
|
||||
describe "Design" do
|
||||
describe CouchRest::Model::Designs do
|
||||
|
||||
it "should accessable from model" do
|
||||
DesignModel.respond_to?(:design).should be_true
|
|
@ -1,10 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'base')
|
||||
require "spec_helper"
|
||||
|
||||
class WithCastedModelMixin < Hash
|
||||
include CouchRest::Model::CastedModel
|
|
@ -1,4 +1,4 @@
|
|||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require 'spec_helper'
|
||||
|
||||
class PlainParent
|
||||
class_inheritable_accessor :foo
|
|
@ -1,13 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'base')
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'more', 'event')
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Model Persistence" do
|
||||
describe CouchRest::Model::Persistence do
|
||||
|
||||
before(:each) do
|
||||
@obj = WithDefaultValues.new
|
|
@ -1,4 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require "spec_helper"
|
||||
|
||||
describe "Model Attributes" do
|
||||
|
|
@ -1,18 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'more', 'invoice')
|
||||
require File.join(FIXTURE_PATH, 'more', 'service')
|
||||
require File.join(FIXTURE_PATH, 'more', 'event')
|
||||
require File.join(FIXTURE_PATH, 'more', 'user')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require File.join(FIXTURE_PATH, "more", "key_chain")
|
||||
require 'spec_helper'
|
||||
|
||||
|
||||
describe "Model properties" do
|
||||
describe CouchRest::Model::Property do
|
||||
|
||||
before(:each) do
|
||||
reset_test_db!
|
|
@ -1,6 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require "spec_helper"
|
||||
|
||||
class DummyProxyable < CouchRest::Model::Base
|
||||
proxy_database_method :db
|
||||
|
@ -12,7 +10,7 @@ end
|
|||
class ProxyKitten < CouchRest::Model::Base
|
||||
end
|
||||
|
||||
describe "Proxyable" do
|
||||
describe CouchRest::Model::Proxyable do
|
||||
|
||||
describe "#proxy_database" do
|
||||
|
|
@ -1,8 +1,4 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require "spec_helper"
|
||||
|
||||
# add a default value
|
||||
Card.property :bg_color, :default => '#ccc'
|
|
@ -1,8 +1,5 @@
|
|||
# encoding: utf-8
|
||||
require File.expand_path('../../spec_helper', __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Type Casting" do
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require "spec_helper"
|
||||
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
require File.join(FIXTURE_PATH, 'base')
|
||||
|
||||
# TODO Move validations from other specs to here
|
||||
|
||||
describe "Validations" do
|
||||
describe CouchRest::Model::Validations do
|
||||
|
||||
describe "Uniqueness" do
|
||||
|
|
@ -1,10 +1,6 @@
|
|||
require File.expand_path("../../spec_helper", __FILE__)
|
||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||
require File.join(FIXTURE_PATH, 'more', 'article')
|
||||
require File.join(FIXTURE_PATH, 'more', 'course')
|
||||
require "spec_helper"
|
||||
|
||||
describe "Model views" do
|
||||
describe CouchRest::Model::Views do
|
||||
|
||||
class Unattached < CouchRest::Model::Base
|
||||
property :title
|
||||
|
@ -17,7 +13,6 @@ describe "Model views" do
|
|||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "ClassMethods" do
|
||||
# NOTE! Add more unit tests!
|
Loading…
Reference in a new issue