1
0
forked from MTSR/mapserver
mapserver/mapobjectdb/sqlite/mapobjects_test.go

215 lines
3.2 KiB
Go
Raw Normal View History

2019-02-05 13:25:01 +01:00
package sqlite
2019-01-14 12:38:26 +01:00
import (
2019-01-25 11:18:35 +01:00
"fmt"
2019-01-14 12:38:26 +01:00
"io/ioutil"
2019-02-07 08:02:13 +01:00
"mapserver/mapobjectdb"
2023-12-29 16:00:11 +01:00
"mapserver/types"
2019-01-14 12:38:26 +01:00
"os"
"testing"
)
func TestMigrate(t *testing.T) {
2019-01-17 08:01:48 +01:00
tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite")
2019-01-14 12:38:26 +01:00
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
2019-02-05 13:25:01 +01:00
db, err := New(tmpfile.Name())
2019-01-14 12:38:26 +01:00
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
2019-01-14 12:48:46 +01:00
2019-01-14 12:38:26 +01:00
}
2019-01-23 14:23:24 +01:00
func TestMapObjects(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
if err != nil {
panic(err)
}
2019-01-24 14:04:52 +01:00
//defer os.Remove(tmpfile.Name())
2019-01-23 14:23:24 +01:00
2019-02-05 13:25:01 +01:00
db, err := New(tmpfile.Name())
2019-01-23 14:23:24 +01:00
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
attrs := make(map[string]string)
attrs["X"] = "y"
2023-12-29 16:00:11 +01:00
pos := types.NewMapBlockCoords(0, 0, 0)
2019-01-23 14:23:24 +01:00
2019-02-05 13:25:01 +01:00
o := mapobjectdb.MapObject{
2019-02-06 08:38:08 +01:00
MBPos: pos,
2019-02-06 08:09:21 +01:00
X: 15,
Y: 15,
Z: 15,
2019-01-23 16:06:56 +01:00
Type: "xy",
Mtime: 1234,
2019-01-23 14:23:24 +01:00
Attributes: attrs,
}
2019-01-24 08:26:28 +01:00
err = db.AddMapData(&o)
2019-01-23 14:23:24 +01:00
if err != nil {
panic(err)
}
2022-01-17 09:11:24 +01:00
limit := 1000
2019-02-05 13:25:01 +01:00
q := mapobjectdb.SearchQuery{
2022-01-17 09:11:24 +01:00
Pos1: pos,
Pos2: pos,
Type: "xy",
Limit: &limit,
2019-01-25 10:39:17 +01:00
}
2019-02-22 18:30:38 +01:00
objs, err := db.GetMapData(&q)
2019-01-25 10:39:17 +01:00
if err != nil {
panic(err)
}
2019-01-25 11:02:36 +01:00
for _, mo := range objs {
fmt.Println(mo)
}
2019-01-25 10:39:17 +01:00
2019-01-23 14:23:24 +01:00
}
func TestMapObjectsQueryWithAttribute(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
if err != nil {
panic(err)
}
//defer os.Remove(tmpfile.Name())
db, err := New(tmpfile.Name())
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
attrs := make(map[string]string)
attrs["X"] = "y"
2023-12-29 16:00:11 +01:00
pos := types.NewMapBlockCoords(0, 0, 0)
o := mapobjectdb.MapObject{
MBPos: pos,
X: 15,
Y: 15,
Z: 15,
Type: "xy",
Mtime: 1234,
Attributes: attrs,
}
err = db.AddMapData(&o)
if err != nil {
panic(err)
}
2022-01-17 09:11:24 +01:00
limit := 1000
q := mapobjectdb.SearchQuery{
Pos1: pos,
Pos2: pos,
Type: "xy",
AttributeLike: &mapobjectdb.SearchAttributeLike{
Key: "X",
Value: "%y",
},
2022-01-17 09:11:24 +01:00
Limit: &limit,
}
objs, err := db.GetMapData(&q)
if err != nil {
panic(err)
}
for _, mo := range objs {
fmt.Println(mo)
}
if len(objs) != 1 {
panic("length mismatch")
}
}
2019-06-11 13:26:19 +02:00
func TestMapObjectsQueryWithAttributeIgnoreCase(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
if err != nil {
panic(err)
}
//defer os.Remove(tmpfile.Name())
db, err := New(tmpfile.Name())
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
attrs := make(map[string]string)
attrs["X"] = "ABC"
2023-12-29 16:00:11 +01:00
pos := types.NewMapBlockCoords(0, 0, 0)
2019-06-11 13:26:19 +02:00
o := mapobjectdb.MapObject{
MBPos: pos,
X: 15,
Y: 15,
Z: 15,
Type: "xy",
Mtime: 1234,
Attributes: attrs,
}
err = db.AddMapData(&o)
if err != nil {
panic(err)
}
2022-01-17 09:11:24 +01:00
limit := 1000
2019-06-11 13:26:19 +02:00
q := mapobjectdb.SearchQuery{
Pos1: pos,
Pos2: pos,
Type: "xy",
AttributeLike: &mapobjectdb.SearchAttributeLike{
Key: "X",
Value: "%bc",
},
2022-01-17 09:11:24 +01:00
Limit: &limit,
2019-06-11 13:26:19 +02:00
}
objs, err := db.GetMapData(&q)
if err != nil {
panic(err)
}
for _, mo := range objs {
fmt.Println(mo)
}
if len(objs) != 1 {
panic("length mismatch")
}
}