require 'test/unit' require 'rubygems' gem 'activerecord', '>= 1.15.4.7794' gem 'rails' require 'active_record' require "#{File.dirname(__FILE__)}/../init" ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:") def setup_db ActiveRecord::Schema.define(:version => 1) do create_table "books", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "reviews", :force => true do |t| t.integer "user_id", :null => false t.integer "book_id", :null => false t.integer "score", :default => 0 end create_table "users", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end end end def teardown_db ActiveRecord::Base.connection.tables.each do |table| ActiveRecord::Base.connection.drop_table(table) end end class Book < ActiveRecord::Base has_many :reviews has_many :users, :through => :reviews end class Review < ActiveRecord::Base belongs_to :book belongs_to :user end class User < ActiveRecord::Base has_many :reviews has_many :books, :through => :reviews acts_as_recommendable :books, :through => :reviews end class RecommendableTest < Test::Unit::TestCase def setup setup_db @leo = User.create :name => 'Leo' @bru = User.create :name => 'Bru' @carol = User.create :name => 'Carol' @ipod = Book.create :name => 'ipod' @wii = Book.create :name => 'wii' @bla = Book.create :name => 'Dr. bla' Review.create(:book => @ipod, :user =>@leo) Review.create(:book => @wii, :user => @leo) Review.create(:book => @wii, :user => @bru) Review.create(:book => @bla, :user => @carol) end def teardown teardown_db end def test_available_methods user = User.new assert_not_nil user assert_respond_to user, :similar_users assert_respond_to user, :recommended_books end def test_similar_users sim_users = @leo.similar_users assert sim_users.include?(@bru) end def test_similar_users_format sim_users = @leo.similar_users assert_kind_of Array, sim_users assert_kind_of User, sim_users.first assert_kind_of Numeric, sim_users.first.similar_score end def test_similar_users_results sim_users = @leo.similar_users assert sim_users.include?(@bru) assert_respond_to sim_users[0], :similar_score assert !sim_users.include?(@carol) end def test_similar_users_scores sim_users = @leo.similar_users assert_respond_to sim_users[0], :similar_score assert sim_users[0].similar_score > 0 end def test_recommended_books recommended_books = @leo.recommended_books assert_not_nil recommended_books end def test_recommended_books_format recommended_books = @bru.recommended_books assert_kind_of Array, recommended_books assert_kind_of Book, recommended_books.first assert_kind_of Numeric, recommended_books.first.recommendation_score end def test_recommended_books_results recommended_books = @bru.recommended_books assert_equal true, recommended_books.include?(@ipod) assert recommended_books.find {|b| b == @ipod }.recommendation_score > 0 end def test_recommended_books_scores recommended_books = @bru.recommended_books assert_respond_to recommended_books[0], :recommendation_score assert recommended_books[0].recommendation_score > 0 end end