Sha256: 8f9b2fe5b4c562deb316af62be1fba36e56a92a0f7f43deead3238af73b79d20

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'CONFIG.rb')

require 'test/unit'
require 'ostruct'

require 'og'
require 'glue/hierarchical'

$og = Og.setup(
  :store => 'mysql',
  :name => 'test',
  :user => 'root',
  :password => 'navelrulez',
  :destroy => true
)

class TC_OgHierarchical < Test::Unit::TestCase # :nodoc: all

  class Comment 
    property :body, String
    property :create_time, Time
    
    include Og::NestedSets

    def initialize(body = nil)
      @body = body
      @create_time = Time.now
    end

    def to_s
      sprintf("%3d %3d %s", @lft, @rgt, @body)
    end
  end

  def test_all
    $og.manage_classes    

    root = Comment.create('root')
    c1 = Comment.new('1')
    root.add_comment c1
    c2 = Comment.new('1.1')
    c1.add_comment c2
    c3 = Comment.new('1.2')
    c1.add_comment c3
    c4 = Comment.new('1.1.1')
    c2.add_comment c4
    c5 = Comment.new('1.2.1')
    c3.add_comment c5
    c6 = Comment.new('1.1.1.1')
    c4.add_comment c6
    c7 = Comment.new('2')
    root.add_comment c7
    c8 = Comment.new('3')
    root.add_comment c8
    c9 = Comment.new('2.1')
    c7.add_comment c9

    c1.reload
    
    Comment.all(:order => "lft, rgt").each { |c|
      puts sprintf("%3d %3d %s", c.lft, c.rgt, c.body)
      # p c  
    }
    puts '--1'
    c1.comments(:order => "lft, rgt").each { |c| puts c.body }
    puts '--2'
    c1.full_comments(:order => "lft, rgt").each { |c| puts c.body }
    puts '--3'
    c1.direct_comments(:order => "lft, rgt").each { |c| puts c.body }

    assert_equal 6, c1.full_comments.size
    assert_equal 5, c1.comments.size
    assert_equal 2, c1.direct_comments.size
    
    c8.reload
    
    assert_equal 'root', c8.parent.body
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
og-0.25.0 test/og/mixin/tc_hierarchical.rb
og-0.26.0 test/og/mixin/tc_hierarchical.rb