github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/zorder/zorder_test.go (about) 1 package zorder_test 2 3 import ( 4 "testing" 5 6 "github.com/ncruces/go-sqlite3" 7 "github.com/ncruces/go-sqlite3/driver" 8 _ "github.com/ncruces/go-sqlite3/embed" 9 "github.com/ncruces/go-sqlite3/ext/zorder" 10 _ "github.com/ncruces/go-sqlite3/tests/testcfg" 11 ) 12 13 func TestRegister_zorder(t *testing.T) { 14 t.Parallel() 15 16 db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error { 17 zorder.Register(c) 18 return nil 19 }) 20 if err != nil { 21 t.Fatal(err) 22 } 23 defer db.Close() 24 25 var got int64 26 err = db.QueryRow(`SELECT zorder(2, 3)`).Scan(&got) 27 if err != nil { 28 t.Fatal(err) 29 } 30 if got != 14 { 31 t.Errorf("got %d, want 14", got) 32 } 33 34 err = db.QueryRow(`SELECT zorder(4, 5)`).Scan(&got) 35 if err != nil { 36 t.Fatal(err) 37 } 38 if got != 50 { 39 t.Errorf("got %d, want 14", got) 40 } 41 42 var check bool 43 err = db.QueryRow(`SELECT zorder(3, 4) BETWEEN zorder(2, 3) AND zorder(4, 5)`).Scan(&check) 44 if err != nil { 45 t.Fatal(err) 46 } 47 if !check { 48 t.Error("want true") 49 } 50 51 err = db.QueryRow(`SELECT zorder(2, 2) NOT BETWEEN zorder(2, 3) AND zorder(4, 5)`).Scan(&check) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if !check { 56 t.Error("want true") 57 } 58 } 59 60 func TestRegister_unzorder(t *testing.T) { 61 t.Parallel() 62 63 db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error { 64 zorder.Register(c) 65 return nil 66 }) 67 if err != nil { 68 t.Fatal(err) 69 } 70 defer db.Close() 71 72 var got int64 73 err = db.QueryRow(`SELECT unzorder(zorder(3, 4), 2, 0)`).Scan(&got) 74 if err != nil { 75 t.Fatal(err) 76 } 77 if got != 3 { 78 t.Errorf("got %d, want 3", got) 79 } 80 81 err = db.QueryRow(`SELECT unzorder(zorder(3, 4), 2, 1)`).Scan(&got) 82 if err != nil { 83 t.Fatal(err) 84 } 85 if got != 4 { 86 t.Errorf("got %d, want 4", got) 87 } 88 } 89 90 func TestRegister_error(t *testing.T) { 91 t.Parallel() 92 93 db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error { 94 zorder.Register(c) 95 return nil 96 }) 97 if err != nil { 98 t.Fatal(err) 99 } 100 defer db.Close() 101 102 var got int64 103 err = db.QueryRow(`SELECT zorder(1, 2, 3, 100000)`).Scan(&got) 104 if err == nil { 105 t.Error("want error") 106 } 107 }