github.com/dolthub/go-mysql-server@v0.18.0/driver/e2e_test.go (about) 1 package driver_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/dolthub/go-mysql-server/sql/types" 11 ) 12 13 func TestQuery(t *testing.T) { 14 mtb, records := personMemTable("db", "person") 15 db := sqlOpen(t, mtb, t.Name()+"?jsonAs=object") 16 now := time.Now() 17 18 var id uint64 19 var name, email string 20 var numbers []any 21 var created time.Time 22 var count int 23 var blob []byte 24 25 cases := []struct { 26 Name, Query string 27 Args []any 28 Pointers Pointers 29 Expect Records 30 }{ 31 {"Select All", "SELECT * FROM db.person", nil, []any{&id, &name, &email, &numbers, &created}, records}, 32 {"Select First", "SELECT * FROM db.person LIMIT 1", nil, []any{&id, &name, &email, &numbers, &created}, records.Rows(0)}, 33 {"Select Name", "SELECT name FROM db.person", nil, []any{&name}, records.Columns(1)}, 34 {"Select Count", "SELECT COUNT(1) FROM db.person", nil, []any{&count}, Records{{len(records)}}}, 35 36 {"Insert", `INSERT INTO db.person (name, email, phone_numbers, created_at) VALUES ('foo', 'bar', '["baz"]', NOW())`, nil, []any{}, Records{}}, 37 {"Select Inserted", "SELECT name, email, phone_numbers FROM db.person WHERE name = 'foo'", nil, []any{&name, &email, &numbers}, Records{{"foo", "bar", []any{"baz"}}}}, 38 39 {"Update", "UPDATE db.person SET name = 'asdf' WHERE name = 'foo'", nil, []any{}, Records{}}, 40 {"Delete", "DELETE FROM db.person WHERE name = 'asdf'", nil, []any{}, Records{}}, 41 42 {"Select Binary Args", `SELECT ?`, []any{[]byte{1, 2, 3}}, []any{&blob}, Records{{[]byte{1, 2, 3}}}}, 43 {"Insert With time.Time", `INSERT INTO db.person (name, email, phone_numbers, created_at) VALUES ('foo', 'bar', '["baz"]', ?)`, []any{now}, []any{}, Records{}}, 44 } 45 46 for _, c := range cases { 47 t.Run(c.Name, func(t *testing.T) { 48 rows, err := db.Query(c.Query, c.Args...) 49 require.NoError(t, err, "Query") 50 51 var i int 52 for ; rows.Next(); i++ { 53 require.NoError(t, rows.Scan(c.Pointers...), "Scan") 54 values := c.Pointers.Values() 55 56 if i >= len(c.Expect) { 57 t.Errorf("Got row %d, expected %d total: %v", i+1, len(c.Expect), values) 58 continue 59 } 60 61 expect := c.Expect[i] 62 if !assert.Equal(t, len(expect), len(values)) { 63 continue 64 } 65 for i := range expect { 66 expect := expect[i] 67 actual := values[i] 68 if jv, ok := expect.(types.JSONDocument); ok { 69 expect = jv.Val 70 } 71 assert.Equal(t, expect, actual, "Values") 72 } 73 } 74 75 require.NoError(t, rows.Err(), "Rows.Err") 76 77 if i < len(c.Expect) { 78 t.Errorf("Expected %d row(s), got %d", len(c.Expect), i) 79 } 80 }) 81 } 82 } 83 84 func TestExec(t *testing.T) { 85 mtb, records := personMemTable("db", "person") 86 db := sqlOpen(t, mtb, t.Name()) 87 88 cases := []struct { 89 Name, Statement string 90 RowsAffected int 91 }{ 92 {"Insert", `INSERT INTO db.person (name, email, phone_numbers, created_at) VALUES ('asdf', 'qwer', '["zxcv"]', NOW())`, 1}, 93 {"Update", "UPDATE db.person SET name = 'foo' WHERE name = 'asdf'", 1}, 94 {"Delete", "DELETE FROM db.person WHERE name = 'foo'", 1}, 95 {"Delete All", "DELETE FROM db.person WHERE LENGTH(name) < 100", len(records)}, 96 } 97 98 for _, c := range cases { 99 t.Run(c.Name, func(t *testing.T) { 100 res, err := db.Exec(c.Statement) 101 require.NoError(t, err, "Exec") 102 103 count, err := res.RowsAffected() 104 require.NoError(t, err, "RowsAffected") 105 assert.EqualValues(t, c.RowsAffected, count, "RowsAffected") 106 }) 107 } 108 109 errCases := []struct { 110 Name, Statement string 111 Error string 112 }{ 113 {"Select", "SELECT * FROM db.person", "no result"}, 114 } 115 116 for _, c := range errCases { 117 t.Run(c.Name, func(t *testing.T) { 118 res, err := db.Exec(c.Statement) 119 require.NoError(t, err, "Exec") 120 121 _, err = res.RowsAffected() 122 require.Error(t, err, "RowsAffected") 123 assert.Equal(t, c.Error, err.Error()) 124 }) 125 } 126 }