1
0
forked from MTSR/mapserver
mapserver/db/sqlite_test.go

126 lines
2.0 KiB
Go
Raw Normal View History

2019-01-08 18:33:49 +03:00
package db
import (
2019-01-10 19:50:31 +03:00
"database/sql"
"fmt"
"io"
"io/ioutil"
"mapserver/coords"
"os"
"testing"
_ "github.com/mattn/go-sqlite3"
2019-01-08 18:33:49 +03:00
)
const emptyBlocksScript = `
create table blocks (
pos int,
data blob
);
`
2019-01-09 11:56:36 +03:00
const testDatabase = "./testdata/map.sqlite"
2019-01-10 19:50:31 +03:00
func createEmptyDatabase(filename string) {
db, err := sql.Open("sqlite3", filename)
if err != nil {
panic(err)
}
rows, err := db.Query(emptyBlocksScript)
if err != nil {
panic(err)
}
rows.Next()
fmt.Println(rows)
db.Close()
2019-01-08 18:33:49 +03:00
}
2019-01-09 11:56:36 +03:00
func copy(src, dst string) error {
2019-01-10 19:50:31 +03:00
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
2019-01-09 11:56:36 +03:00
}
func createTestDatabase(filename string) error {
2019-01-10 19:50:31 +03:00
return copy(testDatabase, filename)
2019-01-09 11:56:36 +03:00
}
2019-01-10 19:50:31 +03:00
func TestMigrateEmpty(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
createEmptyDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
2019-01-08 18:33:49 +03:00
}
2019-01-09 11:56:36 +03:00
2019-01-10 19:50:31 +03:00
func TestMigrate(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
2019-01-09 11:56:36 +03:00
}
2019-01-09 12:16:22 +03:00
2019-01-10 19:50:31 +03:00
func TestMigrateAndQuery(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateAndQuery.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
block, err := a.GetBlock(coords.NewMapBlockCoords(0, 0, 0))
if err != nil {
panic(err)
}
if block == nil {
t.Fatal("no data")
}
2019-01-09 12:16:22 +03:00
}