1
0
forked from MTSR/mapserver

testutils

This commit is contained in:
NatureFreshMilk 2019-01-11 10:24:10 +01:00
parent 5ebc282bf9
commit 1956bfa7f8
6 changed files with 78 additions and 93 deletions

View File

@ -1,63 +1,14 @@
package db
import (
"database/sql"
"fmt"
"io"
"io/ioutil"
"mapserver/coords"
"os"
"testing"
"mapserver/testutils"
_ "github.com/mattn/go-sqlite3"
)
const emptyBlocksScript = `
create table blocks (
pos int,
data blob
);
`
const testDatabase = "./testdata/map.sqlite"
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()
}
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 {
return copy(testDatabase, filename)
}
func TestMigrateEmpty(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite")
@ -66,7 +17,7 @@ func TestMigrateEmpty(t *testing.T) {
}
defer os.Remove(tmpfile.Name())
createEmptyDatabase(tmpfile.Name())
testutils.CreateEmptyDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
@ -84,7 +35,7 @@ func TestMigrate(t *testing.T) {
}
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name())
testutils.CreateEmptyDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
@ -102,11 +53,12 @@ func TestMigrateAndQuery(t *testing.T) {
}
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name())
testutils.CreateTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)

View File

@ -1,63 +1,35 @@
package mapblockaccessor
import (
"io"
"os"
"io/ioutil"
"mapserver/coords"
"mapserver/db"
"os"
"testing"
"mapserver/testutils"
"mapserver/db"
"github.com/sirupsen/logrus"
)
func copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
func TestSimpleAccess(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
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()
}
const testDatabase = "./testdata/map.sqlite"
func createTestDatabase(filename string) error {
return copy(testDatabase, filename)
}
func GetTestDatabase() db.DBAccessor {
tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
testutils.CreateTestDatabase(tmpfile.Name())
createTestDatabase(tmpfile.Name())
a, err := db.NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
return a
}
func TestSimpleAccess(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
a := GetTestDatabase()
cache := NewMapBlockAccessor(a)
mb, err := cache.GetMapBlock(coords.NewMapBlockCoords(0, 0, 0))

Binary file not shown.

View File

@ -3,17 +3,18 @@ package mapblockrenderer
import (
"mapserver/colormapping"
"mapserver/coords"
"mapserver/mapblockaccessor"
)
type MapBlockRenderer struct {
accessor *coords.DBAccessor
accessor *mapblockaccessor.MapBlockAccessor
colors *colormapping.ColorMapping
}
func NewMapBlockRenderer(accessor *coords.DBAccessor, colors *colormapping.ColorMapping) {
//TODO
func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *colormapping.ColorMapping) MapBlockRenderer {
return MapBlockRenderer{accessor: accessor, colors: colors}
}
func (r *MapBlockRenderer) Render(r coords.MapBlockRange) {
func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) {
//TODO
}

60
testutils/database.go Normal file
View File

@ -0,0 +1,60 @@
package testutils
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"io"
"path/filepath"
"os"
"fmt"
"runtime"
)
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 {
_, currentfilename, _, _ := runtime.Caller(0)
return copy(filepath.Dir(currentfilename) + "/testdata/map.sqlite", filename)
}
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()
}