More tests.

This commit is contained in:
Nuno Cruces
2023-01-26 02:48:31 +00:00
parent 17b720a217
commit a327cf5e08
4 changed files with 314 additions and 4 deletions

View File

@@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="108" height="20" role="img" aria-label="coverage: 50.6%"><title>coverage: 50.6%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="108" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="47" height="20" fill="#fe7d37"/><rect width="108" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">50.6%</text><text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="370">50.6%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="108" height="20" role="img" aria-label="coverage: 56.6%"><title>coverage: 56.6%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="108" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="47" height="20" fill="#fe7d37"/><rect width="108" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">56.6%</text><text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="370">56.6%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -1,6 +1,7 @@
package sqlite3_test
import (
"errors"
"os"
"path/filepath"
"testing"
@@ -23,6 +24,23 @@ func TestOpen_file(t *testing.T) {
testOpen(t, filepath.Join(dir, "test.db"))
}
func TestOpen_dir(t *testing.T) {
_, err := sqlite3.Open(".")
if err == nil {
t.Fatal("want error")
}
var serr *sqlite3.Error
if !errors.As(err, &serr) {
t.Fatal("want sqlite3.Error")
}
if serr.Code != sqlite3.CANTOPEN {
t.Fatal("want sqlite3.CANTOPEN")
}
if got := err.Error(); got != "sqlite3: unable to open database file" {
t.Fatal("got message: ", got)
}
}
func testOpen(t *testing.T, name string) {
db, err := sqlite3.Open(name)
if err != nil {
@@ -51,17 +69,17 @@ func testOpen(t *testing.T, name string) {
idx := 0
for ; stmt.Step(); idx++ {
if ids[idx] != stmt.ColumnInt(0) {
t.Errorf("expected %d got %d", ids[idx], stmt.ColumnInt(0))
t.Errorf("want %d got %d", ids[idx], stmt.ColumnInt(0))
}
if names[idx] != stmt.ColumnText(1) {
t.Errorf("expected %q got %q", names[idx], stmt.ColumnText(1))
t.Errorf("want %q got %q", names[idx], stmt.ColumnText(1))
}
}
if err := stmt.Err(); err != nil {
t.Fatal(err)
}
if idx != 3 {
t.Errorf("expected %d rows got %d", len(ids), idx)
t.Errorf("want %d rows got %d", len(ids), idx)
}
err = stmt.Close()

157
mock_test.go Normal file
View File

@@ -0,0 +1,157 @@
package sqlite3
import (
"context"
"encoding/binary"
"math"
"github.com/tetratelabs/wazero/api"
)
var (
_ api.Module = &MockModule{}
_ api.Memory = &MockMemory{}
)
type MockModule struct {
memory api.Memory
}
func (m *MockModule) Memory() api.Memory { return m.memory }
func (m *MockModule) String() string { return "MockModule" }
func (m *MockModule) Name() string { return "MockModule" }
func (m *MockModule) ExportedGlobal(name string) api.Global { return nil }
func (m *MockModule) ExportedMemory(name string) api.Memory { return nil }
func (m *MockModule) ExportedFunction(name string) api.Function { return nil }
func (m *MockModule) ExportedMemoryDefinitions() map[string]api.MemoryDefinition { return nil }
func (m *MockModule) ExportedFunctionDefinitions() map[string]api.FunctionDefinition { return nil }
func (m *MockModule) CloseWithExitCode(ctx context.Context, exitCode uint32) error { return nil }
func (m *MockModule) Close(context.Context) error { return nil }
type MockMemory []byte
func (m MockMemory) Definition() api.MemoryDefinition { return nil }
func (m MockMemory) Size() uint32 { return uint32(len(m)) }
func (m MockMemory) ReadByte(offset uint32) (byte, bool) {
if offset >= m.Size() {
return 0, false
}
return m[offset], true
}
func (m MockMemory) ReadUint16Le(offset uint32) (uint16, bool) {
if !m.hasSize(offset, 2) {
return 0, false
}
return binary.LittleEndian.Uint16(m[offset : offset+2]), true
}
func (m MockMemory) ReadUint32Le(offset uint32) (uint32, bool) {
if !m.hasSize(offset, 4) {
return 0, false
}
return binary.LittleEndian.Uint32(m[offset : offset+4]), true
}
func (m MockMemory) ReadFloat32Le(offset uint32) (float32, bool) {
v, ok := m.ReadUint32Le(offset)
if !ok {
return 0, false
}
return math.Float32frombits(v), true
}
func (m MockMemory) ReadUint64Le(offset uint32) (uint64, bool) {
if !m.hasSize(offset, 8) {
return 0, false
}
return binary.LittleEndian.Uint64(m[offset : offset+8]), true
}
func (m MockMemory) ReadFloat64Le(offset uint32) (float64, bool) {
v, ok := m.ReadUint64Le(offset)
if !ok {
return 0, false
}
return math.Float64frombits(v), true
}
func (m MockMemory) Read(offset, byteCount uint32) ([]byte, bool) {
if !m.hasSize(offset, byteCount) {
return nil, false
}
return m[offset : offset+byteCount : offset+byteCount], true
}
func (m MockMemory) WriteByte(offset uint32, v byte) bool {
if offset >= m.Size() {
return false
}
m[offset] = v
return true
}
func (m MockMemory) WriteUint16Le(offset uint32, v uint16) bool {
if !m.hasSize(offset, 2) {
return false
}
binary.LittleEndian.PutUint16(m[offset:], v)
return true
}
func (m MockMemory) WriteUint32Le(offset, v uint32) bool {
if !m.hasSize(offset, 4) {
return false
}
binary.LittleEndian.PutUint32(m[offset:], v)
return true
}
func (m MockMemory) WriteFloat32Le(offset uint32, v float32) bool {
return m.WriteUint32Le(offset, math.Float32bits(v))
}
func (m MockMemory) WriteUint64Le(offset uint32, v uint64) bool {
if !m.hasSize(offset, 8) {
return false
}
binary.LittleEndian.PutUint64(m[offset:], v)
return true
}
func (m MockMemory) WriteFloat64Le(offset uint32, v float64) bool {
return m.WriteUint64Le(offset, math.Float64bits(v))
}
func (m MockMemory) Write(offset uint32, val []byte) bool {
if !m.hasSize(offset, uint32(len(val))) {
return false
}
copy(m[offset:], val)
return true
}
func (m MockMemory) WriteString(offset uint32, val string) bool {
if !m.hasSize(offset, uint32(len(val))) {
return false
}
copy(m[offset:], val)
return true
}
func (m *MockMemory) Grow(delta uint32) (result uint32, ok bool) {
mem := append(*m, make([]byte, 65536)...)
m = &mem
return delta, true
}
func (m MockMemory) PageSize() (result uint32) {
return uint32(len(m) / 65536)
}
func (m MockMemory) hasSize(offset uint32, byteCount uint32) bool {
return uint64(offset)+uint64(byteCount) <= uint64(len(m))
}

135
vfs_test.go Normal file
View File

@@ -0,0 +1,135 @@
package sqlite3
import (
"bytes"
"context"
"math/rand"
"testing"
"time"
"github.com/ncruces/julianday"
)
func Test_vfsLocaltime(t *testing.T) {
memory := make(MockMemory, 128)
module := &MockModule{&memory}
memory.Write(0, []byte("zero"))
rc := vfsLocaltime(context.TODO(), module, 0, 4)
if rc != 0 {
t.Fatal("returned", rc)
}
epoch := time.Unix(0, 0)
if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
t.Fatal("overwrote zero address")
}
if s, _ := memory.ReadUint32Le(4 + 0*4); int(s) != epoch.Second() {
t.Fatal("wrong second")
}
if m, _ := memory.ReadUint32Le(4 + 1*4); int(m) != epoch.Minute() {
t.Fatal("wrong minute")
}
if h, _ := memory.ReadUint32Le(4 + 2*4); int(h) != epoch.Hour() {
t.Fatal("wrong hour")
}
if d, _ := memory.ReadUint32Le(4 + 3*4); int(d) != epoch.Day() {
t.Fatal("wrong day")
}
if m, _ := memory.ReadUint32Le(4 + 4*4); time.Month(1+m) != epoch.Month() {
t.Fatal("wrong month")
}
if y, _ := memory.ReadUint32Le(4 + 5*4); 1900+int(y) != epoch.Year() {
t.Fatal("wrong year")
}
if w, _ := memory.ReadUint32Le(4 + 6*4); time.Weekday(w) != epoch.Weekday() {
t.Fatal("wrong weekday")
}
if d, _ := memory.ReadUint32Le(4 + 7*4); int(d) != epoch.YearDay()-1 {
t.Fatal("wrong yearday")
}
}
func Test_vfsRandomness(t *testing.T) {
memory := make(MockMemory, 128)
module := &MockModule{&memory}
memory.Write(0, []byte("zero"))
rand.Seed(0)
rc := vfsRandomness(context.TODO(), module, 0, 16, 4)
if rc != 16 {
t.Fatal("returned", rc)
}
if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
t.Fatal("overwrote zero address")
}
var want [16]byte
rand.Seed(0)
rand.Read(want[:])
if got, _ := memory.Read(4, 16); !bytes.Equal(got, want[:]) {
t.Fatalf("got %q, want %q", got, want)
}
}
func Test_vfsSleep(t *testing.T) {
start := time.Now()
rc := vfsSleep(context.TODO(), 0, 123456)
if rc != 0 {
t.Fatal("returned", rc)
}
want := 123456 * time.Microsecond
if got := time.Since(start); got < want {
t.Fatalf("got %v, want %v", got, want)
}
}
func Test_vfsCurrentTime(t *testing.T) {
memory := make(MockMemory, 128)
module := &MockModule{&memory}
memory.Write(0, []byte("zero"))
now := time.Now()
rc := vfsCurrentTime(context.TODO(), module, 0, 4)
if rc != 0 {
t.Fatal("returned", rc)
}
if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
t.Fatal("overwrote zero address")
}
want := julianday.Float(now)
if got, _ := memory.ReadFloat64Le(4); float32(got) != float32(want) {
t.Fatalf("got %v, want %v", got, want)
}
}
func Test_vfsCurrentTime64(t *testing.T) {
memory := make(MockMemory, 128)
module := &MockModule{&memory}
memory.Write(0, []byte("zero"))
now := time.Now()
time.Sleep(time.Millisecond)
rc := vfsCurrentTime64(context.TODO(), module, 0, 4)
if rc != 0 {
t.Fatal("returned", rc)
}
if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
t.Fatal("overwrote zero address")
}
day, nsec := julianday.Date(now)
want := day*86_400_000 + nsec/1_000_000
if got, _ := memory.ReadUint64Le(4); int64(got)-want > 100 {
t.Fatalf("got %v, want %v", got, want)
}
}