Add API datetime/format matcher for serialization specs, reduce factories (#33325)

This commit is contained in:
Matt Jankowski 2024-12-17 08:38:57 -05:00 committed by GitHub
parent 978142ac9e
commit ce5c33c65d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 127 additions and 96 deletions

View file

@ -11,14 +11,9 @@ RSpec.describe REST::AccountRelationshipSeveranceEventSerializer do
it 'returns expected values' do it 'returns expected values' do
expect(subject) expect(subject)
.to include( .to include(
'id' => be_a(String).and(eq('123')) 'id' => be_a(String).and(eq('123')),
'created_at' => match_api_datetime_format
) )
end end
end end
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
end end

View file

@ -6,14 +6,16 @@ RSpec.describe REST::AccountSerializer::FieldSerializer do
subject { serialized_record_json(field, described_class) } subject { serialized_record_json(field, described_class) }
let(:default_datetime) { DateTime.new(2024, 11, 28, 16, 20, 0) } let(:default_datetime) { DateTime.new(2024, 11, 28, 16, 20, 0) }
let(:user) { Fabricate(:user) } let(:account) { Fabricate.build :account }
let(:account) { user.account }
context 'when verified_at is populated' do context 'when verified_at is populated' do
let(:field) { Account::Field.new(account, 'name' => 'Foo', 'value' => 'Bar', 'verified_at' => default_datetime) } let(:field) { Account::Field.new(account, 'name' => 'Foo', 'value' => 'Bar', 'verified_at' => default_datetime) }
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['verified_at']) }.to_not raise_error expect(subject)
.to include(
'verified_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -52,7 +52,10 @@ RSpec.describe REST::AccountSerializer do
end end
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end

View file

@ -12,9 +12,9 @@ RSpec.describe REST::AccountWarningSerializer do
expect(subject) expect(subject)
.to include( .to include(
'id' => be_a(String).and(eq('123')), 'id' => be_a(String).and(eq('123')),
'status_ids' => be_a(Array).and(eq(['456', '789'])) 'status_ids' => be_a(Array).and(eq(['456', '789'])),
'created_at' => match_api_datetime_format
) )
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end end
end end
end end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::AccountSerializer do
let(:record) { Fabricate :account, user: Fabricate(:user) } let(:record) { Fabricate :account, user: Fabricate(:user) }
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end

View file

@ -11,11 +11,11 @@ RSpec.describe REST::Admin::CohortSerializer do
it 'returns expected values' do it 'returns expected values' do
expect(subject) expect(subject)
.to include( .to include(
'data' => be_a(Array), 'data' => be_a(Array).and(
'period' => /2024-01-01/ all(include('date' => match_api_datetime_format))
),
'period' => match(/2024-01-01/).and(match_api_datetime_format)
) )
expect { DateTime.rfc3339(subject['period']) }.to_not raise_error
subject['data'].each { |datum| expect { DateTime.rfc3339(datum['date']) }.to_not raise_error }
end end
end end
end end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::DomainAllowSerializer do
context 'when created_at is populated' do context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::DomainBlockSerializer do
context 'when created_at is populated' do context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::EmailDomainBlockSerializer do
context 'when created_at is populated' do context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -5,19 +5,15 @@ require 'rails_helper'
RSpec.describe REST::Admin::IpBlockSerializer do RSpec.describe REST::Admin::IpBlockSerializer do
subject { serialized_record_json(record, described_class) } subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate(:ip_block) } context 'when timestamps are populated' do
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
context 'when expires_at is populated' do
let(:record) { Fabricate(:ip_block, expires_at: DateTime.new(2024, 11, 28, 16, 20, 0)) } let(:record) { Fabricate(:ip_block, expires_at: DateTime.new(2024, 11, 28, 16, 20, 0)) }
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['expires_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format,
'expires_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::IpSerializer do
context 'when used_at is populated' do context 'when used_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['used_at']) }.to_not raise_error expect(subject)
.to include(
'used_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -12,7 +12,12 @@ RSpec.describe REST::Admin::MeasureSerializer do
context 'when start_at is populated' do context 'when start_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
subject['data'].each { |datum| expect { DateTime.rfc3339(datum['date']) }.to_not raise_error } expect(subject)
.to include(
'data' => all(
include('date' => match_api_datetime_format)
)
)
end end
end end
end end

View file

@ -5,23 +5,15 @@ require 'rails_helper'
RSpec.describe REST::Admin::ReportSerializer do RSpec.describe REST::Admin::ReportSerializer do
subject { serialized_record_json(report, described_class) } subject { serialized_record_json(report, described_class) }
let(:report) { Fabricate(:report) } context 'with timestamps' do
let(:report) { Fabricate(:report, action_taken_at: 3.days.ago) }
context 'with created_at' do
it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
context 'with action_taken_at' do
let(:acting_account) { Fabricate(:account) }
before do
report.resolve!(acting_account)
end
it 'is serialized as RFC 3339 datetime' do it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['action_taken_at']) }.to_not raise_error expect(subject)
.to include(
'action_taken_at' => match_api_datetime_format,
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -19,10 +19,13 @@ RSpec.describe REST::AnnouncementSerializer do
context 'when date fields are populated' do context 'when date fields are populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['starts_at']) }.to_not raise_error expect(subject)
expect { DateTime.rfc3339(subject['ends_at']) }.to_not raise_error .to include(
expect { DateTime.rfc3339(subject['published_at']) }.to_not raise_error 'starts_at' => match_api_datetime_format,
expect { DateTime.rfc3339(subject['updated_at']) }.to_not raise_error 'ends_at' => match_api_datetime_format,
'published_at' => match_api_datetime_format,
'updated_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -10,11 +10,14 @@ RSpec.describe REST::FilterSerializer do
) )
end end
let(:filter) { Fabricate :custom_filter, expires_at: DateTime.new(2024, 11, 28, 16, 20, 0) } let(:filter) { Fabricate.build :custom_filter, expires_at: DateTime.new(2024, 11, 28, 16, 20, 0) }
context 'when expires_at is populated' do context 'when expires_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['expires_at']) }.to_not raise_error expect(subject)
.to include(
'expires_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -14,7 +14,10 @@ RSpec.describe REST::MarkerSerializer do
context 'when updated_at is populated' do context 'when updated_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['updated_at']) }.to_not raise_error expect(subject)
.to include(
'updated_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -25,7 +25,10 @@ RSpec.describe REST::MutedAccountSerializer do
end end
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['mute_expires_at']) }.to_not raise_error expect(subject)
.to include(
'mute_expires_at' => match_api_datetime_format
)
end end
end end

