Sha256: 085415e1b5ea9db9598855b80fae2e4428559bc16030b9c5c40313ba40dc1f82

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'

describe DataMapper::Is::CounterCacheable do
  before(:all) do
    DataMapper.auto_migrate!

    User.create(
      :name => 'bob',
      :posts => [
        {:title => 'Hello', :body => 'Hello there.'}
      ]
    )
  end

  before(:each) do
    @user = User.first
    @post = @user.posts.first
  end

  it "should define the default counter cache property" do
    Post.properties.should be_named('comments_counter')
  end

  it "should allow defining custom named counter cache properties" do
    User.properties.should be_named('post_comments_counter')
  end

  it "should optionally define a counter index column" do
    Comment.properties.should be_named('user_comments_index')
  end

  it "should have a counter cache of 0 by default" do
    @post.comments_counter.should == 0
  end

  it "should increment the counter cache by 1 when a new resource is created" do
    orig_counter = @post.comments_counter

    @post.comments.create(
      :body => 'lol',
      :user => @user
    )

    new_counter = @post.comments_counter

    (new_counter - orig_counter).should == 1
  end

  it "should increment the counter cache by 1 when a new resource is saved" do
    orig_counter = @post.comments_counter

    @post.comments.new(
      :body => 'omg',
      :user => @user
    ).save

    new_counter = @post.comments_counter

    (new_counter - orig_counter).should == 1
  end

  it "should set the counter index to the counter value when a new resource is created" do
    @post.comments.create(
      :body => 'lol',
      :user => @user
    )

    @post.comments.last.user_comments_index.should == @user.post_comments_counter
  end

  it "should decrement the counter cache by 1 when a resource is destroyed" do
    @post.comments.create(
      :body => 'wtf',
      :user => @user
    )

    orig_counter = @post.comments_counter

    @post.comments.first.destroy

    new_counter = @post.comments_counter

    (new_counter - orig_counter).should == -1
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-is-counter_cacheable-0.1.1 spec/integration/counter_cacheable_spec.rb
dm-is-counter_cacheable-0.1.0 spec/integration/counter_cacheable_spec.rb