require File.dirname(__FILE__) + '/test_helper' class TC_NERA_PARAM_RECS < Test::Unit::TestCase def setup @testdir = 'test_sim_recs' FileUtils.mkdir(@testdir) FileUtils.chdir(@testdir) @path_to_db = 'sim_recs_table.marshal' NERA::ParameterRecords.create_table( @path_to_db, Ising) @db = NERA::ParameterRecords.new( @path_to_db, Ising) end def teardown FileUtils.chdir('..') FileUtils.rm_r( @testdir) end def test_initialize assert_raise( RuntimeError) { NERA::ParameterRecords.new('unknown_file', Ising) } assert_raise( ArgumentError) { NERA::ParameterRecords.new( @path_to_db, XY) } assert_raise( ArgumentError) { NERA::ParameterRecords.new(@path_to_db, Temp) } end def test_create_table f = NERA::ParameterRecords.create_table( @path_to_db, Ising) assert_nil( f) assert_raise( ArgumentError) { NERA::ParameterRecords.create_table( @path_to_db, XY) } assert_raise( ArgumentError) { NERA::ParameterRecords.create_table( @path_to_db, Temp) } end def test_keys assert_equal( [:id,:created_at,:updated_at,:in_trashbox?,:L,:K,:tmax], @db.keys) end def test_add param1 = { :L => 32, :K => 0.22, :tmax => 512 } f = @db.add( param1) d = DateTime.now assert_equal( 1, f) l_all = @db.list_all r1 = l_all[0] assert_equal( 1, l_all.size) assert_equal( 1, r1[:id] ) assert_equal( d.year, r1[:created_at].year) assert_equal( d.month, r1[:created_at].month) assert_equal( d.day, r1[:created_at].day) assert_equal( d.hour, r1[:created_at].hour) assert_equal( d.min, r1[:created_at].min) assert_equal( r1[:created_at], r1[:updated_at]) assert_equal( :active, r1[:in_trashbox?] ) assert_equal( param1[:L], r1[:L] ) assert_equal( param1[:K], r1[:K] ) assert_equal( param1[:tmax], r1[:tmax] ) param2 = { :L => 96, :K => 0.32, :tmax => 512 } f = @db.add( param2) assert_equal( 2, f) l_all = @db.list_all assert_equal( 2, l_all.size) r2 = l_all[1] assert_equal( 2, r2[:id] ) assert_equal( d.year, r2[:created_at].year) assert_equal( d.month, r2[:created_at].month) assert_equal( d.day, r2[:created_at].day) assert_equal( d.hour, r2[:created_at].hour) assert_equal( d.min, r2[:created_at].min) assert_equal( r2[:created_at], r2[:updated_at]) assert_equal( :active, r2[:in_trashbox?] ) assert_equal( param2[:L], r2[:L] ) assert_equal( param2[:K], r2[:K] ) assert_equal( param2[:tmax], r2[:tmax] ) param3 = { :L => 32, :K => 0.22, :tmax => 512 } f = @db.add( param3) assert_nil( f) l_all2 = @db.list_all assert_equal( 2, l_all2.size) assert_equal( l_all, l_all2) param4 = { :L => 128, :tmax => 512 } f = @db.add( param4) assert_equal( 3, f) l_all = @db.list_all assert_equal( 3, l_all.size) r2 = l_all[2] assert_equal( 3, r2[:id] ) assert_equal( d.year, r2[:created_at].year) assert_equal( d.month, r2[:created_at].month) assert_equal( d.day, r2[:created_at].day) assert_equal( d.hour, r2[:created_at].hour) assert_equal( d.min, r2[:created_at].min) assert_equal( r2[:created_at], r2[:updated_at]) assert_equal( :active, r2[:in_trashbox?] ) assert_equal( param4[:L], r2[:L] ) assert_nil( r2[:K] ) assert_equal( param4[:tmax], r2[:tmax] ) param5 = [32,0.22,64] assert_raise( ArgumentError) { f = @db.add( param5) } param6 = { :Lx => 32, :K => 0.22, :tmax => 512 } assert_raise( ArgumentError) { f = @db.add( param6) } end def add_three p1 = { :L => 32, :K => 0.22, :tmax => 512 } p2 = { :L => 64, :K => 0.22, :tmax => 512 } p3 = { :L => 32, :K => 0.32, :tmax => 128 } @db.add(p1) @db.add(p2) @db.add(p3) end def test_list assert_equal( 0, @db.list_all.size) assert_equal( 0, @db.list_active.size) assert_equal( 0, @db.list_trashbox.size) add_three l_all = @db.list_all assert_equal( 3, l_all.size) assert( l_all.is_a?(Array) ) l_active = @db.list_active assert_equal( 3, l_active.size) assert( l_active.is_a?(Array) ) assert_equal( l_all, l_active) l_trashbox = @db.list_trashbox assert( l_trashbox.is_a?(Array) ) assert_equal( 0, l_trashbox.size) end def test_find add_three l = @db.list_all r1 = @db.find_by_id(1) assert_equal( l[0], r1) assert_equal( 1, r1[:id]) r3 = @db.find_by_id(3) assert_equal( l[2], r3) assert_equal( 3, r3[:id]) r23 = @db.find_by_id(2..5) assert( r23.is_a?(Array) ) assert_equal( 2, r23.size) assert_equal( 2, r23[0][:id]) assert_equal( 3, r23[1][:id]) r254 = @db.find_by_id( [2,5,4]) assert( r254.is_a?(Array) ) assert_equal( 3, r254.size) assert_equal( 2, r254[0][:id]) assert_nil( r254[1]) assert_nil( r254[2]) assert_nil( @db.find_by_id(500) ) assert_nil( @db.find_by_id( 100..500) ) assert_raise( ArgumentError) { @db.find_by_id("3") } end def test_update_state add_three r1 = @db.find_by_id(1) r1[:in_trashbox?] = :trashbox f = @db.update_to_state_trashbox(1) updated = @db.find_by_id(1) assert_equal( true, f) assert_equal( r1, updated) assert_equal( :trashbox, updated[:in_trashbox?] ) assert_equal( 3, @db.list_all.size ) assert_equal( 2, @db.list_active.size ) assert_equal( 1, @db.list_trashbox.size ) f = @db.update_to_state_trashbox(3) assert_equal( true, f) assert_equal( :trashbox, @db.find_by_id(3)[:in_trashbox?] ) assert_equal( 3, @db.list_all.size ) assert_equal( 1, @db.list_active.size ) assert_equal( 2, @db.list_trashbox.size ) f = @db.update_to_state_trashbox(3) assert_nil( f) assert_equal( 3, @db.list_all.size ) assert_equal( 1, @db.list_active.size ) assert_equal( 2, @db.list_trashbox.size ) f = @db.update_to_state_trashbox(500) assert_nil( f) f = @db.update_to_state_active(1) updated2 = @db.find_by_id(1) updated[:in_trashbox?] = :active updated[:updated_at] = updated2[:updated_at] assert_equal( updated, updated2) assert_equal( :active, updated2[:in_trashbox?] ) assert_equal( 3, @db.list_all.size ) assert_equal( 2, @db.list_active.size ) assert_equal( 1, @db.list_trashbox.size ) f = @db.update_to_state_active(1) assert_nil( f) assert_raise( ArgumentError) { @db.update_to_state_active( "3") } assert_raise( ArgumentError) { @db.update_to_state_trashbox( [3,4]) } end def test_touch add_three a = @db.find_by_id(1) sleep 3 f = @db.touch( 1) assert_equal(true, f) b = @db.find_by_id(1) assert( a[:updated_at] < b[:updated_at] ) b[:updated_at] = a[:updated_at] assert_equal( a, b) f = @db.touch(500) assert_nil( f) end def test_destroy add_three @db.update_to_state_trashbox(3) r3 = @db.find_by_id(3) f = @db.destroy(3) assert_equal( r3, f) assert_equal( 2, @db.list_all.size ) assert_equal( 2, @db.list_active.size ) assert_equal( 0, @db.list_trashbox.size ) f = @db.destroy(3) assert_nil(f) f = @db.destroy(1) assert_nil(f) end def test_transaction add_three assert_raise(RuntimeError) { @db.transaction { @db.update_to_state_trashbox(3) raise } } assert_equal( 3, @db.list_active.size) assert_raise(RuntimeError) { @db.transaction { @db.update_to_state_trashbox(1) @db.destroy(1) assert_equal(2,@db.list_all.size) raise } } assert_equal( 3, @db.list_all.size) end end