diff --git a/ancestry.gemspec b/ancestry.gemspec index d2c9b880..388a2e03 100644 --- a/ancestry.gemspec +++ b/ancestry.gemspec @@ -1,7 +1,7 @@ # frozen_string_literal: true lib = File.expand_path('../lib/', __FILE__) -$:.unshift lib unless $:.include?(lib) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'ancestry/version' Gem::Specification.new do |s| @@ -24,6 +24,7 @@ EOF "changelog_uri" => "https://github.com/stefankroes/ancestry/blob/master/CHANGELOG.md", "source_code_uri" => "https://github.com/stefankroes/ancestry/", "bug_tracker_uri" => "https://github.com/stefankroes/ancestry/issues", + "rubygems_mfa_required" => "true" } s.version = Ancestry::VERSION @@ -32,19 +33,19 @@ EOF s.homepage = 'https://github.com/stefankroes/ancestry' s.license = 'MIT' - s.files = Dir[ + s.files = Dir[ "{lib}/**/*", 'CHANGELOG.md', 'MIT-LICENSE', 'README.md' ] s.require_paths = ["lib"] - - s.required_ruby_version = '>= 2.5' + + s.required_ruby_version = '>= 2.5' s.add_runtime_dependency 'activerecord', '>= 5.2.6' s.add_development_dependency 'appraisal' s.add_development_dependency 'minitest' - s.add_development_dependency 'rake', '~> 13.0' + s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'simplecov' s.add_development_dependency 'yard' end diff --git a/test/concerns/arrangement_test.rb b/test/concerns/arrangement_test.rb index fb967160..bc0b125d 100644 --- a/test/concerns/arrangement_test.rb +++ b/test/concerns/arrangement_test.rb @@ -8,7 +8,7 @@ def root_node(model) end def middle_node(model) - root_node(model).children.sort_by(&:id).first + root_node(model).children.min_by(&:id) end def leaf_node(model) @@ -25,7 +25,7 @@ def assert_tree(arranged_nodes, size_at_depth) assert_equal size_at_depth[1], children.size assert_equal node.children.sort_by(&:id), children.keys.sort_by(&:id) - assert_tree(children, size_at_depth[1..-1]) + assert_tree(children, size_at_depth[1..]) end end @@ -41,7 +41,7 @@ def assert_tree_path(arranged_nodes, expected_ids) arranged_nodes.each do |node, children| assert_equal expected_ids[0], node.id - assert_tree_path(children, expected_ids[1..-1]) + assert_tree_path(children, expected_ids[1..]) end end @@ -133,20 +133,25 @@ def test_arrange_serializable AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, _roots| col = model.ancestry_column # materialized path 2 has a slash at the beginning and end - fmt = AncestryTestDatabase.materialized_path2? ? -> (a) { a ? "/#{a}/" : "/" } : -> (a) {a} - result = [{ - col=>fmt[nil], "id"=>4, "children"=> [{ - col=>fmt["4"], "id"=>6, "children" => [] - }, { - col=>fmt["4"], "id"=>5, "children" => [] - }] - }, { - col=>fmt[nil], "id"=>1, "children"=> [{ - col=>fmt["1"], "id"=>3, "children"=>[] + fmt = + if AncestryTestDatabase.materialized_path2? + ->(a) { a ? "/#{a}/" : "/" } + else + ->(a) { a } + end + result = [ + { + col => fmt[nil], "id" => 4, "children" => [ + {col => fmt["4"], "id" => 6, "children" => []}, + {col => fmt["4"], "id" => 5, "children" => []} + ] }, { - col=>fmt["1"], "id"=>2, "children"=>[] - }] - }] + col => fmt[nil], "id" => 1, "children" => [ + {col => fmt["1"], "id" => 3, "children" => []}, + {col => fmt["1"], "id" => 2, "children" => []} + ] + } + ] assert_equal model.arrange_serializable(order: "id desc"), result end @@ -154,19 +159,19 @@ def test_arrange_serializable def test_arrange_serializable_with_block AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, _roots| - expected_result = [{ - "id"=>4, "children"=>[{ - "id"=>6 - }, { - "id"=>5 - }] - }, { - "id"=>1, "children"=> [{ - "id"=>3 + expected_result = [ + { + "id" => 4, "children" => [ + {"id" => 6}, + {"id" => 5} + ] }, { - "id"=>2 - }] - }] + "id" => 1, "children" => [ + {"id" => 3}, + {"id" => 2} + ] + } + ] result = model.arrange_serializable(order: "id desc") do |parent, children| out = {} out["id"] = parent.id diff --git a/test/concerns/has_ancestry_test.rb b/test/concerns/has_ancestry_test.rb index 2bd3bf47..3a46d06a 100644 --- a/test/concerns/has_ancestry_test.rb +++ b/test/concerns/has_ancestry_test.rb @@ -98,7 +98,7 @@ def test_setup_test_nodes end def test_primary_key_is_an_integer - AncestryTestDatabase.with_model(extra_columns: { string_id: :string }) do |model| + AncestryTestDatabase.with_model(extra_columns: {string_id: :string}) do |model| model.primary_key = :string_id assert !model.primary_key_is_an_integer? diff --git a/test/concerns/hooks_test.rb b/test/concerns/hooks_test.rb index d16c50c9..033f3a12 100644 --- a/test/concerns/hooks_test.rb +++ b/test/concerns/hooks_test.rb @@ -9,7 +9,6 @@ def test_has_ancestry_in_after_save :orphan_strategy => :adopt, :extra_columns => {:name => :string, :name_path => :string} ) do |model| - model.class_eval do before_save :before_save_hook @@ -35,9 +34,8 @@ def before_save_hook def test_update_descendants_with_changed_parent_value AncestryTestDatabase.with_model( - extra_columns: { name: :string, name_path: :string } + extra_columns: {name: :string, name_path: :string} ) do |model| - model.class_eval do before_save :update_name_path # this example will only work if the name field is unique across all levels @@ -78,9 +76,9 @@ def update_descendants_hook(descendants_clause, old_ancestry, new_ancestry) assert_equal("changed", m5.reload.name_path) assert_equal([m5.id], m2.reload.ancestor_ids) assert_equal("changed/child", m2.reload.name_path) - assert_equal([m5.id,m2.id], m3.reload.ancestor_ids) + assert_equal([m5.id, m2.id], m3.reload.ancestor_ids) assert_equal("changed/child/grandchild", m3.reload.name_path) - assert_equal([m5.id,m2.id,m3.id], m4.reload.ancestor_ids) + assert_equal([m5.id, m2.id, m3.id], m4.reload.ancestor_ids) assert_equal("changed/child/grandchild/grandchild's grand", m4.reload.name_path) end end @@ -134,15 +132,16 @@ def before_hook # see f94b22ba https://github.com/stefankroes/ancestry/pull/263 def test_node_creation_in_after_commit AncestryTestDatabase.with_model do |model| - children=[] + children = [] model.instance_eval do attr_accessor :idx - self.after_commit do - children << self.children.create!(:idx => self.idx - 1) if self.idx > 0 + + after_commit do + children << self.children.create!(:idx => idx - 1) if idx > 0 end end model.create!(:idx => 3) - assert_equal [1,2,3], children.first.ancestor_ids + assert_equal [1, 2, 3], children.first.ancestor_ids end end end diff --git a/test/concerns/integrity_checking_and_restoration_test.rb b/test/concerns/integrity_checking_and_restoration_test.rb index 5586f21e..474a66e4 100644 --- a/test/concerns/integrity_checking_and_restoration_test.rb +++ b/test/concerns/integrity_checking_and_restoration_test.rb @@ -51,7 +51,7 @@ def test_integrity_checking end end - def assert_integrity_restoration model + def assert_integrity_restoration(model) assert_raise Ancestry::AncestryIntegrityException do model.check_ancestry_integrity! end @@ -59,8 +59,8 @@ def assert_integrity_restoration model assert_nothing_raised do model.check_ancestry_integrity! end - assert model.all.any? {|node| node.has_parent? }, "Expected some nodes not to be roots" - assert_equal model.count, model.roots.collect {|node| node.descendants.count + 1 }.sum + assert model.all.any?(&:has_parent?), "Expected some nodes not to be roots" + assert_equal model.count, model.roots.collect { |node| node.descendants.count + 1 }.sum end def test_integrity_restoration diff --git a/test/concerns/materialized_path_test.rb b/test/concerns/materialized_path_test.rb index 7179321a..9a2c1a38 100644 --- a/test/concerns/materialized_path_test.rb +++ b/test/concerns/materialized_path_test.rb @@ -16,23 +16,23 @@ def test_ancestry_column_values # saved node.save! - assert_ancestry node, nil, child: "#{node.id}" + assert_ancestry node, nil, child: node.id.to_s # changed node.ancestor_ids = [root.id] - assert_ancestry node, "#{root.id}", db: nil, child: "#{node.id}" + assert_ancestry node, root.id.to_s, db: nil, child: node.id.to_s # changed saved node.save! - assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}" + assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}" # reloaded node.reload - assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}" + assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}" # fresh node node = model.find(node.id) - assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}" + assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}" end end diff --git a/test/concerns/orphan_strategies_test.rb b/test/concerns/orphan_strategies_test.rb index 73a31d88..18d6a260 100644 --- a/test/concerns/orphan_strategies_test.rb +++ b/test/concerns/orphan_strategies_test.rb @@ -12,7 +12,7 @@ def test_setting_invalid_orphan_strategy end def test_orphan_rootify_strategy - AncestryTestDatabase.with_model orphan_strategy: :rootify, :depth => 3, :width => 3 do |model, roots| + AncestryTestDatabase.with_model orphan_strategy: :rootify, :depth => 3, :width => 3 do |_model, roots| root = roots.first.first children = root.children.to_a root.destroy @@ -50,7 +50,7 @@ def verify_parent_exists end def test_orphan_restrict_strategy - AncestryTestDatabase.with_model orphan_strategy: :restrict, :depth => 3, :width => 3 do |model, roots| + AncestryTestDatabase.with_model orphan_strategy: :restrict, :depth => 3, :width => 3 do |_model, roots| root = roots.first.first assert_raise Ancestry::AncestryException do root.destroy @@ -63,11 +63,11 @@ def test_orphan_restrict_strategy def test_orphan_adopt_strategy AncestryTestDatabase.with_model orphan_strategy: :adopt do |model| - n1 = model.create! #create a root node - n2 = model.create!(:parent => n1) #create child with parent=root - n3 = model.create!(:parent => n2) #create child with parent=n2, depth = 2 - n4 = model.create!(:parent => n2) #create child with parent=n2, depth = 2 - n5 = model.create!(:parent => n4) #create child with parent=n4, depth = 3 + n1 = model.create! # create a root node + n2 = model.create!(:parent => n1) # create child with parent=root + n3 = model.create!(:parent => n2) # create child with parent=n2, depth = 2 + n4 = model.create!(:parent => n2) # create child with parent=n2, depth = 2 + n5 = model.create!(:parent => n4) # create child with parent=n4, depth = 3 n2.destroy # delete a node with desecendants n3.reload n5.reload @@ -84,7 +84,7 @@ def test_orphan_adopt_strategy # DEPRECATED - please see test_apply_orphan_strategy_none for pattern instead def test_override_apply_orphan_strategy - AncestryTestDatabase.with_model orphan_strategy: :destroy do |model, roots| + AncestryTestDatabase.with_model orphan_strategy: :destroy do |model, _roots| root = model.create! child = model.create!(:parent => root) model.class_eval do @@ -101,7 +101,7 @@ def apply_orphan_strategy end def test_apply_orphan_strategy_none - AncestryTestDatabase.with_model orphan_strategy: :none do |model, roots| + AncestryTestDatabase.with_model orphan_strategy: :none do |model, _roots| root = model.create! child = model.create!(:parent => root) model.class_eval do @@ -127,7 +127,7 @@ def apply_orphan_strategy_abc end end - root = model.create! + root = model.create! 3.times { root.children.create! } model.create! # a node that is not affected assert_difference 'model.count', -4 do @@ -147,7 +147,7 @@ def apply_orphan_strategy_abc has_ancestry orphan_strategy: :abc, ancestry_column: AncestryTestDatabase.ancestry_column end - root = model.create! + root = model.create! 3.times { root.children.create! } model.create! # a node that is not affected assert_difference 'model.count', -4 do @@ -158,17 +158,17 @@ def apply_orphan_strategy_abc def test_basic_delete AncestryTestDatabase.with_model do |model| - n1 = model.create! #create a root node - n2 = model.create!(:parent => n1) #create child with parent=root + n1 = model.create! # create a root node + n2 = model.create!(:parent => n1) # create child with parent=root n2.destroy! - model.find(n1.id) # parent should exist + model.find(n1.id) # parent should exist - n1 = model.create! #create a root node - n2 = model.create!(:parent => n1) #create child with parent=root + n1 = model.create! # create a root node + n2 = model.create!(:parent => n1) # create child with parent=root n1.destroy! assert_nil(model.find_by(:id => n2.id)) # child should not exist - n1 = model.create! #create a root node + n1 = model.create! # create a root node n1.destroy! end end diff --git a/test/concerns/scopes_test.rb b/test/concerns/scopes_test.rb index a765bafa..b31b168e 100644 --- a/test/concerns/scopes_test.rb +++ b/test/concerns/scopes_test.rb @@ -66,7 +66,7 @@ def test_order_by_reverse AncestryTestDatabase.with_model(:width => 1, :depth => 3) do |model, _roots| child = model.last assert child - assert_nothing_raised do #IrreversibleOrderError + assert_nothing_raised do # IrreversibleOrderError assert child.ancestors.last end end @@ -106,9 +106,9 @@ def test_scoping_in_callbacks model.class_eval do define_method :after_create_callback do # We don't want to be in the #children scope here when creating the child - self.parent + parent self.parent_id = record.id if record - self.root + root end end diff --git a/test/concerns/sort_by_ancestry_test.rb b/test/concerns/sort_by_ancestry_test.rb index 6124cc35..03a34448 100644 --- a/test/concerns/sort_by_ancestry_test.rb +++ b/test/concerns/sort_by_ancestry_test.rb @@ -7,7 +7,7 @@ class SortByAncestryTest < ActiveSupport::TestCase # This highlights where/why a non-correct sorting order is returned CORRECT = (ENV["CORRECT"] == "true") - RANK_SORT = -> (a, b) { a.rank <=> b.rank } + RANK_SORT = ->(a, b) { a.rank <=> b.rank } # tree is of the form: # - n1 @@ -19,7 +19,7 @@ class SortByAncestryTest < ActiveSupport::TestCase # @returns [Array] list of nodes def build_tree(model) # inflate the node id to test id wrap around edge cases - ENV["NODES"].to_i.times { model.create!.destroy } if ENV["NODES"] + ENV["NODES"]&.to_i&.times { model.create!.destroy } n1 = model.create! n2 = model.create!(:parent => n1) @@ -59,7 +59,7 @@ def test_sort_by_ancestry_no_parents_siblings end # TODO: thinking about dropping this one - # only keep if we can find a + # only keep if we can find a way to sort in the db def test_sort_by_ancestry_no_parents_same_level AncestryTestDatabase.with_model do |model| _, _, n3, n4, n5, _ = build_tree(model) @@ -118,7 +118,7 @@ def test_sort_by_ancestry_missing_parent_middle_of_tree def build_ranked_tree(model) # inflate the node id to test id wrap around edge cases # NODES=4..9 seem like edge cases - ENV["NODES"].to_i.times { model.create!.destroy } if ENV["NODES"] + ENV["NODES"]&.to_i&.times { model.create!.destroy } n1 = model.create!(:rank => 0) n2 = model.create!(:rank => 1) diff --git a/test/concerns/sti_support_test.rb b/test/concerns/sti_support_test.rb index 3b297820..123f481f 100644 --- a/test/concerns/sti_support_test.rb +++ b/test/concerns/sti_support_test.rb @@ -67,7 +67,7 @@ def test_sti_support_with_from_subclass def test_sti_support_for_counter_cache AncestryTestDatabase.with_model :counter_cache => true, :extra_columns => {:type => :string} do |model| - # NOTE had to use subclasses other than Subclass1/Subclass2 from above + # NOTE: had to use subclasses other than Subclass1/Subclass2 from above # due to (I think) Rails caching those STI classes and that not getting # reset between specs diff --git a/test/concerns/touching_test.rb b/test/concerns/touching_test.rb index 4f2d121a..2b912416 100644 --- a/test/concerns/touching_test.rb +++ b/test/concerns/touching_test.rb @@ -8,7 +8,6 @@ def test_touch_option_disabled :extra_columns => {:name => :string, :updated_at => :datetime}, :touch => false ) do |model| - wayback = Time.new(1984) recently = Time.now - 1.minute @@ -27,7 +26,6 @@ def test_touch_option_enabled_propagates_with_modification :extra_columns => {:updated_at => :datetime}, :touch => true ) do |model| - way_back = Time.new(1984) recently = Time.now - 1.minute @@ -54,7 +52,6 @@ def test_touch_option_enabled_propagates_with_modification def test_touch_propogates_multiple_levels AncestryTestDatabase.with_model(:extra_columns => {:name => :string, :updated_at => :datetime}, :touch => true) do |model| - way_back = Time.new(1984) recently = Time.now - 1.minute @@ -84,7 +81,6 @@ def test_touch_option_enabled_doesnt_propagate_without_modification :extra_columns => {:updated_at => :datetime}, :touch => true ) do |model| - way_back = Time.new(1984) recently = Time.now - 1.minute @@ -107,7 +103,6 @@ def test_touch_option_with_scope :extra_columns => {:updated_at => :datetime}, :touch => true ) do |model| - way_back = Time.new(1984) recently = Time.now - 1.minute diff --git a/test/concerns/tree_navigation_test.rb b/test/concerns/tree_navigation_test.rb index a4e6fbb3..be61967d 100644 --- a/test/concerns/tree_navigation_test.rb +++ b/test/concerns/tree_navigation_test.rb @@ -9,15 +9,15 @@ class TreeNavigationTest < ActiveSupport::TestCase # across: |siblings|subtree |path | ATTRIBUTE_MATRIX = { root: {attribute_id: :root_id}, - parent: {attribute_id: :parent_id, exists: :has_parent?, db: true}, - ancestors: {attribute_ids: :ancestor_ids, exists: :ancestors?, db: true}, - children: {attribute_ids: :child_ids, exists: :children?}, + parent: {attribute_id: :parent_id, exists: :has_parent?, db: true}, + ancestors: {attribute_ids: :ancestor_ids, exists: :ancestors?, db: true}, + children: {attribute_ids: :child_ids, exists: :children?}, descendants: {attribute_ids: :descendant_ids}, indirects: {attribute_ids: :indirect_ids}, - siblings: {attribute_ids: :sibling_ids, exists: :siblings?}, + siblings: {attribute_ids: :sibling_ids, exists: :siblings?}, subtree: {attribute_ids: :subtree_ids}, - path: {attribute_ids: :path_ids, db: true}, - } + path: {attribute_ids: :path_ids, db: true} + }.freeze # NOTE: has_ancestors? is an alias for parent? / ancestors? but not tested # class level getters are in test/concerns/scopes_test.rb @@ -196,7 +196,8 @@ def test_node_in_database_children # in database (again - but in a different hierarchy) node11.save! - node1.reload ; node2.reload + node1.reload + node2.reload # are these necessary? # do we want this to work without? @@ -244,6 +245,7 @@ def update_descendants_with_new_ancestry raise "callback disabled for #{id}" if id == 2 else raise "callback eabled for #{id}" if id != 2 + # want to make sure we're pointing at the correct nodes actual = unscoped_descendants_before_last_save.order(:id).map(&:id) raise "unscoped_descendants_before_last_save was #{actual}" unless actual == [3, 4, 5] @@ -316,7 +318,7 @@ def assert_attribute(node, attribute_name, value, db: :value, exists: :value) # @param value [Array] expected output # @param attribute_name [Symbol] attribute to test # @param exists [true|false] test the exists "attribute? (default values.present?) - # @param db [Array[AR]] value that should be reflected _in_database (default: use values) + # @param db [Array[AR]] value that should be reflected _in_database (default: use values) # skips if not supported in matrix def assert_attributes(node, attribute_name, values, db: :values, exists: :values) attribute_ids = ATTRIBUTE_MATRIX[attribute_name][:attribute_ids] diff --git a/test/concerns/tree_predicate_test.rb b/test/concerns/tree_predicate_test.rb index d2502646..38be4841 100644 --- a/test/concerns/tree_predicate_test.rb +++ b/test/concerns/tree_predicate_test.rb @@ -21,7 +21,7 @@ def test_tree_predicates # Children assertions assert root.has_children? assert !root.is_childless? - assert children.map { |n| n.is_childless? }.all? + assert children.map(&:is_childless?).all? assert children.map { |n| !root.child_of?(n) }.all? assert children.map { |n| n.child_of?(root) }.all? # Siblings assertions diff --git a/test/environment.rb b/test/environment.rb index 4176c4dc..0fa03d6f 100644 --- a/test/environment.rb +++ b/test/environment.rb @@ -62,7 +62,7 @@ def self.setup puts "testing #{db_type} #{Ancestry.default_update_strategy == :sql ? "(sql) " : ""}(with #{column_type} #{ancestry_column})" puts "column format: #{Ancestry.default_ancestry_format} options: #{column_options.inspect}" - rescue => e + rescue StandardError => e if ENV["CI"] raise else