Merge branch 'master' into task/12346-rubocop-rails-pluck-and-pluck-where

This commit is contained in:
Ahmed Ejaz
2024-06-25 11:53:50 +05:00
committed by GitHub
6 changed files with 146 additions and 61 deletions

View File

@@ -625,12 +625,6 @@ Rails/LexicallyScopedActionFilter:
- 'app/controllers/spree/admin/zones_controller.rb'
- 'app/controllers/spree/users_controller.rb'
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/RelativeDateConstant:
Exclude:
- 'lib/tasks/data/remove_transient_data.rb'
# Offense count: 56
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Include.

View File

@@ -1,7 +1,7 @@
# frozen_string_literal: true
class RemoveTransientData
RETENTION_PERIOD = 3.months.ago.to_date
RETENTION_PERIOD = 3.months
# This model lets us operate on the sessions DB table using ActiveRecord's
# methods within the scope of this service. This relies on the AR's
@@ -9,12 +9,18 @@ class RemoveTransientData
class Session < ApplicationRecord
end
attr_reader :expiration_date
def initialize
@expiration_date = RETENTION_PERIOD.ago.to_date
end
def call
Rails.logger.info("#{self.class.name}: processing")
Spree::StateChange.where("created_at < ?", RETENTION_PERIOD).delete_all
Spree::LogEntry.where("created_at < ?", RETENTION_PERIOD).delete_all
Session.where("updated_at < ?", RETENTION_PERIOD).delete_all
Spree::StateChange.where("created_at < ?", expiration_date).delete_all
Spree::LogEntry.where("created_at < ?", expiration_date).delete_all
Session.where("updated_at < ?", expiration_date).delete_all
clear_old_cart_data!
end
@@ -23,7 +29,7 @@ class RemoveTransientData
def clear_old_cart_data!
old_carts = Spree::Order.
where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", RETENTION_PERIOD).
where("spree_orders.state = 'cart' AND spree_orders.updated_at < ?", expiration_date).
merge(orders_without_payments)
old_cart_line_items = Spree::LineItem.where(order_id: old_carts)

View File

@@ -5,8 +5,6 @@ require 'tasks/data/remove_transient_data'
RSpec.describe RemoveTransientData do
describe '#call' do
let(:retention_period) { RemoveTransientData::RETENTION_PERIOD }
before do
allow(Spree::StateChange).to receive(:delete_all)
allow(Spree::LogEntry).to receive(:delete_all)
@@ -14,24 +12,28 @@ RSpec.describe RemoveTransientData do
allow(Rails.logger).to receive(:info)
end
it 'deletes state changes older than rentention_period' do
Spree::StateChange.create(created_at: retention_period - 1.day)
it 'deletes state changes older than retention period' do
remover = RemoveTransientData.new
Spree::StateChange.create(created_at: remover.expiration_date - 1.day)
remover.call
RemoveTransientData.new.call
expect(Spree::StateChange.all).to be_empty
end
it 'deletes log entries older than retention_period' do
Spree::LogEntry.create(created_at: retention_period - 1.day)
it 'deletes log entries older than retention period' do
remover = RemoveTransientData.new
Spree::LogEntry.create(created_at: remover.expiration_date - 1.day)
expect { RemoveTransientData.new.call }
expect { remover.call }
.to change { Spree::LogEntry.count }.by(-1)
end
it 'deletes sessions older than retention_period' do
RemoveTransientData::Session.create(session_id: 1, updated_at: retention_period - 1.day)
it 'deletes sessions older than retention period' do
remover = RemoveTransientData.new
RemoveTransientData::Session.create(session_id: 1,
updated_at: remover.expiration_date - 1.day)
RemoveTransientData.new.call
remover.call
expect(RemoveTransientData::Session.all).to be_empty
end
@@ -44,16 +46,13 @@ RSpec.describe RemoveTransientData do
let!(:line_item) { create(:line_item, order: cart, variant:) }
let!(:adjustment) { create(:adjustment, order: cart) }
let!(:old_cart) { create(:order, state: 'cart', updated_at: retention_period - 1.day) }
let!(:remover) { RemoveTransientData.new }
let!(:old_cart) { create(:order, state: 'cart', updated_at: remover.expiration_date - 1.day) }
let!(:old_line_item) { create(:line_item, order: old_cart, variant:) }
let!(:old_adjustment) { create(:adjustment, order: old_cart) }
before do
old_cart.update_columns(updated_at: retention_period - 1.day)
end
it 'deletes cart orders and related objects older than retention_period' do
RemoveTransientData.new.call
it 'deletes cart orders and related objects older than retention period' do
remover.call
expect{ cart.reload }.not_to raise_error
expect{ line_item.reload }.not_to raise_error

View File

