A platform for community discussion. Free, open, simple.
DEV: find_each in CSV exports (#22573)
Andrei Prigorshnevf4e424d7d411
diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rbindex 11c850d1185..ddf0bc2ce3c 100644--- a/app/jobs/regular/export_csv_file.rb+++ b/app/jobs/regular/export_csv_file.rb@@ -168,17 +168,17 @@ module Jobsdef screened_email_exportreturn enum_for(:screened_email_export) unless block_given?- ScreenedEmail- .order("last_match_at DESC")- .find_each { |screened_email| yield get_screened_email_fields(screened_email) }+ ScreenedEmail.find_each(order: :desc) do |screened_email|+ yield get_screened_email_fields(screened_email)+ endenddef screened_ip_exportreturn enum_for(:screened_ip_export) unless block_given?- ScreenedIpAddress- .order("id DESC")- .each { |screened_ip| yield get_screened_ip_fields(screened_ip) }+ ScreenedIpAddress.find_each(order: :desc) do |screened_ip|+ yield get_screened_ip_fields(screened_ip)+ endenddef screened_url_exportdiff --git a/spec/fabricators/screened_email_fabricator.rb b/spec/fabricators/screened_email_fabricator.rbindex 42f18fe7865..425f1c71b61 100644--- a/spec/fabricators/screened_email_fabricator.rb+++ b/spec/fabricators/screened_email_fabricator.rb@@ -3,4 +3,8 @@Fabricator(:screened_email) doemail { sequence(:email) { |n| "bad#{n}@spammers.org" } }action_type ScreenedEmail.actions[:block]+ match_count { sequence(:match_count) { |n| n } }+ last_match_at { sequence(:last_match_at) { |n| Time.now + n.days } }+ created_at { sequence(:created_at) { |n| Time.now + n.days } }+ ip_address { sequence(:ip_address) { |i| "99.232.23.#{i % 254}" } }enddiff --git a/spec/fabricators/screened_ip_address_fabricator.rb b/spec/fabricators/screened_ip_address_fabricator.rbindex 957f20ad77d..3c15e8d1b46 100644--- a/spec/fabricators/screened_ip_address_fabricator.rb+++ b/spec/fabricators/screened_ip_address_fabricator.rb@@ -1,5 +1,9 @@# frozen_string_literal: trueFabricator(:screened_ip_address) do- ip_address { sequence(:ip_address) { |n| "123.#{(n * 3) % 255}.#{(n * 2) % 255}.#{n % 255}" } }+ action_type ScreenedIpAddress.actions[:block]+ ip_address { sequence(:ip_address) { |i| "99.232.23.#{i % 254}" } }+ match_count { sequence(:match_count) { |n| n } }+ last_match_at { sequence(:last_match_at) { |n| Time.now + n.days } }+ created_at { sequence(:created_at) { |n| Time.now + n.days } }enddiff --git a/spec/models/screened_ip_address_spec.rb b/spec/models/screened_ip_address_spec.rbindex 12dc678ae33..b6f47256067 100644--- a/spec/models/screened_ip_address_spec.rb+++ b/spec/models/screened_ip_address_spec.rb@@ -217,6 +217,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:block],+ match_count: 0,)expect(described_class.should_block?("222.12.12.12")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)@@ -257,6 +258,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:do_nothing],+ match_count: 0,)expect(described_class.should_block?("111.234.23.11")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)@@ -268,6 +270,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:block],+ match_count: 0,)expect(described_class.should_block?("111.234.23.11")).to eq(true)expect(screened_ip_address.reload.match_count).to eq(1)@@ -281,6 +284,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "2001:db8::ff00:42:8329",action_type: described_class.actions[:do_nothing],+ match_count: 0,)expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)@@ -292,6 +296,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "2001:db8::ff00:42:8329",action_type: described_class.actions[:block],+ match_count: 0,)expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(true)expect(screened_ip_address.reload.match_count).to eq(1)@@ -310,6 +315,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:do_nothing],+ match_count: 0,)expect(described_class.is_allowed?("222.12.12.12")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)@@ -322,6 +328,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:do_nothing],+ match_count: 0,)expect(described_class.is_allowed?("111.234.23.11")).to eq(true)expect(screened_ip_address.reload.match_count).to eq(1)@@ -333,6 +340,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "111.234.23.11",action_type: described_class.actions[:block],+ match_count: 0,)expect(described_class.is_allowed?("111.234.23.11")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)@@ -346,6 +354,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "2001:db8::ff00:42:8329",action_type: described_class.actions[:do_nothing],+ match_count: 0,)expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(true)expect(screened_ip_address.reload.match_count).to eq(1)@@ -357,6 +366,7 @@ RSpec.describe ScreenedIpAddress do:screened_ip_address,ip_address: "2001:db8::ff00:42:8329",action_type: described_class.actions[:block],+ match_count: 0,)expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(false)expect(screened_ip_address.reload.match_count).to eq(0)diff --git a/spec/system/csv_exports_spec.rb b/spec/system/csv_exports_spec.rbindex 9a477bddcd2..8cea6e1d675 100644--- a/spec/system/csv_exports_spec.rb+++ b/spec/system/csv_exports_spec.rb@@ -3,8 +3,7 @@RSpec.describe "CSV Exports", type: :system dofab!(:admin) { Fabricate(:admin) }let(:csv_export_pm_page) { PageObjects::Pages::CSVExportPM.new }-- time_format = "%Y-%m-%d %H:%M:%S UTC"+ let(:time_format) { "%Y-%m-%d %H:%M:%S UTC" }before doJobs.run_immediately!@@ -200,16 +199,8 @@ RSpec.describe "CSV Exports", type: :system doendcontext "with screened emails" do- fab!(:screened_email) do- Fabricate(- :screened_email,- action_type: ScreenedEmail.actions[:do_nothing],- match_count: 5,- last_match_at: Time.now,- created_at: Time.now,- ip_address: "94.99.101.228",- )- end+ fab!(:screened_email_1) { Fabricate(:screened_email) }+ fab!(:screened_email_2) { Fabricate(:screened_email) }xit "exports data" dovisit "admin/logs/screened_emails"@@ -220,36 +211,33 @@ RSpec.describe "CSV Exports", type: :system doexpect(csv_export_pm_page).to have_download_linkexported_data = csv_export_pm_page.download_and_extract- expect(exported_data.length).to be(2)+ expect(exported_data.length).to be(3)expect(exported_data.first).to eq(%w[email action match_count last_match_at created_at ip_address],)- expect(exported_data.second).to eq(+ assert_export(exported_data.second, screened_email_2)+ assert_export(exported_data.third, screened_email_1)+ ensure+ csv_export_pm_page.clear_downloads+ end++ def assert_export(exported_email, email)+ expect(exported_email).to eq([- screened_email.email,- "do_nothing",- screened_email.match_count.to_s,- screened_email.last_match_at.strftime(time_format),- screened_email.created_at.strftime(time_format),- screened_email.ip_address.to_s,+ email.email,+ ScreenedEmail.actions[email.action_type].to_s,+ email.match_count.to_s,+ email.last_match_at.strftime(time_format),+ email.created_at.strftime(time_format),+ email.ip_address.to_s,],)- ensure- csv_export_pm_page.clear_downloadsendendcontext "with screened ips" do- fab!(:screened_ip) do- Fabricate(- :screened_ip_address,- action_type: ScreenedIpAddress.actions[:do_nothing],- match_count: 5,- ip_address: "99.232.23.124",- last_match_at: Time.now,- created_at: Time.now,- )- end+ fab!(:screened_ip_1) { Fabricate(:screened_ip_address) }+ fab!(:screened_ip_2) { Fabricate(:screened_ip_address) }xit "exports data" dovisit "admin/logs/screened_ip_addresses"@@ -261,17 +249,22 @@ RSpec.describe "CSV Exports", type: :system doexported_data = csv_export_pm_page.download_and_extractexpect(exported_data.first).to eq(%w[ip_address action match_count last_match_at created_at])- expect(exported_data.second).to eq(+ assert_exported_row(exported_data.second, screened_ip_2)+ assert_exported_row(exported_data.third, screened_ip_1)+ ensure+ csv_export_pm_page.clear_downloads+ end++ def assert_exported_row(exported_ip, ip)+ expect(exported_ip).to eq([- screened_ip.ip_address.to_s,- "do_nothing",- screened_ip.match_count.to_s,- screened_ip.last_match_at.strftime(time_format),- screened_ip.created_at.strftime(time_format),+ ip.ip_address.to_s,+ ScreenedIpAddress.actions[ip.action_type].to_s,+ ip.match_count.to_s,+ ip.last_match_at.strftime(time_format),+ ip.created_at.strftime(time_format),],)- ensure- csv_export_pm_page.clear_downloadsendend