Sha256: e7a4d839541de7fff119064e1a99af39a40ecd5cf77469d96f5e465f1aa873f9

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# -*- encoding : utf-8 -*-
require 'active_record/test_helper'

class ActiveRecord::FetchByUinqKeyTest < Minitest::Test
  def setup
    DatabaseCleaner[:active_record].start
    @user = User.create :name => 'hooopo', :email => 'hoooopo@gmail.com'
    @post = Post.create :slug => "foobar", :topic_id => 2
  end

  def test_cache_uniq_key
    assert_equal User.send(:cache_uniq_key, { :name => "hooopo" } ), "uniq_key_User_name_hooopo"
    assert_equal User.send(:cache_uniq_key, { :foo => 1, :bar => 2 } ), "uniq_key_User_foo_1,bar_2"
    long_val = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    assert_equal User.send(:cache_uniq_key, { :foo => 1, :bar => long_val } ), "uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
  end

  def test_should_query_from_db_using_primary_key
    Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
    $sql_logger = nil
    Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
    assert_equal $sql_logger.strip, 'SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1'
  end

  def test_should_not_hit_db_using_fetch_by_uniq_key_twice
    post = Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
    assert_equal post, @post
    no_connection do
      Post.fetch_by_uniq_keys(:topic_id => 2, :slug => "foobar")
    end
  end

  def test_should_fail_when_fetch_by_uniq_key_with_bang_method
    assert_raises(ActiveRecord::RecordNotFound) do
      Post.fetch_by_uniq_keys!(:topic_id => 2, :slug => "foobar1")
    end
    
    assert_raises(ActiveRecord::RecordNotFound) do
      User.fetch_by_uniq_key!("xxxxx", :name)
    end
  end
  
  def test_should_work_with_fetch_by_uniq_key
    user = User.fetch_by_uniq_key(@user.name, :name)
    assert_equal user, @user
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
second_level_cache-2.1.0.rc2 test/active_record/fetch_by_uniq_key_test.rb