require File.dirname(__FILE__) + '/test_helper' class TC_NERA_SIM_RECS < Test::Unit::TestCase def setup @testdir = 'test_sim_recs' FileUtils.mkdir(@testdir) FileUtils.chdir(@testdir) @path_to_db = 'sim_recs_table.marshal' NERA::SimulatorRecords.create_table( @path_to_db) @db = NERA::SimulatorRecords.new( @path_to_db) end def teardown FileUtils.chdir('..') FileUtils.rm_r(@testdir) end def test_constructor assert_raise( RuntimeError) { NERA::SimulatorRecords.new('unknown_file') } end def test_set_yaml_file @db.set_yaml_file( 'simulators.yml') assert( ! File.exist?( 'simulators.yml') ) @db.add(Ising) assert( File.exist?( 'simulators.yml') ) loaded = YAML.load( File.open('simulators.yml','r') ) sleep 2 @db.touch(1) loaded2 = YAML.load( File.open('simulators.yml','r') ) assert( loaded[0][:updated_at] < loaded2[0][:updated_at] ) end def test_create_table f = NERA::SimulatorRecords.create_table( @path_to_db) assert_equal( nil, f) end def test_keys assert_equal( [:id,:name,:created_at,:updated_at], @db.keys) end def test_add assert_raise( ArgumentError) { @db.add( Temp) } given_id = @db.add( Ising ) l = @db.list assert_equal(1, given_id) assert_equal( 1, l[0][:id]) assert_equal( "Ising", l[0][:name]) assert_equal( l[0][:updated_at], l[0][:created_at]) sleep 2 given_id = @db.add( XY ) l = @db.list assert_equal(2, given_id) assert_equal( 2, l[1][:id]) assert_equal( "XY", l[1][:name]) assert_equal( l[1][:updated_at], l[1][:created_at]) assert( l[0][:updated_at] < l[1][:updated_at]) flag = @db.add( XY ) assert_equal( nil, flag) end def add_three @db.add( Ising ) @db.add( XY ) @db.add( Heisenberg ) end def test_get_class add_three klass = @db.get_class(1) assert_equal( Ising, klass) klass = @db.get_class(3) assert_equal( Heisenberg, klass) klass = @db.get_class(5) assert_nil(klass) assert_raise( ArgumentError) { @db.get_class('2') } end def test_list # ------ l = @db.list assert( l.is_a?(Array) ) assert( l.size == 0 ) # ------ add_three l = @db.list assert_equal( 3, l.size) l2 = l.dup l2.sort! do |a,b| a[:id] <=> b[:id] end assert_equal( l, l2) end def test_touch add_three sleep 3 flag = @db.touch( 3) assert_equal(true, flag) r3 = @db.list[2] assert( r3[:created_at] < r3[:updated_at]) r2 = @db.list[1] assert( r2[:created_at] == r2[:updated_at]) # ------ flag = @db.touch( 500) assert_nil( flag) end def test_update add_three sleep 3 flag = @db.update( 3) assert_equal(true, flag) r3 = @db.list[2] assert( r3[:created_at] < r3[:updated_at]) r2 = @db.list[1] assert( r2[:created_at] == r2[:updated_at]) # ------ flag = @db.update( 500) assert_nil( flag) end def test_destroy add_three r3 = @db.list[2] flag = @db.destroy(3) assert_equal(r3, flag) l = @db.list assert_equal( 2, l.size) assert_nil( l.find do |r| r[:id]==3 end ) flag = @db.destroy(500) assert_nil(flag) end def test_transaction add_three assert_raise(RuntimeError) { @db.transaction { @db.destroy(2) raise } } l = @db.list assert_equal( 3, l.size) end end