Browse Source

Maintenance: Improve online notifications spec.

Florian Liebe 1 year ago
parent
commit
95a3a305b6

+ 5 - 1
.rubocop/cop/zammad/faker_unique.rb

@@ -9,16 +9,20 @@ module RuboCop
       #   # bad
       #   Faker::Number.number(...)
       #   Faker::Name.first_name
+      #   Faker::Date.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01"))
+      #   Faker::Time.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01"))
       #
       #   # good
       #   Faker::Number.unique.number(...)
       #   Faker::Name.unique.first_name
+      #   Faker::Date.unique.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01"))
+      #   Faker::Time.unique.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01"))
 
       class FakerUnique < Base
         extend AutoCorrector
 
         def_node_matcher :faker_call?, <<-PATTERN
-          $(send (const (const _ :Faker) {:Name :Number}) _ ...)
+          $(send (const (const _ :Faker) {:Name :Number :Date :Time}) _ ...)
         PATTERN
 
         MSG = 'Always use Faker::*::.unique to prevent race conditions in tests.'.freeze

+ 3 - 1
spec/graphql/gql/queries/online_notifications_spec.rb

@@ -83,7 +83,9 @@ RSpec.describe Gql::Queries::OnlineNotifications, authenticated_as: :user, type:
     context 'with some more notifications' do
       let(:notification)              { nil }
       let(:another_user_notification) { nil }
-      let(:notifications)             { Array.new(10) { create(:online_notification, user: user, created_at: Faker::Date.between(from: 1.year.ago, to: 50.weeks.from_now).to_datetime) } }
+
+      # Don't use relative dates here as they disable generation of unique values.
+      let(:notifications) { Array.new(10) { create(:online_notification, user: user, created_at: Faker::Date.unique.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime) } }
 
       it 'returns notifications in correct order' do
         notifications

+ 22 - 0
spec/rubocop/cop/zammad/faker_unique_spec.rb

@@ -8,6 +8,8 @@ RSpec.describe RuboCop::Cop::Zammad::FakerUnique, :aggregate_failures, type: :ru
   it 'accepts unique calls' do
     expect_no_offenses('Faker::Number.unique.number')
     expect_no_offenses('Faker::Name.unique.first_name')
+    expect_no_offenses('Faker::Date.unique.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01")).to_datetime')
+    expect_no_offenses('Faker::Time.unique.between(from: Date.parse("2022-01-01"), to: Date.parse("2024-01-01")).to_datetime')
   end
 
   it 'rejects other calls for Faker::Number' do
@@ -29,4 +31,24 @@ RSpec.describe RuboCop::Cop::Zammad::FakerUnique, :aggregate_failures, type: :ru
       Faker::Name.unique.first_name
     RUBY
   end
+
+  it 'rejects other calls for Faker::Date' do
+    expect_offense(<<~RUBY)
+      Faker::Date.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime
+      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Always use Faker::*::.unique to prevent race conditions in tests.
+    RUBY
+    expect_correction(<<~RUBY)
+      Faker::Date.unique.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime
+    RUBY
+  end
+
+  it 'rejects other calls for Faker::Time' do
+    expect_offense(<<~RUBY)
+      Faker::Time.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime
+      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Always use Faker::*::.unique to prevent race conditions in tests.
+    RUBY
+    expect_correction(<<~RUBY)
+      Faker::Time.unique.between(from: Date.parse('2022-01-01'), to: Date.parse('2024-01-01')).to_datetime
+    RUBY
+  end
 end