Skip to content

chore: Migrate Generator tests to Minitest #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
runs-on: ubuntu-latest
services:
mysql:
image: mysql/mysql-server:8.0.30
image: mysql/mysql-server
ports:
- "3306:3306"
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: closure_tree_test
MYSQL_ROOT_HOST: '%'
postgres:
image: 'postgres:13'
image: 'postgres'
ports: ['5432:5432']
env:
POSTGRES_PASSWORD: postgres
Expand All @@ -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
Expand All @@ -78,6 +59,7 @@ jobs:
rubygems: latest
env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
RAILS_ENV: test

- name: RSpec
env:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ tmp/
.yardoc/
.rvmrc
*.lock
tmp/
.ruby-*
*.iml
coverage/
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.0.5
11 changes: 8 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
24 changes: 14 additions & 10 deletions test/closure_tree/cuisine_type_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test_helper'

def assert_lineage(e, m)
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions test/closure_tree/generator_test.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 8 additions & 6 deletions test/closure_tree/hierarchy_maintenance_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
34 changes: 19 additions & 15 deletions test/closure_tree/metal_test.rb
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/closure_tree/model_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test_helper'

describe '#_ct' do
Expand Down
12 changes: 7 additions & 5 deletions test/closure_tree/namespace_type_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion test/closure_tree/pool_test.rb
Original file line number Diff line number Diff line change
@@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions test/closure_tree/support_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test_helper'

describe ClosureTree::Support do
Expand Down
4 changes: 3 additions & 1 deletion test/closure_tree/tag_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "test_helper"
# frozen_string_literal: true

require 'test_helper'

describe Tag do
include TagExamples
Expand Down
4 changes: 3 additions & 1 deletion test/closure_tree/uuid_tag_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "test_helper"
# frozen_string_literal: true

require 'test_helper'

describe UUIDTag do
include TagExamples
Expand Down
Loading