1
0
forked from MTSR/mapserver

mapblock accessor stub

This commit is contained in:
Thomas Rudin 2019-01-10 17:50:31 +01:00
parent de15862cae
commit 9abfa2fd83
5 changed files with 199 additions and 98 deletions

View File

@ -1,14 +1,15 @@
package db
import (
"os"
"io"
"fmt"
"testing"
"io/ioutil"
"database/sql"
"mapserver/coords"
_ "github.com/mattn/go-sqlite3"
"database/sql"
"fmt"
"io"
"io/ioutil"
"mapserver/coords"
"os"
"testing"
_ "github.com/mattn/go-sqlite3"
)
const emptyBlocksScript = `
@ -20,106 +21,105 @@ create table blocks (
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 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()
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()
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()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func createTestDatabase(filename string) error {
return copy(testDatabase, filename)
return copy(testDatabase, filename)
}
func TestMigrateEmpty(t *testing.T){
tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
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)
}
createEmptyDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
}
func TestMigrate(t *testing.T){
tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
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)
}
createTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
}
func TestMigrateAndQuery(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateAndQuery.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
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)
}
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))
block, err := a.GetBlock(coords.NewMapBlockCoords(0,0,0))
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
if block == nil {
t.Fatal("no data")
}
if block == nil {
t.Fatal("no data")
}
}

View File

@ -0,0 +1,33 @@
package mapblockaccessor
import (
"mapserver/coords"
"mapserver/db"
"mapserver/mapblockparser"
)
type MapBlockAccessor struct {
accessor db.DBAccessor
}
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
return &MapBlockAccessor{accessor: accessor}
}
func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
//TODO: cache
}
func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
block, err := a.accessor.GetBlock(pos)
if err != nil {
return nil, err
}
mapblock, err := mapblockparser.Parse(block.Data)
if err != nil {
return nil, err
}
return mapblock, nil
}

View File

@ -0,0 +1,69 @@
package mapblockaccessor
import (
"io"
"io/ioutil"
"mapserver/coords"
"mapserver/db"
"os"
"testing"
)
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()
}
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)
}
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) {
a := GetTestDatabase()
cache := NewMapBlockAccessor(a)
mb, err := cache.GetMapBlock(coords.NewMapBlockCoords(0, 0, 0))
if err != nil {
panic(err)
}
if mb == nil {
t.Fatal("Mapblock is nil")
}
}

BIN
mapblockaccessor/testdata/map.sqlite vendored Normal file

Binary file not shown.

View File

@ -1,20 +1,19 @@
package mapblockrenderer
import (
"mapserver/coords"
"mapserver/db"
"mapserver/colormapping"
"mapserver/colormapping"
"mapserver/coords"
)
type MapBlockRenderer struct {
accessor *coords.DBAccessor
colors *colormapping.ColorMapping
accessor *coords.DBAccessor
colors *colormapping.ColorMapping
}
func NewMapBlockRenderer(accessor *coords.DBAccessor, colors *colormapping.ColorMapping){
//TODO
func NewMapBlockRenderer(accessor *coords.DBAccessor, colors *colormapping.ColorMapping) {
//TODO
}
func (r *MapBlockRenderer) Render(range coords.MapBlockRange){
//TODO
func (r *MapBlockRenderer) Render(r coords.MapBlockRange) {
//TODO
}