-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from gregorw/feature/boolean
feat: discriminable booleans
- Loading branch information
Showing
2 changed files
with
56 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
require "active_record" | ||
|
||
class TestBoolean < Case | ||
class Response < ActiveRecord::Base | ||
include Discriminable | ||
|
||
discriminable affirmative: { true => "TestBoolean::Yes" } | ||
end | ||
|
||
class Yes < Response | ||
end | ||
|
||
def setup | ||
ActiveRecord::Schema.define do | ||
create_table :responses do |t| | ||
t.boolean :affirmative, null: false, default: false | ||
end | ||
end | ||
end | ||
|
||
def test_class_methods | ||
assert_equal Response.inheritance_column, "affirmative" | ||
assert_nil Response.sti_name | ||
assert_equal Yes.sti_name, true | ||
end | ||
|
||
def test_count | ||
Response.create! | ||
Yes.create! | ||
assert_equal 2, Response.count | ||
assert_equal 1, Response.where(affirmative: true).count | ||
assert_equal 1, Yes.count | ||
end | ||
|
||
def test_kind | ||
Response.create! | ||
Yes.create! | ||
assert_instance_of TestBoolean::Response, Response.where(affirmative: false).first | ||
assert_instance_of TestBoolean::Yes, Response.where(affirmative: true).first | ||
assert_instance_of TestBoolean::Yes, Response.where(affirmative: true).build | ||
# assert_instance_of TestBoolean::Yes, Response.create(affirmative: true) | ||
assert_instance_of TestBoolean::Yes, Yes.first | ||
end | ||
|
||
def test_new | ||
refute_predicate Response.new, :changed? | ||
assert_equal Response.new.changes, {} | ||
|
||
assert_predicate Yes.new, :changed? | ||
assert_predicate Yes.new, :affirmative? | ||
assert_equal Yes.new.changes, { "affirmative" => [false, true] } | ||
end | ||
end |