Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test flag_shih_tzu using a real ActiveRecord class

* Modify test/database.yml as needed and create the test db
* Use rake test:plugins from your rails app to run the tests
  • Loading branch information...
commit 81076d45cbdc61bbf0515fe021911eba0c196ff8 1 parent 6bfb5dd
@boosty boosty authored boosty committed
View
1  .gitignore
@@ -0,0 +1 @@
+test/debug.log
View
3  init.rb
@@ -1,2 +1 @@
-# Include hook code here
-require 'flag_shih_tzu'
+require 'flag_shih_tzu'
View
21 test/database.yml
@@ -0,0 +1,21 @@
+# sqlite:
+# :adapter: sqlite
+# :dbfile: vendor/plugins/flag_shih_tzu/test/flag_shih_tzu_plugin.sqlite.db
+#
+# sqlite3:
+# :adapter: sqlite3
+# :dbfile: vendor/plugins/flag_shih_tzu/test/flag_shih_tzu_plugin.sqlite3.db
+#
+# postgresql:
+# :adapter: postgresql
+# :username: postgres
+# :password: postgres
+# :database: flag_shih_tzu_plugin_test
+# :min_messages: ERROR
+#
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: root
+ :password:
+ :database: flag_shih_tzu_plugin_test
View
138 test/flag_shih_tzu_test.rb
@@ -1,40 +1,23 @@
-require 'test/unit'
-require File.expand_path(File.dirname(__FILE__) + '/../lib/flag_shih_tzu')
+require File.dirname(__FILE__) + '/test_helper.rb'
+load_schema
-class FlagColumn
- def name; 'flags'; end
- def type; :integer; end
-end
-
-class FlagRecord < Hash
- def self.columns
- [FlagColumn.new]
- end
- def flags=(value)
- self[:flags] = value
- end
-end
-
-class Foo < FlagRecord
- def self.table_name
- "foos"
- end
-
+class Spaceship < ActiveRecord::Base
include FlagShihTzu
- has_flags 1 => :deleted,
- 2 => :organizer_edited_comment
+ has_flags 1 => :warpdrive,
+ 2 => :shields,
+ 3 => :electrolytes
end
-
-
-class FlagShihTzuTest < Test::Unit::TestCase
- def test_should_raise_an_exception_when_bit_position_is_negative
+class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
+
+ def test_has_flags_should_raise_an_exception_when_bit_position_is_negative
assert_raises ArgumentError do
eval(<<-EOF
- class Invalid < FlagRecord
+ class InvalidSpaceship < ActiveRecord::Base
+ set_table_name 'spaceships'
include FlagShihTzu
has_flags({ -1 => :error })
@@ -43,93 +26,72 @@ class Invalid < FlagRecord
)
end
end
-
- def setup
- @foo = Foo.new
- end
- def test_should_enable_the_flag
- @foo.enable_flag(:deleted)
- assert @foo.flag_enabled?(:deleted)
+ def test_should_define_a_sql_condition_method_for_flag_enabled
+ assert_equal "(spaceships.flags & 1 = 1)", Spaceship.warpdrive_condition
end
- def test_should_enable_multiple_flags
- @foo.enable_flag(:deleted)
- @foo.enable_flag(:organizer_edited_comment)
- assert @foo.flag_enabled?(:deleted)
- assert @foo.flag_enabled?(:organizer_edited_comment)
- end
-
- def test_should_leave_the_flag_enabled_when_called_twice
- 2.times do
- @foo.enable_flag(:deleted)
- assert @foo.flag_enabled?(:deleted)
- end
+ def test_should_define_a_sql_condition_method_for_flag_not_enabled
+ assert_equal "(spaceships.flags & 1 = 0)", Spaceship.not_warpdrive_condition
end
+
+end
- def test_should_define_a_deleted_method
- assert_equal false, @foo.deleted
- end
- def test_should_define_a_deleted_predicate
- assert_equal false, @foo.deleted?
+class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
+
+ def setup
+ @spaceship = Spaceship.new
end
- def test_should_define_a_deleted=_method
- @foo.deleted = true
- assert @foo.deleted
+ def test_should_enable_flag
+ @spaceship.enable_flag(:warpdrive)
+ assert @spaceship.flag_enabled?(:warpdrive)
end
- def test_should_define_a_deleted_condition_method
- assert_equal "(#{Foo.table_name}.flags & #{Foo.flag_mapping[:deleted]} = 1)", Foo.deleted_condition
+ def test_should_disable_flag
+ @spaceship.enable_flag(:warpdrive)
+ assert @spaceship.flag_enabled?(:warpdrive)
+ @spaceship.disable_flag(:warpdrive)
+ assert @spaceship.flag_disabled?(:warpdrive)
end
- def test_should_define_a_not_deleted_condition_method
- assert_equal "(#{Foo.table_name}.flags & #{Foo.flag_mapping[:deleted]} = 0)", Foo.not_deleted_condition
+ def test_enable_flag_should_leave_the_flag_enabled_when_called_twice
+ 2.times do
+ @spaceship.enable_flag(:warpdrive)
+ assert @spaceship.flag_enabled?(:warpdrive)
+ end
end
- def test_should_disable_the_flag
- @foo.disable_flag(:deleted)
- assert @foo.flag_disabled?(:deleted)
- end
-
- def test_should_disable_multiple_flags
- @foo.disable_flag(:deleted)
- @foo.disable_flag(:organizer_edited_comment)
- assert @foo.flag_disabled?(:deleted)
- assert @foo.flag_disabled?(:organizer_edited_comment)
- end
-
- def test_should_leave_the_flag_disabled_when_called_twice
+ def test_disable_flag_should_leave_the_flag_disabled_when_called_twice
2.times do
- @foo.disable_flag(:deleted)
- assert @foo.flag_disabled?(:deleted)
+ @spaceship.disable_flag(:warpdrive)
+ assert !@spaceship.flag_enabled?(:warpdrive)
end
end
-
- def test_should_be_possible_to_query_a_foo_that_has_never_been_set
- assert_equal false, Foo.new.deleted?
+
+ def test_should_define_an_attribute_reader_method
+ assert_equal false, @spaceship.warpdrive
end
- def test_should_return_0_by_default
- foo = Foo.new
- assert foo.flags == 0
+ def test_should_define_an_attribute_reader_predicate_method
+ assert_equal false, @spaceship.warpdrive?
end
-
- def test_should_have_flag_mapping_for_each_flag
- assert_equal 1, Foo.flag_mapping[:deleted]
- assert_equal 2, Foo.flag_mapping[:organizer_edited_comment]
+
+ def test_should_define_an_attribute_writer_method
+ @spaceship.warpdrive = true
+ assert @spaceship.warpdrive
end
def test_should_respect_true_values_like_active_record
[true, 1, '1', 't', 'T', 'true', 'TRUE'].each do |true_value|
- @foo.deleted = true_value
- assert @foo.deleted
+ @spaceship.warpdrive = true_value
+ assert @spaceship.warpdrive
end
[false, 0, '0', 'f', 'F', 'false', 'FALSE'].each do |false_value|
- @foo.deleted = false_value
- assert !@foo.deleted
+ @spaceship.warpdrive = false_value
+ assert !@spaceship.warpdrive
end
end
View
5 test/schema.rb
@@ -0,0 +1,5 @@
+ActiveRecord::Schema.define(:version => 0) do
+ create_table :spaceships, :force => true do |t|
+ t.integer :flags, :null => false, :default => 0
+ end
+end
View
33 test/test_helper.rb
@@ -0,0 +1,33 @@
+ENV['RAILS_ENV'] = 'test'
+ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
+
+require 'test/unit'
+require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
+
+def load_schema
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
+ db_adapter = ENV['DB']
+
+ # no db passed, try one of these fine config-free DBs before bombing.
+ db_adapter ||=
+ begin
+ require 'rubygems'
+ require 'sqlite'
+ 'sqlite'
+ rescue MissingSourceFile
+ begin
+ require 'sqlite3'
+ 'sqlite3'
+ rescue MissingSourceFile
+ end
+ end
+
+ if db_adapter.nil?
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
+ end
+
+ ActiveRecord::Base.establish_connection(config[db_adapter])
+ load(File.dirname(__FILE__) + "/schema.rb")
+ require File.dirname(__FILE__) + '/../init.rb'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.