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
|
require_relative "spec_helper"
describe "Sequel::Plugins::UpdateOrCreate" do
before do
@db = Sequel.mock(:autoid=>proc{1}, :numrows=>1)
@c = Class.new(Sequel::Model(@db[:test]))
@c.plugin :update_or_create
@c.columns :id, :a, :b
@db.sqls
end
it ".update_or_create should update an existing record if one exists" do
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.update_or_create(:a=>2){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>2, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1", "UPDATE test SET b = 4 WHERE (id = 1)"]
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.update_or_create({:a=>2}, :b=>4).must_equal @c.load(:id=>1, :a=>2, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1", "UPDATE test SET b = 4 WHERE (id = 1)"]
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.update_or_create({:a=>2}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1",
'UPDATE test SET a = 3, b = 4 WHERE (id = 1)']
end
it ".update_or_create should create a record if an existing record does not exist" do
@db.fetch = [[], [{:id=>1, :a=>1, :b=>4}]]
@c.update_or_create(:a=>1){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>1, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1",
"INSERT INTO test (a, b) VALUES (1, 4)",
"SELECT * FROM test WHERE (id = 1) LIMIT 1"]
@db.fetch = [[], [{:id=>1, :a=>1, :b=>4}]]
@c.update_or_create({:a=>1}, :b=>4).must_equal @c.load(:id=>1, :a=>1, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1",
"INSERT INTO test (a, b) VALUES (1, 4)",
"SELECT * FROM test WHERE (id = 1) LIMIT 1"]
@db.fetch = [[], [{:id=>1, :a=>3, :b=>4}]]
@c.update_or_create({:a=>1}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1",
"INSERT INTO test (a, b) VALUES (3, 4)",
"SELECT * FROM test WHERE (id = 1) LIMIT 1"]
end
it ".update_or_create should return an existing record even if no changes necessary" do
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.update_or_create(:a=>2){|t| t.b = 3}.must_equal @c.load(:id=>1, :a=>2, :b=>3)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
end
it ".find_or_new should return an existing record" do
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.find_or_new(:a=>2){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>2, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.find_or_new({:a=>2}, :b=>4).must_equal @c.load(:id=>1, :a=>2, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
@db.fetch = [[{:id=>1, :a=>2, :b=>3}]]
@c.find_or_new({:a=>2}, :a=>3){|t| t.b = 4}.must_equal @c.load(:id=>1, :a=>3, :b=>4)
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 2) LIMIT 1"]
end
it ".find_or_new should return a new record if no record exists" do
o = @c.find_or_new(:a=>1){|t| t.b = 4}
o.must_equal @c.load(:a=>1, :b=>4)
o.new?.must_equal true
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
o = @c.find_or_new({:a=>1}, :b=>4)
o.must_equal @c.load(:a=>1, :b=>4)
o.new?.must_equal true
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
o = @c.find_or_new({:a=>1}, :a=>3){|t| t.b = 4}
o.must_equal @c.load(:a=>3, :b=>4)
o.new?.must_equal true
@db.sqls.must_equal ["SELECT * FROM test WHERE (a = 1) LIMIT 1"]
end
end
|