diff --git a/db/sqlite_test.go b/db/sqlite_test.go index 74059cc..1bd737a 100644 --- a/db/sqlite_test.go +++ b/db/sqlite_test.go @@ -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) diff --git a/mapblockaccessor/mapblockaccessor_test.go b/mapblockaccessor/mapblockaccessor_test.go index 2efeab8..be15160 100644 --- a/mapblockaccessor/mapblockaccessor_test.go +++ b/mapblockaccessor/mapblockaccessor_test.go @@ -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)) diff --git a/mapblockaccessor/testdata/map.sqlite b/mapblockaccessor/testdata/map.sqlite deleted file mode 100644 index d4c5728..0000000 Binary files a/mapblockaccessor/testdata/map.sqlite and /dev/null differ diff --git a/mapblockrenderer/renderer.go b/mapblockrenderer/renderer.go index a21fd3e..bd53497 100644 --- a/mapblockrenderer/renderer.go +++ b/mapblockrenderer/renderer.go @@ -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 } diff --git a/testutils/database.go b/testutils/database.go new file mode 100644 index 0000000..38c2f2f --- /dev/null +++ b/testutils/database.go @@ -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() +} diff --git a/db/testdata/map.sqlite b/testutils/testdata/map.sqlite similarity index 100% rename from db/testdata/map.sqlite rename to testutils/testdata/map.sqlite