require 'pathname' require Pathname.new( File.dirname(__FILE__)).join( 'test_helper' ).cleanpath require 'sequel' require 'sequel/extensions/migration' # Create a memory database database = Sequel.sqlite Sequel::Migrator.apply( database, 'db/migrations' ) require 'build-tool/history' class TestHistory < Test::Unit::TestCase def test_command_log cmd = BuildTool::History::CommandLog.new( :command => "test" ) assert_equal cmd.command, "test" assert_nil cmd.started_at assert_nil cmd.finished_at assert_nil cmd.id assert_nil cmd.state # #started sets the started_at attribute and saves the object. Nothing else. cmd.started assert_not_nil cmd.id assert_equal cmd.command, "test" assert_not_nil cmd.started_at assert_nil cmd.finished_at assert_equal BuildTool::History::CommandLog::STARTED, cmd.state # Check that the started state will lead to an exception on finished assert_raises StandardError do cmd.finished( BuildTool::History::CommandLog::STARTED ) end # #finished sets the finished_at attribute and saves the object. Nothing else. cmd.finished( BuildTool::History::CommandLog::FINISHED_SUCCESSFUL ) assert_not_nil cmd.id assert_equal cmd.command, "test" assert_not_nil cmd.started_at assert_not_nil cmd.finished_at assert cmd.finished_at > cmd.started_at assert_equal BuildTool::History::CommandLog::FINISHED_SUCCESSFUL, cmd.state # Reload the object from db id = cmd.id cmd = nil cmd = BuildTool::History::CommandLog[id] assert_equal id, cmd.id assert_equal cmd.command, "test" assert_not_nil cmd.started_at assert_not_nil cmd.finished_at assert cmd.finished_at > cmd.started_at # Create more entried for i in 1..50 do cmd = BuildTool::History::CommandLog.new( :command => "test #{i}" ) cmd.started cmd.finished( BuildTool::History::CommandLog::FINISHED_SUCCESSFUL ) # Remember the last id maxid = cmd.id end # Get the latest history entry cmd = BuildTool::History::CommandLog::most_recent assert_equal maxid, cmd.id # Get the latest ten history entries arr = BuildTool::History::CommandLog.last( 10 ) assert_equal 10, arr.size assert_equal maxid, arr[0].id lastid = maxid+1 while nxt = arr.shift assert lastid > nxt.id lastid = nxt.id end end # test_command_log def test_module_log # Get a command to work with cmd = BuildTool::History::CommandLog::most_recent # Create a empty one mod = BuildTool::History::ModuleLog.new assert_nil mod.id assert_nil mod.module assert_nil mod.command_log_id assert_nil mod.event assert_nil mod.logfile assert_nil mod.started_at assert_nil mod.finished_at assert_nil mod.state assert_raises Sequel::DatabaseError do mod.save end mod.module = "test" assert_raises Sequel::DatabaseError do mod.save end mod.event = "update" assert_raises Sequel::DatabaseError do mod.save end # SQLite3 does not check foreign keys (just to remember mod.command_log_id = 45000 mod.save mod = BuildTool::History::ModuleLog.new( :command_log_id => cmd.id, :module => "test_module", :event => "update", :logfile => "/path/to/my/logfile") assert_equal mod.command_log_id, cmd.id assert_equal "test_module", mod.module assert_equal "update", mod.event assert_equal "/path/to/my/logfile", mod.logfile assert_nil mod.id # Start the event mod.started assert_not_nil mod.id assert_not_nil mod.started_at assert_equal mod.state, BuildTool::History::ModuleLog::STARTED # Finish the event assert_raises StandardError do mod.finished( BuildTool::History::ModuleLog::STARTED ) end mod.finished( BuildTool::History::ModuleLog::FINISHED_SUCCESSFUL ) assert_not_nil mod.finished_at assert_equal mod.state, BuildTool::History::ModuleLog::FINISHED_SUCCESSFUL cmds = BuildTool::History::CommandLog.last_by_module( "test_module" ) puts cmds.inspect assert_equal 1, cmds.count end end # class TestHistory