This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #826 from omu/develop
Merge develop into master
- Loading branch information
Showing
70 changed files
with
612 additions
and
476 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nokul | ||
VERSION = '0.5.4' | ||
VERSION = '0.5.5' | ||
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
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'minitest/association_helper' | ||
require_relative 'minitest/callback_helper' | ||
require_relative 'minitest/enumeration_helper' | ||
require_relative 'minitest/validation_helper' |
48 changes: 48 additions & 0 deletions
48
plugins/support/lib/nokul/support/minitest/association_helper.rb
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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nokul | ||
module Support | ||
module Minitest | ||
module AssociationHelper | ||
SUFFIX = 'Test' | ||
RELATIONS = %i[belongs_to has_many has_one has_and_belongs_to_many].freeze | ||
|
||
RELATIONS.each do |relation| | ||
define_method(relation) do |attribute, **options| | ||
association = relations_for(relation, attribute) | ||
|
||
test "can respond to #{relation} #{attribute}" do | ||
assert association | ||
options.each do |key, value| | ||
assert_equal association.options[key], value, "Option: #{key}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
def accepts_nested_attributes_for(attribute, **options) | ||
nested_attributes_options = klass.nested_attributes_options[attribute] | ||
|
||
test "#{attribute} must be nested attribute" do | ||
assert nested_attributes_options | ||
options.each do |key, value| | ||
assert_equal nested_attributes_options[key], value, "Option: #{key}" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def relations_for(relation, attribute) | ||
klass.reflect_on_all_associations(relation).select do |association| | ||
association.name == attribute | ||
end.first | ||
end | ||
|
||
def klass | ||
to_s.delete_suffix(SUFFIX).constantize | ||
end | ||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
plugins/support/lib/nokul/support/minitest/callback_helper.rb
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nokul | ||
module Support | ||
module Minitest | ||
module CallbackHelper | ||
CALLBACKS = %i[ | ||
after_commit | ||
after_create | ||
after_destroy | ||
after_find | ||
after_initialize | ||
after_rollback | ||
after_save | ||
after_touch | ||
after_update | ||
after_validation | ||
around_create | ||
around_destroy | ||
around_save | ||
around_update | ||
before_create | ||
before_destroy | ||
before_save | ||
before_update | ||
before_validation | ||
].freeze | ||
|
||
CALLBACKS.each do |callback| | ||
define_method(callback) do |action| | ||
test "has #{callback} for #{action}" do | ||
kind, method = callback.to_s.split('_') | ||
klass = class_name.delete_suffix('Test').constantize | ||
callbacks = klass.send("_#{method}_callbacks") | ||
assert callbacks.select { |cb| cb.filter.eql?(action.to_sym) && cb.kind.eql?(kind.to_sym) }.any? | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
plugins/support/lib/nokul/support/minitest/enumeration_helper.rb
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nokul | ||
module Support | ||
module Minitest | ||
module EnumerationHelper | ||
def enum(definitions) | ||
definitions.each do |attribute, values| | ||
values.each do |key, value| | ||
test "has a enum key (#{key}) with a value of #{value}" do | ||
klass = class_name.delete_suffix('Test').constantize | ||
defined_value = klass.defined_enums.dig(attribute.to_s, key.to_s) | ||
assert_equal defined_value, value, "Enum: #{attribute}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
90 changes: 90 additions & 0 deletions
90
plugins/support/lib/nokul/support/minitest/validation_helper.rb
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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nokul | ||
module Support | ||
module Minitest | ||
module ValidationHelper | ||
SUFFIX = 'Test' | ||
|
||
def validates_presence_of(*attributes) | ||
attributes.each do |attribute| | ||
test "#{attribute} must be present (presence: true)" do | ||
object = class_name.delete_suffix(SUFFIX).constantize.take | ||
object.send("#{attribute}=", nil) | ||
assert_not object.valid? | ||
assert_not_empty object.errors[attribute] | ||
end | ||
end | ||
end | ||
|
||
def validates_presence_of_nested_model(attribute, ids: nil) | ||
test "nested model (#{attribute}) must be present" do | ||
ids ||= "#{attribute.to_s.singularize}_ids" | ||
object = class_name.delete_suffix(SUFFIX).constantize.take | ||
object.send("#{ids}=", nil) | ||
assert_not object.valid? | ||
assert_not_empty object.errors[attribute] | ||
end | ||
end | ||
|
||
def validates_uniqueness_of(*attributes) | ||
attributes.each do |attribute| | ||
test "#{attribute} must be unique (uniqueness: true)" do | ||
duplicate_object = class_name.delete_suffix(SUFFIX).constantize.take.dup | ||
assert_not duplicate_object.valid? | ||
assert_not_empty duplicate_object.errors[attribute] | ||
end | ||
end | ||
end | ||
|
||
def validates_length_of(attribute, **option) | ||
option[:maximum] = 255 if option.blank? | ||
|
||
controls = { | ||
is: { value: 1, error_key: :wrong_length }, | ||
minimum: { value: -1, error_key: :too_short }, | ||
maximum: { value: 1, error_key: :too_long } | ||
} | ||
key = option.keys.first | ||
value = option[key].to_i + controls.dig(key, :value).to_i | ||
error_key = controls.dig(key, :error_key) | ||
|
||
test "#{attribute} length must be #{option}" do | ||
object = class_name.delete_suffix(SUFFIX).constantize.take | ||
object.send("#{attribute}=", (0..value).map { ('a'..'z').to_a[rand(26)] }.join) | ||
assert_not object.valid? | ||
assert object.errors.details[attribute].map { |err| err[:error] }.include?(error_key) | ||
end | ||
end | ||
|
||
def validates_numericality_of(attribute) | ||
test "#{attribute} must be a number" do | ||
object = class_name.delete_suffix(SUFFIX).constantize.take | ||
object.send("#{attribute}=", 'some string') | ||
assert_not object.valid? | ||
assert object.errors.details[attribute].map { |err| err[:error] }.include?(:not_a_number) | ||
end | ||
end | ||
|
||
def validates_numerical_range(attribute, **option) | ||
controls = { | ||
greater_than: 0, | ||
less_than: 0, | ||
greater_than_or_equal_to: -1, | ||
less_than_or_equal_to: 1 | ||
} | ||
|
||
key = option.keys.first | ||
value = option[key].to_i + controls[key].to_i | ||
|
||
test "#{attribute} must be #{key} #{value}" do | ||
object = class_name.delete_suffix(SUFFIX).constantize.take | ||
object.send("#{attribute}=", value) | ||
assert_not object.valid? | ||
assert object.errors.details[attribute].map { |err| err[:error] }.include?(key) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.