require File.join(File.dirname(__FILE__), 'CONFIG.rb') # $DBG = true require 'rubygems' require 'facets' require 'test/unit' require 'og' class TC_Join < Test::Unit::TestCase # :nodoc: all include Og class Category property :title, String joins_many Article joins_many :third_join, Article, :table => :ogj_article_category_third joins_many :fourth_join, Article, :table => :ogj_article_category_fourth def initialize(title) @title = title end end class Article property :title, String joins_many :first_join, Category, :through => ArticleToCategory joins_many :second_join, Category, :through => ArticleToCategory joins_many :third_join, Category, :table => :ogj_article_category_third joins_many :fourth_join, Category, :table => :ogj_article_category_fourth joins_many Category def initialize(title) @title = title end end class ArticleToCategory property :rate, Float property :hits, Fixnum has_one Article has_one Category end $og1.manage_classes(Category, Article, ArticleToCategory) def setup end def test_all c1 = Category.create('tech') c2 = Category.create('funny') c3 = Category.create('finance') a = Article.create('a1') a2 = Article.create('a2') a3 = Article.create('a2') # Put the categories into seperate relations a.first_join.push(c1, :hits =>3, :rate => 2.3) a.second_join.push(c2, :rate => 1.2) a.third_join << c1 a.fourth_join << c2 a2.third_join << c1 a3.fourth_join << c1 a.categories << c3 # ++ Join Through Tests # Test that each relationship appears where it should join = a.first_join_join_data(c1) assert_equal 2.3, join.rate assert_equal 3, join.hits join = a.second_join_join_data(c2) assert_equal 1.2, join.rate assert_equal nil, join.hits # This feature should be available but I cannot think # of the best way to implement it right now. # Test that each relationship does not appears where # it should not. # join = a.second_join_join_data(c1) # assert_nil(join) # join = a.first_join_join_data(c2) # assert_nil(join) # join = a.first_join_join_data(c3) # assert_nil(join) # -- Join Through Tests # ++ Tripple join table tests # Test each relationship appears where it should assert(a.third_join.include?(c1), "c1 does not appear in third join relationship") assert(a.fourth_join.include?(c2), "c2 does not appear in fourth join relationship") assert(a.categories.include?(c3), "c3 does not appear in categories (un-named) join relationship") # Tests the same thing backwards assert(c3.articles.include?(a), "article does not appear in c3 (reverse join broken)") assert(c1.third_join.include?(a2), "a2 does not appear in c1.third_join (reverse join broken)") assert(c1.fourth_join.include?(a3), "a3 does not appear in c1.fourth_join (reverse join broken)") assert(!c1.third_join.include?(a3), "a3 appears in c1.third_join (reverse join broken)") # Test that each relationship does not appears where # it should not. assert(!a.third_join.include?(c2), "c2 appears in third join relationship") assert(!a.third_join.include?(c3), "c3 appears in third join relationship") assert(!a.fourth_join.include?(c1), "c1 appears in fourth join relationship") assert(!a.fourth_join.include?(c3), "c3 appears in fourth join relationship") assert(!a.categories.include?(c1), "c1 appears in categories (un-named) join relationship") assert(!a.categories.include?(c2), "c2 appears in categories (un-named) join relationship") # -- Tripple join table tests end end