Sha256: 19147c845039d84b7c6e075f1db4571785567b0cafe8123a82dafa813794fd02

Contents?: true

Size: 1.71 KB

Versions: 11

Compression:

Stored size: 1.71 KB

Contents

require "spec_helper"

class Driver < CouchRest::Model::Base
  use_database TEST_SERVER.default_database
  # You have to add a casted_by accessor if you want to reach a casted extended doc parent
  attr_accessor :casted_by
  
  property :name
end

class Car < CouchRest::Model::Base
  use_database TEST_SERVER.default_database
  
  property :name
  property :driver, Driver
end

describe "casting an extended document" do
  
  before(:each) do
    @driver = Driver.new(:name => 'Matt')
    @car    = Car.new(:name => 'Renault 306', :driver => @driver)
  end

  it "should retain all properties of the casted attribute" do
    @car.driver.should == @driver
  end
  
  it "should let the casted document know who casted it" do
    @car.driver.casted_by.should == @car
  end
end

describe "assigning a value to casted attribute after initializing an object" do

  before(:each) do
    @car    = Car.new(:name => 'Renault 306')
    @driver = Driver.new(:name => 'Matt')
  end
  
  it "should not create an empty casted object" do
    @car.driver.should be_nil
  end
  
  it "should let you assign the value" do
    @car.driver = @driver
    @car.driver.name.should == 'Matt'
  end
  
  it "should cast attribute" do
    @car.driver = JSON.parse(@driver.to_json)
    @car.driver.should be_instance_of(Driver)
  end

end

describe "casting a model from parsed JSON" do

  before(:each) do
    @driver = Driver.new(:name => 'Matt')
    @car    = Car.new(:name => 'Renault 306', :driver => @driver)
    @new_car = Car.new(JSON.parse(@car.to_json))
  end

  it "should cast casted attribute" do
    @new_car.driver.should be_instance_of(Driver)
  end
  
  it "should retain all properties of the casted attribute" do
    @new_car.driver.should == @driver
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
couchrest_model-2.0.4 spec/unit/casted_spec.rb
couchrest_model-2.0.3 spec/unit/casted_spec.rb
couchrest_model-2.0.1 spec/unit/casted_spec.rb
couchrest_model-2.0.0 spec/unit/casted_spec.rb
couchrest_model-2.0.0.beta2 spec/unit/casted_spec.rb
couchrest_model-2.0.0.beta spec/unit/casted_spec.rb
couchrest_model-1.2.0.beta spec/unit/casted_spec.rb
openlogic-couchrest_model-1.0.0 spec/unit/casted_spec.rb
couchrest_model-1.1.2 spec/unit/casted_spec.rb
couchrest_model-1.1.1 spec/unit/casted_spec.rb
couchrest_model-1.1.0 spec/unit/casted_spec.rb