From 53d13b20f383751e4cc0c1a254b3755a21f0ecf5 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 2 Feb 2015 14:44:37 +0100 Subject: [PATCH] Added unit tests for "id in seeds.rb failed" issue. --- db/seeds.rb | 14 ++++---- test/unit/db_auto_increment_test.rb | 56 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 test/unit/db_auto_increment_test.rb diff --git a/db/seeds.rb b/db/seeds.rb index 8ceb17f73..7f8e21d1f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1335,13 +1335,13 @@ Link::Object.create_if_not_exists( :name => 'Question/Answer' ) Link::Object.create_if_not_exists( :name => 'Idea' ) Link::Object.create_if_not_exists( :name => 'Bug' ) -Ticket::StateType.create_if_not_exists( :id => 1, :name => 'new', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 2, :name => 'open', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 3, :name => 'pending reminder', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 4, :name => 'pending action', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 5, :name => 'closed', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 6, :name => 'merged', :updated_by_id => 1 ) -Ticket::StateType.create_if_not_exists( :id => 7, :name => 'removed', :updated_by_id => 1 ) +Ticket::StateType.create_if_not_exists( :id => 1, :name => 'new' ) +Ticket::StateType.create_if_not_exists( :id => 2, :name => 'open' ) +Ticket::StateType.create_if_not_exists( :id => 3, :name => 'pending reminder' ) +Ticket::StateType.create_if_not_exists( :id => 4, :name => 'pending action' ) +Ticket::StateType.create_if_not_exists( :id => 5, :name => 'closed' ) +Ticket::StateType.create_if_not_exists( :id => 6, :name => 'merged' ) +Ticket::StateType.create_if_not_exists( :id => 7, :name => 'removed' ) Ticket::State.create_if_not_exists( :id => 1, :name => 'new', :state_type_id => Ticket::StateType.where(:name => 'new').first.id ) Ticket::State.create_if_not_exists( :id => 2, :name => 'open', :state_type_id => Ticket::StateType.where(:name => 'open').first.id ) diff --git a/test/unit/db_auto_increment_test.rb b/test/unit/db_auto_increment_test.rb new file mode 100644 index 000000000..a83a1a6c3 --- /dev/null +++ b/test/unit/db_auto_increment_test.rb @@ -0,0 +1,56 @@ +# encoding: utf-8 +require 'test_helper' + +class DbAutoIncrementTest < ActiveSupport::TestCase + + test 'id overwrite' do + + setting_backup = Setting.get('system_init_done') + + Setting.set('system_init_done', false) + + Ticket::StateType.create_if_not_exists( :id => 200, :name => 'unit test 1', :updated_by_id => 1, :created_by_id => 1 ) + state_type = Ticket::StateType.where( :name => 'unit test 1' ).first + assert_equal( Ticket::StateType.to_s, state_type.class.to_s ) + assert_equal( 'unit test 1', state_type.name ) + + Ticket::StateType.create_if_not_exists( :id => 200, :name => 'unit test 1 _ should not be created', :updated_by_id => 1, :created_by_id => 1 ) + state_type = Ticket::StateType.where( :id => 200 ).first + assert_equal( Ticket::StateType.to_s, state_type.class.to_s ) + assert_equal( 'unit test 1', state_type.name ) + + Ticket::StateType.create_or_update( :id => 200, :name => 'unit test 1 _ should be updated', :updated_by_id => 1, :created_by_id => 1 ) + state_type = Ticket::StateType.where( :name => 'unit test 1 _ should be updated' ).first + assert_equal( Ticket::StateType.to_s, state_type.class.to_s ) + assert_equal( 'unit test 1 _ should be updated', state_type.name ) + + state_type = Ticket::StateType.where( :id => 200 ).first + assert_equal( Ticket::StateType.to_s, state_type.class.to_s ) + assert_equal( 'unit test 1 _ should be updated', state_type.name ) + + + Ticket::State.create_if_not_exists( :id => 210, :name => 'unit test 1', :state_type_id => Ticket::StateType.where(:name => 'unit test 1 _ should be updated').first.id, :updated_by_id => 1, :created_by_id => 1 ) + state = Ticket::State.where( :name => 'unit test 1' ).first + assert_equal( Ticket::State.to_s, state.class.to_s ) + assert_equal( 'unit test 1', state.name ) + + Ticket::State.create_if_not_exists( :id => 210, :name => 'unit test 1 _ should not be created', :state_type_id => Ticket::StateType.where(:name => 'unit test 1 _ should be updated').first.id, :updated_by_id => 1, :created_by_id => 1 ) + state = Ticket::State.where( :id => 210 ).first + assert_equal( Ticket::State.to_s, state.class.to_s ) + assert_equal( 'unit test 1', state.name ) + + Ticket::State.create_or_update( :id => 210, :name => 'unit test 1 _ should be updated', :state_type_id => Ticket::StateType.where(:name => 'unit test 1 _ should be updated').first.id, :updated_by_id => 1, :created_by_id => 1 ) + state = Ticket::State.where( :name => 'unit test 1 _ should be updated' ).first + assert_equal( Ticket::State.to_s, state.class.to_s ) + assert_equal( 'unit test 1 _ should be updated', state.name ) + + state = Ticket::State.where( :id => 210 ).first + assert_equal( Ticket::State.to_s, state.class.to_s ) + assert_equal( 'unit test 1 _ should be updated', state.name ) + + + + Setting.set('system_init_done', setting_backup) + + end +end \ No newline at end of file