require 'test/unit' require 'tagtools' require 'yaml' begin ActiveRecord::Base.connection rescue possible_config_files = [ "./database.yml", "./config/database.yml", "../database.yml", "../config/database.yml" ] database_config_file = nil for file in possible_config_files if File.exists? file database_config_file = file break end end database_config_hash = File.open(database_config_file) do |file| config_hash = YAML::load(file) unless config_hash['test'].nil? config_hash = config_hash['test'] end config_hash end ActiveRecord::Base.configurations = database_config_hash ActiveRecord::Base.establish_connection(database_config_hash) ActiveRecord::Base.connection end class User < ActiveRecord::Base end class Tag < ActiveRecord::Base extend TaggingHelpers end class Bookmark < ActiveRecord::Base acts_as_taggable(:scope => :user, :user_class_name => "User", :tag_class_name => "Tag") end class UserTagsTest < Test::Unit::TestCase def setup ActiveRecord::Base.connection.execute("DELETE FROM bookmarks_tags_users") ActiveRecord::Base.connection.execute("DELETE FROM bookmarks") ActiveRecord::Base.connection.execute("DELETE FROM users") ActiveRecord::Base.connection.execute("DELETE FROM tags") end def teardown ActiveRecord::Base.connection.execute("DELETE FROM bookmarks_tags_users") ActiveRecord::Base.connection.execute("DELETE FROM bookmarks") ActiveRecord::Base.connection.execute("DELETE FROM users") ActiveRecord::Base.connection.execute("DELETE FROM tags") end def test_add_tags_existing_record some_guy = User.new some_guy.name = "Joe Normal" some_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" ruby_on_rails.save ruby_on_rails.user_tags(some_guy.id).concat( ["ruby", "rails", "framework"]) assert_equal(Tag.find_by_name("framework"), ruby_on_rails.user_tags(some_guy.id, true)[0]) assert_equal(Tag.find_by_name("rails"), ruby_on_rails.user_tags(some_guy.id, true)[1]) assert_equal(Tag.find_by_name("ruby"), ruby_on_rails.user_tags(some_guy.id, true)[2]) end def test_add_tags_new_record some_guy = User.new some_guy.name = "Joe Normal" some_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" assert_equal(true, ruby_on_rails.new_record?) ruby_on_rails.user_tags(some_guy.id).concat( ["ruby", "rails", "framework"]) assert_equal(true, ruby_on_rails.new_record?) ruby_on_rails.save assert_equal(false, ruby_on_rails.new_record?) assert_equal(Tag.find_by_name("framework"), ruby_on_rails.user_tags(some_guy.id, true)[0]) assert_equal(Tag.find_by_name("rails"), ruby_on_rails.user_tags(some_guy.id, true)[1]) assert_equal(Tag.find_by_name("ruby"), ruby_on_rails.user_tags(some_guy.id, true)[2]) end def test_tag_query some_guy = User.new some_guy.name = "Joe Normal" some_guy.save some_other_guy = User.new some_other_guy.name = "Joe Abnormal" some_other_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" ruby_on_rails.save ruby_on_rails.user_tags(some_guy.id) << "ruby" ruby_on_rails.user_tags(some_guy.id) << "rails" ruby_on_rails.user_tags(some_guy.id) << "framework" red_handed = Bookmark.new red_handed.url = "http://redhanded.hobix.com" red_handed.save red_handed.user_tags(some_guy.id) << "ruby" red_handed.user_tags(some_guy.id) << "cult" red_handed.user_tags(some_other_guy.id) << "spinach" red_handed.user_tags(some_other_guy.id) << "turkey" red_handed.user_tags(some_other_guy.id) << "atlantis" red_handed.user_tags(some_other_guy.id) << "ruby" results = Bookmark.tag_query(:with_all_tags => ["ruby", "cult"]) assert_equal(red_handed, results.first) assert_equal(1, results.size) results = Bookmark.tag_query(:with_all_tags => ["ruby", "framework"]) assert_equal(ruby_on_rails, results.first) assert_equal(1, results.size) results = Bookmark.tag_query(:with_any_tags => ["cult", "framework"]) assert(results.include?(ruby_on_rails)) assert(results.include?(red_handed)) assert_equal(2, results.size) results = Bookmark.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"]) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) assert_equal(1, results.size) results = Bookmark.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"], :user_id => some_guy.id) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) assert_equal(1, results.size) results = Bookmark.tag_query(:with_all_tags => ["spinach"], :with_any_tags => ["ruby", "cult", "rails", "framework"], :without_tags => ["cult"], :user_id => some_other_guy.id) assert_equal(red_handed, results.first) assert(!results.include?(ruby_on_rails)) assert_equal(1, results.size) results = Bookmark.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"], :user_id => -1) assert_equal(0, results.size) end def test_include some_guy = User.new some_guy.name = "Joe Normal" some_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" ruby_on_rails.save ruby_on_rails.user_tags(some_guy.id) << "ruby" ruby_on_rails.user_tags(some_guy.id) << "rails" ruby_on_rails.user_tags(some_guy.id) << "framework" assert_equal(true, ruby_on_rails.user_tags(some_guy.id, true).include?("rails")) assert_equal(false, ruby_on_rails.user_tags(some_guy.id, true).include?("notpresent")) assert_equal(true, ruby_on_rails.user_tags(some_guy.id, true).include?( Tag.find_by_name("framework"))) assert_equal(false, ruby_on_rails.user_tags(some_guy.id, true).include?( Tag.find_by_name("alsonotpresent"))) end def test_size some_guy = User.new some_guy.name = "Joe Normal" some_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" ruby_on_rails.save ruby_on_rails.user_tags(some_guy.id) << "ruby" ruby_on_rails.user_tags(some_guy.id) << "rails" ruby_on_rails.user_tags(some_guy.id) << "framework" assert_equal(3, ruby_on_rails.user_tags(some_guy.id, true).size) ruby_on_rails.user_tags(some_guy.id, true) << "rails" assert_equal(3, ruby_on_rails.user_tags(some_guy.id, true).size) ruby_on_rails.user_tags(some_guy.id) << "web" assert_equal(4, ruby_on_rails.user_tags(some_guy.id, true).size) ruby_on_rails.user_tags(some_guy.id).delete("framework") assert_equal(3, ruby_on_rails.user_tags(some_guy.id).size) ruby_on_rails.user_tags(some_guy.id).delete("ruby") assert_equal(2, ruby_on_rails.user_tags(some_guy.id, true).size) end def test_tag_helpers some_guy = User.new some_guy.name = "Joe Normal" some_guy.save some_other_guy = User.new some_other_guy.name = "Joe Abnormal" some_other_guy.save ruby_on_rails = Bookmark.new ruby_on_rails.url = "http://www.rubyonrails.com" ruby_on_rails.save ruby_on_rails.user_tags(some_guy.id) << "ruby" ruby_on_rails.user_tags(some_guy.id) << "rails" ruby_on_rails.user_tags(some_guy.id) << "framework" red_handed = Bookmark.new red_handed.url = "http://redhanded.hobix.com" red_handed.save red_handed.user_tags(some_guy.id) << "ruby" red_handed.user_tags(some_guy.id) << "cult" red_handed.user_tags(some_other_guy.id) << "spinach" red_handed.user_tags(some_other_guy.id) << "turkey" red_handed.user_tags(some_other_guy.id) << "atlantis" red_handed.user_tags(some_other_guy.id) << "ruby" tag = Tag.get_tag("ruby") assert_equal("ruby", tag.name) tag = Tag.get_tag("non-existant") assert_equal(nil, tag) tag = Tag.create_tag("ruby") assert_equal("ruby", tag.name) tag = Tag.create_tag("non-existant") assert_equal("non-existant", tag.name) results = Tag.tag_query(:with_all_tags => ["ruby", "cult"]) assert_equal(red_handed, results.first) assert_equal(1, results.size) $foo = true results = Tag.tag_query(:with_all_tags => ["ruby", "framework"]) $foo = false assert_equal(ruby_on_rails, results.first) assert_equal(1, results.size) results = Tag.tag_query(:with_any_tags => ["cult", "framework"]) assert(results.include?(ruby_on_rails)) assert(results.include?(red_handed)) assert_equal(2, results.size) results = Tag.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"]) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) assert_equal(1, results.size) results = Tag.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"], :user_id => some_guy.id) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) assert_equal(1, results.size) results = Tag.tag_query(:with_all_tags => ["spinach"], :with_any_tags => ["ruby", "cult", "rails", "framework"], :without_tags => ["cult"], :user_id => some_other_guy.id) assert_equal(red_handed, results.first) assert(!results.include?(ruby_on_rails)) assert_equal(1, results.size) results = Tag.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"], :user_id => -1) assert_equal(0, results.size) end end