github.com/letsencrypt/boulder@v0.20251208.0/db/multi_test.go (about) 1 package db 2 3 import ( 4 "testing" 5 6 "github.com/letsencrypt/boulder/test" 7 ) 8 9 func TestNewMulti(t *testing.T) { 10 _, err := NewMultiInserter("", []string{"colA"}) 11 test.AssertError(t, err, "Empty table name should fail") 12 13 _, err = NewMultiInserter("myTable", nil) 14 test.AssertError(t, err, "Empty fields list should fail") 15 16 mi, err := NewMultiInserter("myTable", []string{"colA"}) 17 test.AssertNotError(t, err, "Single-column construction should not fail") 18 test.AssertEquals(t, len(mi.fields), 1) 19 20 mi, err = NewMultiInserter("myTable", []string{"colA", "colB", "colC"}) 21 test.AssertNotError(t, err, "Multi-column construction should not fail") 22 test.AssertEquals(t, len(mi.fields), 3) 23 24 _, err = NewMultiInserter("foo\"bar", []string{"colA"}) 25 test.AssertError(t, err, "expected error for invalid table name") 26 27 _, err = NewMultiInserter("myTable", []string{"colA", "foo\"bar"}) 28 test.AssertError(t, err, "expected error for invalid column name") 29 } 30 31 func TestMultiAdd(t *testing.T) { 32 mi, err := NewMultiInserter("table", []string{"a", "b", "c"}) 33 test.AssertNotError(t, err, "Failed to create test MultiInserter") 34 35 err = mi.Add([]any{}) 36 test.AssertError(t, err, "Adding empty row should fail") 37 38 err = mi.Add([]any{"foo"}) 39 test.AssertError(t, err, "Adding short row should fail") 40 41 err = mi.Add([]any{"foo", "bar", "baz", "bing", "boom"}) 42 test.AssertError(t, err, "Adding long row should fail") 43 44 err = mi.Add([]any{"one", "two", "three"}) 45 test.AssertNotError(t, err, "Adding correct-length row shouldn't fail") 46 test.AssertEquals(t, len(mi.values), 1) 47 48 err = mi.Add([]any{1, "two", map[string]int{"three": 3}}) 49 test.AssertNotError(t, err, "Adding heterogeneous row shouldn't fail") 50 test.AssertEquals(t, len(mi.values), 2) 51 // Note that .Add does *not* enforce that each row is of the same types. 52 } 53 54 func TestMultiQuery(t *testing.T) { 55 mi, err := NewMultiInserter("table", []string{"a", "b", "c"}) 56 test.AssertNotError(t, err, "Failed to create test MultiInserter") 57 err = mi.Add([]any{"one", "two", "three"}) 58 test.AssertNotError(t, err, "Failed to insert test row") 59 err = mi.Add([]any{"egy", "kettö", "három"}) 60 test.AssertNotError(t, err, "Failed to insert test row") 61 62 query, queryArgs := mi.query() 63 test.AssertEquals(t, query, "INSERT INTO table (a,b,c) VALUES (?,?,?),(?,?,?)") 64 test.AssertDeepEquals(t, queryArgs, []any{"one", "two", "three", "egy", "kettö", "három"}) 65 }