require File.join(File.dirname(__FILE__), '..', 'og', 'CONFIG.rb') require 'rubygems' require 'facets' require 'test/unit' require 'ostruct' require 'og' require 'glue/taggable' class TestOgTaggable < Test::Unit::TestCase # :nodoc: all class Article attr_accessor :title, :body, String is Taggable def initialize(title = nil) @title = title end end class Second attr_accessor :haha, String is Taggable end $og1.manage_classes(Article, Tag, Second) def setup @a1 = Article.create('Hello') @a1.tag('great gmosx sexy') @a1.save @a2 = Article.create('Other') @a2.tag('gmosx rain') @a2.save @a3 = Article.create('George') @a3.tag('phd name') @a3.save @s1 = Second.create @s1.tag('gmosx') @s1.save end def teardown [Tag, Article, Second].each {|x| x.delete_all } end def test_all a1, a2, a3 = @a1, @a2, @a3 assert_equal 6, Tag.count assert_equal 3, a1.tags.size assert a1.tag_names.include?('great') assert a1.tagged_with?('great') assert_equal false, a1.tagged_with?('photo') res = Article.find_with_tags('great', 'gmosx') assert_equal 1, res.size assert_equal 'Hello', res[0].title res = Article.find_with_tag('gmosx') assert_equal 2, res.size res = res.map { |o| o.title } assert res.include?('Hello') assert res.include?('Other') res = Article.find_with_any_tag('great', 'gmosx', 'phd') assert_equal 3, res.size a1.delete_all_tags assert_equal 0, a1.tags.size assert_equal 4, Tag.count Tag.all.each do |tag| n = case tag.name when 'rain', 'phd', 'name': 1 when 'gmosx': 1 else flunk tag.name + ' not expected' end assert n, tag.count end a = Second.create b = Second.create b.tag('hello world heh') a.tag('hello heh george gmosx') assert_equal 2, Second.find_with_tags('hello', 'heh').size =begin TODO: Article.fing_with_no_tag('gmosx') Article.find_by_tags('+name +gmosx -sexy') Article.find_by_tags(:with => '', :any => '', :no => '') =end end def test_tag_tagged t = Tag.find_by_name('gmosx') assert_not_nil t assert_equal 'gmosx', t.name a = Article.relations.reject {|x| x.name != :tags }.first assert_equal Article, a.owner_class assert_equal [@a1, @a2, @s1], t.tagged end end