Skip to content

Commit 5b83cff

Browse files
committed
test: is_activeカラム削除に伴うテストコードの修正
- Factory: is_activeを削除し、inactivated_atとinactiveトレイトを追加 - spec/models/stat_spec.rb: is_active参照を削除 - spec/requests/dojos_spec.rb: is_active参照をinactivated_atに変更 - spec/lib/tasks/dojos_spec.rb: is_activeテストをinactivated_atテストに置き換え - spec/models/dojo_spec.rb: is_active関連のテストを削除・簡素化 これにより全158個のテストが成功することを確認
1 parent 71b502d commit 5b83cff

File tree

5 files changed

+34
-74
lines changed

5 files changed

+34
-74
lines changed

spec/factories/dojos.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
url { 'https://example.com' }
99
logo { '/img/dojos/default.webp' }
1010
counter { 1 }
11-
is_active { true }
1211
order { 131001 }
12+
# デフォルトはアクティブ(inactivated_at: nil)
13+
inactivated_at { nil }
14+
15+
# 非アクティブなDojoを作成するためのtrait
16+
trait :inactive do
17+
inactivated_at { Time.current }
18+
end
1319
end
1420
end

spec/lib/tasks/dojos_spec.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
expect(new_records.first.url).to eq('http://tinkerkids.jp/coderdojo.html')
5555
expect(new_records.first.description).to eq('毎月開催')
5656
expect(new_records.first.tags).to eq(%w(Scratch ラズベリーパイ Python JavaScript))
57-
expect(new_records.first.is_active).to eq(true)
57+
expect(new_records.first.active?).to eq(true)
5858
expect(new_records.first.is_private).to eq(false)
5959
end
6060

@@ -75,65 +75,69 @@
7575
expect(mod_record.name).to eq('dojo_1(mod)')
7676
end
7777

78-
context 'is_active' do
78+
context 'inactivated_at (活性状態管理)' do
7979
let(:dojo_base) do
8080
@dojo_2.attributes.keep_if { |k,v| %w(id order name prefecture_id logo url description tags).include?(k) }
8181
end
8282

83-
it '指定なし ⇒ アクティブ' do
83+
it 'inactivated_at 指定なし ⇒ アクティブ' do
8484
allow(YAML).to receive(:unsafe_load_file).and_return([
8585
dojo_base
8686
])
8787

8888
# before
89-
@dojo_2.update_columns(is_active: false)
89+
@dojo_2.update_columns(inactivated_at: Time.current)
9090
expect(Dojo.count).to eq(3)
91-
expect(@dojo_2.is_active).to eq(false)
91+
expect(@dojo_2.reload.active?).to eq(false)
9292

9393
# exec
9494
expect(@rake[task].invoke).to be_truthy
9595

9696
# after
9797
expect(Dojo.count).to eq(3)
9898
mod_record = Dojo.find_by(id: @dojo_2.id)
99-
expect(mod_record.is_active).to eq(true)
99+
expect(mod_record.active?).to eq(true)
100+
expect(mod_record.inactivated_at).to be_nil
100101
end
101102

102-
it 'true 指定 ⇒ アクティブ' do
103+
it 'inactivated_at: nil 指定 ⇒ アクティブ' do
103104
allow(YAML).to receive(:unsafe_load_file).and_return([
104-
dojo_base.merge('is_active' => true)
105+
dojo_base.merge('inactivated_at' => nil)
105106
])
106107

107108
# before
108-
@dojo_2.update_columns(is_active: false)
109+
@dojo_2.update_columns(inactivated_at: Time.current)
109110
expect(Dojo.count).to eq(3)
110-
expect(@dojo_2.is_active).to eq(false)
111+
expect(@dojo_2.reload.active?).to eq(false)
111112

112113
# exec
113114
expect(@rake[task].invoke).to be_truthy
114115

115116
# after
116117
expect(Dojo.count).to eq(3)
117118
mod_record = Dojo.find_by(id: @dojo_2.id)
118-
expect(mod_record.is_active).to eq(true)
119+
expect(mod_record.active?).to eq(true)
120+
expect(mod_record.inactivated_at).to be_nil
119121
end
120122

