Sha256: c8b08175a91f520fbb0441835764fb4e0d2659c1b51d2c5ac226e9ca5245609d

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# encoding: UTF-8

require 'spec_helper'

describe Garage do 

  it "should populate materializers" do 
    @garage = Factory.create(:garage, :name => 'the-name', :location => 'the-location')

    @garage.materializers.should == [
      { :into => :name, :using => :name_as_json },
      { :into => :all,  :using => :as_json }
    ]
  end

  it "should materialize into names after save" do 
    @garage = Factory.create(:garage, :name => 'the-name', :location => 'the-location')

    @garage.name_json.should == @garage.from_materialized(:profile => :name)
    @garage.name_json.should == {:name => "the-name"}.to_json
  end

  it "should materialize into all after save" do 
    @garage = Factory.create(:garage, :name => 'the-name', :location => 'the-location')

    @garage.all_json.should == @garage.from_materialized(:profile => :all)
    @garage.all_json.should == {:name => "the-name", :location => "the-location", :cars => []}.to_json
  end

  it "should render the objects faster than normally" do 
    @garages = []
    @output = []

    10.times do 
      @garage = Factory.create(:garage)
      2.times do 
        @car = Factory.create(:car)
        @garage.cars << @car
      end
      @garages << @garage
    end

    time_before_native = Time.now.to_f
    @garages.each do |g|
      @output << g.as_json.to_json
    end
    time_after_native = Time.now.to_f

    time_before_optimized = Time.now.to_f
    @garages.each do |g|
      @output << g.from_materialized(:profile => :all)
    end
    time_after_optimized = Time.now.to_f

    optimized_time = (time_after_optimized - time_before_optimized)
    normal_time    = (time_after_native    - time_before_native)

    optimized_time.should < normal_time
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
materializer-0.0.1 spec/models/garage_spec.rb