1
0
forked from MTSR/mapserver
mapserver/testutils/database.go

65 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-01-11 12:24:10 +03:00
package testutils
import (
2019-01-13 18:37:03 +03:00
"database/sql"
"fmt"
2019-01-11 12:24:10 +03:00
"io"
"os"
2019-01-13 18:37:03 +03:00
"path/filepath"
"runtime"
_ "modernc.org/sqlite"
2019-01-11 12:24:10 +03:00
)
const emptyBlocksScript = `
create table blocks (
pos int,
data blob
);
`
func copy(src, dst string) error {
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()
}
func CreateTestDatabase(filename string) error {
2019-01-13 18:37:03 +03:00
_, currentfilename, _, _ := runtime.Caller(0)
2019-02-01 16:14:21 +03:00
return copy(filepath.Dir(currentfilename)+"/testdata/map1.sqlite", filename)
2019-01-11 12:24:10 +03:00
}
2019-01-23 10:02:59 +03:00
//DB with metadata at 0,0,0
func CreateTestDatabase2(filename string) error {
_, currentfilename, _, _ := runtime.Caller(0)
return copy(filepath.Dir(currentfilename)+"/testdata/map2.sqlite", filename)
}
2019-01-11 12:24:10 +03:00
func CreateEmptyDatabase(filename string) {
db, err := sql.Open("sqlite", filename)
2019-01-11 12:24:10 +03:00
if err != nil {
panic(err)
}
rows, err := db.Query(emptyBlocksScript)
if err != nil {
panic(err)
}
rows.Next()
fmt.Println(rows)
db.Close()
}