Skip to content

Commit

Permalink
refactor: add feature id if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
iwpnd committed May 1, 2023
1 parent 592c3d6 commit 122f55d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
9 changes: 6 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type Database struct {

// Fence ...
type Fence struct {
id string
object geojson.Feature
}

Expand All @@ -75,8 +74,12 @@ func (db *Database) Create(g *geojson.Feature) error {
return &ErrInvalidGeometryType{Type: g.Geometry.Type}
}

id := uuid.Must(uuid.NewRandom()).String()
f := &Fence{object: *g, id: id}
if g.ID == nil {
id := uuid.Must(uuid.NewRandom()).String()
g.ID = id
}

f := &Fence{object: *g}

rect := toExtent(g.Geometry.Polygon[0])
db.tree.Insert(
Expand Down
47 changes: 47 additions & 0 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,53 @@ func setupDatabase() *Database {
return db
}

func TestCreate(t *testing.T) {
db := setupDatabase()

expected := []byte(`{"id":"foobar","type":"Feature","geometry":{"type":"Polygon","coordinates":[[[13.3967096231641,52.47425410999395],[13.3967096231641,52.4680479999262],[13.413318577304466,52.4680479999262],[13.413318577304466,52.47425410999395],[13.3967096231641,52.47425410999395]]]},"properties":{"id":"foobar"}}`)

f, err := geojson.UnmarshalFeature(expected)
if err != nil {
t.Fatal("failed to unmarshal feature: ", err)
}

err = db.Create(f)
if err != nil {
t.Fatal("failed to create feature")
}

p := []float64{13.40532627661105, 52.471361312503575}

matches := db.Intersects(p)
got, _ := matches[0].MarshalJSON()

assert.Equal(t, string(expected), string(got))
}

func TestCreateGenerateID(t *testing.T) {
db := setupDatabase()

data := []byte(`{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[13.3967096231641,52.47425410999395],[13.3967096231641,52.4680479999262],[13.413318577304466,52.4680479999262],[13.413318577304466,52.47425410999395],[13.3967096231641,52.47425410999395]]]}}`)

f, err := geojson.UnmarshalFeature(data)
if err != nil {
t.Fatal("failed to unmarshal feature: ", err)
}

assert.Nil(t, f.ID)

err = db.Create(f)
if err != nil {
t.Fatal("failed to create feature")
}

p := []float64{13.40532627661105, 52.471361312503575}

matches := db.Intersects(p)

assert.NotNil(t, matches[0].ID)
}

func TestCreateFailed(t *testing.T) {
db := setupDatabase()

Expand Down
2 changes: 1 addition & 1 deletion handlers/location/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/stretchr/testify/assert"
)

var data = []byte(`{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-3.0988311767578125,40.837710162420045],[-3.121490478515625,40.820045086716505],[-3.0978012084960938,40.80237530523985],[-3.0754852294921875,40.8210843390845],[-3.0988311767578125,40.837710162420045]],[[-3.0988311767578125,40.82783908257347],[-3.1098175048828125,40.820045086716505],[-3.0988311767578125,40.81147063339219],[-3.086471557617187,40.820304901335035],[-3.0988311767578125,40.82783908257347]]]}}`)
var data = []byte(`{"id":"foobar","type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-3.0988311767578125,40.837710162420045],[-3.121490478515625,40.820045086716505],[-3.0978012084960938,40.80237530523985],[-3.0754852294921875,40.8210843390845],[-3.0988311767578125,40.837710162420045]],[[-3.0988311767578125,40.82783908257347],[-3.1098175048828125,40.820045086716505],[-3.0988311767578125,40.81147063339219],[-3.086471557617187,40.820304901335035],[-3.0988311767578125,40.82783908257347]]]}}`)

func locationRequest(lat float64, lng float64) []byte {
return []byte(fmt.Sprintf(`{"lat":%v,"lng":%v}`, lat, lng))
Expand Down

0 comments on commit 122f55d

Please sign in to comment.