require "test/unit" require "glue/logger"; $log = Logger.new(STDERR) unless $log require "og" module Test # :nodoc: all # bug: creates a table that fucks-up postgres if not # correctly escaped. class User prop_accessor :name, String def initialize(name = nil) @name = name end end class Comment; end class Article prop_accessor :title, String prop_accessor :body, String prop_accessor :owner_oid, Fixnum, :sql_index => true prop_accessor :another_oid, Fixnum sql_index 'owner_oid, another_oid' prop_accessor :options, Hash has_many :comments, Test::Comment def initialize(title = nil, body = nil) @title = title @body = body @options = {"hello" => "world"} end def og_pre_insert(conn) puts "-- PRE INSERT CALLED FOR ARTICLE" end def og_post_insert_update(conn) puts "-- POST INSERT UPDATE CALLED FOR ARTICLE" end def og_post_update(conn) puts "-- POST UPDATE CALLED FOR ARTICLE" end end class Comment belongs_to :article, Test::Article belongs_to :author, Test::User prop_accessor :body, String prop_accessor :create_time, Time def initialize(body = nil) @create_time = Time.now @body = body end def og_pre_insert(conn) puts "-- PRE INSERT CALLED FOR COMMENT" end def og_post_insert(conn) puts "-- POST INSERT CALLED FOR COMMENT" end end class TC_N_OG < Test::Unit::TestCase def setup psql = true if psql config = { :backend => "psql", :address => "localhost", :database => "test", :user => "postgres", :password => "navelrulez", :connection_count => 1 } else config = { :backend => "mysql", :address => "localhost", :database => "test", :user => "root", :password => "navelrulez", :connection_count => 1 } end Og::Database.drop_db!(config) $og = Og::Database.new(config) end def teardown $og.shutdown() end # gmosx: hmm, implemented in one method to enforce order. # def test_all $DBG = true $og.get_connection() article = Article.new("Title", "Here comes the body") $og << article article.title = "Changed" article.save! $og.pupdate("body='Hello'", article) article.update_properties "body='Hello'" another = Article[1] assert_equal(1, another.oid) # bug: yaml load problem. assert_equal("world", another.options["hello"]) # bug: YAMLing nil property another.options = nil another.save! assert_equal(nil, $og.load(30000, Article)) articles = $og.load_all(Article) # p articles # p Article[23] user = User.new("gmosx") user.save! user = User["gmosx"] assert_equal("gmosx", user.name) users1 = $og.select("name='gmosx' ORDER BY oid", User) users = $og.select("SELECT * FROM #{User::DBTABLE} WHERE name='gmosx' ORDER BY oid", User) users2 = User.select "name='gmosx' ORDER BY oid" assert_equal(users1.size, users2.size) article = Article.new("Title", "Body") article.save! comment = Comment.new("This is a comment") comment.article = article comment.author = User["gmosx"] comment.save! comment = Comment.new("This is another comment") comment.article = article.oid comment.author = User["gmosx"] comment.save! assert_equal(article.oid, comment.article_oid) comments = article.comments assert_equal(2, comments.size) assert_equal("gmosx", comments[0].author.name) Article.delete(article) assert_equal(nil, Comment.all) comment.delete! end end end