121-
it 'false 指定 ⇒ 非アクティブ' do
123+
it 'inactivated_at に日付指定 ⇒ 非アクティブ' do
124+
inactivation_date = '2023-01-15'
122125
allow(YAML).to receive(:unsafe_load_file).and_return([
123-
dojo_base.merge('is_active' => false)
126+
dojo_base.merge('inactivated_at' => inactivation_date)
124127
])
125128

126129
# before
127130
expect(Dojo.count).to eq(3)
128-
expect(@dojo_2.is_active).to eq(true)
131+
expect(@dojo_2.active?).to eq(true)
129132

130133
# exec
131134
expect(@rake[task].invoke).to be_truthy
132135

133136
# after
134137
expect(Dojo.count).to eq(3)
135138
mod_record = Dojo.find_by(id: @dojo_2.id)
136-
expect(mod_record.is_active).to eq(false)
139+
expect(mod_record.active?).to eq(false)
140+
expect(mod_record.inactivated_at).to eq(Time.zone.parse(inactivation_date))
137141
end
138142
end
139143

spec/models/dojo_spec.rb

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,12 @@
8686
end
8787
end
8888

89-
describe 'validate inactivated_at for inactive dojos' do
90-
it 'ensures all inactive dojos in YAML have inactivated_at date' do
89+
describe 'validate inactivated_at dates' do
90+
it 'verifies inactivated_at dates are valid when present' do
9191
yaml_data = Dojo.load_attributes_from_yaml
92-
inactive_dojos = yaml_data.select { |dojo| dojo['is_active'] == false }
93-
94-
missing_dates = inactive_dojos.select { |dojo| dojo['inactivated_at'].nil? }
95-
96-
if missing_dates.any?
97-
missing_info = missing_dates.map { |d| "ID: #{d['id']} (#{d['name']})" }.join(", ")
98-
fail "以下の非アクティブDojoにinactivated_atが設定されていません: #{missing_info}"
99-
end
100-
101-
expect(inactive_dojos.all? { |dojo| dojo['inactivated_at'].present? }).to be true
102-
end
103-
104-
it 'verifies inactivated_at dates are valid' do
105-
yaml_data = Dojo.load_attributes_from_yaml
106-
inactive_dojos = yaml_data.select { |dojo| dojo['is_active'] == false }
92+
dojos_with_inactivated_at = yaml_data.select { |dojo| dojo['inactivated_at'].present? }
10793

108-
inactive_dojos.each do |dojo|
109-
next if dojo['inactivated_at'].nil?
110-
94+
dojos_with_inactivated_at.each do |dojo|
11195
# 日付が正しくパースできることを確認
11296
expect {
11397
Time.zone.parse(dojo['inactivated_at'])
@@ -124,32 +108,6 @@
124108
end
125109
end
126110
end
127-
128-
it 'ensures all dojos with inactivated_at have is_active column' do
129-
yaml_data = Dojo.load_attributes_from_yaml
130-
dojos_with_inactivated_at = yaml_data.select { |dojo| dojo['inactivated_at'].present? }
131-
132-
dojos_with_inactivated_at.each do |dojo|
133-
# inactivated_atがあるDojoは必ずis_activeカラムを持つべき
134-
# (再活性化されたDojoはis_active: trueの可能性があるため、値は問わない)
135-
unless dojo.key?('is_active')
136-
fail "ID: #{dojo['id']} (#{dojo['name']}) はinactivated_atを持っていますが、is_activeカラムがありません"
137-
end
138-
end
139-
140-
# 統計情報として表示
141-
if dojos_with_inactivated_at.any?
142-
reactivated_count = dojos_with_inactivated_at.count { |d| d['is_active'] == true }
143-
inactive_count = dojos_with_inactivated_at.count { |d| d['is_active'] == false }
144-
145-
# テスト出力には表示されないが、デバッグ時に有用
146-
# puts "inactivated_atを持つDojo数: #{dojos_with_inactivated_at.count}"
147-
# puts " - 現在非アクティブ: #{inactive_count}"
148-
# puts " - 再活性化済み: #{reactivated_count}"
149-
150-
expect(dojos_with_inactivated_at.count).to eq(inactive_count + reactivated_count)
151-
end
152-
end
153111
end
154112

155113
# inactivated_at カラムの基本的なテスト

spec/models/stat_spec.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
url: 'https://test1.coderdojo.jp',
1616
created_at: Time.zone.local(2020, 3, 1),
1717
prefecture_id: 13,
18-
is_active: false,
1918
inactivated_at: Time.zone.local(2022, 6, 15)
2019
)
2120