View file

@ -14,7 +14,10 @@ RSpec.describe REST::NotificationGroupSerializer do
context 'when latest_page_notification_at is populated' do context 'when latest_page_notification_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['latest_page_notification_at']) }.to_not raise_error expect(subject)
.to include(
'latest_page_notification_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -17,15 +17,13 @@ RSpec.describe REST::NotificationRequestSerializer do
let(:current_user) { Fabricate(:user) } let(:current_user) { Fabricate(:user) }
let(:notification_request) { Fabricate :notification_request } let(:notification_request) { Fabricate :notification_request }
context 'when created_at is populated' do context 'when timestampts are populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
end .to include(
end 'created_at' => match_api_datetime_format,
'updated_at' => match_api_datetime_format
context 'when updated_at is populated' do )
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['updated_at']) }.to_not raise_error
end end
end end
end end

View file

@ -14,7 +14,10 @@ RSpec.describe REST::NotificationSerializer do
context 'when created_at is populated' do context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -15,11 +15,14 @@ RSpec.describe REST::PollSerializer do
end end
let(:current_user) { Fabricate(:user) } let(:current_user) { Fabricate(:user) }
let(:poll) { Fabricate :poll } let(:poll) { Fabricate.build :poll, expires_at: 5.days.from_now }
context 'when expires_at is populated' do context 'when expires_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['expires_at']) }.to_not raise_error expect(subject)
.to include(
'expires_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -10,24 +10,15 @@ RSpec.describe REST::ReportSerializer do
) )
end end
let(:status) { Fabricate(:status) } context 'with timestamps' do
let(:report) { Fabricate(:report, status_ids: [status.id]) } let(:report) { Fabricate(:report, action_taken_at: 3.days.ago) }
context 'with created_at' do
it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
context 'with action_taken_at' do
let(:acting_account) { Fabricate(:account) }
before do
report.resolve!(acting_account)
end
it 'is serialized as RFC 3339 datetime' do it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['action_taken_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format,
'action_taken_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -10,19 +10,15 @@ RSpec.describe REST::ScheduledStatusSerializer do
) )
end end
let(:account) { Fabricate(:account) } let(:scheduled_status) { Fabricate.build(:scheduled_status, scheduled_at: 4.minutes.from_now, params: { application_id: 123 }) }
let(:scheduled_status) { Fabricate.build(:scheduled_status, scheduled_at: 4.minutes.from_now, account: account, params: { application_id: 123 }) }
describe 'serialization' do describe 'serialization' do
it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['scheduled_at']) }
.to_not raise_error
end
it 'returns expected values and removes application_id from params' do it 'returns expected values and removes application_id from params' do
expect(subject.deep_symbolize_keys) expect(subject.deep_symbolize_keys)
.to include(scheduled_at: be_a(String)) .to include(
.and include(params: not_include(:application_id)) scheduled_at: be_a(String).and(match_api_datetime_format),
params: not_include(:application_id)
)
end end
end end
end end

View file

@ -14,7 +14,10 @@ RSpec.describe REST::StatusEditSerializer do
context 'when created_at is populated' do context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -54,7 +54,10 @@ RSpec.describe REST::StatusSerializer do
context 'with created_at' do context 'with created_at' do
it 'is serialized as RFC 3339 datetime' do it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end end
end end
@ -62,7 +65,10 @@ RSpec.describe REST::StatusSerializer do
let(:status) { Fabricate.build :status, edited_at: 3.days.ago } let(:status) { Fabricate.build :status, edited_at: 3.days.ago }
it 'is serialized as RFC 3339 datetime' do it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['edited_at']) }.to_not raise_error expect(subject)
.to include(
'edited_at' => match_api_datetime_format
)
end end
end end
end end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
RSpec::Matchers.define :match_api_datetime_format do
match(notify_expectation_failures: true) do |value|
expect { DateTime.rfc3339(value) }
.to_not raise_error
end
end