github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/tests/driver_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/ncruces/go-sqlite3/driver"
     8  	_ "github.com/ncruces/go-sqlite3/embed"
     9  	_ "github.com/ncruces/go-sqlite3/tests/testcfg"
    10  )
    11  
    12  func TestDriver(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	ctx, cancel := context.WithCancel(context.Background())
    16  	defer cancel()
    17  
    18  	db, err := driver.Open(":memory:", nil)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer db.Close()
    23  
    24  	conn, err := db.Conn(ctx)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	defer conn.Close()
    29  
    30  	res, err := conn.ExecContext(ctx,
    31  		`CREATE TABLE users (id INT, name VARCHAR(10))`)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	changes, err := res.RowsAffected()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	if changes != 0 {
    40  		t.Errorf("got %d want 0", changes)
    41  	}
    42  	id, err := res.LastInsertId()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if id != 0 {
    47  		t.Errorf("got %d want 0", changes)
    48  	}
    49  
    50  	res, err = conn.ExecContext(ctx,
    51  		`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	changes, err = res.RowsAffected()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if changes != 3 {
    60  		t.Errorf("got %d want 3", changes)
    61  	}
    62  
    63  	stmt, err := conn.PrepareContext(context.Background(),
    64  		`SELECT id, name FROM users`)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	defer stmt.Close()
    69  
    70  	rows, err := stmt.Query()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	defer rows.Close()
    75  
    76  	typs, err := rows.ColumnTypes()
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if got := typs[0].DatabaseTypeName(); got != "INT" {
    81  		t.Errorf("got %s, want INT", got)
    82  	}
    83  	if got := typs[1].DatabaseTypeName(); got != "VARCHAR" {
    84  		t.Errorf("got %s, want INT", got)
    85  	}
    86  
    87  	row := 0
    88  	ids := []int{0, 1, 2}
    89  	names := []string{"go", "zig", "whatever"}
    90  	for ; rows.Next(); row++ {
    91  		var id int
    92  		var name string
    93  		err := rows.Scan(&id, &name)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  
    98  		if id != ids[row] {
    99  			t.Errorf("got %d, want %d", id, ids[row])
   100  		}
   101  		if name != names[row] {
   102  			t.Errorf("got %q, want %q", name, names[row])
   103  		}
   104  	}
   105  	if row != 3 {
   106  		t.Errorf("got %d, want %d", row, len(ids))
   107  	}
   108  
   109  	err = rows.Close()
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	err = stmt.Close()
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	err = conn.Close()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	err = db.Close()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  }