From 6ab964b161880c7affec0ac10c7613d28e29ab49 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Fri, 25 Aug 2023 00:11:41 -0700 Subject: [PATCH] chore: fix flaky spec (#7686) - fix flaky reporting specs - fix flaky export spec --- spec/builders/v2/report_builder_spec.rb | 158 +++++++++--------- .../api/v2/accounts/report_controller_spec.rb | 2 +- spec/jobs/account/contacts_export_job_spec.rb | 10 +- 3 files changed, 87 insertions(+), 83 deletions(-) diff --git a/spec/builders/v2/report_builder_spec.rb b/spec/builders/v2/report_builder_spec.rb index 0b0273a0f..304e7bd97 100644 --- a/spec/builders/v2/report_builder_spec.rb +++ b/spec/builders/v2/report_builder_spec.rb @@ -9,46 +9,48 @@ describe V2::ReportBuilder do # Update this spec to use travel_to # This spec breaks in certain timezone describe '#timeseries' do - before_all do - user = create(:user, account: account) - inbox = create(:inbox, account: account) - create(:inbox_member, user: user, inbox: inbox) + before do + travel_to(Time.zone.today) do + user = create(:user, account: account) + inbox = create(:inbox, account: account) + create(:inbox_member, user: user, inbox: inbox) - gravatar_url = 'https://www.gravatar.com' - stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404) + gravatar_url = 'https://www.gravatar.com' + stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404) - perform_enqueued_jobs do - 10.times do - conversation = create(:conversation, account: account, - inbox: inbox, assignee: user, - created_at: Time.zone.today) - create_list(:message, 5, message_type: 'outgoing', - account: account, inbox: inbox, - conversation: conversation, created_at: Time.zone.today + 2.hours) - create_list(:message, 2, message_type: 'incoming', - account: account, inbox: inbox, - conversation: conversation, - created_at: Time.zone.today + 3.hours) - conversation.update_labels('label_1') - conversation.label_list - conversation.save! - end + perform_enqueued_jobs do + 10.times do + conversation = create(:conversation, account: account, + inbox: inbox, assignee: user, + created_at: Time.zone.today) + create_list(:message, 5, message_type: 'outgoing', + account: account, inbox: inbox, + conversation: conversation, created_at: Time.zone.today + 2.hours) + create_list(:message, 2, message_type: 'incoming', + account: account, inbox: inbox, + conversation: conversation, + created_at: Time.zone.today + 3.hours) + conversation.update_labels('label_1') + conversation.label_list + conversation.save! + end - 5.times do - conversation = create(:conversation, account: account, - inbox: inbox, assignee: user, - created_at: (Time.zone.today - 2.days)) - create_list(:message, 3, message_type: 'outgoing', - account: account, inbox: inbox, - conversation: conversation, - created_at: (Time.zone.today - 2.days)) - create_list(:message, 1, message_type: 'incoming', - account: account, inbox: inbox, - conversation: conversation, - created_at: (Time.zone.today - 2.days)) - conversation.update_labels('label_2') - conversation.label_list - conversation.save! + 5.times do + conversation = create(:conversation, account: account, + inbox: inbox, assignee: user, + created_at: (Time.zone.today - 2.days)) + create_list(:message, 3, message_type: 'outgoing', + account: account, inbox: inbox, + conversation: conversation, + created_at: (Time.zone.today - 2.days)) + create_list(:message, 1, message_type: 'incoming', + account: account, inbox: inbox, + conversation: conversation, + created_at: (Time.zone.today - 2.days)) + conversation.update_labels('label_2') + conversation.label_list + conversation.save! + end end end end @@ -100,28 +102,30 @@ describe V2::ReportBuilder do end it 'return resolutions count' do - params = { - metric: 'resolutions_count', - type: :account, - since: (Time.zone.today - 3.days).to_time.to_i.to_s, - until: Time.zone.today.end_of_day.to_time.to_i.to_s - } + travel_to(Time.zone.today) do + params = { + metric: 'resolutions_count', + type: :account, + since: (Time.zone.today - 3.days).to_time.to_i.to_s, + until: Time.zone.today.end_of_day.to_time.to_i.to_s + } - conversations = account.conversations.where('created_at < ?', 1.day.ago) - perform_enqueued_jobs do - # Resolve all 5 conversations - conversations.each(&:resolved!) + conversations = account.conversations.where('created_at < ?', 1.day.ago) + perform_enqueued_jobs do + # Resolve all 5 conversations + conversations.each(&:resolved!) - # Reopen 1 conversation - conversations.first.open! + # Reopen 1 conversation + conversations.first.open! + end + + builder = described_class.new(account, params) + metrics = builder.timeseries + + # 4 conversations are resolved + expect(metrics[Time.zone.today]).to be 4 + expect(metrics[Time.zone.today - 2.days]).to be 0 end - - builder = described_class.new(account, params) - metrics = builder.timeseries - - # 4 conversations are resolved - expect(metrics[Time.zone.today]).to be 4 - expect(metrics[Time.zone.today - 2.days]).to be 0 end it 'returns average first response time' do @@ -218,30 +222,32 @@ describe V2::ReportBuilder do end it 'return resolutions count' do - params = { - metric: 'resolutions_count', - type: :label, - id: label_2.id, - since: (Time.zone.today - 3.days).to_time.to_i.to_s, - until: (Time.zone.today + 1.day).to_time.to_i.to_s - } + travel_to(Time.zone.today) do + params = { + metric: 'resolutions_count', + type: :label, + id: label_2.id, + since: (Time.zone.today - 3.days).to_time.to_i.to_s, + until: (Time.zone.today + 1.day).to_time.to_i.to_s + } - conversations = account.conversations.where('created_at < ?', 1.day.ago) + conversations = account.conversations.where('created_at < ?', 1.day.ago) - perform_enqueued_jobs do - # ensure 5 reporting events are created - conversations.each(&:resolved!) + perform_enqueued_jobs do + # ensure 5 reporting events are created + conversations.each(&:resolved!) - # open one of the conversations to check if it is not counted - conversations.last.open! + # open one of the conversations to check if it is not counted + conversations.last.open! + end + + builder = described_class.new(account, params) + metrics = builder.timeseries + + # this should count only 4 since the last conversation was reopened + expect(metrics[Time.zone.today]).to be 4 + expect(metrics[Time.zone.today - 2.days]).to be 0 end - - builder = described_class.new(account, params) - metrics = builder.timeseries - - # this should count only 4 since the last conversation was reopened - expect(metrics[Time.zone.today]).to be 4 - expect(metrics[Time.zone.today - 2.days]).to be 0 end it 'returns average first response time' do diff --git a/spec/controllers/api/v2/accounts/report_controller_spec.rb b/spec/controllers/api/v2/accounts/report_controller_spec.rb index 574330df3..27a596243 100644 --- a/spec/controllers/api/v2/accounts/report_controller_spec.rb +++ b/spec/controllers/api/v2/accounts/report_controller_spec.rb @@ -15,7 +15,7 @@ RSpec.describe 'Reports API', type: :request do before do create_list(:conversation, 10, account: account, inbox: inbox, - assignee: user, created_at: Time.zone.today) + assignee: user, created_at: Time.current.in_time_zone(default_timezone).to_date) end describe 'GET /api/v2/accounts/:account_id/reports/account' do diff --git a/spec/jobs/account/contacts_export_job_spec.rb b/spec/jobs/account/contacts_export_job_spec.rb index cdab668a8..e597ff637 100644 --- a/spec/jobs/account/contacts_export_job_spec.rb +++ b/spec/jobs/account/contacts_export_job_spec.rb @@ -37,15 +37,13 @@ RSpec.describe Account::ContactsExportJob do described_class.perform_now(account.id, []) csv_data = CSV.parse(account.contacts_export.download, headers: true) - first_row = csv_data[0] - last_row = csv_data[csv_data.length - 1] - first_contact = account.contacts.find_by(email: 'test1@text.example') - last_contact = account.contacts.find_by(email: 'test2@text.example') + emails = csv_data.pluck('email') + phone_numbers = csv_data.pluck('phone_number') expect(csv_data.length).to eq(account.contacts.count) - expect([first_row['email'], last_row['email']]).to contain_exactly(first_contact.email, last_contact.email) - expect([first_row['phone_number'], last_row['phone_number']]).to contain_exactly(first_contact.phone_number, last_contact.phone_number) + expect(emails).to include('test1@text.example', 'test2@text.example') + expect(phone_numbers).to include('+910808080818', '+910808080808') end end end