File: shared_caching_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (156 lines) | stat: -rw-r--r-- 5,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require_relative "spec_helper"

describe "Shared caching behavior" do
  before do
    @db = Sequel.mock

    class ::LookupModel < ::Sequel::Model(@db)
      columns :id, :caching_model_id, :caching_model_id2
      many_to_one :caching_model
      many_to_one :caching_model2, :key=>[:caching_model_id, :caching_model_id2], :class=>:CachingModel
    end
    @c = LookupModel

    class ::CachingModel < Sequel::Model(@db)
      columns :id, :id2
    end
    @cc = CachingModel
  end
  after do
    Object.send(:remove_const, :CachingModel)
    Object.send(:remove_const, :LookupModel)
  end

  many_to_one_cpk_specs = Module.new do
    extend Minitest::Spec::DSL

    it "should use a simple primary key lookup when retrieving many_to_one associated records with a composite key" do
      @db.sqls.must_equal []
      @c.load(:id=>3, :caching_model_id=>1, :caching_model_id2=>2).caching_model2.must_be_same_as(@cm12)
      @c.load(:id=>3, :caching_model_id=>2, :caching_model_id2=>1).caching_model2.must_be_same_as(@cm21)
      @db.sqls.must_equal []
      @db.fetch = []
      @c.load(:id=>4, :caching_model_id=>2, :caching_model_id2=>2).caching_model2.must_be_nil
    end
  end

  many_to_one_pk_specs = Module.new do
    extend Minitest::Spec::DSL

    it "should use a simple primary key lookup when retrieving many_to_one associated records" do
      @cc.set_primary_key([:id, :id2])
      @db.sqls.must_equal []
      @c.load(:id=>3, :caching_model_id=>1).caching_model.must_be_same_as(@cm1)
      @c.load(:id=>4, :caching_model_id=>2).caching_model.must_be_same_as(@cm2)
      @db.sqls.must_equal []
      @db.fetch = []
      @c.load(:id=>4, :caching_model_id=>3).caching_model.must_be_nil
    end

    it "should not use a simple primary key lookup if the assocation has a nil :key option" do
      @c.many_to_one :caching_model, :key=>nil, :dataset=>proc{CachingModel.filter(:caching_model_id=>caching_model_id)}
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation has a nil :key option" do
      @c.many_to_one :caching_model, :many_to_one_pk_lookup=>false
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation's :primary_key option doesn't match the primary key of the associated class" do
      @c.many_to_one :caching_model, :primary_key=>:id2
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation has :conditions" do
      @c.many_to_one :caching_model, :conditions=>{:a=>1}
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation has :select" do
      @c.many_to_one :caching_model, :select=>[:a, :b]
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation has a block" do
      @c.many_to_one(:caching_model){|ds| ds.where{a > 1}}
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should not use a simple primary key lookup if the assocation has a non-default :dataset option" do
      cc = @cc
      @c.many_to_one :caching_model, :dataset=>proc{cc.where(:id=>caching_model_id)}
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.wont_equal []
    end

    it "should use a simple primary key lookup if explicitly set" do
      @c.many_to_one :caching_model, :select=>[:a, :b], :many_to_one_pk_lookup=>true
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.must_equal []
    end
  end

  describe "With caching plugin" do
    before do
      @cache_class = Class.new(Hash) do
        attr_accessor :ttl
        def set(k, v, ttl); self[k] = v; @ttl = ttl; end
        def get(k); self[k]; end
      end
      cache = @cache_class.new
      @cache = cache
      
      @cc.plugin :caching, @cache
      @db.fetch = {:id=>1}
      @cm1 = @cc[1]
      @cm2 = @cc[2]
      @cm12 = @cc[1, 2]
      @cm21 = @cc[2, 1]

      @db.sqls
    end

    include many_to_one_cpk_specs
    include many_to_one_pk_specs
  end

  describe "With static_cache plugin with single key" do
    before do
      @db.fetch = [{:id=>1}, {:id=>2}]
      @cc.plugin :static_cache
      @cm1 = @cc[1]
      @cm2 = @cc[2]
      @db.sqls
    end

    include many_to_one_pk_specs

    it "should not issue regular query if primary key lookup returns no rows" do
      def @cc.primary_key_lookup(pk); end
      @cc.singleton_class.send(:private, :primary_key_lookup)
      @c.many_to_one :caching_model
      @c.load(:id=>3, :caching_model_id=>1).caching_model
      @db.sqls.must_equal []
    end
  end

  describe "With static_cache plugin with composite key" do
    before do
      @cc.set_primary_key([:id, :id2])
      @db.fetch = [{:id=>1, :id2=>2}, {:id=>2, :id2=>1}]
      @cc.plugin :static_cache
      @cm12 = @cc[[1, 2]]
      @cm21 = @cc[[2, 1]]
      @db.sqls
    end

    include many_to_one_cpk_specs
  end
end