2019-02-05 15:25:01 +03:00
|
|
|
package sqlite
|
2019-01-14 14:38:26 +03:00
|
|
|
|
|
|
|
import (
|
2019-01-25 13:18:35 +03:00
|
|
|
"fmt"
|
2019-01-14 14:38:26 +03:00
|
|
|
"io/ioutil"
|
2019-02-05 15:25:01 +03:00
|
|
|
"mapserver/mapobjectdb"
|
2019-01-16 18:36:28 +03:00
|
|
|
"mapserver/coords"
|
2019-01-14 14:38:26 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrate(t *testing.T) {
|
2019-01-17 10:01:48 +03:00
|
|
|
tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite")
|
2019-01-14 14:38:26 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
db, err := New(tmpfile.Name())
|
2019-01-14 14:38:26 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Migrate()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-14 14:48:46 +03:00
|
|
|
|
2019-01-17 10:53:07 +03:00
|
|
|
pos := coords.NewTileCoords(0, 0, 13, 0)
|
|
|
|
tile, err := db.GetTile(pos)
|
2019-01-14 14:48:46 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:01:48 +03:00
|
|
|
if tile != nil {
|
|
|
|
t.Fatal("non-empty tile found")
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte{0x01, 0x02}
|
2019-02-05 15:25:01 +03:00
|
|
|
tile2 := mapobjectdb.Tile{Pos: pos, Data: data}
|
2019-01-17 10:01:48 +03:00
|
|
|
err = db.SetTile(&tile2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:53:07 +03:00
|
|
|
tile3, err := db.GetTile(pos)
|
2019-01-17 10:01:48 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tile3 == nil {
|
|
|
|
t.Fatal("no data returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tile2.Data) != len(tile3.Data) {
|
|
|
|
t.Fatal("inserted data does not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.SetTile(&tile2)
|
2019-01-16 18:36:28 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-14 14:48:46 +03:00
|
|
|
|
2019-01-14 14:38:26 +03:00
|
|
|
}
|
2019-01-23 16:23:24 +03:00
|
|
|
|
|
|
|
func TestMapObjects(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-24 16:04:52 +03:00
|
|
|
//defer os.Remove(tmpfile.Name())
|
2019-01-23 16:23:24 +03:00
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
db, err := New(tmpfile.Name())
|
2019-01-23 16:23:24 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Migrate()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs := make(map[string]string)
|
|
|
|
attrs["X"] = "y"
|
|
|
|
|
2019-01-23 18:06:56 +03:00
|
|
|
pos := coords.NewMapBlockCoords(0, 0, 0)
|
2019-01-23 16:23:24 +03:00
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
o := mapobjectdb.MapObject{
|
2019-01-23 18:06:56 +03:00
|
|
|
MBPos: &pos,
|
2019-02-06 10:09:21 +03:00
|
|
|
X: 15,
|
|
|
|
Y: 15,
|
|
|
|
Z: 15,
|
2019-01-23 18:06:56 +03:00
|
|
|
Type: "xy",
|
|
|
|
Mtime: 1234,
|
2019-01-23 16:23:24 +03:00
|
|
|
Attributes: attrs,
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:26:28 +03:00
|
|
|
err = db.AddMapData(&o)
|
2019-01-23 16:23:24 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:25:01 +03:00
|
|
|
q := mapobjectdb.SearchQuery{
|
2019-01-25 12:39:17 +03:00
|
|
|
Pos1: pos,
|
|
|
|
Pos2: pos,
|
|
|
|
Type: "xy",
|
|
|
|
}
|
|
|
|
|
2019-01-25 13:02:36 +03:00
|
|
|
objs, err := db.GetMapData(q)
|
2019-01-25 12:39:17 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-25 13:02:36 +03:00
|
|
|
for _, mo := range objs {
|
|
|
|
fmt.Println(mo)
|
|
|
|
}
|
2019-01-25 12:39:17 +03:00
|
|
|
|
2019-01-23 16:23:24 +03:00
|
|
|
}
|