@@ -28,7 +27,6 @@
2827
url: 'https://test2.coderdojo.jp',
2928
created_at: Time.zone.local(2021, 1, 1),
3029
prefecture_id: 13,
31-
is_active: true,
3230
inactivated_at: nil
3331
)
3432

@@ -41,7 +39,6 @@
4139
url: 'https://test3.coderdojo.jp',
4240
created_at: Time.zone.local(2019, 1, 1),
4341
prefecture_id: 13,
44-
is_active: false,
4542
inactivated_at: Time.zone.local(2020, 3, 1)
4643
)
4744
end
@@ -95,7 +92,7 @@
9592
url: 'https://test1.coderdojo.jp',
9693
created_at: Time.zone.local(2012, 4, 1),
9794
prefecture_id: 13,
98-
is_active: true
95+
inactivated_at: nil
9996
)
10097

10198
# 2022年に非アクティブ化される道場
@@ -107,7 +104,6 @@
107104
url: 'https://test2.coderdojo.jp',
108105
created_at: Time.zone.local(2019, 1, 1),
109106
prefecture_id: 14,
110-
is_active: false,
111107
inactivated_at: Time.zone.local(2022, 6, 1)
112108
)
113109

@@ -120,7 +116,6 @@
120116
url: 'https://test3.coderdojo.jp',
121117
created_at: Time.zone.local(2020, 1, 1),
122118
prefecture_id: 27,
123-
is_active: false,
124119
inactivated_at: Time.zone.local(2023, 3, 1)
125120
)
126121

spec/requests/dojos_spec.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,35 @@
88
@dojo_2020_active = create(:dojo,
99
name: "Test Dojo 2020",
1010
created_at: "2020-06-01",
11-
is_active: true
11+
inactivated_at: nil
1212
)
1313

1414
# 2020年に作成、2021年に非アクティブ化
1515
@dojo_2020_inactive = create(:dojo,
1616
name: "Test Dojo 2020 Inactive",
1717
created_at: "2020-01-01",
18-
is_active: false,
1918
inactivated_at: "2021-03-01"
2019
)
2120

2221
# 2021年に作成されたアクティブな道場
2322
@dojo_2021_active = create(:dojo,
2423
name: "Test Dojo 2021",
2524
created_at: "2021-01-01",
26-
is_active: true
25+
inactivated_at: nil
2726
)
2827

2928
# 2019年に作成、2020年に非アクティブ化(2020年末時点では非アクティブ)
3029
@dojo_2019_inactive = create(:dojo,
3130
name: "Test Dojo 2019 Inactive",
3231
created_at: "2019-01-01",
33-
is_active: false,
3432
inactivated_at: "2020-06-01"
3533
)
3634

3735
# counter > 1 の道場
3836
@dojo_multi_branch = create(:dojo,
3937
name: "Multi Branch Dojo",
4038
created_at: "2020-01-01",
41-
is_active: true,
39+
inactivated_at: nil,
4240
counter: 3
4341
)
4442
end
@@ -151,7 +149,6 @@
151149

152150
# 重要: この道場は2021年3月に非アクティブ化されたが、
153151
# 2020年末時点ではアクティブだったので、inactive-item クラスを持たないべき
154-
# 現在のコードはここで失敗するはず(現在の is_active: false を使っているため)
155152
expect(dojo_row).not_to include('class="inactive-item"')
156153
end
157154

0 commit comments

Comments
 (0)