Sha256: 676c64ce8b85d0bbddcf8a6ebb6859108d9d7c0f19f9c50b9ff6ff7cb2238de7

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

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

require 'rubygems'
require 'facets'
require 'test/unit'
require 'og'

# 'testreverse' is a legacy database schema. Og's advanced
# reverse engineering features allows us to map any schema
# to our objects.


class TestReverse < Test::Unit::TestCase # :nodoc: all
  include Og
  
  class User
    property :name, String, :field => :thename, :uniq => true
    property :password, String
    property :age, Fixnum, :field => :age3    
    has_many Comment, :foreign_field => :usr
    set_table :my_usrs
    set_primary_key :name, String
    
    def initialize(name, password, age)
      @name, @password, @age = name, password, age
    end
  end

  class Comment
    @@pk = $og1.store.primary_key_type
    
    property :cid, Fixnum, :sql => @@pk
    property :body, String
    belongs_to User, :field => :usr
    set_table :my_comments
    set_primary_key :cid
    
    def initialize(body)
      @body = body
    end
  end

  def setup
    # create a 'legacy' schema.
    store = $og1.store
    pk = store.primary_key_type
    store.exec "create table my_usrs (thename VARCHAR(32) PRIMARY KEY, password TEXT, age3 INTEGER)"
    store.exec "create table my_comments (cid #{pk}, body TEXT, usr VARCHAR(32))"
    
    $og1.manage_classes(User, Comment)
  end

  def test_all
    assert_equal 'my_usrs', User.table
    assert_equal 'my_comments', Comment.table
    
    User.new('gmosx', 'nitro', 30).insert
    User.new('Helen', 'kontesa', 25).insert
    
    gmosx = User.find_by_name('gmosx')
    assert_equal 'gmosx', gmosx.name
    
    helen = User.find_all_by_age(25).first
    assert_equal 'Helen', helen.name
    
    c = Comment.new('hello')
    c.insert
    helen.comments << c
  
    assert_equal 1, helen.comments(:reload => true).size
    assert_equal 'hello', helen.comments[0].body
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
og-0.41.0 test/og/tc_reverse.rb
og-0.40.0 test/og/tc_reverse.rb