From 4b70beb3bde953c683f9139f2d449721d4b3f543 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Wed, 9 Jan 2019 11:11:45 +0100 Subject: [PATCH] testing cleanup --- coords/mapblockcoords.go | 5 +++++ db/accessor.go | 10 +++++++--- db/sqlite.go | 19 ++++++++++++++++--- db/sqlite_test.go | 15 +++++++++++---- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/coords/mapblockcoords.go b/coords/mapblockcoords.go index 3db8d38..120c652 100644 --- a/coords/mapblockcoords.go +++ b/coords/mapblockcoords.go @@ -7,3 +7,8 @@ type MapBlockCoords struct { func NewMapBlockCoords(x,y,z int) MapBlockCoords { return MapBlockCoords{X:x, Y:y, Z:z} } + +const ( + MaxCoord = 2047 + MinCoord = -2047 +) diff --git a/db/accessor.go b/db/accessor.go index cef75b8..0361713 100644 --- a/db/accessor.go +++ b/db/accessor.go @@ -1,7 +1,11 @@ package db +import ( + "mapserver/coords" +) + type Block struct { - posx, posy, posz int + pos coords.MapBlockCoords data []byte mtime int64 } @@ -9,6 +13,6 @@ type Block struct { type DBAccessor interface { Migrate() error FindLatestBlocks(mintime int64, limit int) ([]Block, error) - FindBlocks(posx int, posz int, posystart int, posyend int) ([]Block, error) - CountBlocks(x1, x2, y1, y2, z1, z2 int) (int, error) + FindBlocks(pos1, pos2 coords.MapBlockCoords) ([]Block, error) + CountBlocks(pos1, pos2 coords.MapBlockCoords) (int, error) } diff --git a/db/sqlite.go b/db/sqlite.go index 74c140f..fe329f0 100644 --- a/db/sqlite.go +++ b/db/sqlite.go @@ -4,6 +4,7 @@ import ( "database/sql" _ "github.com/mattn/go-sqlite3" "github.com/sirupsen/logrus" + "mapserver/coords" "time" "errors" ) @@ -69,12 +70,24 @@ func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block, return make([]Block, 0), nil } -func (db *Sqlite3Accessor) FindBlocks(posx int, posz int, posystart int, posyend int) ([]Block, error) { +func (db *Sqlite3Accessor) FindBlocks(pos1, pos2 coords.MapBlockCoords) ([]Block, error) { return make([]Block, 0), nil } -func (db *Sqlite3Accessor) CountBlocks(x1, x2, y1, y2, z1, z2 int) (int, error) { - rows, err := db.db.Query("select count(*) from blocks") +const countBlockQuery = ` +select count(*) from blocks b +where b.pos >= ? and b.pos <= ? +` + +func (db *Sqlite3Accessor) CountBlocks(pos1, pos2 coords.MapBlockCoords) (int, error) { + ppos1 := coords.CoordToPlain(pos1) + ppos2 := coords.CoordToPlain(pos2) + + if ppos1 > ppos2 { + ppos1, ppos2 = ppos2, ppos1 + } + + rows, err := db.db.Query(countBlockQuery, ppos1, ppos2) if err != nil { return 0, err } diff --git a/db/sqlite_test.go b/db/sqlite_test.go index 42b5ff9..8b6a18b 100644 --- a/db/sqlite_test.go +++ b/db/sqlite_test.go @@ -7,6 +7,7 @@ import ( "testing" "io/ioutil" "database/sql" + "mapserver/coords" _ "github.com/mattn/go-sqlite3" ) @@ -58,10 +59,11 @@ func createTestDatabase(filename string) error { } func TestMigrateEmpty(t *testing.T){ - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite") if err != nil { panic(err) } + defer os.Remove(tmpfile.Name()) createEmptyDatabase(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name()) @@ -75,10 +77,11 @@ func TestMigrateEmpty(t *testing.T){ } func TestMigrate(t *testing.T){ - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite") if err != nil { panic(err) } + defer os.Remove(tmpfile.Name()) createTestDatabase(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name()) @@ -93,10 +96,11 @@ func TestMigrate(t *testing.T){ func TestMigrateAndQuery(t *testing.T){ - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := ioutil.TempFile("", "TestMigrateAndQuery.*.sqlite") if err != nil { panic(err) } + defer os.Remove(tmpfile.Name()) createTestDatabase(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name()) @@ -108,7 +112,10 @@ func TestMigrateAndQuery(t *testing.T){ panic(err) } - count, err := a.CountBlocks(-1000, 1000, -1000, 1000, -1000, 1000) + count, err := a.CountBlocks( + coords.NewMapBlockCoords(coords.MinCoord,coords.MinCoord,coords.MinCoord), + coords.NewMapBlockCoords(coords.MaxCoord,coords.MaxCoord,coords.MaxCoord)) + if err != nil { panic(err) }