require File.dirname(__FILE__) + '/test_helper' class TC_NERA_JOB_RECS < Test::Unit::TestCase def setup @testdir = 'test_job_recs' FileUtils.mkdir(@testdir) FileUtils.chdir(@testdir) @path_to_db = 'job_recs_table.marshal' NERA::JobRecords.create_table( @path_to_db) @db = NERA::JobRecords.new( @path_to_db) end def teardown FileUtils.chdir('..') FileUtils.rm_r(@testdir) end def test_constructor assert_raise( RuntimeError) { NERA::JobRecords.new('unknown_file') } end def test_create_table f = NERA::JobRecords.create_table( @path_to_db) assert_nil( f) end def test_keys k = [ :id,:state,:simulator,:parameter_id,:run_id, :number_of_runs,:created_at,:updated_at,:host_name ] assert_equal( k , @db.keys) end def test_add assert_raise( ArgumentError) { @db.add( 2,3,5,3) } assert_raise( ArgumentError) { @db.add( "hoge",5,3,3) } assert_raise( ArgumentError) { @db.add( 8,5,3,-1) } given_id = @db.add( Ising,5,3,4 ) d = DateTime.now l = @db.list_all rec = l[0] assert_equal(1, given_id) assert_equal( 1, rec[:id]) assert_equal( :created, rec[:state]) assert_equal( "Ising", rec[:simulator]) assert_equal( 5, rec[:parameter_id]) assert_equal( 3, rec[:run_id]) assert_equal( 4, rec[:number_of_runs] ) assert_equal( d.year, rec[:updated_at].year ) assert_equal( d.month, rec[:updated_at].month ) assert_equal( d.day, rec[:updated_at].day ) assert_equal( d.hour , rec[:updated_at].hour ) assert_equal( d.min, rec[:updated_at].min ) assert_equal( rec[:created_at], rec[:updated_at] ) assert_nil( rec[:host_name]) sleep 2 given_id = @db.add( XY,4,2,5 ) d = DateTime.now l = @db.list_all rec = l[1] assert_equal( 2, rec[:id]) assert_equal( :created, rec[:state]) assert_equal( "XY", rec[:simulator]) assert_equal( 4, rec[:parameter_id]) assert_equal( 2, rec[:run_id]) assert_equal( 5, rec[:number_of_runs] ) assert_equal( d.year, rec[:updated_at].year ) assert_equal( d.month, rec[:updated_at].month ) assert_equal( d.day, rec[:updated_at].day ) assert_equal( d.hour , rec[:updated_at].hour ) assert_equal( d.min, rec[:updated_at].min ) assert_equal( rec[:created_at], rec[:updated_at] ) assert_nil( rec[:host_name]) assert( l[0][:created_at] < l[1][:created_at]) end def add_three @db.add( Ising,2,3,4) @db.add( XY,6,7,8) @db.add( Heisenberg,10,11,12) end def test_list # ------ l = @db.list_all assert( l.is_a?(Array) ) assert( l.size == 0 ) # ------ add_three l = @db.list_all assert( l.is_a?(Array) ) assert_equal( 3, l.size) l2 = l.dup l2.sort! do |a,b| a[:id] <=> b[:id] end assert_equal( l, l2) lc = @db.list_created assert_equal( l, lc) end def test_find_by_id assert_nil( @db.find_by_id(1) ) add_three r = @db.find_by_id(1) assert( r.is_a?(Hash) ) assert_equal( 1, r[:id]) assert_equal( :created, r[:state]) assert_equal( "Ising", r[:simulator]) assert_equal( 2, r[:parameter_id]) assert_equal( 3, r[:run_id]) assert_equal( 4, r[:number_of_runs] ) assert_equal( r[:created_at], r[:updated_at] ) assert_nil( r[:host_name]) assert_nil( @db.find_by_id(500) ) end def test_update_to_state_copied add_three r = @db.find_by_id(2) assert_equal( :created, r[:state]) assert_nil( r[:host_name]) sleep 2 f = @db.update_to_state_copied( 2, "supacon.com") assert_equal( true, f) r2 = @db.find_by_id(2) assert_equal( :copied, r2[:state]) assert_equal( r[:simulator], r2[:simulator]) assert_equal( r[:parameter_id], r2[:parameter_id]) assert_equal( r[:run_id], r2[:run_id]) assert_equal( r[:number_of_runs], r2[:number_of_runs]) assert_equal( r[:created_at], r2[:created_at]) assert( r[:updated_at] < r2[:updated_at]) assert_equal( "supacon.com", r2[:host_name]) la = @db.list_all lc = @db.list_created diff = la - lc assert_equal( 1, diff.size) assert_equal( r2, diff[0]) f = @db.update_to_state_copied( 2, "another_host") assert_equal( true, f) r2 = @db.find_by_id(2) assert_equal( :copied, r2[:state]) assert_equal( "another_host", r2[:host_name]) assert_nil( @db.update_to_state_copied( 500, "unknown_host") ) assert_raise( ArgumentError) { @db.update_to_state_copied( "hoge",2) } assert_raise( ArgumentError) { @db.update_to_state_copied( 2, 2) } lc2 = @db.list_created assert_equal( lc, lc2) end def test_update_to_state_submitted add_three r = @db.find_by_id(2) assert_equal( :created, r[:state]) assert_nil( r[:host_name]) sleep 2 f = @db.update_to_state_submitted( 2, "supacon.com") assert_equal( true, f) r2 = @db.find_by_id(2) assert_equal( :submitted, r2[:state]) assert_equal( r[:simulator], r2[:simulator]) assert_equal( r[:parameter_id], r2[:parameter_id]) assert_equal( r[:run_id], r2[:run_id]) assert_equal( r[:number_of_runs], r2[:number_of_runs]) assert_equal( r[:created_at], r2[:created_at]) assert( r[:updated_at] < r2[:updated_at]) assert_equal( "supacon.com", r2[:host_name]) la = @db.list_all lc = @db.list_created diff = la - lc assert_equal( 1, diff.size) assert_equal( r2, diff[0]) f = @db.update_to_state_copied( 1, "another_host") assert_equal( true, f) f = @db.update_to_state_submitted( 1, "supacon.com") r1 = @db.find_by_id(1) assert_equal( :submitted, r1[:state]) assert_equal( "supacon.com", r1[:host_name]) assert_nil( @db.update_to_state_copied( 500, "unknown_host") ) assert_raise( ArgumentError) { @db.update_to_state_copied( "hoge",2) } assert_raise( ArgumentError) { @db.update_to_state_copied( 2, 2) } la = @db.list_all lc = @db.list_created diff = la - lc assert_equal( 2, diff.size) end def test_destroy add_three r3 = @db.find_by_id(3) destroyed = @db.destroy(3) assert_equal( r3, destroyed) l = @db.list_all assert_equal( 2, l.size) assert_nil( @db.destroy(3) ) @db.add( Ising,14,15,16) l = @db.list_all assert_equal( 3, l.size) r4 = @db.find_by_id(4) assert_equal(4, r4[:id]) assert_nil( @db.destroy(500) ) assert_raise(ArgumentError) { @db.destroy("4") } end def test_transaction add_three r3 = @db.find_by_id(3) assert_raise(RuntimeError) { @db.transaction { destroyed = @db.destroy(3) assert_equal( r3, destroyed) l = @db.list_all assert_equal( 2, l.size) raise } } assert_equal( r3, @db.find_by_id(3) ) assert_equal( 3, @db.list_all.size) end end