Skip to content

Commit a639234

Browse files
authored
chore: Migrate Generator tests to Minitest (#411)
Add tests for ruby 3.2
1 parent 8550dda commit a639234

16 files changed

+361
-297
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ jobs:
1111
runs-on: ubuntu-latest
1212
services:
1313
mysql:
14-
image: mysql/mysql-server:8.0.30
14+
image: mysql/mysql-server
1515
ports:
1616
- "3306:3306"
1717
env:
1818
MYSQL_ROOT_PASSWORD: root
1919
MYSQL_DATABASE: closure_tree_test
2020
MYSQL_ROOT_HOST: '%'
2121
postgres:
22-
image: 'postgres:13'
22+
image: 'postgres'
2323
ports: ['5432:5432']
2424
env:
2525
POSTGRES_PASSWORD: postgres
@@ -34,41 +34,22 @@ jobs:
3434
fail-fast: false
3535
matrix:
3636
ruby:
37+
- '3.2'
3738
- '3.1'
3839
- '3.0'
3940
- '2.7'
40-
- truffleruby
4141
rails:
4242
- activerecord_7.0
4343
- activerecord_6.1
4444
- activerecord_6.0
45-
- activerecord_edge
4645
adapter:
4746
- 'sqlite3:///:memory:'
4847
- mysql2://root:root@0/closure_tree_test
4948
- postgres://closure_tree:closure_tree@0/closure_tree_test
50-
include:
51-
- ruby: jruby
52-
rails: activerecord_6.0
53-
adapter: jdbcmysql://root:root@0/closure_tree_test
54-
- ruby: jruby
55-
rails: activerecord_6.0
56-
adapter: jdbcsqlite3:///:memory:'
57-
- ruby: jruby
58-
rails: activerecord_6.0
59-
adapter: jdbcpostgresql://closure_tree:closure_tree@0/closure_tree_test
60-
- ruby: jruby
61-
rails: activerecord_6.1
62-
adapter: jdbcmysql://root:root@0/with_advisory_lock_test
63-
- ruby: jruby
64-
rails: activerecord_6.1
65-
adapter: jdbcsqlite3:///tmp/test.sqlite3
66-
- ruby: jruby
67-
rails: activerecord_6.1
68-
adapter: jdbcpostgresql://closure_tree:closure_tree@0/closure_tree_test
49+
6950
steps:
7051
- name: Checkout
71-
uses: actions/checkout@v2
52+
uses: actions/checkout@v3
7253

7354
- name: Setup Ruby
7455
uses: ruby/setup-ruby@v1
@@ -78,6 +59,7 @@ jobs:
7859
rubygems: latest
7960
env:
8061
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
62+
RAILS_ENV: test
8163

8264
- name: RSpec
8365
env:

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ tmp/
1111
.yardoc/
1212
.rvmrc
1313
*.lock
14-
tmp/
1514
.ruby-*
1615
*.iml
1716
coverage/

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby 3.0.5

Gemfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
gemspec
46

5-
6-
gem "bump", "~> 0.10.0"
7-
gem "github_changelog_generator", "~> 1.16"
7+
platform :mri do
8+
group :development do
9+
gem 'bump', '~> 0.10.0'
10+
gem 'github_changelog_generator', '~> 1.16'
11+
end
12+
end

test/closure_tree/cuisine_type_test.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
def assert_lineage(e, m)
@@ -10,27 +12,29 @@ def assert_lineage(e, m)
1012
end
1113

1214
describe CuisineType do
13-
it "finds self and parents when children << is used" do
14-
e = CuisineType.new(:name => "e")
15-
m = CuisineType.new(:name => "m")
15+
it 'finds self and parents when children << is used' do
16+
e = CuisineType.new(name: 'e')
17+
m = CuisineType.new(name: 'm')
1618
e.children << m
1719
e.save
1820
assert_lineage(e, m)
1921
end
2022

21-
it "finds self and parents properly if the constructor is used" do
22-
e = CuisineType.create(:name => "e")
23-
m = CuisineType.create(:name => "m", :parent => e)
23+
it 'finds self and parents properly if the constructor is used' do
24+
e = CuisineType.create(name: 'e')
25+
m = CuisineType.create(name: 'm', parent: e)
2426
assert_lineage(e, m)
2527
end
2628

27-
it "sets the table_name of the hierarchy class properly" do
28-
assert_equal(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix, CuisineTypeHierarchy.table_name)
29+
it 'sets the table_name of the hierarchy class properly' do
30+
assert_equal(
31+
"#{ActiveRecord::Base.table_name_prefix}cuisine_type_hierarchies#{ActiveRecord::Base.table_name_suffix}", CuisineTypeHierarchy.table_name
32+
)
2933
end
3034

3135
it 'fixes self_and_ancestors properly on reparenting' do
32-
a = CuisineType.create! :name => 'a'
33-
b = CuisineType.create! :name => 'b'
36+
a = CuisineType.create! name: 'a'
37+
b = CuisineType.create! name: 'b'
3438
assert_equal([b], b.self_and_ancestors.to_a)
3539
a.children << b
3640
assert_equal([b, a], b.self_and_ancestors.to_a)

test/closure_tree/generator_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require 'generators/closure_tree/migration_generator'
5+
6+
module ClosureTree
7+
module Generators
8+
class MigrationGeneratorTest < Rails::Generators::TestCase
9+
tests MigrationGenerator
10+
destination File.expand_path('../tmp', __dir__)
11+
setup :prepare_destination
12+
13+
def test_generator_output
14+
run_generator %w[tag]
15+
migration_file = migration_file_name('db/migrate/create_tag_hierarchies.rb')
16+
content = File.read(migration_file)
17+
assert_match(/t.integer :ancestor_id, null: false/, content)
18+
assert_match(/t.integer :descendant_id, null: false/, content)
19+
assert_match(/t.integer :generations, null: false/, content)
20+
assert_match(/add_index :tag_hierarchies/, content)
21+
end
22+
23+
def test_generator_output_with_namespaced_model
24+
run_generator %w[Namespace::Type]
25+
migration_file = migration_file_name('db/migrate/create_namespace_type_hierarchies.rb')
26+
content = File.read(migration_file)
27+
assert_match(/t.integer :ancestor_id, null: false/, content)
28+
assert_match(/t.integer :descendant_id, null: false/, content)
29+
assert_match(/t.integer :generations, null: false/, content)
30+
assert_match(/add_index :namespace_type_hierarchies/, content)
31+
end
32+
33+
def test_generator_output_with_namespaced_model_with_slash
34+
run_generator %w[namespace/type]
35+
migration_file = migration_file_name('db/migrate/create_namespace_type_hierarchies.rb')
36+
content = File.read(migration_file)
37+
assert_match(/t.integer :ancestor_id, null: false/, content)
38+
assert_match(/t.integer :descendant_id, null: false/, content)
39+
assert_match(/t.integer :generations, null: false/, content)
40+
assert_match(/add_index :namespace_type_hierarchies/, content)
41+
end
42+
43+
def test_should_run_all_tasks_in_generator_without_errors
44+
gen = generator %w[tag]
45+
capture_io { gen.invoke_all }
46+
end
47+
end
48+
end
49+
end

test/closure_tree/hierarchy_maintenance_test.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
describe ClosureTree::HierarchyMaintenance do
46
describe '.rebuild!' do
57
it 'rebuild tree' do
68
20.times do |counter|
7-
Metal.create(:value => "Nitro-#{counter}", parent: Metal.all.sample)
9+
Metal.create(value: "Nitro-#{counter}", parent: Metal.all.sample)
810
end
911
hierarchy_count = MetalHierarchy.count
10-
assert_operator hierarchy_count, :>, (20*2)-1 # shallowest-possible case, where all children use the first root
12+
assert_operator hierarchy_count, :>, (20 * 2) - 1 # shallowest-possible case, where all children use the first root
1113
MetalHierarchy.delete_all
1214
Metal.rebuild!
1315
assert_equal MetalHierarchy.count, hierarchy_count
@@ -16,8 +18,8 @@
1618

1719
describe '.cleanup!' do
1820
before do
19-
@parent = Metal.create(:value => "parent metal")
20-
@child = Metal.create(:value => "child metal", parent: @parent)
21+
@parent = Metal.create(value: 'parent metal')
22+
@child = Metal.create(value: 'child metal', parent: @parent)
2123
MetalHierarchy.delete_all
2224
Metal.rebuild!
2325
end
@@ -39,8 +41,8 @@
3941
end
4042

4143
it 'should not delete other hierarchies' do
42-
other_parent = Metal.create(:value => "other parent metal")
43-
other_child = Metal.create(:value => "other child metal", parent: other_parent)
44+
other_parent = Metal.create(value: 'other parent metal')
45+
other_child = Metal.create(value: 'other child metal', parent: other_parent)
4446
Metal.rebuild!
4547

4648
@child.delete

test/closure_tree/metal_test.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
describe Metal do
46
describe '#find_or_create_by_path' do
57
def assert_correctness(grandchild)
68
assert(Metal, grandchild)
7-
assert_equal "slag", grandchild.description
9+
assert_equal 'slag', grandchild.description
810
child = grandchild.parent
911
assert(Unobtanium, child)
10-
assert_equal "frames", child.description
11-
assert_equal "child", child.value
12+
assert_equal 'frames', child.description
13+
assert_equal 'child', child.value
1214
parent = child.parent
1315
assert(Adamantium, parent)
14-
assert_equal "claws", parent.description
15-
assert_equal "parent", parent.value
16+
assert_equal 'claws', parent.description
17+
assert_equal 'parent', parent.value
1618
end
1719

1820
let(:attr_path) do
1921
[
20-
{value: 'parent', description: 'claws', metal_type: 'Adamantium'},
21-
{value: 'child', description: 'frames', metal_type: 'Unobtanium'},
22-
{value: 'grandchild', description: 'slag', metal_type: 'Metal'}
22+
{ value: 'parent', description: 'claws', metal_type: 'Adamantium' },
23+
{ value: 'child', description: 'frames', metal_type: 'Unobtanium' },
24+
{ value: 'grandchild', description: 'slag', metal_type: 'Metal' }
2325
]
2426
end
2527

26-
before do
27-
# ensure the correct root is used in find_or_create_by_path:
28-
[Metal, Adamantium, Unobtanium].each do |metal|
29-
metal.find_or_create_by_path(%w(parent child grandchild))
28+
if false
29+
before do
30+
# ensure the correct root is used in find_or_create_by_path:
31+
[Metal, Adamantium, Unobtanium].each do |metal|
32+
metal.find_or_create_by_path(%w[parent child grandchild])
33+
end
3034
end
31-
end if false
35+
end
3236

3337
it 'creates children from the proper root' do
3438
assert_correctness(Metal.find_or_create_by_path(attr_path))
@@ -44,9 +48,9 @@ def assert_correctness(grandchild)
4448
end
4549

4650
it 'maintains the current STI subclass if attributes are not specified' do
47-
leaf = Unobtanium.find_or_create_by_path(%w(a b c d))
51+
leaf = Unobtanium.find_or_create_by_path(%w[a b c d])
4852
assert(Unobtanium, leaf)
49-
assert_equal %w(c b a), leaf.ancestors.map(&:value)
53+
assert_equal %w[c b a], leaf.ancestors.map(&:value)
5054
leaf.ancestors.each do |anc|
5155
assert(Unobtanium, anc)
5256
end

test/closure_tree/model_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
describe '#_ct' do
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
describe Namespace::Type do
4-
describe "class injection" do
5-
it "should build hierarchy classname correctly" do
6-
assert_equal "Namespace::TypeHierarchy", Namespace::Type.hierarchy_class.to_s
7-
assert_equal "Namespace::TypeHierarchy", Namespace::Type._ct.hierarchy_class_name
8-
assert_equal "TypeHierarchy", Namespace::Type._ct.short_hierarchy_class_name
6+
describe 'class injection' do
7+
it 'should build hierarchy classname correctly' do
8+
assert_equal 'Namespace::TypeHierarchy', Namespace::Type.hierarchy_class.to_s
9+
assert_equal 'Namespace::TypeHierarchy', Namespace::Type._ct.hierarchy_class_name
10+
assert_equal 'TypeHierarchy', Namespace::Type._ct.short_hierarchy_class_name
911
end
1012
end
1113
end

test/closure_tree/pool_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

3-
describe "Configuration" do
5+
describe 'Configuration' do
46
it 'returns connection to the pool after has_closure_tree setup' do
57
class TypeDuplicate < ActiveRecord::Base
68
self.table_name = "namespace_type#{table_name_suffix}"

test/closure_tree/support_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'test_helper'
24

35
describe ClosureTree::Support do

test/closure_tree/tag_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require "test_helper"
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
24

35
describe Tag do
46
include TagExamples

test/closure_tree/uuid_tag_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require "test_helper"
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
24

35
describe UUIDTag do
46
include TagExamples

0 commit comments

Comments
 (0)