|
846 | 846 | end |
847 | 847 | end |
848 | 848 |
|
| 849 | + describe 'cache_attribute_values option' do |
| 850 | + context 'when not set in the config' do |
| 851 | + it 'defaults to false' do |
| 852 | + Mongoid::Config.reset |
| 853 | + configuration = CONFIG.merge(options: {}) |
| 854 | + |
| 855 | + Mongoid.configure { |config| config.load_configuration(configuration) } |
| 856 | + |
| 857 | + expect(Mongoid::Config.cache_attribute_values).to be(false) |
| 858 | + end |
| 859 | + end |
| 860 | + |
| 861 | + context 'when set to true in the config' do |
| 862 | + it 'enables field value caching' do |
| 863 | + Mongoid::Config.reset |
| 864 | + configuration = CONFIG.merge(options: { cache_attribute_values: true }) |
| 865 | + |
| 866 | + Mongoid.configure { |config| config.load_configuration(configuration) } |
| 867 | + |
| 868 | + expect(Mongoid::Config.cache_attribute_values).to be(true) |
| 869 | + end |
| 870 | + end |
| 871 | + |
| 872 | + context 'when set to false in the config' do |
| 873 | + it 'disables field value caching' do |
| 874 | + Mongoid::Config.reset |
| 875 | + configuration = CONFIG.merge(options: { cache_attribute_values: false }) |
| 876 | + |
| 877 | + Mongoid.configure { |config| config.load_configuration(configuration) } |
| 878 | + |
| 879 | + expect(Mongoid::Config.cache_attribute_values).to be(false) |
| 880 | + end |
| 881 | + end |
| 882 | + |
| 883 | + context 'functional behavior' do |
| 884 | + let(:band_class) do |
| 885 | + Class.new do |
| 886 | + include Mongoid::Document |
| 887 | + store_in collection: 'bands' |
| 888 | + field :name, type: String |
| 889 | + field :updated, type: Time |
| 890 | + end |
| 891 | + end |
| 892 | + |
| 893 | + before do |
| 894 | + stub_const('CacheBand', band_class) |
| 895 | + end |
| 896 | + |
| 897 | + it 'uses caching when enabled' do |
| 898 | + Mongoid::Config.cache_attribute_values = true |
| 899 | + |
| 900 | + band = CacheBand.new(name: 'Test', updated: Time.current) |
| 901 | + |
| 902 | + # First access should populate cache |
| 903 | + first_result = band.updated |
| 904 | + |
| 905 | + # Second access should return cached value (same object_id if caching works) |
| 906 | + second_result = band.updated |
| 907 | + |
| 908 | + expect(first_result.object_id).to eq(second_result.object_id) |
| 909 | + end |
| 910 | + |
| 911 | + it 'does not use caching when disabled' do |
| 912 | + Mongoid::Config.cache_attribute_values = false |
| 913 | + |
| 914 | + band = CacheBand.new(name: 'Test', updated: Time.current) |
| 915 | + |
| 916 | + # Each access should call process_raw_attribute |
| 917 | + first_result = band.updated |
| 918 | + second_result = band.updated |
| 919 | + |
| 920 | + # When caching is disabled, cache objects should not be initialized |
| 921 | + expect(band.instance_variable_get(:@__demongoized_cache)).to be_nil |
| 922 | + expect(band.instance_variable_get(:@__projector_cache)).to be_nil |
| 923 | + end |
| 924 | + end |
| 925 | + end |
| 926 | + |
849 | 927 | describe 'deprecations' do |
850 | 928 | {}.each do |option, default| |
851 | 929 |
|
|
0 commit comments