Adding tests to some expected behaviors

This commit is contained in:
Marcos Tapajós 2010-08-03 22:55:17 -03:00
parent e3386a45f4
commit 1a551b54eb
5 changed files with 44 additions and 32 deletions

View file

@ -2,7 +2,7 @@ module CouchRest
module Model module Model
module Associations module Associations
# Basic support for relationships between ExtendedDocuments # Basic support for relationships between CouchRest::Model::Base
def self.included(base) def self.included(base)
base.extend(ClassMethods) base.extend(ClassMethods)
@ -175,14 +175,17 @@ module CouchRest
end end
super(array) super(array)
end end
def << obj def << obj
casted_by[property.to_s] << obj.id casted_by[property.to_s] << obj.id
super(obj) super(obj)
end end
def push(obj) def push(obj)
casted_by[property.to_s].push obj.id casted_by[property.to_s].push obj.id
super(obj) super(obj)
end end
def unshift(obj) def unshift(obj)
casted_by[property.to_s].unshift obj.id casted_by[property.to_s].unshift obj.id
super(obj) super(obj)
@ -197,6 +200,7 @@ module CouchRest
casted_by[property.to_s].pop casted_by[property.to_s].pop
super super
end end
def shift def shift
casted_by[property.to_s].shift casted_by[property.to_s].shift
super super

View file

@ -1,31 +1,6 @@
# encoding: utf-8 # encoding: utf-8
require File.expand_path('../../spec_helper', __FILE__) require File.expand_path('../../spec_helper', __FILE__)
require File.join(FIXTURE_PATH, 'more', 'sale_invoice')
class Client < CouchRest::Model::Base
use_database DB
property :name
property :tax_code
end
class SaleEntry < CouchRest::Model::Base
use_database DB
property :description
property :price
end
class SaleInvoice < CouchRest::Model::Base
use_database DB
belongs_to :client
belongs_to :alternate_client, :class_name => 'Client', :foreign_key => 'alt_client_id'
collection_of :entries, :class_name => 'SaleEntry'
property :date, Date
property :price, Integer
end
describe "Assocations" do describe "Assocations" do
@ -159,7 +134,7 @@ describe "Assocations" do
end end
describe "proxy" do describe "proxy" do
it "should ensure new entries to proxy are matched" do it "should ensure new entries to proxy are matched" do
@invoice.entries << @entries.first @invoice.entries << @entries.first
@invoice.entry_ids.first.should eql(@entries.first.id) @invoice.entry_ids.first.should eql(@entries.first.id)
@ -202,12 +177,17 @@ describe "Assocations" do
@invoice.entries.first.should eql(@entries[1]) @invoice.entries.first.should eql(@entries[1])
@invoice.entry_ids.first.should eql(@entries[1].id) @invoice.entry_ids.first.should eql(@entries[1].id)
end end
it "should save all entries when invoice is saved" do
SaleEntry.find_by_description('test entry').should be_nil
entry = SaleEntry.new(:description => 'test entry', :price => 500)
@invoice.entries << entry
@invoice.save.should be_true
SaleEntry.find_by_description('test entry').should_not be_nil
end
end end
end end
end end

6
spec/fixtures/more/client.rb vendored Normal file
View file

@ -0,0 +1,6 @@
class Client < CouchRest::Model::Base
use_database DB
property :name
property :tax_code
end

9
spec/fixtures/more/sale_entry.rb vendored Normal file
View file

@ -0,0 +1,9 @@
class SaleEntry < CouchRest::Model::Base
use_database DB
property :description
property :price
view_by :description
end

13
spec/fixtures/more/sale_invoice.rb vendored Normal file
View file

@ -0,0 +1,13 @@
require File.join(FIXTURE_PATH, 'more', 'client')
require File.join(FIXTURE_PATH, 'more', 'sale_entry')
class SaleInvoice < CouchRest::Model::Base
use_database DB
belongs_to :client
belongs_to :alternate_client, :class_name => 'Client', :foreign_key => 'alt_client_id'
collection_of :entries, :class_name => 'SaleEntry'
property :date, Date
property :price, Integer
end