github.com/acoshift/pgsql@v0.15.3/pgstmt/insert_test.go (about)

     1  package pgstmt_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/acoshift/pgsql/pgstmt"
     9  )
    10  
    11  func TestInsert(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	t.Run("insert", func(t *testing.T) {
    15  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
    16  			b.Into("users")
    17  			b.Columns("username", "name", "created_at")
    18  			b.Value("tester1", "Tester 1", pgstmt.Default)
    19  			b.Value("tester2", "Tester 2", "now()")
    20  			b.OnConflictIndex("username").DoNothing()
    21  			b.Returning("id", "name")
    22  		}).SQL()
    23  
    24  		assert.Equal(t,
    25  			"insert into users (username, name, created_at) values ($1, $2, default), ($3, $4, $5) on conflict (username) do nothing returning id, name",
    26  			q,
    27  		)
    28  		assert.EqualValues(t,
    29  			[]any{
    30  				"tester1", "Tester 1",
    31  				"tester2", "Tester 2", "now()",
    32  			},
    33  			args,
    34  		)
    35  	})
    36  
    37  	t.Run("insert on conflict do nothing", func(t *testing.T) {
    38  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
    39  			b.Into("users")
    40  			b.Columns("username", "name")
    41  			b.Value("tester1", "Tester 1")
    42  			b.OnConflictDoNothing()
    43  			b.Returning("id")
    44  		}).SQL()
    45  
    46  		assert.Equal(t,
    47  			"insert into users (username, name) values ($1, $2) on conflict do nothing returning id",
    48  			q,
    49  		)
    50  		assert.EqualValues(t,
    51  			[]any{
    52  				"tester1", "Tester 1",
    53  			},
    54  			args,
    55  		)
    56  	})
    57  
    58  	t.Run("insert select", func(t *testing.T) {
    59  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
    60  			b.Into("films")
    61  			b.Select(func(b pgstmt.SelectStatement) {
    62  				b.Columns("*")
    63  				b.From("tmp_films")
    64  				b.Where(func(b pgstmt.Cond) {
    65  					b.LtRaw("date_prod", "2004-05-07")
    66  				})
    67  			})
    68  		}).SQL()
    69  
    70  		assert.Equal(t,
    71  			"insert into films select * from tmp_films where (date_prod < 2004-05-07)",
    72  			q,
    73  		)
    74  		assert.Empty(t, args)
    75  	})
    76  
    77  	t.Run("insert on conflict partial index do update", func(t *testing.T) {
    78  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
    79  			b.Into("users")
    80  			b.Columns("username", "email")
    81  			b.Value("tester1", "tester1@localhost")
    82  			b.OnConflict(func(b pgstmt.ConflictTarget) {
    83  				b.Index("username")
    84  				b.Where(func(b pgstmt.Cond) {
    85  					b.IsNull("deleted_at")
    86  				})
    87  			}).DoUpdate(func(b pgstmt.UpdateStatement) {
    88  				b.Set("email").ToRaw("excluded.email")
    89  				b.Set("updated_at").ToRaw("now()")
    90  			})
    91  			b.Returning("id")
    92  		}).SQL()
    93  
    94  		assert.Equal(t,
    95  			stripSpace(`
    96  				insert into users (username, email)
    97  				values ($1, $2)
    98  				on conflict (username) where (deleted_at is null) do update
    99  				set email = excluded.email,
   100  					updated_at = now()
   101  				returning id
   102  			`),
   103  			q,
   104  		)
   105  		assert.EqualValues(t,
   106  			[]any{
   107  				"tester1", "tester1@localhost",
   108  			},
   109  			args,
   110  		)
   111  	})
   112  
   113  	t.Run("insert on conflict index do update", func(t *testing.T) {
   114  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
   115  			b.Into("users")
   116  			b.Columns("username", "email")
   117  			b.Value("tester1", "tester1@localhost")
   118  			b.OnConflictIndex("username").DoUpdate(func(b pgstmt.UpdateStatement) {
   119  				b.Set("email").ToRaw("excluded.email")
   120  				b.Set("updated_at").ToRaw("now()")
   121  			})
   122  			b.Returning("id")
   123  		}).SQL()
   124  
   125  		assert.Equal(t,
   126  			stripSpace(`
   127  				insert into users (username, email)
   128  				values ($1, $2)
   129  				on conflict (username) do update
   130  				set email = excluded.email,
   131  					updated_at = now()
   132  				returning id
   133  			`),
   134  			q,
   135  		)
   136  		assert.EqualValues(t,
   137  			[]any{
   138  				"tester1", "tester1@localhost",
   139  			},
   140  			args,
   141  		)
   142  	})
   143  
   144  	t.Run("insert on conflict on constraint do update", func(t *testing.T) {
   145  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
   146  			b.Into("users")
   147  			b.Columns("username", "email")
   148  			b.Value("tester1", "tester1@localhost")
   149  			b.OnConflictOnConstraint("username_key").DoUpdate(func(b pgstmt.UpdateStatement) {
   150  				b.Set("email").ToRaw("excluded.email")
   151  				b.Set("updated_at").ToRaw("now()")
   152  			})
   153  			b.Returning("id")
   154  		}).SQL()
   155  
   156  		assert.Equal(t,
   157  			stripSpace(`
   158  				insert into users (username, email)
   159  				values ($1, $2)
   160  				on conflict on constraint username_key do update
   161  				set email = excluded.email,
   162  					updated_at = now()
   163  				returning id
   164  			`),
   165  			q,
   166  		)
   167  		assert.EqualValues(t,
   168  			[]any{
   169  				"tester1", "tester1@localhost",
   170  			},
   171  			args,
   172  		)
   173  	})
   174  
   175  	t.Run("values", func(t *testing.T) {
   176  		q, args := pgstmt.Insert(func(b pgstmt.InsertStatement) {
   177  			b.Into("users")
   178  			b.Columns("username", "name")
   179  			b.Values([][]any{
   180  				{"tester1", "Tester 1"},
   181  				{"tester2", "Tester 2"},
   182  			}...)
   183  		}).SQL()
   184  
   185  		assert.Equal(t,
   186  			stripSpace(`
   187  				insert into users (username, name)
   188  				values ($1, $2),
   189  				       ($3, $4)
   190              `),
   191  			q,
   192  		)
   193  		assert.EqualValues(t,
   194  			[]any{
   195  				"tester1", "Tester 1",
   196  				"tester2", "Tester 2",
   197  			},
   198  			args,
   199  		)
   200  	})
   201  }