1
0
forked from MTSR/mapserver

testing cleanup

This commit is contained in:
Thomas Rudin 2019-01-09 11:11:45 +01:00
parent a72886c5ad
commit 4b70beb3bd
4 changed files with 39 additions and 10 deletions

View File

@ -7,3 +7,8 @@ type MapBlockCoords struct {
func NewMapBlockCoords(x,y,z int) MapBlockCoords { func NewMapBlockCoords(x,y,z int) MapBlockCoords {
return MapBlockCoords{X:x, Y:y, Z:z} return MapBlockCoords{X:x, Y:y, Z:z}
} }
const (
MaxCoord = 2047
MinCoord = -2047
)

View File

@ -1,7 +1,11 @@
package db package db
import (
"mapserver/coords"
)
type Block struct { type Block struct {
posx, posy, posz int pos coords.MapBlockCoords
data []byte data []byte
mtime int64 mtime int64
} }
@ -9,6 +13,6 @@ type Block struct {
type DBAccessor interface { type DBAccessor interface {
Migrate() error Migrate() error
FindLatestBlocks(mintime int64, limit int) ([]Block, error) FindLatestBlocks(mintime int64, limit int) ([]Block, error)
FindBlocks(posx int, posz int, posystart int, posyend int) ([]Block, error) FindBlocks(pos1, pos2 coords.MapBlockCoords) ([]Block, error)
CountBlocks(x1, x2, y1, y2, z1, z2 int) (int, error) CountBlocks(pos1, pos2 coords.MapBlockCoords) (int, error)
} }

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"mapserver/coords"
"time" "time"
"errors" "errors"
) )
@ -69,12 +70,24 @@ func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block,
return make([]Block, 0), nil 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 return make([]Block, 0), nil
} }
func (db *Sqlite3Accessor) CountBlocks(x1, x2, y1, y2, z1, z2 int) (int, error) { const countBlockQuery = `
rows, err := db.db.Query("select count(*) from blocks") 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 { if err != nil {
return 0, err return 0, err
} }

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"io/ioutil" "io/ioutil"
"database/sql" "database/sql"
"mapserver/coords"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -58,10 +59,11 @@ func createTestDatabase(filename string) error {
} }
func TestMigrateEmpty(t *testing.T){ func TestMigrateEmpty(t *testing.T){
tmpfile, err := ioutil.TempFile("", "example") tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer os.Remove(tmpfile.Name())
createEmptyDatabase(tmpfile.Name()) createEmptyDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name())
@ -75,10 +77,11 @@ func TestMigrateEmpty(t *testing.T){
} }
func TestMigrate(t *testing.T){ func TestMigrate(t *testing.T){
tmpfile, err := ioutil.TempFile("", "example") tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name()) createTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name())
@ -93,10 +96,11 @@ func TestMigrate(t *testing.T){
func TestMigrateAndQuery(t *testing.T){ func TestMigrateAndQuery(t *testing.T){
tmpfile, err := ioutil.TempFile("", "example") tmpfile, err := ioutil.TempFile("", "TestMigrateAndQuery.*.sqlite")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer os.Remove(tmpfile.Name())
createTestDatabase(tmpfile.Name()) createTestDatabase(tmpfile.Name())
a, err := NewSqliteAccessor(tmpfile.Name()) a, err := NewSqliteAccessor(tmpfile.Name())
@ -108,7 +112,10 @@ func TestMigrateAndQuery(t *testing.T){
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }