From 344e9473746c2b0ec2a04d1cd9625f499d0223b2 Mon Sep 17 00:00:00 2001 From: Peter Bhat Harkins Date: Wed, 1 Jun 2022 05:57:18 -0500 Subject: [PATCH] Fix fake_data task; add CI test for it (#1087) The test is overdue for how brittle fake_data.rb is, but as that's a slow process that's not critical functionality, I only want it running on CI. --- .github/workflows/check.yml | 4 +++- README.md | 3 +++ db/seeds.rb | 2 +- lib/tasks/fake_data.rake | 22 ++++++++++++++-------- spec/slow/README.md | 2 ++ spec/slow/fake_data_spec.rb | 12 ++++++++++++ spec/spec_helper.rb | 3 +++ 7 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 spec/slow/README.md create mode 100644 spec/slow/fake_data_spec.rb diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 75d29fec..a2a7562d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -26,7 +26,9 @@ jobs: - name: Prepare database run: ./bin/rails db:schema:load - name: Run tests - run: bundle exec rspec + run: | + bundle exec rspec + bundle exec rspec spec/slow/*_spec.rb - name: Run linters run: | bundle exec rubocop diff --git a/README.md b/README.md index 6e748a41..bd39a52f 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,9 @@ running tests: * See `config/initializers/production.rb.sample` for GitHub/Twitter integration help. +* You probably want to use [git-imerge](https://lobste.rs/s/dbm2d4) to pull in + changes from Lobsters to your site. + #### Administration Basic moderation happens on-site, but most other administrative tasks require use of the rails console in production. diff --git a/db/seeds.rb b/db/seeds.rb index 86784822..895da242 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -31,4 +31,4 @@ puts " * inactive-user for disowned comments by deleted users" puts " * a test tag" puts puts "If this is a dev environment, you probably want to run `rails fake_data`" -puts "If this is production, you want to run `rails console` to rename your admin, category, and tag" +puts "If this is production, you want to run `rails console` to rename your admin. Edit your category, and tag on-site." diff --git a/lib/tasks/fake_data.rake b/lib/tasks/fake_data.rake index d7a553d0..c5f6adfc 100644 --- a/lib/tasks/fake_data.rake +++ b/lib/tasks/fake_data.rake @@ -19,7 +19,7 @@ class FakeDataGenerator email: Faker::Internet.email(name: name), password: password, password_confirmation: password, - username: Faker::Internet.user_name(specifier: 5..40, separators: %w(_)), + username: Faker::Internet.user_name(specifier: name, separators: %w(_))[..23], created_at: (User::NEW_USER_DAYS + 1).days.ago, karma: Random.rand(User::MIN_KARMA_TO_FLAG * 2), about: Faker::Lorem.sentence(word_count: 7), @@ -27,14 +27,20 @@ class FakeDataGenerator invited_by_user: User.select(&:can_invite?).sample, } create_args.merge!(is_admin: true) if i % 8 == 0 - users << User.create!(create_args) - if i % 7 == 0 - users[i].grant_moderatorship_by_user!(mod) - end - if i % 6 == 0 - users[i].disable_invite_by_user_for_reason!(mod, Faker::Lorem.sentence(word_count: 5)) + begin + users << User.create!(create_args) + if i % 7 == 0 + users.last.grant_moderatorship_by_user!(mod) + end + if i % 6 == 0 + users.last.disable_invite_by_user_for_reason!(mod, Faker::Lorem.sentence(word_count: 5)) + end + rescue ActiveRecord::RecordInvalid => e + puts "caught #{e}" + next if e.message == 'Validation failed: Username has already been taken' end end + users.compact! puts print 'Categories ' @@ -225,7 +231,7 @@ class FakeDataGenerator user.ban_by_user_for_reason!(User.moderators.sample, Faker::Lorem.sentence(word_count: 5)) if i.even? - user.unban_by_user!(User.moderators.sample) + user.unban_by_user!(User.moderators.sample, "reformed") end end puts diff --git a/spec/slow/README.md b/spec/slow/README.md new file mode 100644 index 00000000..5867b399 --- /dev/null +++ b/spec/slow/README.md @@ -0,0 +1,2 @@ +Tests in this directory do not run as part of the regular test suite with +`bundle exec rspec`. They are excluded by config.exclude diff --git a/spec/slow/fake_data_spec.rb b/spec/slow/fake_data_spec.rb new file mode 100644 index 00000000..cec80353 --- /dev/null +++ b/spec/slow/fake_data_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' + +Rails.application.load_tasks + +describe "fake_data" do + before { Rails.application.load_seed } + + # basic smoke test, task shouldn't throw exceptions + it 'runs' do + FakeDataGenerator.new.generate + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ed310d91..feaac8f7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -37,6 +37,9 @@ RSpec.configure do |config| mocks.verify_partial_doubles = true end + # Some tests are slow, rarely catch a bug, and are not critical functionality. + config.exclude_pattern = 'spec/slow/*_spec.rb' + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will # have no way to turn it off -- the option exists only for backwards # compatibility in RSpec 3). It causes shared context metadata to be