Skip to content

Commit

Permalink
Add rubocop-factory_bot (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp authored Oct 14, 2023
1 parent 5321f2b commit d36e3dc
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 50 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require:
- rubocop-factory_bot
- rubocop-minitest
- rubocop-rails

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ end

group :development do
gem 'annotate', '~> 3.2' # Remove workaround in lib/tasks/annotate.rb when https://github.com/ctran/annotate_models/issues/696 is fixed
gem 'rubocop-factory_bot', '~> 2.24'
gem 'rubocop-minitest', '~> 0.32.2'
gem 'rubocop-rails', '~> 2.21'
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-factory_bot (2.24.0)
rubocop (~> 1.33)
rubocop-minitest (0.32.2)
rubocop (>= 1.39, < 2.0)
rubocop-rails (2.21.2)
Expand Down Expand Up @@ -277,6 +279,7 @@ DEPENDENCIES
pundit (~> 2.3)
rack-cors (~> 2.0)
rails (~> 7.0)
rubocop-factory_bot (~> 2.24)
rubocop-minitest (~> 0.32.2)
rubocop-rails (~> 2.21)
simplecov (~> 0.21)
Expand Down
11 changes: 11 additions & 0 deletions gemset.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions test/controllers/albums_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class AlbumsControllerTest < ActionDispatch::IntegrationTest
setup do
@album = create :album, :with_image
@album = create(:album, :with_image)
sign_in_as create(:user)
end

Expand All @@ -22,7 +22,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should create album for moderator' do
sign_in_as create(:moderator)
album = build :album
album = build(:album)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand All @@ -46,7 +46,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should not create album without title' do
sign_in_as create(:moderator)
album = build :album
album = build(:album)

assert_difference('Album.count', 0) do
post albums_url, params: { album: { release: album.release } }
Expand All @@ -57,7 +57,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should create dependent album_labels' do
sign_in_as create(:moderator)
album = build :album
album = build(:album)

album_labels = (1..5).map do |_|
{
Expand All @@ -82,7 +82,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should create dependent album_artists' do
sign_in_as create(:moderator)
album = build :album
album = build(:album)

album_artists = (1..5).map do |i|
{
Expand Down Expand Up @@ -147,7 +147,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should update album for moderator' do
sign_in_as create(:moderator)
album = create :album
album = create(:album)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand All @@ -165,7 +165,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should destroy previous image when image is replaced' do
sign_in_as create(:moderator)
album = create :album, :with_image
album = create(:album, :with_image)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand All @@ -185,7 +185,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should destroy previous image when image is cleared' do
sign_in_as create(:moderator)
album = create :album, :with_image
album = create(:album, :with_image)

image = {
data: nil,
Expand Down Expand Up @@ -234,8 +234,8 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest

test 'should destroy empty albums for moderator' do
sign_in_as create(:moderator)
album2 = create :album
create :track, album: album2
album2 = create(:album)
create(:track, album: album2)
assert_difference('Image.count', -1) do
assert_difference('ActiveStorage::Blob.count', -1) do
assert_difference('Album.count', -1) do
Expand Down
18 changes: 9 additions & 9 deletions test/controllers/artists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ArtistsControllerTest < ActionDispatch::IntegrationTest
setup do
@artist = create :artist, :with_image
@artist = create(:artist, :with_image)
sign_in_as create(:user)
end

Expand All @@ -22,7 +22,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest

test 'should create artist for moderator' do
sign_in_as create(:moderator)
artist = build :artist
artist = build(:artist)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand Down Expand Up @@ -94,7 +94,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest

test 'should update artist for moderator' do
sign_in_as create(:moderator)
artist = create :artist
artist = create(:artist)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand All @@ -112,7 +112,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest

test 'should destroy previous image when image is replaced' do
sign_in_as create(:moderator)
artist = create :artist, :with_image
artist = create(:artist, :with_image)

image = {
data: Base64.encode64(Rails.root.join('test/files/image.jpg').read),
Expand All @@ -132,7 +132,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest

test 'should destroy previous image when image is cleared' do
sign_in_as create(:moderator)
artist = create :artist, :with_image
artist = create(:artist, :with_image)

image = {
data: nil,
Expand Down Expand Up @@ -182,8 +182,8 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest
test 'should destroy empty artists for moderator (track_artist)' do
sign_in_as create(:moderator)

artist2 = create :artist
create :track_artist, artist: artist2
artist2 = create(:artist)
create(:track_artist, artist: artist2)

assert_difference('Image.count', -1) do
assert_difference('ActiveStorage::Blob.count', -1) do
Expand All @@ -202,8 +202,8 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest
test 'should destroy empty artists for moderator (album_artist)' do
sign_in_as create(:moderator)

artist2 = create :artist
create :album_artist, artist: artist2
artist2 = create(:artist)
create(:album_artist, artist: artist2)

assert_difference('Image.count', -1) do
assert_difference('ActiveStorage::Blob.count', -1) do
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/auth_tokens_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class AuthTokensControllerTest < ActionDispatch::IntegrationTest

test 'should show auth_token' do
sign_in_as(@user)
auth_token = create :auth_token, user: @user
auth_token = create(:auth_token, user: @user)
get auth_token_url(auth_token)

assert_response :success
end

test 'should destroy auth_token' do
sign_in_as(@user)
auth_token = create :auth_token, user: @user
auth_token = create(:auth_token, user: @user)
assert_difference('AuthToken.count', -1) do
delete auth_token_url(auth_token)
end
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/cover_filenames_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest
end

test 'should not create cover_filename for user' do
cover_filename = build :cover_filename
cover_filename = build(:cover_filename)
assert_difference('CoverFilename.count', 0) do
post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } }
end
Expand All @@ -46,7 +46,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest

test 'should create cover_filename for moderator' do
sign_in_as create(:moderator)
cover_filename = build :cover_filename
cover_filename = build(:cover_filename)
assert_difference('CoverFilename.count', 1) do
post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } }
end
Expand All @@ -56,7 +56,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest

test 'should create cover_filename for admin' do
sign_in_as create(:admin)
cover_filename = build :cover_filename
cover_filename = build(:cover_filename)
assert_difference('CoverFilename.count', 1) do
post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } }
end
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/genres_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class GenresControllerTest < ActionDispatch::IntegrationTest

