diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-03-10 15:09:49 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-03-10 15:09:49 +0000 |
commit | c514316a2efcf4e8df4bc2f2681548ebffa30adf (patch) | |
tree | b9b7bc9ccbbbad1cce2701430788f4dfbf872610 /vendor/plugins/acts_as_versioned/test | |
parent | 8b98ceb92c8fba72315d28c3b7664f481547bf24 (diff) | |
download | redmine-c514316a2efcf4e8df4bc2f2681548ebffa30adf.tar.gz redmine-c514316a2efcf4e8df4bc2f2681548ebffa30adf.zip |
wiki branch merged into trunk
git-svn-id: http://redmine.rubyforge.org/svn/trunk@323 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'vendor/plugins/acts_as_versioned/test')
16 files changed, 615 insertions, 0 deletions
diff --git a/vendor/plugins/acts_as_versioned/test/abstract_unit.rb b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb new file mode 100644 index 000000000..1740db8dc --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb @@ -0,0 +1,40 @@ +$:.unshift(File.dirname(__FILE__) + '/../lib') + +require 'test/unit' +require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb')) +require 'active_record/fixtures' + +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") +ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite']) + +load(File.dirname(__FILE__) + "/schema.rb") + +# set up custom sequence on widget_versions for DBs that support sequences +if ENV['DB'] == 'postgresql' + ActiveRecord::Base.connection.execute "DROP SEQUENCE widgets_seq;" rescue nil + ActiveRecord::Base.connection.remove_column :widget_versions, :id + ActiveRecord::Base.connection.execute "CREATE SEQUENCE widgets_seq START 101;" + ActiveRecord::Base.connection.execute "ALTER TABLE widget_versions ADD COLUMN id INTEGER PRIMARY KEY DEFAULT nextval('widgets_seq');" +end + +Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/" +$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path) + +class Test::Unit::TestCase #:nodoc: + def create_fixtures(*table_names) + if block_given? + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield } + else + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) + end + end + + # Turn off transactional fixtures if you're working with MyISAM tables in MySQL + self.use_transactional_fixtures = true + + # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david) + self.use_instantiated_fixtures = false + + # Add more helper methods to be used by all tests here... +end
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/database.yml b/vendor/plugins/acts_as_versioned/test/database.yml new file mode 100644 index 000000000..506e6bd37 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/database.yml @@ -0,0 +1,18 @@ +sqlite: + :adapter: sqlite + :dbfile: acts_as_versioned_plugin.sqlite.db +sqlite3: + :adapter: sqlite3 + :dbfile: acts_as_versioned_plugin.sqlite3.db +postgresql: + :adapter: postgresql + :username: postgres + :password: postgres + :database: acts_as_versioned_plugin_test + :min_messages: ERROR +mysql: + :adapter: mysql + :host: localhost + :username: rails + :password: + :database: acts_as_versioned_plugin_test
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/authors.yml b/vendor/plugins/acts_as_versioned/test/fixtures/authors.yml new file mode 100644 index 000000000..bd7a5aed6 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/authors.yml @@ -0,0 +1,6 @@ +caged: + id: 1 + name: caged +mly: + id: 2 + name: mly
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/landmark.rb b/vendor/plugins/acts_as_versioned/test/fixtures/landmark.rb new file mode 100644 index 000000000..cb9b93057 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/landmark.rb @@ -0,0 +1,3 @@ +class Landmark < ActiveRecord::Base + acts_as_versioned :if_changed => [ :name, :longitude, :latitude ] +end diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/landmark_versions.yml b/vendor/plugins/acts_as_versioned/test/fixtures/landmark_versions.yml new file mode 100644 index 000000000..2dbd54ed2 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/landmark_versions.yml @@ -0,0 +1,7 @@ +washington: + id: 1 + landmark_id: 1 + version: 1 + name: Washington, D.C. + latitude: 38.895 + longitude: -77.036667 diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml b/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml new file mode 100644 index 000000000..46d96176a --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml @@ -0,0 +1,6 @@ +washington: + id: 1 + name: Washington, D.C. + latitude: 38.895 + longitude: -77.036667 + version: 1 diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages.yml b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages.yml new file mode 100644 index 000000000..318e776cb --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages.yml @@ -0,0 +1,10 @@ +welcome: + id: 1 + title: Welcome to the weblog + lock_version: 24 + type: LockedPage +thinking: + id: 2 + title: So I was thinking + lock_version: 24 + type: SpecialLockedPage diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml new file mode 100644 index 000000000..5c978e629 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml @@ -0,0 +1,27 @@ +welcome_1: + id: 1 + page_id: 1 + title: Welcome to the weblg + version: 23 + version_type: LockedPage + +welcome_2: + id: 2 + page_id: 1 + title: Welcome to the weblog + version: 24 + version_type: LockedPage + +thinking_1: + id: 3 + page_id: 2 + title: So I was thinking!!! + version: 23 + version_type: SpecialLockedPage + +thinking_2: + id: 4 + page_id: 2 + title: So I was thinking + version: 24 + version_type: SpecialLockedPage diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb b/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb new file mode 100644 index 000000000..9512b5e82 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb @@ -0,0 +1,13 @@ +class AddVersionedTables < ActiveRecord::Migration + def self.up + create_table("things") do |t| + t.column :title, :text + end + Thing.create_versioned_table + end + + def self.down + Thing.drop_versioned_table + drop_table "things" rescue nil + end +end
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/page.rb b/vendor/plugins/acts_as_versioned/test/fixtures/page.rb new file mode 100644 index 000000000..f133e351a --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/page.rb @@ -0,0 +1,43 @@ +class Page < ActiveRecord::Base + belongs_to :author + has_many :authors, :through => :versions, :order => 'name' + belongs_to :revisor, :class_name => 'Author' + has_many :revisors, :class_name => 'Author', :through => :versions, :order => 'name' + acts_as_versioned :if => :feeling_good? do + def self.included(base) + base.cattr_accessor :feeling_good + base.feeling_good = true + base.belongs_to :author + base.belongs_to :revisor, :class_name => 'Author' + end + + def feeling_good? + @@feeling_good == true + end + end +end + +module LockedPageExtension + def hello_world + 'hello_world' + end +end + +class LockedPage < ActiveRecord::Base + acts_as_versioned \ + :inheritance_column => :version_type, + :foreign_key => :page_id, + :table_name => :locked_pages_revisions, + :class_name => 'LockedPageRevision', + :version_column => :lock_version, + :limit => 2, + :if_changed => :title, + :extend => LockedPageExtension +end + +class SpecialLockedPage < LockedPage +end + +class Author < ActiveRecord::Base + has_many :pages +end
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/page_versions.yml b/vendor/plugins/acts_as_versioned/test/fixtures/page_versions.yml new file mode 100644 index 000000000..ef565fa4f --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/page_versions.yml @@ -0,0 +1,16 @@ +welcome_2: + id: 1 + page_id: 1 + title: Welcome to the weblog + body: Such a lovely day + version: 24 + author_id: 1 + revisor_id: 1 +welcome_1: + id: 2 + page_id: 1 + title: Welcome to the weblg + body: Such a lovely day + version: 23 + author_id: 2 + revisor_id: 2 diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml b/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml new file mode 100644 index 000000000..07ac51f97 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml @@ -0,0 +1,7 @@ +welcome: + id: 1 + title: Welcome to the weblog + body: Such a lovely day + version: 24 + author_id: 1 + revisor_id: 1
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb b/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb new file mode 100644 index 000000000..3c38f2fcf --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb @@ -0,0 +1,6 @@ +class Widget < ActiveRecord::Base + acts_as_versioned :sequence_name => 'widgets_seq', :association_options => { + :dependent => nil, :order => 'version desc' + } + non_versioned_columns << 'foo' +end
\ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/migration_test.rb b/vendor/plugins/acts_as_versioned/test/migration_test.rb new file mode 100644 index 000000000..d85e95883 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/migration_test.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), 'abstract_unit') + +if ActiveRecord::Base.connection.supports_migrations? + class Thing < ActiveRecord::Base + attr_accessor :version + acts_as_versioned + end + + class MigrationTest < Test::Unit::TestCase + self.use_transactional_fixtures = false + def teardown + ActiveRecord::Base.connection.initialize_schema_information + ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" + + Thing.connection.drop_table "things" rescue nil + Thing.connection.drop_table "thing_versions" rescue nil + Thing.reset_column_information + end + + def test_versioned_migration + assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' } + # take 'er up + ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') + t = Thing.create :title => 'blah blah' + assert_equal 1, t.versions.size + + # now lets take 'er back down + ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') + assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' } + end + end +end diff --git a/vendor/plugins/acts_as_versioned/test/schema.rb b/vendor/plugins/acts_as_versioned/test/schema.rb new file mode 100644 index 000000000..7d5153d07 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/schema.rb @@ -0,0 +1,68 @@ +ActiveRecord::Schema.define(:version => 0) do + create_table :pages, :force => true do |t| + t.column :version, :integer + t.column :title, :string, :limit => 255 + t.column :body, :text + t.column :updated_on, :datetime + t.column :author_id, :integer + t.column :revisor_id, :integer + end + + create_table :page_versions, :force => true do |t| + t.column :page_id, :integer + t.column :version, :integer + t.column :title, :string, :limit => 255 + t.column :body, :text + t.column :updated_on, :datetime + t.column :author_id, :integer + t.column :revisor_id, :integer + end + + create_table :authors, :force => true do |t| + t.column :page_id, :integer + t.column :name, :string + end + + create_table :locked_pages, :force => true do |t| + t.column :lock_version, :integer + t.column :title, :string, :limit => 255 + t.column :type, :string, :limit => 255 + end + + create_table :locked_pages_revisions, :force => true do |t| + t.column :page_id, :integer + t.column :version, :integer + t.column :title, :string, :limit => 255 + t.column :version_type, :string, :limit => 255 + t.column :updated_at, :datetime + end + + create_table :widgets, :force => true do |t| + t.column :name, :string, :limit => 50 + t.column :foo, :string + t.column :version, :integer + t.column :updated_at, :datetime + end + + create_table :widget_versions, :force => true do |t| + t.column :widget_id, :integer + t.column :name, :string, :limit => 50 + t.column :version, :integer + t.column :updated_at, :datetime + end + + create_table :landmarks, :force => true do |t| + t.column :name, :string + t.column :latitude, :float + t.column :longitude, :float + t.column :version, :integer + end + + create_table :landmark_versions, :force => true do |t| + t.column :landmark_id, :integer + t.column :name, :string + t.column :latitude, :float + t.column :longitude, :float + t.column :version, :integer + end +end diff --git a/vendor/plugins/acts_as_versioned/test/versioned_test.rb b/vendor/plugins/acts_as_versioned/test/versioned_test.rb new file mode 100644 index 000000000..c1e1a4b98 --- /dev/null +++ b/vendor/plugins/acts_as_versioned/test/versioned_test.rb @@ -0,0 +1,313 @@ +require File.join(File.dirname(__FILE__), 'abstract_unit') +require File.join(File.dirname(__FILE__), 'fixtures/page') +require File.join(File.dirname(__FILE__), 'fixtures/widget') + +class VersionedTest < Test::Unit::TestCase + fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors, :landmarks, :landmark_versions + + def test_saves_versioned_copy + p = Page.create :title => 'first title', :body => 'first body' + assert !p.new_record? + assert_equal 1, p.versions.size + assert_equal 1, p.version + assert_instance_of Page.versioned_class, p.versions.first + end + + def test_saves_without_revision + p = pages(:welcome) + old_versions = p.versions.count + + p.save_without_revision + + p.without_revision do + p.update_attributes :title => 'changed' + end + + assert_equal old_versions, p.versions.count + end + + def test_rollback_with_version_number + p = pages(:welcome) + assert_equal 24, p.version + assert_equal 'Welcome to the weblog', p.title + + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" + assert_equal 23, p.version + assert_equal 'Welcome to the weblg', p.title + end + + def test_versioned_class_name + assert_equal 'Version', Page.versioned_class_name + assert_equal 'LockedPageRevision', LockedPage.versioned_class_name + end + + def test_versioned_class + assert_equal Page::Version, Page.versioned_class + assert_equal LockedPage::LockedPageRevision, LockedPage.versioned_class + end + + def test_special_methods + assert_nothing_raised { pages(:welcome).feeling_good? } + assert_nothing_raised { pages(:welcome).versions.first.feeling_good? } + assert_nothing_raised { locked_pages(:welcome).hello_world } + assert_nothing_raised { locked_pages(:welcome).versions.first.hello_world } + end + + def test_rollback_with_version_class + p = pages(:welcome) + assert_equal 24, p.version + assert_equal 'Welcome to the weblog', p.title + + assert p.revert_to!(p.versions.first), "Couldn't revert to 23" + assert_equal 23, p.version + assert_equal 'Welcome to the weblg', p.title + end + + def test_rollback_fails_with_invalid_revision + p = locked_pages(:welcome) + assert !p.revert_to!(locked_pages(:thinking)) + end + + def test_saves_versioned_copy_with_options + p = LockedPage.create :title => 'first title' + assert !p.new_record? + assert_equal 1, p.versions.size + assert_instance_of LockedPage.versioned_class, p.versions.first + end + + def test_rollback_with_version_number_with_options + p = locked_pages(:welcome) + assert_equal 'Welcome to the weblog', p.title + assert_equal 'LockedPage', p.versions.first.version_type + + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" + assert_equal 'Welcome to the weblg', p.title + assert_equal 'LockedPage', p.versions.first.version_type + end + + def test_rollback_with_version_class_with_options + p = locked_pages(:welcome) + assert_equal 'Welcome to the weblog', p.title + assert_equal 'LockedPage', p.versions.first.version_type + + assert p.revert_to!(p.versions.first), "Couldn't revert to 1" + assert_equal 'Welcome to the weblg', p.title + assert_equal 'LockedPage', p.versions.first.version_type + end + + def test_saves_versioned_copy_with_sti + p = SpecialLockedPage.create :title => 'first title' + assert !p.new_record? + assert_equal 1, p.versions.size + assert_instance_of LockedPage.versioned_class, p.versions.first + assert_equal 'SpecialLockedPage', p.versions.first.version_type + end + + def test_rollback_with_version_number_with_sti + p = locked_pages(:thinking) + assert_equal 'So I was thinking', p.title + + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1" + assert_equal 'So I was thinking!!!', p.title + assert_equal 'SpecialLockedPage', p.versions.first.version_type + end + + def test_lock_version_works_with_versioning + p = locked_pages(:thinking) + p2 = LockedPage.find(p.id) + + p.title = 'fresh title' + p.save + assert_equal 2, p.versions.size # limit! + + assert_raises(ActiveRecord::StaleObjectError) do + p2.title = 'stale title' + p2.save + end + end + + def test_version_if_condition + p = Page.create :title => "title" + assert_equal 1, p.version + + Page.feeling_good = false + p.save + assert_equal 1, p.version + Page.feeling_good = true + end + + def test_version_if_condition2 + # set new if condition + Page.class_eval do + def new_feeling_good() title[0..0] == 'a'; end + alias_method :old_feeling_good, :feeling_good? + alias_method :feeling_good?, :new_feeling_good + end + + p = Page.create :title => "title" + assert_equal 1, p.version # version does not increment + assert_equal 1, p.versions(true).size + + p.update_attributes(:title => 'new title') + assert_equal 1, p.version # version does not increment + assert_equal 1, p.versions(true).size + + p.update_attributes(:title => 'a title') + assert_equal 2, p.version + assert_equal 2, p.versions(true).size + + # reset original if condition + Page.class_eval { alias_method :feeling_good?, :old_feeling_good } + end + + def test_version_if_condition_with_block + # set new if condition + old_condition = Page.version_condition + Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' } + + p = Page.create :title => "title" + assert_equal 1, p.version # version does not increment + assert_equal 1, p.versions(true).size + + p.update_attributes(:title => 'a title') + assert_equal 1, p.version # version does not increment + assert_equal 1, p.versions(true).size + + p.update_attributes(:title => 'b title') + assert_equal 2, p.version + assert_equal 2, p.versions(true).size + + # reset original if condition + Page.version_condition = old_condition + end + + def test_version_no_limit + p = Page.create :title => "title", :body => 'first body' + p.save + p.save + 5.times do |i| + assert_page_title p, i + end + end + + def test_version_max_limit + p = LockedPage.create :title => "title" + p.update_attributes(:title => "title1") + p.update_attributes(:title => "title2") + 5.times do |i| + assert_page_title p, i, :lock_version + assert p.versions(true).size <= 2, "locked version can only store 2 versions" + end + end + + def test_track_changed_attributes_default_value + assert !Page.track_changed_attributes + assert LockedPage.track_changed_attributes + assert SpecialLockedPage.track_changed_attributes + end + + def test_version_order + assert_equal 23, pages(:welcome).versions.first.version + assert_equal 24, pages(:welcome).versions.last.version + assert_equal 23, pages(:welcome).find_versions.first.version + assert_equal 24, pages(:welcome).find_versions.last.version + end + + def test_track_changed_attributes + p = LockedPage.create :title => "title" + assert_equal 1, p.lock_version + assert_equal 1, p.versions(true).size + + p.title = 'title' + assert !p.save_version? + p.save + assert_equal 2, p.lock_version # still increments version because of optimistic locking + assert_equal 1, p.versions(true).size + + p.title = 'updated title' + assert p.save_version? + p.save + assert_equal 3, p.lock_version + assert_equal 1, p.versions(true).size # version 1 deleted + + p.title = 'updated title!' + assert p.save_version? + p.save + assert_equal 4, p.lock_version + assert_equal 2, p.versions(true).size # version 1 deleted + end + + def assert_page_title(p, i, version_field = :version) + p.title = "title#{i}" + p.save + assert_equal "title#{i}", p.title + assert_equal (i+4), p.send(version_field) + end + + def test_find_versions + assert_equal 2, locked_pages(:welcome).versions.size + assert_equal 1, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%weblog%']).length + assert_equal 2, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%web%']).length + assert_equal 0, locked_pages(:thinking).find_versions(:conditions => ['title LIKE ?', '%web%']).length + assert_equal 2, locked_pages(:welcome).find_versions.length + end + + def test_with_sequence + assert_equal 'widgets_seq', Widget.versioned_class.sequence_name + Widget.create :name => 'new widget' + Widget.create :name => 'new widget' + Widget.create :name => 'new widget' + assert_equal 3, Widget.count + assert_equal 3, Widget.versioned_class.count + end + + def test_has_many_through + assert_equal [authors(:caged), authors(:mly)], pages(:welcome).authors + end + + def test_has_many_through_with_custom_association + assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors + end + + def test_referential_integrity + pages(:welcome).destroy + assert_equal 0, Page.count + assert_equal 0, Page::Version.count + end + + def test_association_options + association = Page.reflect_on_association(:versions) + options = association.options + assert_equal :delete_all, options[:dependent] + assert_equal 'version', options[:order] + + association = Widget.reflect_on_association(:versions) + options = association.options + assert_nil options[:dependent] + assert_equal 'version desc', options[:order] + assert_equal 'widget_id', options[:foreign_key] + + widget = Widget.create :name => 'new widget' + assert_equal 1, Widget.count + assert_equal 1, Widget.versioned_class.count + widget.destroy + assert_equal 0, Widget.count + assert_equal 1, Widget.versioned_class.count + end + + def test_versioned_records_should_belong_to_parent + page = pages(:welcome) + page_version = page.versions.last + assert_equal page, page_version.page + end + + def test_unchanged_attributes + landmarks(:washington).attributes = landmarks(:washington).attributes + assert !landmarks(:washington).changed? + end + + def test_unchanged_string_attributes + landmarks(:washington).attributes = landmarks(:washington).attributes.inject({}) { |params, (key, value)| params.update key => value.to_s } + assert !landmarks(:washington).changed? + end +end |