File: update_refresh_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 (72 lines) | stat: -rw-r--r-- 2,945 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
72
require_relative "spec_helper"

describe "Sequel::Plugins::UpdateRefresh" do
  before do
    @db = Sequel.mock(:numrows=>1, :fetch=>{:id=>1, :name=>'b'})
    @c = Class.new(Sequel::Model(@db[:test]))
    @ds = @c.dataset
    @c.columns :id, :name
    @c.plugin :update_refresh
    @db.sqls
  end

  it "should refresh the instance after updating" do
    o = @c.load(:id=>1, :name=>'a')
    o.save
    @db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1)", "SELECT * FROM test WHERE (id = 1) LIMIT 1"]
    o.name.must_equal 'b'
  end

  it "should refresh the instance after updating" do
    @db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
    @c.dataset = @db[:test]
    @db.sqls
    o = @c.load(:id=>1, :name=>'a')
    o.save
    @db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING *"]
    o.name.must_equal 'b'
  end

  it "should support specifying columns to return" do
    @db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
    @c.plugin :update_refresh, :columns => [ :a ]
    @c.dataset = @db[:test]
    @db.sqls
    o = @c.load(:id=>1, :name=>'a')
    o.save
    @db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING a"]
    o.name.must_equal 'b'
  end

  it "should refresh the instance after updating when returning specific columns" do
    @db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
    @c.plugin :insert_returning_select
    @c.dataset = @db[:test].select(:id, :name)
    @db.sqls
    o = @c.load(:id=>1, :name=>'a')
    o.instance_variable_set(:@this, o.this.returning(:id, :name))
    o.save
    @db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING id, name"]
    o.name.must_equal 'b'
  end

  it "should raise error without refreshing when updating when returning specific columns modifies multiple rows" do
    @db.fetch = [{:id=>1, :name=>'b'}, {:id=>1, :name=>'c'}]
    @db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
    @c.plugin :insert_returning_select
    @c.dataset = @db[:test].select(:id, :name)
    @db.sqls
    o = @c.load(:id=>1, :name=>'a')
    o.instance_variable_set(:@this, o.this.returning(:id, :name))
    proc{o.save}.must_raise Sequel::NoExistingObject
    o.values.must_equal(:id=>1, :name=>'a')
    @db.sqls.must_equal ["UPDATE test SET name = 'a' WHERE (id = 1) RETURNING id, name"]
  end

  it "should freeze update refresh columns when freezing model class" do
    @db.extend_datasets{def supports_returning?(x) true end; def update_sql(*); sql = super; update_returning_sql(sql); sql end}
    @c.plugin :update_refresh, :columns => [ :a ]
    @c.freeze
    @c.update_refresh_columns.frozen?.must_equal true
  end
end