-
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.
Add sti classic tests with alias Make aliases work Fix cops
- Loading branch information
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
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
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
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
class TestOpenClosedPrincipleAliasedInteger < Case | ||
class Property < ActiveRecord::Base | ||
include Discriminable | ||
|
||
alias_attribute :kind, :kind_with_some_postfix | ||
|
||
# No enum this time | ||
discriminable_by :kind | ||
end | ||
|
||
class NumberProperty < Property | ||
discriminable_as 1 | ||
end | ||
|
||
class OptionProperty < Property | ||
discriminable_as 2, 3 | ||
end | ||
|
||
def setup | ||
ActiveRecord::Schema.define do | ||
create_table :properties do |t| | ||
t.integer :kind_with_some_postfix, limit: 1, null: true | ||
end | ||
end | ||
end | ||
|
||
def test_sti_name_default | ||
assert_equal 1, NumberProperty.sti_name | ||
assert_equal 2, OptionProperty.sti_name | ||
end | ||
|
||
def test_creation_and_loading | ||
assert_equal 1, NumberProperty.create.kind | ||
assert_equal 2, OptionProperty.create.kind | ||
assert_instance_of NumberProperty, Property.first | ||
assert_instance_of OptionProperty, Property.last | ||
end | ||
|
||
def test_creation_using_parent | ||
assert_instance_of NumberProperty, Property.create(kind: 1) | ||
assert_instance_of OptionProperty, Property.create(kind: 3) | ||
end | ||
|
||
def test_building | ||
assert_instance_of NumberProperty, Property.new(kind: 1) | ||
assert_instance_of OptionProperty, Property.new(kind: 2) | ||
assert_instance_of OptionProperty, Property.new(kind: 3) | ||
end | ||
end |
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
class TestStiAliased < Case | ||
class Order < ActiveRecord::Base | ||
self.inheritance_column = "type_with_some_postfix" | ||
alias_attribute :type, :type_with_some_postfix | ||
end | ||
|
||
class Cart < Order | ||
end | ||
|
||
def setup | ||
ActiveRecord::Schema.define do | ||
create_table :orders do |t| | ||
t.string :type_with_some_postfix | ||
end | ||
end | ||
end | ||
|
||
def test_class_methods | ||
assert_equal Order.inheritance_column, "type_with_some_postfix" | ||
assert_equal Order.sti_name, "TestStiAliased::Order" | ||
assert_equal Cart.sti_name, "TestStiAliased::Cart" | ||
end | ||
|
||
def test_count | ||
Order.create | ||
Cart.create | ||
assert_equal 2, Order.count | ||
|
||
assert_equal 1, Cart.count | ||
end | ||
|
||
def test_loading | ||
Order.create | ||
Cart.create | ||
assert_instance_of TestStiAliased::Order, Order.first | ||
assert_instance_of TestStiAliased::Cart, Cart.first | ||
|
||
assert_instance_of TestStiAliased::Cart, Order.where(type: "TestStiAliased::Cart").first | ||
end | ||
|
||
def test_creating_and_building | ||
assert_instance_of TestStiAliased::Cart, Order.new(type_with_some_postfix: "TestStiAliased::Cart") | ||
assert_instance_of TestStiAliased::Cart, Order.where(type: "TestStiAliased::Cart").build | ||
|
||
skip "This is not supported" | ||
assert_instance_of TestStiAliased::Cart, Order.new(type: "TestStiAliased::Cart") | ||
end | ||
|
||
def test_changes | ||
refute_predicate Order.new, :changed? | ||
assert_empty Order.new.changes | ||
|
||
# See https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html | ||
assert_predicate Cart.new, :changed? | ||
assert_equal({ "type_with_some_postfix" => [nil, "TestStiAliased::Cart"] }, Cart.new.changes) | ||
end | ||
end |