Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add test case.

  • Loading branch information...
commit f86b2ec07e62c0eca38c75c30847c72c93f4d6ac 1 parent 3440c27
huangzg authored
View
2  .gitignore
@@ -8,4 +8,6 @@
*.log
*.gem
*.sqlite3
+*.swp
+*.swo
View
97 Gemfile.lock
@@ -0,0 +1,97 @@
+PATH
+ remote: .
+ specs:
+ second_level_cache (1.1.1)
+ rails (> 3.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.1)
+ actionpack (= 3.2.1)
+ mail (~> 2.4.0)
+ actionpack (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.1)
+ activesupport (= 3.2.1)
+ builder (~> 3.0.0)
+ activerecord (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ arel (~> 3.0.0)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.1)
+ activemodel (= 3.2.1)
+ activesupport (= 3.2.1)
+ activesupport (3.2.1)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.0)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.1)
+ json (1.6.5)
+ mail (2.4.1)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.17.2)
+ multi_json (1.0.4)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.1)
+ actionmailer (= 3.2.1)
+ actionpack (= 3.2.1)
+ activerecord (= 3.2.1)
+ activeresource (= 3.2.1)
+ activesupport (= 3.2.1)
+ bundler (~> 1.0)
+ railties (= 3.2.1)
+ railties (3.2.1)
+ actionpack (= 3.2.1)
+ activesupport (= 3.2.1)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ redis (2.2.2)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.5)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.31)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord (> 3.0)
+ rake
+ redis
+ second_level_cache!
+ sqlite3
View
10 Rakefile
@@ -1,2 +1,10 @@
#!/usr/bin/env rake
-require "bundler/gem_tasks"
+require 'bundler/gem_tasks'
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << "lib" << "test"
+ t.test_files = FileList['test/**/*_test.rb']
+end
View
1  second_level_cache.gemspec
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "rails", ["> 3.0"]
gem.add_development_dependency "activerecord", ["> 3.0"]
+ gem.add_development_dependency "sqlite3"
gem.add_development_dependency "redis"
gem.add_development_dependency "rake"
end
View
11 test/active_record/model/book.rb
@@ -0,0 +1,11 @@
+ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
+ t.string :title
+ t.string :body
+ t.integer :user_id
+end
+
+class Book < ActiveRecord::Base
+ acts_as_cached
+
+ belongs_to :user
+end
View
10 test/active_record/model/user.rb
@@ -0,0 +1,10 @@
+ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
+ t.string :name
+ t.string :email
+end
+
+class User < ActiveRecord::Base
+ acts_as_cached
+
+ has_many :books
+end
View
43 test/active_record/second_level_cache_test.rb
@@ -0,0 +1,43 @@
+require 'active_record/test_helper'
+
+class ActiveRecord::SecondLevelCacheTest < Test::Unit::TestCase
+ def setup
+ @user = User.create :name => 'csdn', :email => 'test@csdn.com'
+ end
+
+ def teardown
+ User.delete_all
+ end
+
+ def test_should_get_cache_key
+ assert_equal "users/#{@user.id}", @user.second_level_cache_key
+ end
+
+ def test_should_have_cache_when_create
+ no_connection do
+ assert_not_nil User.cache_store.get(@user.second_level_cache_key)
+ assert_equal @user, User.find(@user.id)
+ end
+ end
+
+ def test_should_update_cache_when_update
+ @user.update_attributes :name => 'change'
+
+ no_connection do
+ assert_equal 'change', User.find(@user.id).name
+ end
+ end
+
+ def test_should_get_cache_when_use_singular_association
+ book = @user.books.create
+
+ no_connection do
+ assert_equal @user, book.user
+ end
+ end
+
+ def test_should_expire_cache_when_destroy
+ @user.destroy
+ assert_nil User.cache_store.get(@user.second_level_cache_key)
+ end
+end
View
26 test/active_record/test_helper.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+require 'active_record'
+
+def open_test_db_connect
+ ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => 'test/test.sqlite3'
+ )
+end
+open_test_db_connect
+
+def close_test_db_connect
+ ActiveRecord::Base.connection.disconnect!
+end
+
+class Test::Unit::TestCase
+ def no_connection
+ close_test_db_connect
+ assert_nothing_raised { yield }
+ ensure
+ open_test_db_connect
+ end
+end
+
+require 'active_record/model/user'
+require 'active_record/model/book'
View
6 test/test_helper.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'second_level_cache'
+require 'test/unit'
+
+$redis = Redis.new
Please sign in to comment.
Something went wrong with that request. Please try again.