2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2019-01-09 23:57:35 +00:00
require './lib/qx.rb'
require 'minitest/autorun'
class UpsertTest < Minitest :: Test
2019-07-30 21:29:24 +00:00
def setup ; end
2019-01-09 23:57:35 +00:00
def test_upsert
table = 'x'
2019-07-30 21:29:24 +00:00
column1 = 'a'
2019-01-09 23:57:35 +00:00
column2 = 'b'
2019-07-30 21:29:24 +00:00
idx = 'idx_something_more'
2019-01-09 23:57:35 +00:00
2019-07-30 21:29:24 +00:00
result = Qx . insert_into ( table ) . values ( column1 : column1 , column2 : column2 ) . on_conflict . upsert ( idx ) . parse
2019-01-09 23:57:35 +00:00
2019-07-30 21:29:24 +00:00
expected = %( INSERT INTO " #{ table } " ( "column1", "column2" ) VALUES ( $Q$ #{ column1 } $Q$, $Q$ #{ column2 } $Q$ ) ON CONFLICT ON CONSTRAINT #{ idx } DO UPDATE SET "column1" = EXCLUDED."column1", "column2" = EXCLUDED."column2" )
2019-01-09 23:57:35 +00:00
assert_equal ( expected , result )
end
2019-07-30 21:29:24 +00:00
end