mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-31 21:37:16 +00:00
We found that our database is quite busy querying the country all the time. One source of these queries is the shop front which serializes enterprises including their addresses. But addresses should be safe to cache because: - country records never change - state records never change - updating any other attribute changes the cache key The caching was previously removed when the state_name attribute was added. That is technically correct because it's possible to change a state's name without updating the address. Then the cache would be out of date. But we never update state names. And if we did, we would need to invalidate the cache now.
41 lines
905 B
Ruby
41 lines
905 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
describe Api::AddressSerializer do
|
|
subject(:serializer) { described_class.new(address) }
|
|
let(:address) { build(:address) }
|
|
|
|
describe "#country_name" do
|
|
it "provides the country's name" do
|
|
address.country.name = "Australia"
|
|
expect(serializer.country_name).to eq "Australia"
|
|
end
|
|
end
|
|
|
|
describe "#state_name" do
|
|
it "provides the state's abbreviation" do
|
|
address.state.abbr = "Vic"
|
|
expect(serializer.state_name).to eq "Vic"
|
|
end
|
|
end
|
|
|
|
describe "caching" do
|
|
it "updates with the record" do
|
|
expect {
|
|
address.update!(first_name: "Nick")
|
|
}.to change {
|
|
serializer.to_json
|
|
}
|
|
end
|
|
|
|
it "uses stored result when database wasn't changed" do
|
|
expect {
|
|
address.first_name = "Nick"
|
|
}.to_not change {
|
|
serializer.to_json
|
|
}
|
|
end
|
|
end
|
|
end
|