File: index_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 (71 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download | duplicates (3)
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
require_relative "spec_helper"

describe "index_caching extension" do
  before do
    @db = Sequel.connect('mock://postgres').extension(:index_caching)
    @indexes = {'"table"'=>{:table_idx_unique=>{:columns=>[:first_col, :second_col], :unique=>true, :deferrable=>nil}}}
    @filename = "spec/files/test_indexes_#$$.dump"
    @db.instance_variable_set(:@indexes, @indexes)
  end
  after do
    File.delete(@filename) if File.exist?(@filename)
  end

  it "#indexes should return cached index information" do
    @db.indexes(:table).must_equal @indexes['"table"']
    @db.indexes(:table, {}).must_equal @indexes['"table"']
  end 

  it "#indexes should skip cached information if given options" do
    @db.indexes(:table, :schema=>:b).must_equal({})
  end 

  it "Database should remove cached indexes when schema is changed" do
    @db.create_table(:table){Integer :a}
    @db.indexes(:table).must_equal({})
  end

  it "Database#freeze should allow cached information to work" do
    @db.freeze.indexes(:table).must_equal @indexes['"table"']
  end
  
  it "Database#freeze should allow removing index information" do
    @db.freeze
    @db.create_table(:table){Integer :a}
    @db.indexes(:table).must_equal({})
  end

  it "Database#dump_index_cache should dump the index cache to the given file" do
    File.exist?(@filename).must_equal false
    @db.dump_index_cache(@filename)
    File.exist?(@filename).must_equal true
    File.size(@filename).must_be :>,  0
  end

  it "Database#load_index_cache should load the index cache from the given file dumped by #dump_index_cache" do
    @db.dump_index_cache(@filename)
    db = Sequel::Database.new.extension(:index_caching)
    db.load_index_cache(@filename)
    db.extension(:index_caching)
    @db.instance_variable_get(:@indexes).must_equal @indexes
  end

  it "Database#dump_index_cache? should dump the index cache to the given file unless the file exists" do
    @db.dump_index_cache?(@filename)
    File.size(@filename).wont_equal 0
    File.open(@filename, 'wb'){|f|}
    File.size(@filename).must_equal 0
    @db.dump_index_cache?(@filename)
    File.size(@filename).must_equal 0
  end

  it "Database#load_index_cache? should load the index cache from the given file if it exists" do
    db = Sequel::Database.new.extension(:index_caching)
    File.exist?(@filename).must_equal false
    db.load_index_cache?(@filename)
    db.instance_variable_get(:@indexes).must_equal({})
    @db.dump_index_cache(@filename)
    db.load_index_cache?(@filename)
    @db.instance_variable_get(:@indexes).must_equal @indexes
  end
end