@@ -105,14 +105,7 @@ RSpec.describe '
Spree::Config[:enterprise_number_required_on_invoices?] = false
end
context "with multiple orders with differents states" do
before do
order2.update(state: "complete")
order3.update(state: "resumed")
order4.update(state: "canceled")
order5.update(state: "payment")
end
shared_examples "can bulk send confirmation email from 2 orders" do
it "can bulk send invoices per email, but only for the 'complete' or 'resumed' ones" do
within "#listing_orders" do
page.find("input[name='bulk_ids[]'][value='#{order2.id}']").click
@@ -122,6 +115,7 @@ RSpec.describe '
end
page.find("span.icon-reorder", text: "ACTIONS").click
within ".ofn-drop-down .menu" do
page.find("span", text: "Send Invoices").click
end
@@ -140,24 +134,65 @@ RSpec.describe '
end
end
it "can bulk send confirmation email from 2 orders" do
page.find("#listing_orders tbody tr:nth-child(1) input[name='bulk_ids[]']").click
page.find("#listing_orders tbody tr:nth-child(2) input[name='bulk_ids[]']").click
page.find("span.icon-reorder", text: "ACTIONS").click
within ".ofn-drop-down .menu" do
page.find("span", text: "Resend Confirmation").click
context "with multiple orders with differents states" do
before do
order2.update(state: "canceled")
order3.update(state: "payment")
order4.update(state: "complete")
order5.update(state: "resumed")
end
expect(page).to have_content "Are you sure you want to proceed?"
it_behaves_like "can bulk send confirmation email from 2 orders"
within ".reveal-modal" do
expect {
find_button("Confirm").click
}.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:twice)
describe "ABN" do
context "ABN is not required" do
before do
allow(Spree::Config).to receive(:enterprise_number_required_on_invoices?)
.and_return false
end
it_behaves_like "can bulk send confirmation email from 2 orders"
end
context "ABN is required" do
before do
allow(Spree::Config).to receive(:enterprise_number_required_on_invoices?)
.and_return true
end
context "All the distributors setup the ABN" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: "987654321")
end
context "all the orders are invoiceable (completed/resumed)" do
it_behaves_like "can bulk send confirmation email from 2 orders"
end
end
context "the distributor of one of the order didn't set the ABN" do
context "ABN is nil" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: nil)
end
context "with legal invoices feature disabled" do
before { pending("Emails are not sent in this case") }
it_behaves_like "can bulk send confirmation email from 2 orders"
end
end
context "ABN is an empty string" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: "")
end
context "with legal invoices feature disabled" do
before { pending("Emails are not sent in this case") }
it_behaves_like "can bulk send confirmation email from 2 orders"
end
end
end
end
end
expect(page).to have_content "Confirmation emails sent for 2 orders."
end
end
@@ -337,11 +372,6 @@ RSpec.describe '
end
end
context "the distributor of one of the order didn't set the ABN" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: nil)
end
shared_examples "should not print the invoice" do
it "should render a warning message" do
page.find(order4_selector).click
@@ -363,9 +393,36 @@ RSpec.describe '
} must have a valid ABN before invoices can be used."
end
end
it_behaves_like "should not print the invoice"
context "with legal invoices feature", feature: :invoices do
it_behaves_like "should not print the invoice"
context "ABN is nil" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: nil)
end
context "with legal invoices feature disabled" do
it_behaves_like "should not print the invoice"
end
context "with legal invoices feature", feature: :invoices do
it_behaves_like "should not print the invoice"
end
end
context "ABN is an empty string" do
before do
order4.distributor.update(abn: "123456789")
order5.distributor.update(abn: "")
end
context "with legal invoices feature disabled" do
it_behaves_like "can bulk print invoices from 2 orders"
end
context "with legal invoices feature", feature: :invoices do
before { pending("#12373") }
it_behaves_like "should not print the invoice"
end
end
end
end

View File

@@ -197,6 +197,30 @@ RSpec.describe '
expect(page).to have_content "Unit value is not a number"
end
it "creating product with empty product category" do
pending("#12591")
login_as_admin
visit spree.admin_products_path
click_link 'New Product'
fill_in 'product_name', with: 'Hot Cakes'
select 'New supplier', from: 'product_supplier_id'
select "Weight (kg)", from: 'product_variant_unit_with_scale'
fill_in "product_unit_value", with: '1'
fill_in 'product_price', with: '1.99'
fill_in 'product_on_hand', with: 0
check 'product_on_demand'
select 'Test Tax Category', from: 'product_tax_category_id'
fill_in_trix_editor 'product_description',
with: 'In demand, and on_demand! The hottest cakes in town.'
click_button 'Create'
expect(current_path).to eq spree.admin_products_path
expect(page).to have_content "Product Category must exist"
end
describe "localization settings" do
shared_examples "with different price values" do |localized_number, price|
context "when enable_localized_number is set to #{localized_number}" do

View File

@@ -14,12 +14,13 @@ RSpec.describe "Shops caching", caching: true do
}
describe "caching enterprises AMS data" do
it "caches data for all enterprises, with the provided options" do
before do
# Trigger lengthy tasks like JS compilation before testing caching:
visit shops_path
Rails.cache.clear
end
# Now run the test, hopefully in a timely manner:
it "caches data for all enterprises, with the provided options" do
visit shops_path
key, options = CacheService::FragmentCaching.ams_shops
@@ -73,6 +74,10 @@ RSpec.describe "Shops caching", caching: true do
before do
exchange.variants << product.variants.first
# Trigger lengthy tasks like JS compilation before testing caching:
visit enterprise_shop_path(distributor)
Rails.cache.clear
end
it "caches rendered response for taxons and properties, with the provided options" do