diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ad5455..9409e40 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest services: mysql: - image: mysql/mysql-server:8.0.30 + image: mysql/mysql-server ports: - "3306:3306" env: @@ -19,7 +19,7 @@ jobs: MYSQL_DATABASE: closure_tree_test MYSQL_ROOT_HOST: '%' postgres: - image: 'postgres:13' + image: 'postgres' ports: ['5432:5432'] env: POSTGRES_PASSWORD: postgres @@ -34,41 +34,22 @@ jobs: fail-fast: false matrix: ruby: + - '3.2' - '3.1' - '3.0' - '2.7' - - truffleruby rails: - activerecord_7.0 - activerecord_6.1 - activerecord_6.0 - - activerecord_edge adapter: - 'sqlite3:///:memory:' - mysql2://root:root@0/closure_tree_test - postgres://closure_tree:closure_tree@0/closure_tree_test - include: - - ruby: jruby - rails: activerecord_6.0 - adapter: jdbcmysql://root:root@0/closure_tree_test - - ruby: jruby - rails: activerecord_6.0 - adapter: jdbcsqlite3:///:memory:' - - ruby: jruby - rails: activerecord_6.0 - adapter: jdbcpostgresql://closure_tree:closure_tree@0/closure_tree_test - - ruby: jruby - rails: activerecord_6.1 - adapter: jdbcmysql://root:root@0/with_advisory_lock_test - - ruby: jruby - rails: activerecord_6.1 - adapter: jdbcsqlite3:///tmp/test.sqlite3 - - ruby: jruby - rails: activerecord_6.1 - adapter: jdbcpostgresql://closure_tree:closure_tree@0/closure_tree_test + steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup Ruby uses: ruby/setup-ruby@v1 @@ -78,6 +59,7 @@ jobs: rubygems: latest env: BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile + RAILS_ENV: test - name: RSpec env: diff --git a/.gitignore b/.gitignore index a3fada8..2f6900c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ tmp/ .yardoc/ .rvmrc *.lock -tmp/ .ruby-* *.iml coverage/ diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..5de817e --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.0.5 diff --git a/Gemfile b/Gemfile index c56e6e7..4c07fb6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,12 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec - -gem "bump", "~> 0.10.0" -gem "github_changelog_generator", "~> 1.16" +platform :mri do + group :development do + gem 'bump', '~> 0.10.0' + gem 'github_changelog_generator', '~> 1.16' + end +end diff --git a/test/closure_tree/cuisine_type_test.rb b/test/closure_tree/cuisine_type_test.rb index 60fc537..7f2b6ba 100644 --- a/test/closure_tree/cuisine_type_test.rb +++ b/test/closure_tree/cuisine_type_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' def assert_lineage(e, m) @@ -10,27 +12,29 @@ def assert_lineage(e, m) end describe CuisineType do - it "finds self and parents when children << is used" do - e = CuisineType.new(:name => "e") - m = CuisineType.new(:name => "m") + it 'finds self and parents when children << is used' do + e = CuisineType.new(name: 'e') + m = CuisineType.new(name: 'm') e.children << m e.save assert_lineage(e, m) end - it "finds self and parents properly if the constructor is used" do - e = CuisineType.create(:name => "e") - m = CuisineType.create(:name => "m", :parent => e) + it 'finds self and parents properly if the constructor is used' do + e = CuisineType.create(name: 'e') + m = CuisineType.create(name: 'm', parent: e) assert_lineage(e, m) end - it "sets the table_name of the hierarchy class properly" do - assert_equal(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix, CuisineTypeHierarchy.table_name) + it 'sets the table_name of the hierarchy class properly' do + assert_equal( + "#{ActiveRecord::Base.table_name_prefix}cuisine_type_hierarchies#{ActiveRecord::Base.table_name_suffix}", CuisineTypeHierarchy.table_name + ) end it 'fixes self_and_ancestors properly on reparenting' do - a = CuisineType.create! :name => 'a' - b = CuisineType.create! :name => 'b' + a = CuisineType.create! name: 'a' + b = CuisineType.create! name: 'b' assert_equal([b], b.self_and_ancestors.to_a) a.children << b assert_equal([b, a], b.self_and_ancestors.to_a) diff --git a/test/closure_tree/generator_test.rb b/test/closure_tree/generator_test.rb new file mode 100644 index 0000000..558a143 --- /dev/null +++ b/test/closure_tree/generator_test.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'generators/closure_tree/migration_generator' + +module ClosureTree + module Generators + class MigrationGeneratorTest < Rails::Generators::TestCase + tests MigrationGenerator + destination File.expand_path('../tmp', __dir__) + setup :prepare_destination + + def test_generator_output + run_generator %w[tag] + migration_file = migration_file_name('db/migrate/create_tag_hierarchies.rb') + content = File.read(migration_file) + assert_match(/t.integer :ancestor_id, null: false/, content) + assert_match(/t.integer :descendant_id, null: false/, content) + assert_match(/t.integer :generations, null: false/, content) + assert_match(/add_index :tag_hierarchies/, content) + end + + def test_generator_output_with_namespaced_model + run_generator %w[Namespace::Type] + migration_file = migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') + content = File.read(migration_file) + assert_match(/t.integer :ancestor_id, null: false/, content) + assert_match(/t.integer :descendant_id, null: false/, content) + assert_match(/t.integer :generations, null: false/, content) + assert_match(/add_index :namespace_type_hierarchies/, content) + end + + def test_generator_output_with_namespaced_model_with_slash + run_generator %w[namespace/type] + migration_file = migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') + content = File.read(migration_file) + assert_match(/t.integer :ancestor_id, null: false/, content) + assert_match(/t.integer :descendant_id, null: false/, content) + assert_match(/t.integer :generations, null: false/, content) + assert_match(/add_index :namespace_type_hierarchies/, content) + end + + def test_should_run_all_tasks_in_generator_without_errors + gen = generator %w[tag] + capture_io { gen.invoke_all } + end + end + end +end diff --git a/test/closure_tree/hierarchy_maintenance_test.rb b/test/closure_tree/hierarchy_maintenance_test.rb index 60cf1ed..75dc0a3 100644 --- a/test/closure_tree/hierarchy_maintenance_test.rb +++ b/test/closure_tree/hierarchy_maintenance_test.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + require 'test_helper' describe ClosureTree::HierarchyMaintenance do describe '.rebuild!' do it 'rebuild tree' do 20.times do |counter| - Metal.create(:value => "Nitro-#{counter}", parent: Metal.all.sample) + Metal.create(value: "Nitro-#{counter}", parent: Metal.all.sample) end hierarchy_count = MetalHierarchy.count - assert_operator hierarchy_count, :>, (20*2)-1 # shallowest-possible case, where all children use the first root + assert_operator hierarchy_count, :>, (20 * 2) - 1 # shallowest-possible case, where all children use the first root MetalHierarchy.delete_all Metal.rebuild! assert_equal MetalHierarchy.count, hierarchy_count @@ -16,8 +18,8 @@ describe '.cleanup!' do before do - @parent = Metal.create(:value => "parent metal") - @child = Metal.create(:value => "child metal", parent: @parent) + @parent = Metal.create(value: 'parent metal') + @child = Metal.create(value: 'child metal', parent: @parent) MetalHierarchy.delete_all Metal.rebuild! end @@ -39,8 +41,8 @@ end it 'should not delete other hierarchies' do - other_parent = Metal.create(:value => "other parent metal") - other_child = Metal.create(:value => "other child metal", parent: other_parent) + other_parent = Metal.create(value: 'other parent metal') + other_child = Metal.create(value: 'other child metal', parent: other_parent) Metal.rebuild! @child.delete diff --git a/test/closure_tree/metal_test.rb b/test/closure_tree/metal_test.rb index 624c597..3a52adf 100644 --- a/test/closure_tree/metal_test.rb +++ b/test/closure_tree/metal_test.rb @@ -1,34 +1,38 @@ +# frozen_string_literal: true + require 'test_helper' describe Metal do describe '#find_or_create_by_path' do def assert_correctness(grandchild) assert(Metal, grandchild) - assert_equal "slag", grandchild.description + assert_equal 'slag', grandchild.description child = grandchild.parent assert(Unobtanium, child) - assert_equal "frames", child.description - assert_equal "child", child.value + assert_equal 'frames', child.description + assert_equal 'child', child.value parent = child.parent assert(Adamantium, parent) - assert_equal "claws", parent.description - assert_equal "parent", parent.value + assert_equal 'claws', parent.description + assert_equal 'parent', parent.value end let(:attr_path) do [ - {value: 'parent', description: 'claws', metal_type: 'Adamantium'}, - {value: 'child', description: 'frames', metal_type: 'Unobtanium'}, - {value: 'grandchild', description: 'slag', metal_type: 'Metal'} + { value: 'parent', description: 'claws', metal_type: 'Adamantium' }, + { value: 'child', description: 'frames', metal_type: 'Unobtanium' }, + { value: 'grandchild', description: 'slag', metal_type: 'Metal' } ] end - before do - # ensure the correct root is used in find_or_create_by_path: - [Metal, Adamantium, Unobtanium].each do |metal| - metal.find_or_create_by_path(%w(parent child grandchild)) + if false + before do + # ensure the correct root is used in find_or_create_by_path: + [Metal, Adamantium, Unobtanium].each do |metal| + metal.find_or_create_by_path(%w[parent child grandchild]) + end end - end if false + end it 'creates children from the proper root' do assert_correctness(Metal.find_or_create_by_path(attr_path)) @@ -44,9 +48,9 @@ def assert_correctness(grandchild) end it 'maintains the current STI subclass if attributes are not specified' do - leaf = Unobtanium.find_or_create_by_path(%w(a b c d)) + leaf = Unobtanium.find_or_create_by_path(%w[a b c d]) assert(Unobtanium, leaf) - assert_equal %w(c b a), leaf.ancestors.map(&:value) + assert_equal %w[c b a], leaf.ancestors.map(&:value) leaf.ancestors.each do |anc| assert(Unobtanium, anc) end diff --git a/test/closure_tree/model_test.rb b/test/closure_tree/model_test.rb index 77ca57d..f05783a 100644 --- a/test/closure_tree/model_test.rb +++ b/test/closure_tree/model_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' describe '#_ct' do diff --git a/test/closure_tree/namespace_type_test.rb b/test/closure_tree/namespace_type_test.rb index 72ed640..5f3d9f4 100644 --- a/test/closure_tree/namespace_type_test.rb +++ b/test/closure_tree/namespace_type_test.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'test_helper' describe Namespace::Type do - describe "class injection" do - it "should build hierarchy classname correctly" do - assert_equal "Namespace::TypeHierarchy", Namespace::Type.hierarchy_class.to_s - assert_equal "Namespace::TypeHierarchy", Namespace::Type._ct.hierarchy_class_name - assert_equal "TypeHierarchy", Namespace::Type._ct.short_hierarchy_class_name + describe 'class injection' do + it 'should build hierarchy classname correctly' do + assert_equal 'Namespace::TypeHierarchy', Namespace::Type.hierarchy_class.to_s + assert_equal 'Namespace::TypeHierarchy', Namespace::Type._ct.hierarchy_class_name + assert_equal 'TypeHierarchy', Namespace::Type._ct.short_hierarchy_class_name end end end diff --git a/test/closure_tree/pool_test.rb b/test/closure_tree/pool_test.rb index 1671664..db5dfc6 100644 --- a/test/closure_tree/pool_test.rb +++ b/test/closure_tree/pool_test.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require 'test_helper' -describe "Configuration" do +describe 'Configuration' do it 'returns connection to the pool after has_closure_tree setup' do class TypeDuplicate < ActiveRecord::Base self.table_name = "namespace_type#{table_name_suffix}" diff --git a/test/closure_tree/support_test.rb b/test/closure_tree/support_test.rb index 2b51b7b..3770d95 100644 --- a/test/closure_tree/support_test.rb +++ b/test/closure_tree/support_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' describe ClosureTree::Support do diff --git a/test/closure_tree/tag_test.rb b/test/closure_tree/tag_test.rb index db99bbd..e43252f 100644 --- a/test/closure_tree/tag_test.rb +++ b/test/closure_tree/tag_test.rb @@ -1,4 +1,6 @@ -require "test_helper" +# frozen_string_literal: true + +require 'test_helper' describe Tag do include TagExamples diff --git a/test/closure_tree/uuid_tag_test.rb b/test/closure_tree/uuid_tag_test.rb index 1a2f797..2ed9eef 100644 --- a/test/closure_tree/uuid_tag_test.rb +++ b/test/closure_tree/uuid_tag_test.rb @@ -1,4 +1,6 @@ -require "test_helper" +# frozen_string_literal: true + +require 'test_helper' describe UUIDTag do include TagExamples diff --git a/test/support/tag_examples.rb b/test/support/tag_examples.rb index 3986baa..03d7e54 100644 --- a/test/support/tag_examples.rb +++ b/test/support/tag_examples.rb @@ -1,118 +1,120 @@ +# frozen_string_literal: true + module TagExamples def self.included(mod) @@described_class = mod.name.safe_constantize end - describe "TagExamples" do + describe 'TagExamples' do before do @tag_class = @@described_class @tag_hierarchy_class = @@described_class.hierarchy_class end - describe "class setup" do - it "has correct accessible_attributes" do + describe 'class setup' do + it 'has correct accessible_attributes' do if @tag_class._ct.use_attr_accessible? assert_equal(%w[parent name title].sort, @tag_class.accessible_attributes.to_a.sort) end end - it "should build hierarchy classname correctly" do + it 'should build hierarchy classname correctly' do assert_equal @tag_hierarchy_class, @tag_class.hierarchy_class assert_equal @tag_hierarchy_class.to_s, @tag_class._ct.hierarchy_class_name assert_equal @tag_hierarchy_class.to_s, @tag_class._ct.short_hierarchy_class_name end - it "should have a correct parent column name" do - expected_parent_column_name = @tag_class == UUIDTag ? "parent_uuid" : "parent_id" + it 'should have a correct parent column name' do + expected_parent_column_name = @tag_class == UUIDTag ? 'parent_uuid' : 'parent_id' assert_equal expected_parent_column_name, @tag_class._ct.parent_column_name end end - describe "from empty db" do - describe "with no tags" do - it "should return no entities" do + describe 'from empty db' do + describe 'with no tags' do + it 'should return no entities' do assert_empty @tag_class.roots assert_empty @tag_class.leaves end - it "#find_or_create_by_path with strings" do - a = @tag_class.create!(name: "a") + it '#find_or_create_by_path with strings' do + a = @tag_class.create!(name: 'a') assert_equal(%w[a b c], a.find_or_create_by_path(%w[b c]).ancestry_path) end - it "#find_or_create_by_path with hashes" do - a = @tag_class.create!(name: "a", title: "A") + it '#find_or_create_by_path with hashes' do + a = @tag_class.create!(name: 'a', title: 'A') subject = a.find_or_create_by_path([ - {name: "b", title: "B"}, - {name: "c", title: "C"} - ]) + { name: 'b', title: 'B' }, + { name: 'c', title: 'C' } + ]) assert_equal(%w[a b c], subject.ancestry_path) assert_equal(%w[C B A], subject.self_and_ancestors.map(&:title)) end end - describe "with 1 tag" do + describe 'with 1 tag' do before do - @tag = @tag_class.create!(name: "tag") + @tag = @tag_class.create!(name: 'tag') end - it "should be a leaf" do + it 'should be a leaf' do assert @tag.leaf? end - it "should be a root" do + it 'should be a root' do assert @tag.root? end - it "has no parent" do + it 'has no parent' do assert_nil @tag.parent end - it "should return the only entity as a root and leaf" do + it 'should return the only entity as a root and leaf' do assert_equal [@tag], @tag_class.all assert_equal [@tag], @tag_class.roots assert_equal [@tag], @tag_class.leaves end - it "should not be found by passing find_by_path an array of blank strings" do - assert_nil @tag_class.find_by_path([""]) + it 'should not be found by passing find_by_path an array of blank strings' do + assert_nil @tag_class.find_by_path(['']) end - it "should not be found by passing find_by_path an empty array" do + it 'should not be found by passing find_by_path an empty array' do assert_nil @tag_class.find_by_path([]) end - it "should not be found by passing find_by_path nil" do + it 'should not be found by passing find_by_path nil' do assert_nil @tag_class.find_by_path(nil) end - it "should not be found by passing find_by_path an empty string" do - assert_nil @tag_class.find_by_path("") + it 'should not be found by passing find_by_path an empty string' do + assert_nil @tag_class.find_by_path('') end - it "should not be found by passing find_by_path an array of nils" do + it 'should not be found by passing find_by_path an array of nils' do assert_nil @tag_class.find_by_path([nil]) end - it "should not be found by passing find_by_path an array with an additional blank string" do - assert_nil @tag_class.find_by_path([@tag.name, ""]) + it 'should not be found by passing find_by_path an array with an additional blank string' do + assert_nil @tag_class.find_by_path([@tag.name, '']) end - it "should not be found by passing find_by_path an array with an additional nil" do + it 'should not be found by passing find_by_path an array with an additional nil' do assert_nil @tag_class.find_by_path([@tag.name, nil]) end - it "should be found by passing find_by_path an array with its name" do + it 'should be found by passing find_by_path an array with its name' do assert_equal @tag, @tag_class.find_by_path([@tag.name]) end - it "should be found by passing find_by_path its name" do + it 'should be found by passing find_by_path its name' do assert_equal @tag, @tag_class.find_by_path(@tag.name) end - describe "with child" do + describe 'with child' do before do - @child = @tag_class.create!(name: "tag 2") + @child = @tag_class.create!(name: 'tag 2') end def assert_roots_and_leaves @@ -128,13 +130,13 @@ def assert_parent_and_children assert_equal [@child], @tag.reload.children.to_a end - it "adds children through add_child" do + it 'adds children through add_child' do @tag.add_child @child assert_roots_and_leaves assert_parent_and_children end - it "adds children through collection" do + it 'adds children through collection' do @tag.children << @child assert_roots_and_leaves assert_parent_and_children @@ -142,80 +144,80 @@ def assert_parent_and_children end end - describe "with 2 tags" do + describe 'with 2 tags' do before do - @root = @tag_class.create!(name: "root") - @leaf = @root.add_child(@tag_class.create!(name: "leaf")) + @root = @tag_class.create!(name: 'root') + @leaf = @root.add_child(@tag_class.create!(name: 'leaf')) end - it "should return a simple root and leaf" do + it 'should return a simple root and leaf' do assert_equal [@root], @tag_class.roots assert_equal [@leaf], @tag_class.leaves end - it "should return child_ids for root" do + it 'should return child_ids for root' do assert_equal [@leaf.id], @root.child_ids end - it "should return an empty array for leaves" do + it 'should return an empty array for leaves' do assert_empty @leaf.child_ids end end - describe "3 tag collection.create db" do + describe '3 tag collection.create db' do before do - @root = @tag_class.create! name: "root" - @mid = @root.children.create! name: "mid" - @leaf = @mid.children.create! name: "leaf" + @root = @tag_class.create! name: 'root' + @mid = @root.children.create! name: 'mid' + @leaf = @mid.children.create! name: 'leaf' DestroyedTag.delete_all end - it "should create all tags" do + it 'should create all tags' do assert_equal [@root, @mid, @leaf].sort, @tag_class.all.to_a.sort end - it "should return a root and leaf without middle tag" do + it 'should return a root and leaf without middle tag' do assert_equal [@root], @tag_class.roots assert_equal [@leaf], @tag_class.leaves end - it "should delete leaves" do + it 'should delete leaves' do @tag_class.leaves.destroy_all assert_equal [@root], @tag_class.roots # untouched assert_equal [@mid], @tag_class.leaves end - it "should delete everything if you delete the roots" do + it 'should delete everything if you delete the roots' do @tag_class.roots.destroy_all assert_empty @tag_class.all assert_empty @tag_class.roots assert_empty @tag_class.leaves - assert_equal %w[root mid leaf].sort, DestroyedTag.all.map { |t| t.name }.sort + assert_equal %w[root mid leaf].sort, DestroyedTag.all.map(&:name).sort end - it "fix self_and_ancestors properly on reparenting" do - t = @tag_class.create! name: "moar leaf" + it 'fix self_and_ancestors properly on reparenting' do + t = @tag_class.create! name: 'moar leaf' assert_equal [t], t.self_and_ancestors.to_a @mid.children << t assert_equal [t, @mid, @root], t.self_and_ancestors.to_a end - it "prevents ancestor loops" do + it 'prevents ancestor loops' do @leaf.add_child @root refute @root.valid? assert_includes @root.reload.descendants, @leaf end - it "moves non-leaves" do - new_root = @tag_class.create! name: "new_root" + it 'moves non-leaves' do + new_root = @tag_class.create! name: 'new_root' new_root.children << @mid assert_empty @root.reload.descendants assert_equal [@mid, @leaf], new_root.descendants assert_equal %w[new_root mid leaf], @leaf.reload.ancestry_path end - it "moves leaves" do - new_root = @tag_class.create! name: "new_root" + it 'moves leaves' do + new_root = @tag_class.create! name: 'new_root' new_root.children << @leaf assert_equal [@leaf], new_root.descendants assert_equal [@mid], @root.reload.descendants @@ -223,46 +225,46 @@ def assert_parent_and_children end end - describe "3 tag explicit_create db" do + describe '3 tag explicit_create db' do before do - @root = @tag_class.create!(name: "root") - @mid = @root.add_child(@tag_class.create!(name: "mid")) - @leaf = @mid.add_child(@tag_class.create!(name: "leaf")) + @root = @tag_class.create!(name: 'root') + @mid = @root.add_child(@tag_class.create!(name: 'mid')) + @leaf = @mid.add_child(@tag_class.create!(name: 'leaf')) end - it "should create all tags" do + it 'should create all tags' do assert_equal [@root, @mid, @leaf].sort, @tag_class.all.to_a.sort end - it "should return a root and leaf without middle tag" do + it 'should return a root and leaf without middle tag' do assert_equal [@root], @tag_class.roots assert_equal [@leaf], @tag_class.leaves end - it "should prevent parental loops from torso" do + it 'should prevent parental loops from torso' do @mid.children << @root refute @root.valid? assert_equal [@leaf], @mid.reload.children end - it "should prevent parental loops from toes" do + it 'should prevent parental loops from toes' do @leaf.children << @root refute @root.valid? assert_empty @leaf.reload.children end - it "should support re-parenting" do + it 'should support re-parenting' do @root.children << @leaf assert_equal [@leaf, @mid], @tag_class.leaves end - it "cleans up hierarchy references for leaves" do + it 'cleans up hierarchy references for leaves' do @leaf.destroy assert_empty @tag_hierarchy_class.where(ancestor_id: @leaf.id) assert_empty @tag_hierarchy_class.where(descendant_id: @leaf.id) end - it "cleans up hierarchy references" do + it 'cleans up hierarchy references' do @mid.destroy assert_empty @tag_hierarchy_class.where(ancestor_id: @mid.id) assert_empty @tag_hierarchy_class.where(descendant_id: @mid.id) @@ -273,30 +275,30 @@ def assert_parent_and_children assert_equal root_hiers, @tag_hierarchy_class.where(descendant_id: @root.id) end - it "should have different hash codes for each hierarchy model" do + it 'should have different hash codes for each hierarchy model' do hashes = @tag_hierarchy_class.all.map(&:hash) assert_equal hashes.uniq.sort, hashes.sort end - it "should return the same hash code for equal hierarchy models" do + it 'should return the same hash code for equal hierarchy models' do assert_equal @tag_hierarchy_class.first.hash, @tag_hierarchy_class.first.hash end end - it "performs as the readme says it does" do - grandparent = @tag_class.create(name: "Grandparent") - parent = grandparent.children.create(name: "Parent") - child1 = @tag_class.create(name: "First Child", parent: parent) - child2 = @tag_class.new(name: "Second Child") + it 'performs as the readme says it does' do + grandparent = @tag_class.create(name: 'Grandparent') + parent = grandparent.children.create(name: 'Parent') + child1 = @tag_class.create(name: 'First Child', parent: parent) + child2 = @tag_class.new(name: 'Second Child') parent.children << child2 - child3 = @tag_class.new(name: "Third Child") + child3 = @tag_class.new(name: 'Third Child') parent.add_child child3 assert_equal( - ["Grandparent", "Parent", "First Child", "Second Child", "Third Child"], + ['Grandparent', 'Parent', 'First Child', 'Second Child', 'Third Child'], grandparent.self_and_descendants.collect(&:name) ) - assert_equal(["Grandparent", "Parent", "First Child"], child1.ancestry_path) - assert_equal(["Grandparent", "Parent", "Third Child"], child3.ancestry_path) + assert_equal(['Grandparent', 'Parent', 'First Child'], child1.ancestry_path) + assert_equal(['Grandparent', 'Parent', 'Third Child'], child3.ancestry_path) d = @tag_class.find_or_create_by_path %w[a b c d] h = @tag_class.find_or_create_by_path %w[e f g h] e = h.root @@ -304,13 +306,13 @@ def assert_parent_and_children assert_equal %w[a b c d e f g h], h.ancestry_path end - it "roots sort alphabetically" do - expected = ("a".."z").to_a + it 'roots sort alphabetically' do + expected = ('a'..'z').to_a expected.shuffle.each { |ea| @tag_class.create!(name: ea) } - assert_equal expected, @tag_class.roots.collect { |ea| ea.name } + assert_equal expected, @tag_class.roots.collect(&:name) end - describe "with simple tree" do + describe 'with simple tree' do before do @tag_class.find_or_create_by_path %w[a1 b1 c1a] @tag_class.find_or_create_by_path %w[a1 b1 c1b] @@ -326,30 +328,30 @@ def assert_parent_and_children @expected_only_children = @tag_class.all - @expected_siblings.flatten end - it "should find global roots" do + it 'should find global roots' do assert_equal @expected_roots.sort, @tag_class.roots.to_a.sort end - it "should return root? for roots" do + it 'should return root? for roots' do @expected_roots.each { |ea| assert(ea.root?) } end - it "should not return root? for non-roots" do + it 'should not return root? for non-roots' do [@b1, @b2, @c1a, @c1b].each { |ea| refute(ea.root?) } end - it "should return the correct root" do - {@a1 => @a1, @a2 => @a2, @a3 => @a3, - @b1 => @a1, @b2 => @a2, @c1a => @a1, @c1b => @a1}.each do |node, root| + it 'should return the correct root' do + { @a1 => @a1, @a2 => @a2, @a3 => @a3, + @b1 => @a1, @b2 => @a2, @c1a => @a1, @c1b => @a1 }.each do |node, root| assert_equal(root, node.root) end end - it "should assemble global leaves" do + it 'should assemble global leaves' do assert_equal @expected_leaves.sort, @tag_class.leaves.to_a.sort end - it "assembles siblings properly" do + it 'assembles siblings properly' do @expected_siblings.each do |siblings| siblings.each do |ea| assert_equal siblings.sort, ea.self_and_siblings.to_a.sort @@ -362,7 +364,7 @@ def assert_parent_and_children end end - it "assembles before_siblings" do + it 'assembles before_siblings' do @expected_siblings.each do |siblings| (siblings.size - 1).times do |i| target = siblings[i] @@ -372,7 +374,7 @@ def assert_parent_and_children end end - it "assembles after_siblings" do + it 'assembles after_siblings' do @expected_siblings.each do |siblings| (siblings.size - 1).times do |i| target = siblings[i] @@ -382,25 +384,25 @@ def assert_parent_and_children end end - it "should assemble instance leaves" do - {@a1 => [@b1b, @c1a, @c1b, @c1c], @b1 => [@c1a, @c1b, @c1c], @a2 => [@b2]}.each do |node, leaves| + it 'should assemble instance leaves' do + { @a1 => [@b1b, @c1a, @c1b, @c1c], @b1 => [@c1a, @c1b, @c1c], @a2 => [@b2] }.each do |node, leaves| assert_equal leaves, node.leaves.to_a end @expected_leaves.each { |ea| assert_equal [ea], ea.leaves.to_a } end - it "should return leaf? for leaves" do + it 'should return leaf? for leaves' do @expected_leaves.each { |ea| assert ea.leaf? } end - it "can move roots" do + it 'can move roots' do @c1a.children << @a2 @b2.reload.children << @a3 assert_equal %w[a1 b1 c1a a2 b2 a3], @a3.reload.ancestry_path end - it "cascade-deletes from roots" do + it 'cascade-deletes from roots' do victim_names = @a1.self_and_descendants.map(&:name) survivor_names = @tag_class.all.map(&:name) - victim_names @a1.destroy @@ -408,35 +410,35 @@ def assert_parent_and_children end end - describe "with_ancestor" do - it "works with no rows" do + describe 'with_ancestor' do + it 'works with no rows' do assert_empty @tag_class.with_ancestor.to_a end - it "finds only children" do + it 'finds only children' do c = @tag_class.find_or_create_by_path %w[A B C] a = c.parent.parent b = c.parent - spurious_tags = @tag_class.find_or_create_by_path %w[D E] + @tag_class.find_or_create_by_path %w[D E] assert_equal [b, c], @tag_class.with_ancestor(a).to_a end - it "limits subsequent where clauses" do + it 'limits subsequent where clauses' do a1c = @tag_class.find_or_create_by_path %w[A1 B C] a2c = @tag_class.find_or_create_by_path %w[A2 B C] # different paths! refute_equal a2c, a1c - assert_equal [a1c, a2c].sort, @tag_class.where(name: "C").to_a.sort - assert_equal [a1c], @tag_class.with_ancestor(a1c.parent.parent).where(name: "C").to_a.sort + assert_equal [a1c, a2c].sort, @tag_class.where(name: 'C').to_a.sort + assert_equal [a1c], @tag_class.with_ancestor(a1c.parent.parent).where(name: 'C').to_a.sort end end - describe "with_descendant" do - it "works with no rows" do + describe 'with_descendant' do + it 'works with no rows' do assert_empty @tag_class.with_descendant.to_a end - it "finds only parents" do + it 'finds only parents' do c = @tag_class.find_or_create_by_path %w[A B C] a = c.parent.parent b = c.parent @@ -444,9 +446,9 @@ def assert_parent_and_children assert_equal [a, b], @tag_class.with_descendant(c).to_a end - it "limits subsequent where clauses" do - ac1 = @tag_class.create(name: "A") - ac2 = @tag_class.create(name: "A") + it 'limits subsequent where clauses' do + ac1 = @tag_class.create(name: 'A') + ac2 = @tag_class.create(name: 'A') c1 = @tag_class.find_or_create_by_path %w[B C1] ac1.children << c1.parent @@ -456,26 +458,26 @@ def assert_parent_and_children # different paths! refute_equal ac2, ac1 - assert_equal [ac1, ac2].sort, @tag_class.where(name: "A").to_a.sort - assert_equal [ac1], @tag_class.with_descendant(c1).where(name: "A").to_a + assert_equal [ac1, ac2].sort, @tag_class.where(name: 'A').to_a.sort + assert_equal [ac1], @tag_class.with_descendant(c1).where(name: 'A').to_a end end - describe "lowest_common_ancestor" do + describe 'lowest_common_ancestor' do before do - @t1 = @tag_class.create!(name: "t1") - @t11 = @tag_class.create!(name: "t11", parent: @t1) - @t111 = @tag_class.create!(name: "t111", parent: @t11) - @t112 = @tag_class.create!(name: "t112", parent: @t11) - @t12 = @tag_class.create!(name: "t12", parent: @t1) - @t121 = @tag_class.create!(name: "t121", parent: @t12) - @t2 = @tag_class.create!(name: "t2") - @t21 = @tag_class.create!(name: "t21", parent: @t2) - @t21 = @tag_class.create!(name: "t21", parent: @t2) - @t211 = @tag_class.create!(name: "t211", parent: @t21) - end - - it "finds the parent for siblings" do + @t1 = @tag_class.create!(name: 't1') + @t11 = @tag_class.create!(name: 't11', parent: @t1) + @t111 = @tag_class.create!(name: 't111', parent: @t11) + @t112 = @tag_class.create!(name: 't112', parent: @t11) + @t12 = @tag_class.create!(name: 't12', parent: @t1) + @t121 = @tag_class.create!(name: 't121', parent: @t12) + @t2 = @tag_class.create!(name: 't2') + @t21 = @tag_class.create!(name: 't21', parent: @t2) + @t21 = @tag_class.create!(name: 't21', parent: @t2) + @t211 = @tag_class.create!(name: 't211', parent: @t21) + end + + it 'finds the parent for siblings' do assert_equal @t11, @tag_class.lowest_common_ancestor(@t112, @t111) assert_equal @t1, @tag_class.lowest_common_ancestor(@t12, @t11) @@ -486,19 +488,19 @@ def assert_parent_and_children assert_equal @t1, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t12 t11])) end - it "finds the grandparent for cousins" do + it 'finds the grandparent for cousins' do assert_equal @t1, @tag_class.lowest_common_ancestor(@t112, @t111, @t121) assert_equal @t1, @tag_class.lowest_common_ancestor([@t112, @t111, @t121]) assert_equal @t1, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t112 t111 t121])) end - it "finds the parent/grandparent for aunt-uncle/niece-nephew" do + it 'finds the parent/grandparent for aunt-uncle/niece-nephew' do assert_equal @t1, @tag_class.lowest_common_ancestor(@t12, @t112) assert_equal @t1, @tag_class.lowest_common_ancestor([@t12, @t112]) assert_equal @t1, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t12 t112])) end - it "finds the self/parent for parent/child" do + it 'finds the self/parent for parent/child' do assert_equal @t12, @tag_class.lowest_common_ancestor(@t12, @t121) assert_equal @t1, @tag_class.lowest_common_ancestor(@t1, @t12) @@ -509,7 +511,7 @@ def assert_parent_and_children assert_equal @t1, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t1 t12])) end - it "finds the self/grandparent for grandparent/grandchild" do + it 'finds the self/grandparent for grandparent/grandchild' do assert_equal @t2, @tag_class.lowest_common_ancestor(@t211, @t2) assert_equal @t1, @tag_class.lowest_common_ancestor(@t111, @t1) @@ -520,7 +522,7 @@ def assert_parent_and_children assert_equal @t1, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t111 t1])) end - it "finds the grandparent for a whole extended family" do + it 'finds the grandparent for a whole extended family' do assert_equal @t1, @tag_class.lowest_common_ancestor(@t1, @t11, @t111, @t112, @t12, @t121) assert_equal @t2, @tag_class.lowest_common_ancestor(@t2, @t21, @t211) @@ -528,58 +530,58 @@ def assert_parent_and_children assert_equal @t2, @tag_class.lowest_common_ancestor([@t2, @t21, @t211]) assert_equal @t1, - @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t1 t11 t111 t112 t12 t121])) + @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t1 t11 t111 t112 t12 t121])) assert_equal @t2, @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t2 t21 t211])) end - it "is nil for no items" do + it 'is nil for no items' do assert_nil @tag_class.lowest_common_ancestor assert_nil @tag_class.lowest_common_ancestor([]) assert_nil @tag_class.lowest_common_ancestor(@tag_class.none) end - it "is nil if there are no common ancestors" do + it 'is nil if there are no common ancestors' do assert_nil @tag_class.lowest_common_ancestor(@t111, @t211) assert_nil @tag_class.lowest_common_ancestor([@t111, @t211]) assert_nil @tag_class.lowest_common_ancestor(@tag_class.where(name: %w[t111 t211])) end - it "is itself for single item" do + it 'is itself for single item' do assert_equal @t111, @tag_class.lowest_common_ancestor(@t111) assert_equal @t2, @tag_class.lowest_common_ancestor(@t2) assert_equal @t111, @tag_class.lowest_common_ancestor([@t111]) assert_equal @t2, @tag_class.lowest_common_ancestor([@t2]) - assert_equal @t111, @tag_class.lowest_common_ancestor(@tag_class.where(name: "t111")) - assert_equal @t2, @tag_class.lowest_common_ancestor(@tag_class.where(name: "t2")) + assert_equal @t111, @tag_class.lowest_common_ancestor(@tag_class.where(name: 't111')) + assert_equal @t2, @tag_class.lowest_common_ancestor(@tag_class.where(name: 't2')) end end - describe "paths" do - describe "with grandchild " do + describe 'paths' do + describe 'with grandchild ' do before do @child = @tag_class.find_or_create_by_path([ - {name: "grandparent", title: "Nonnie"}, - {name: "parent", title: "Mom"}, - {name: "child", title: "Kid"} - ]) + { name: 'grandparent', title: 'Nonnie' }, + { name: 'parent', title: 'Mom' }, + { name: 'child', title: 'Kid' } + ]) @parent = @child.parent @grandparent = @parent.parent end - it "should build ancestry path" do + it 'should build ancestry path' do assert_equal %w[grandparent parent child], @child.ancestry_path assert_equal %w[grandparent parent child], @child.ancestry_path(:name) assert_equal %w[Nonnie Mom Kid], @child.ancestry_path(:title) end - it "assembles ancestors" do + it 'assembles ancestors' do assert_equal [@parent, @grandparent], @child.ancestors assert_equal [@child, @parent, @grandparent], @child.self_and_ancestors end - it "should find by path" do + it 'should find by path' do # class method: assert_equal @child, @tag_class.find_by_path(%w[grandparent parent child]) # instance method: @@ -588,9 +590,9 @@ def assert_parent_and_children assert_nil @parent.find_by_path(%w[child larvae]) end - it "should respect attribute hashes with both selection and creation" do - expected_title = "something else" - attrs = {title: expected_title} + it 'should respect attribute hashes with both selection and creation' do + expected_title = 'something else' + attrs = { title: expected_title } existing_title = @grandparent.title new_grandparent = @tag_class.find_or_create_by_path(%w[grandparent], attrs) refute_equal @grandparent, new_grandparent @@ -598,9 +600,9 @@ def assert_parent_and_children assert_equal existing_title, @grandparent.reload.title end - it "should create a hierarchy with a given attribute" do - expected_title = "unicorn rainbows" - attrs = {title: expected_title} + it 'should create a hierarchy with a given attribute' do + expected_title = 'unicorn rainbows' + attrs = { title: expected_title } child = @tag_class.find_or_create_by_path(%w[grandparent parent child], attrs) refute_equal @child, child [child, child.parent, child.parent.parent].each do |ea| @@ -609,20 +611,20 @@ def assert_parent_and_children end end - it "finds correctly rooted paths" do + it 'finds correctly rooted paths' do decoy = @tag_class.find_or_create_by_path %w[a b c d] b_d = @tag_class.find_or_create_by_path %w[b c d] assert_equal b_d, @tag_class.find_by_path(%w[b c d]) assert_nil @tag_class.find_by_path(%w[c d]) end - it "find_by_path for 1 node" do + it 'find_by_path for 1 node' do b = @tag_class.find_or_create_by_path %w[a b] b2 = b.root.find_by_path(%w[b]) assert_equal b, b2 end - it "find_by_path for 2 nodes" do + it 'find_by_path for 2 nodes' do path = %w[a b c] c = @tag_class.find_or_create_by_path path permutations = path.permutation.to_a @@ -633,38 +635,38 @@ def assert_parent_and_children end end - it "find_by_path for 3 nodes" do + it 'find_by_path for 3 nodes' do d = @tag_class.find_or_create_by_path %w[a b c d] assert_equal d, d.root.find_by_path(%w[b c d]) assert_equal d, @tag_class.find_by_path(%w[a b c d]) assert_nil @tag_class.find_by_path(%w[d]) end - it "should return nil for missing nodes" do + it 'should return nil for missing nodes' do assert_nil @tag_class.find_by_path(%w[missing]) assert_nil @tag_class.find_by_path(%w[grandparent missing]) assert_nil @tag_class.find_by_path(%w[grandparent parent missing]) assert_nil @tag_class.find_by_path(%w[grandparent parent missing child]) end - describe ".find_or_create_by_path" do - it "uses existing records" do + describe '.find_or_create_by_path' do + it 'uses existing records' do grandparent = @tag_class.find_or_create_by_path(%w[grandparent]) assert_equal grandparent, grandparent child = @tag_class.find_or_create_by_path(%w[grandparent parent child]) assert_equal child, child end - it "creates 2-deep trees with strings" do + it 'creates 2-deep trees with strings' do subject = @tag_class.find_or_create_by_path(%w[events anniversary]) assert_equal %w[events anniversary], subject.ancestry_path end - it "creates 2-deep trees with hashes" do + it 'creates 2-deep trees with hashes' do subject = @tag_class.find_or_create_by_path([ - {name: "test1", title: "TEST1"}, - {name: "test2", title: "TEST2"} - ]) + { name: 'test1', title: 'TEST1' }, + { name: 'test2', title: 'TEST2' } + ]) assert_equal %w[test1 test2], subject.ancestry_path # `self_and_ancestors` and `ancestors` is ordered parent-first. (!!) assert_equal %w[TEST2 TEST1], subject.self_and_ancestors.map(&:title) @@ -672,20 +674,20 @@ def assert_parent_and_children end end - describe "hash_tree" do + describe 'hash_tree' do before do @d1 = @tag_class.find_or_create_by_path %w[a b c1 d1] @c1 = @d1.parent @b = @c1.parent @a = @b.parent - @a2 = @tag_class.create(name: "a2") + @a2 = @tag_class.create(name: 'a2') @b2 = @tag_class.find_or_create_by_path %w[a b2] @c3 = @tag_class.find_or_create_by_path %w[a3 b3 c3] @b3 = @c3.parent @a3 = @b3.parent @tree2 = { - @a => {@b => {}, @b2 => {}}, @a2 => {}, @a3 => {@b3 => {}} + @a => { @b => {}, @b2 => {} }, @a2 => {}, @a3 => { @b3 => {} } } @one_tree = { @@ -738,100 +740,100 @@ def assert_parent_and_children } end - describe "#hash_tree" do - it "returns {} for depth 0" do + describe '#hash_tree' do + it 'returns {} for depth 0' do assert_equal({}, @tag_class.hash_tree(limit_depth: 0)) end - it "limit_depth 1" do + it 'limit_depth 1' do assert_equal @one_tree, @tag_class.hash_tree(limit_depth: 1) end - it "limit_depth 2" do + it 'limit_depth 2' do assert_equal @two_tree, @tag_class.hash_tree(limit_depth: 2) end - it "limit_depth 3" do + it 'limit_depth 3' do assert_equal @three_tree, @tag_class.hash_tree(limit_depth: 3) end - it "limit_depth 4" do + it 'limit_depth 4' do assert_equal @full_tree, @tag_class.hash_tree(limit_depth: 4) end - it "no limit" do + it 'no limit' do assert_equal @full_tree, @tag_class.hash_tree end end - describe ".hash_tree" do - it "returns {} for depth 0" do + describe '.hash_tree' do + it 'returns {} for depth 0' do assert_equal({}, @b.hash_tree(limit_depth: 0)) end - it "limit_depth 1" do + it 'limit_depth 1' do assert_equal @two_tree[@a].slice(@b), @b.hash_tree(limit_depth: 1) end - it "limit_depth 2" do + it 'limit_depth 2' do assert_equal @three_tree[@a].slice(@b), @b.hash_tree(limit_depth: 2) end - it "limit_depth 3" do + it 'limit_depth 3' do assert_equal @full_tree[@a].slice(@b), @b.hash_tree(limit_depth: 3) end - it "no limit from subsubroot" do + it 'no limit from subsubroot' do assert_equal @full_tree[@a][@b].slice(@c1), @c1.hash_tree end - it "no limit from subroot" do + it 'no limit from subroot' do assert_equal @full_tree[@a].slice(@b), @b.hash_tree end - it "no limit from root" do + it 'no limit from root' do assert_equal @full_tree.slice(@a, @a2), @a.hash_tree.merge(@a2.hash_tree) end end - describe ".hash_tree from relations" do - it "limit_depth 2 from chained activerecord association subroots" do + describe '.hash_tree from relations' do + it 'limit_depth 2 from chained activerecord association subroots' do assert_equal @three_tree[@a], @a.children.hash_tree(limit_depth: 2) end - it "no limit from chained activerecord association subroots" do + it 'no limit from chained activerecord association subroots' do assert_equal @full_tree[@a], @a.children.hash_tree end - it "limit_depth 3 from b.parent" do + it 'limit_depth 3 from b.parent' do assert_equal @three_tree.slice(@a), @b.parent.hash_tree(limit_depth: 3) end - it "no limit_depth from b.parent" do + it 'no limit_depth from b.parent' do assert_equal @full_tree.slice(@a), @b.parent.hash_tree end - it "no limit_depth from c.parent" do + it 'no limit_depth from c.parent' do assert_equal @full_tree[@a].slice(@b), @c1.parent.hash_tree end end end - it "finds_by_path for very deep trees" do - path = (1..20).to_a.map { |ea| ea.to_s } + it 'finds_by_path for very deep trees' do + path = (1..20).to_a.map(&:to_s) subject = @tag_class.find_or_create_by_path(path) assert_equal path, subject.ancestry_path assert_equal subject, @tag_class.find_by_path(path) root = subject.root - assert_equal subject, root.find_by_path(path[1..-1]) + assert_equal subject, root.find_by_path(path[1..]) end - describe "DOT rendering" do - it "should render for an empty scope" do - assert_equal "digraph G {\n}\n", @tag_class.to_dot_digraph(@tag_class.where("0=1")) + describe 'DOT rendering' do + it 'should render for an empty scope' do + assert_equal "digraph G {\n}\n", @tag_class.to_dot_digraph(@tag_class.where('0=1')) end - it "should render for an empty scope" do + it 'should render for an empty scope' do @tag_class.find_or_create_by_path(%w[a b1 c1]) @tag_class.find_or_create_by_path(%w[a b2 c2]) @tag_class.find_or_create_by_path(%w[a b2 c3]) diff --git a/test/test_helper.rb b/test/test_helper.rb index c70dd25..cf87ccf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,34 +1,36 @@ -require "erb" -require "active_record" -require "with_advisory_lock" -require "tmpdir" -require "securerandom" -require "minitest" -require "minitest/autorun" -require "database_cleaner" +# frozen_string_literal: true + +require 'erb' +require 'active_record' +require 'with_advisory_lock' +require 'tmpdir' +require 'securerandom' +require 'minitest' +require 'minitest/autorun' +require 'database_cleaner' ActiveRecord::Base.configurations = { default_env: { - url: ENV.fetch("DATABASE_URL", "sqlite3://#{Dir.tmpdir}/#{SecureRandom.hex}.sqlite3"), - properties: {allowPublicKeyRetrieval: true} # for JRuby madness + url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/#{SecureRandom.hex}.sqlite3"), + properties: { allowPublicKeyRetrieval: true } # for JRuby madness } } -ENV["WITH_ADVISORY_LOCK_PREFIX"] ||= SecureRandom.hex +ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex ActiveRecord::Base.establish_connection def env_db @env_db ||= if ActiveRecord::Base.respond_to?(:connection_db_config) - ActiveRecord::Base.connection_db_config.adapter - else - ActiveRecord::Base.connection_config[:adapter] - end.to_sym + ActiveRecord::Base.connection_db_config.adapter + else + ActiveRecord::Base.connection_config[:adapter] + end.to_sym end ActiveRecord::Migration.verbose = false -ActiveRecord::Base.table_name_prefix = ENV["DB_PREFIX"].to_s -ActiveRecord::Base.table_name_suffix = ENV["DB_SUFFIX"].to_s +ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s +ActiveRecord::Base.table_name_suffix = ENV['DB_SUFFIX'].to_s ActiveRecord::Base.establish_connection # Use in specs to skip some tests @@ -36,26 +38,28 @@ def sqlite? env_db == :sqlite3 end -ENV["WITH_ADVISORY_LOCK_PREFIX"] ||= SecureRandom.hex +ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex -ActiveRecord::Base.connection.recreate_database("closure_tree_test") unless sqlite? +ActiveRecord::Base.connection.recreate_database('closure_tree_test') unless sqlite? puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}" DatabaseCleaner.strategy = :transaction -class MiniTest::Spec - before :each do - ENV["FLOCK_DIR"] = Dir.mktmpdir - DatabaseCleaner.start - end +module MiniTest + class Spec + before :each do + ENV['FLOCK_DIR'] = Dir.mktmpdir + DatabaseCleaner.start + end - after :each do - FileUtils.remove_entry_secure ENV["FLOCK_DIR"] - DatabaseCleaner.clean + after :each do + FileUtils.remove_entry_secure ENV['FLOCK_DIR'] + DatabaseCleaner.clean + end end end -require "closure_tree" -require_relative "../spec/support/schema" -require_relative "../spec/support/models" -require "support/tag_examples" +require 'closure_tree' +require_relative '../spec/support/schema' +require_relative '../spec/support/models' +require 'support/tag_examples'