database_helper_spec.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe DatabaseHelper do
  4. context 'when a replica is enabled' do
  5. around do |example|
  6. ClimateControl.modify REPLICA_DB_NAME: 'prod-relay-quantum-tunnel-mirror' do
  7. example.run
  8. end
  9. end
  10. before { allow(ApplicationRecord).to receive(:connected_to) }
  11. describe '#with_read_replica' do
  12. it 'uses the replica for connections' do
  13. helper.with_read_replica { _x = 1 }
  14. expect(ApplicationRecord)
  15. .to have_received(:connected_to).with(role: :reading, prevent_writes: true)
  16. end
  17. end
  18. describe '#with_primary' do
  19. it 'uses the primary for connections' do
  20. helper.with_primary { _x = 1 }
  21. expect(ApplicationRecord)
  22. .to have_received(:connected_to).with(role: :writing)
  23. end
  24. end
  25. end
  26. context 'when a replica is not enabled' do
  27. around do |example|
  28. ClimateControl.modify REPLICA_DB_NAME: nil do
  29. example.run
  30. end
  31. end
  32. before { allow(ApplicationRecord).to receive(:connected_to) }
  33. describe '#with_read_replica' do
  34. it 'does not use the replica for connections' do
  35. helper.with_read_replica { _x = 1 }
  36. expect(ApplicationRecord)
  37. .to_not have_received(:connected_to).with(role: :reading, prevent_writes: true)
  38. end
  39. end
  40. describe '#with_primary' do
  41. it 'does not use the primary for connections' do
  42. helper.with_primary { _x = 1 }
  43. expect(ApplicationRecord)
  44. .to_not have_received(:connected_to).with(role: :writing)
  45. end
  46. end
  47. end
  48. end