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 Tag < ActiveRecord::Base extend TaggingHelpers end class Image < ActiveRecord::Base acts_as_taggable(:scope => :global, :tag_class_name => "Tag") end class GlobalTagsTest < Test::Unit::TestCase def setup ActiveRecord::Base.connection.execute("DELETE FROM images_tags") ActiveRecord::Base.connection.execute("DELETE FROM images") ActiveRecord::Base.connection.execute("DELETE FROM tags") end def teardown ActiveRecord::Base.connection.execute("DELETE FROM images_tags") ActiveRecord::Base.connection.execute("DELETE FROM images") ActiveRecord::Base.connection.execute("DELETE FROM tags") end def test_add_tags_existing_record ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" ruby_on_rails.save ruby_on_rails.tags.concat(["ruby", "rails", "framework"]) assert_equal(Tag.find_by_name("framework"), ruby_on_rails.tags(true)[0]) assert_equal(Tag.find_by_name("rails"), ruby_on_rails.tags(true)[1]) assert_equal(Tag.find_by_name("ruby"), ruby_on_rails.tags(true)[2]) end def test_add_tags_new_record some_guy = User.new some_guy.name = "Joe Normal" some_guy.save ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" assert_equal(true, ruby_on_rails.new_record?) ruby_on_rails.tags.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.tags(true)[0]) assert_equal(Tag.find_by_name("rails"), ruby_on_rails.tags(true)[1]) assert_equal(Tag.find_by_name("ruby"), ruby_on_rails.tags(true)[2]) end def test_tag_query ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" ruby_on_rails.save ruby_on_rails.tags << "ruby" ruby_on_rails.tags << "rails" ruby_on_rails.tags << "framework" red_handed = Image.new red_handed.url = "http://redhanded.hobix.com/logo.gif" red_handed.save red_handed.tags << "ruby" red_handed.tags << "cult" results = Image.tag_query(:with_all_tags => ["ruby", "cult"]) assert_equal(red_handed, results.first) results = Image.tag_query(:with_all_tags => ["ruby", "framework"]) assert_equal(ruby_on_rails, results.first) results = Image.tag_query(:with_any_tags => ["cult", "framework"]) assert(results.include?(ruby_on_rails)) assert(results.include?(red_handed)) results = Image.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"]) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) end def test_include ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" ruby_on_rails.save ruby_on_rails.tags << "ruby" ruby_on_rails.tags << "rails" ruby_on_rails.tags << "framework" assert_equal(true, ruby_on_rails.tags(true).include?("rails")) assert_equal(false, ruby_on_rails.tags(true).include?("notpresent")) assert_equal(true, ruby_on_rails.tags(true).include?( Tag.find_by_name("framework"))) assert_equal(false, ruby_on_rails.tags(true).include?( Tag.find_by_name("alsonotpresent"))) end def test_size ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" ruby_on_rails.save ruby_on_rails.tags << "ruby" ruby_on_rails.tags << "rails" ruby_on_rails.tags << "framework" assert_equal(3, ruby_on_rails.tags(true).size) ruby_on_rails.tags(true) << "rails" assert_equal(3, ruby_on_rails.tags(true).size) ruby_on_rails.tags << "web" assert_equal(4, ruby_on_rails.tags(true).size) ruby_on_rails.tags.delete("framework") assert_equal(3, ruby_on_rails.tags.size) ruby_on_rails.tags.delete("ruby") assert_equal(2, ruby_on_rails.tags(true).size) end def test_tag_helpers ruby_on_rails = Image.new ruby_on_rails.url = "http://www.rubyonrails.com/logo.gif" ruby_on_rails.save ruby_on_rails.tags << "ruby" ruby_on_rails.tags << "rails" ruby_on_rails.tags << "framework" red_handed = Image.new red_handed.url = "http://redhanded.hobix.com/logo.gif" red_handed.save red_handed.tags << "ruby" red_handed.tags << "cult" 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) results = Tag.tag_query(:with_all_tags => ["ruby", "framework"]) assert_equal(ruby_on_rails, results.first) results = Tag.tag_query(:with_any_tags => ["cult", "framework"]) assert(results.include?(ruby_on_rails)) assert(results.include?(red_handed)) results = Tag.tag_query(:with_all_tags => ["ruby", "framework"], :without_tags => ["cult"]) assert_equal(ruby_on_rails, results.first) assert(!results.include?(red_handed)) end end