test 'should destroy empty genres for moderator' do
sign_in_as(create(:moderator))
genre2 = create :genre
track = create :track
genre2 = create(:genre)
track = create(:track)
track.update(genres: [genre2])

assert_difference('Genre.count', -1) do
Expand All @@ -137,8 +137,8 @@ class GenresControllerTest < ActionDispatch::IntegrationTest

test 'should destroy empty genres for admin' do
sign_in_as(create(:admin))
genre2 = create :genre
track = create :track
genre2 = create(:genre)
track = create(:track)
track.update(genres: [genre2])

assert_difference('Genre.count', -1) do
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/labels_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class LabelsControllerTest < ActionDispatch::IntegrationTest
test 'should destroy empty labels for moderator' do
sign_in_as(create(:moderator))

label2 = create :label
create :album_label, label: label2
label2 = create(:label)
create(:album_label, label: label2)

assert_difference('Label.count', -1) do
post destroy_empty_labels_url
Expand All @@ -139,8 +139,8 @@ class LabelsControllerTest < ActionDispatch::IntegrationTest
test 'should destroy empty labels for admin' do
sign_in_as(create(:admin))

label2 = create :label
create :album_label, label: label2
label2 = create(:label)
create(:album_label, label: label2)

assert_difference('Label.count', -1) do
post destroy_empty_labels_url
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/locations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class LocationsControllerTest < ActionDispatch::IntegrationTest
setup do
@location = create :location
@location = create(:location)
sign_in_as create(:user)
end

Expand All @@ -27,7 +27,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest
end

test 'should not create location for user' do
location = build :location
location = build(:location)
assert_difference('Location.count', 0) do
post locations_url, params: { location: { path: location.path } }
end
Expand All @@ -46,7 +46,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest

test 'should create location for moderator' do
sign_in_as create(:moderator)
location = build :location
location = build(:location)
assert_difference('Location.count', 1) do
post locations_url, params: { location: { path: location.path } }
end
Expand All @@ -56,7 +56,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest

test 'should create location for admin' do
sign_in_as create(:admin)
location = build :location
location = build(:location)
assert_difference('Location.count', 1) do
post locations_url, params: { location: { path: location.path } }
end
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/tracks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class TracksControllerTest < ActionDispatch::IntegrationTest
test 'should destroy empty tracks for moderator' do
sign_in_as(create(:moderator))

create :track
create(:track)

assert_difference('Track.count', -1) do
post destroy_empty_tracks_url
Expand Down Expand Up @@ -309,7 +309,7 @@ class TracksControllerAudioTest < ActionDispatch::IntegrationTest
test 'should not create transcoded_item if it already exists but should queue if file is gone' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
codec_conversion = create :codec_conversion
codec_conversion = create(:codec_conversion)
location = Location.create(path: Rails.root.join('test/files'))
flac = Codec.create(mimetype: 'audio/flac', extension: 'flac')
audio_file = create(:audio_file, location:, filename: '/base.flac', codec: flac)
Expand All @@ -330,7 +330,7 @@ class TracksControllerAudioTest < ActionDispatch::IntegrationTest
test 'should not create transcoded_item if it already exists' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
codec_conversion = create :codec_conversion
codec_conversion = create(:codec_conversion)
location = Location.create(path: Rails.root.join('test/files'))
flac = Codec.create(mimetype: 'audio/flac', extension: 'flac')
audio_file = create(:audio_file, location:, filename: '/base.flac', codec: flac)
Expand Down
2 changes: 1 addition & 1 deletion test/factories/albums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
release { Faker::Date.backward(days: 365 * 100) }

trait :with_image do
association :image, factory: :image
image factory: %i[image]
end

transient do
Expand Down
2 changes: 1 addition & 1 deletion test/factories/artists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
review_comment { Faker::Lorem.word }

trait :with_image do
association :image, factory: :image
image factory: %i[image]
end
end
end
4 changes: 2 additions & 2 deletions test/factories/auth_tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
user_agent { Faker::Internet.user_agent }

factory :moderator_auth_token do
association :user, factory: :moderator
user factory: %i[moderator]
end

factory :admin_auth_token do
association :user, factory: :admin
user factory: %i[admin]
end
end
end
2 changes: 1 addition & 1 deletion test/factories/codec_conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

FactoryBot.define do
factory :codec_conversion do
association :resulting_codec, factory: :codec
resulting_codec factory: %i[codec]
name { Faker::Lorem.unique.word }
ffmpeg_params { Faker::Lorem.words(number: 10) }
end
Expand Down
Loading

0 comments on commit d36e3dc

Please sign in to comment.