Skip to content

Commit

Permalink
Added rubocop-minitest
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Mar 14, 2024
1 parent 7608bb7 commit f47527b
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 84 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ AllCops:
require:
- rubocop-rails
- rubocop-performance
- rubocop-minitest

#
# Layout
Expand Down Expand Up @@ -104,3 +105,10 @@ Style/AsciiComments:
Naming/MethodParameterName:
MinNameLength: 2
AllowedNames: [_, n]

#
# Minitest
#

Minitest/MultipleAssertions:
Max: 5
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ group :development do

# Style
gem 'rubocop', require: false
gem 'rubocop-minitest', require: false
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
end
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-minitest (0.34.5)
rubocop (>= 1.39, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-performance (1.19.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
Expand Down Expand Up @@ -315,6 +318,7 @@ DEPENDENCIES
rails (~> 7.1.3)
rails-i18n (~> 7.0.0)
rubocop
rubocop-minitest
rubocop-performance
rubocop-rails
selenium-webdriver
Expand Down
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
# Don't log any deprecations.
config.active_support.report_deprecations = false

# In our case having SQLite in production is actually just fine!
config.active_record.sqlite3_production_warning = false

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

Expand Down
1 change: 1 addition & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def sign_in!
# Sign out the current user if it's already signed in, otherwise do nothing.
def sign_out_maybe!
click_on I18n.t('sessions.sign-out')

assert_text I18n.t('sessions.title')
rescue Capybara::ElementNotFound
# We were not logged in, do nothing.
Expand Down
12 changes: 6 additions & 6 deletions test/controllers/exports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class ExportsControllerTest < ActionDispatch::IntegrationTest
get search_exports_url(0, format: 'csv')

body = @response.body.split("\n")
assert body.size == 2
assert body.first.split(',')[0] == things(:thing2).target
assert body.last.split(',')[0] == things(:thing1).target
targets = body.map { |b| b.split(',').first }

assert @response.media_type == 'text/csv'
assert_equal targets, [things(:thing2).target, things(:thing1).target]

assert_equal 'text/csv', @response.media_type
end

test 'uoc: works' do
get search_exports_url(0, format: 'uoc')

assert @response.body == file_fixture('all.uoc.tex').read
assert @response.media_type == 'application/x-tex'
assert_equal @response.body, file_fixture('all.uoc.tex').read
assert_equal 'application/x-tex', @response.media_type
end
end
3 changes: 2 additions & 1 deletion test/models/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CommentTest < ActiveSupport::TestCase

test 'has many tags through tag_references' do
comment = comments(:comment1)
assert comment.tags.size == 1

assert_equal 1, comment.tags.size
end
end
51 changes: 22 additions & 29 deletions test/models/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'test_helper'

class SearchTest < ActiveSupport::TestCase
test 'name and body have to be present and unique' do
test 'name and body have to be present' do
search = Search.new(user_id: users(:user).id)
assert_raise(ActiveRecord::RecordInvalid) { search.save! }

Expand All @@ -13,16 +13,17 @@ class SearchTest < ActiveSupport::TestCase
search.name = nil
search.body = 'body'
assert_raise(ActiveRecord::RecordInvalid) { search.save! }
end

test 'name and body have to be unique' do
search = Search.new(user_id: users(:user).id)

search.name = searches(:search1).name
assert_raise(ActiveRecord::RecordInvalid) { search.save! }

search.name = 'another'
search.body = searches(:search1).body
assert_raise(ActiveRecord::RecordInvalid) { search.save! }

search.body = 'another body'
assert_difference('Search.count') { search.save! }
end

##
Expand All @@ -31,60 +32,52 @@ class SearchTest < ActiveSupport::TestCase
test 'returns all things when an empty body is given' do
res = Search.new.results

assert res[:things].size == 2
assert res[:things][0][:target] == things(:thing2).target
assert res[:things][1][:target] == things(:thing1).target
assert_equal 2, res[:things].size
assert_equal res[:things][0][:target], things(:thing2).target
assert_equal res[:things][1][:target], things(:thing1).target
end

test 'returns everything matching a specific tag' do
res = Search.new(body: "tag:'#{tags(:tag1).name}'").results

assert res[:things].size == 2
assert res[:things][0][:target] == things(:thing2).target
assert res[:things][1][:target] == things(:thing1).target
assert res[:comments].size == 1
assert res[:comments][0][:id] == comments(:comment1).id
assert_equal res[:things].map(&:target), [things(:thing2).target, things(:thing1).target]
assert_equal res[:comments].map(&:id), [comments(:comment1).id]
end

test 'returns everything matching two tags' do
res = Search.new(body: "tag:'#{tags(:tag1).name}' tag:'#{tags(:tag2).name}'").results

assert res[:things].size == 1
assert res[:things][0][:target] == things(:thing1).target
assert res[:comments].empty?
assert_equal 1, res[:things].size
assert_equal res[:things][0][:target], things(:thing1).target
assert_empty res[:comments]
end

test 'returns everything matching a given text' do
res = Search.new(body: 'Miquel').results

assert res[:things].size == 2
assert res[:things][0][:target] == things(:thing2).target
assert res[:things][1][:target] == things(:thing1).target
assert res[:comments].size == 1
assert res[:comments][0][:id] == comments(:comment1).id
assert_equal res[:things].map(&:target), [things(:thing2).target, things(:thing1).target]
assert_equal res[:comments].map(&:id), [comments(:comment1).id]
end

test 'returns everything matching two tags and a given text' do
res = Search.new(body: "Miquel tag:'#{tags(:tag1).name}' tag:'#{tags(:tag2).name}'").results

assert res[:things].size == 1
assert res[:things][0][:target] == things(:thing1).target
assert res[:comments].empty?
assert_equal 1, res[:things].size
assert_equal res[:things][0][:target], things(:thing1).target
assert_empty res[:comments]
end

test 'returns everything matching a given compound text' do
res = Search.new(body: 'some other').results

assert res[:things].size == 2
assert res[:things][0][:target] == things(:thing2).target
assert res[:things][1][:target] == things(:thing1).target
assert res[:comments].empty?
assert_equal res[:things].map(&:target), [things(:thing2).target, things(:thing1).target]
assert_empty res[:comments]
end

test 'returns an empty result with unknown fields' do
res = Search.new(body: 'whatever:"unknown"').results

assert res[:things].empty?
assert res[:comments].empty?
assert_empty res[:things]
assert_empty res[:comments]
end
end
24 changes: 9 additions & 15 deletions test/models/thing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,28 @@
require 'test_helper'

class ThingTest < ActiveSupport::TestCase
test 'validates presence' do
assert_raise(ActiveRecord::RecordInvalid) { Thing.new.save! }

# Missing 'title'
test 'presence: title' do
assert_raise(ActiveRecord::RecordInvalid) do
Thing.new(target: 'target', authors: 'Author',
user_id: users(:user).id, rate: 5,
status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
end
end

# Missing 'target'
test 'presence: target' do
assert_raise(ActiveRecord::RecordInvalid) do
Thing.new(title: 'title', authors: 'Author',
user_id: users(:user).id, rate: 5,
status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
end
end

# Missing 'authors'
test 'presence: authors' do
assert_raise(ActiveRecord::RecordInvalid) do
Thing.new(title: 'title', target: 'target',
user_id: users(:user).id, rate: 5,
status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
end

# Valid!
assert_difference('Thing.count') do
Thing.new(title: 'title', target: 'target', authors: 'Author',
user_id: users(:user).id, rate: 5,
status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
end
end

test "'rate' has to be between 0 and 10" do
Expand Down Expand Up @@ -85,11 +77,13 @@ class ThingTest < ActiveSupport::TestCase

test 'has many comments' do
thing = things(:thing1)
assert thing.comments.size == 1

assert_equal 1, thing.comments.size
end

test 'has many tags through tag_references' do
thing = things(:thing1)
assert thing.tags.size == 2

assert_equal 2, thing.tags.size
end
end
16 changes: 7 additions & 9 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
test 'has to provide username and password' do
test 'presence: username' do
user = User.new
user.password = '1234'
user.password_confirmation = '12345'

assert_raise(ActiveRecord::RecordInvalid) { user.save! }
end

test 'presence: password' do
user = User.new
user.username = 'whatever'
assert_raise(ActiveRecord::RecordInvalid) { user.save! }

user.password = '1234'
user.password_confirmation = '12345'
assert_raise(ActiveRecord::RecordInvalid) { user.save! }

user.password_confirmation = '1234'
assert_difference('User.count') do
user.save!
end
end

test 'username has to be unique' do
Expand Down
22 changes: 14 additions & 8 deletions test/system/comments_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class CommentsTest < ApplicationSystemTestCase
visit thing_url(things(:thing1))

click_on I18n.t('comments.new')

assert_text I18n.t('tags.new-action'), count: 1

click_on I18n.t('general.cancel')

assert_text I18n.t('tags.new-action'), count: 0
end

Expand All @@ -28,10 +30,9 @@ class CommentsTest < ApplicationSystemTestCase

click_on I18n.t('helpers.submit.create')

assert_text "#{I18n.t('comments.title')} #2"
assert_text 'a new comment'
assert_text "#{I18n.t('tags.title')}: #{tags(:tag1).name}, #{tags(:tag2).name}"
assert page.current_path == thing_path(things(:thing1))
assert_equal page.current_path, thing_path(things(:thing1))
end

test 'things#show: you can update a comment' do
Expand All @@ -41,16 +42,18 @@ class CommentsTest < ApplicationSystemTestCase
find(:css, '#comment_content').set('updated comment')

find("#comment_#{comments(:comment1).id} input[type=submit]").click

assert_text 'updated comment'
assert page.current_path == thing_path(things(:thing1))
assert_equal page.current_path, thing_path(things(:thing1))
end

test 'things#show: you can delete a comment' do
visit thing_url(things(:thing1))

all('.delete-comment').last.click

assert_text I18n.t('comments.none')
assert page.current_path == thing_path(things(:thing1))
assert_equal page.current_path, thing_path(things(:thing1))
end

##
Expand All @@ -60,9 +63,11 @@ class CommentsTest < ApplicationSystemTestCase
visit edit_thing_url(things(:thing1))

click_on I18n.t('comments.new')

assert_text I18n.t('tags.new-action'), count: 2

click_on I18n.t('general.cancel')

assert_text I18n.t('tags.new-action'), count: 1
end

Expand All @@ -76,10 +81,9 @@ class CommentsTest < ApplicationSystemTestCase

click_on I18n.t('helpers.submit.create')

assert_text "#{I18n.t('comments.title')} #2"
assert_text 'a new comment'
assert_text "#{I18n.t('tags.title')}: #{tags(:tag1).name}, #{tags(:tag2).name}"
assert page.current_path == edit_thing_path(things(:thing1))
assert_equal page.current_path, edit_thing_path(things(:thing1))
end

test 'things#edit: you can update a comment' do
Expand All @@ -89,15 +93,17 @@ class CommentsTest < ApplicationSystemTestCase
find(:css, '#comment_content').set('updated comment')

find("#comment_#{comments(:comment1).id} input[type=submit]").click

assert_text 'updated comment'
assert page.current_path == edit_thing_path(things(:thing1))
assert_equal page.current_path, edit_thing_path(things(:thing1))
end

test 'things#edit: you can delete a comment' do
visit edit_thing_url(things(:thing1))

all('.delete-comment').last.click

assert_text I18n.t('comments.none')
assert page.current_path == edit_thing_path(things(:thing1))
assert_equal page.current_path, edit_thing_path(things(:thing1))
end
end
Loading

0 comments on commit f47527b

Please sign in to comment.