Skip to content

Commit

Permalink
Allow automatic conversion from json to pb (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
irees authored Dec 2, 2023
1 parent 9704440 commit 4a9ad00
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
github.com/graph-gophers/dataloader/v7 v7.1.0
github.com/hypirion/go-filecache v0.0.0-20160810125507-e3e6ef6981f0
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201022717-a7acfe4abe4b/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d h1:0Tfw/JoAkRvDQvuJUhhJALVTl16zTvMd3dGUPecPJUw=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231201023749-4dbd4e03793d/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7 h1:rwkKzYzl05Q4TM++L9RIJbXPjIvURoMN5vEr8dvUV/Q=
github.com/interline-io/transitland-lib v0.14.0-rc1.0.20231202005632-a9ea742322f7/go.mod h1:UcfuCX6DyKt/yn5GECFn3jQ6NcZEjt5XyPjf8a3tXZ4=
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
Expand Down
40 changes: 14 additions & 26 deletions internal/testfinder/testfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/interline-io/transitland-lib/rt/pb"
"github.com/interline-io/transitland-lib/rt"
"github.com/interline-io/transitland-server/auth/authz"
"github.com/interline-io/transitland-server/auth/azcheck"
"github.com/interline-io/transitland-server/config"
Expand All @@ -19,7 +18,6 @@ import (
"github.com/interline-io/transitland-server/internal/testutil"
"github.com/interline-io/transitland-server/model"
"github.com/jmoiron/sqlx"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -60,16 +58,25 @@ func newFinders(t testing.TB, db sqlx.Ext, opts TestFinderOptions) model.Finders
// Setup RT
rtf := rtfinder.NewFinder(rtfinder.NewLocalCache(), db)
rtf.Clock = opts.Clock

// Setup GBFS
gbf := gbfsfinder.NewFinder(nil)
for _, rtj := range opts.RTJsons {
fn := testutil.RelPath("test", "data", "rt", rtj.Fname)
if err := FetchRTJson(rtj.Feed, rtj.Ftype, fn, rtf); err != nil {
msg, err := rt.ReadFile(fn)
if err != nil {
t.Fatal(err)
}
key := fmt.Sprintf("rtdata:%s:%s", rtj.Feed, rtj.Ftype)
rtdata, err := proto.Marshal(msg)
if err != nil {
t.Fatal(err)
}
if err := rtf.AddData(key, rtdata); err != nil {
t.Fatal(err)
}
}

// Setup GBFS
gbf := gbfsfinder.NewFinder(nil)

return model.Finders{
Config: cfg,
Finder: dbf,
Expand Down Expand Up @@ -127,22 +134,3 @@ func DefaultRTJson() []RTJsonFile {
{"CT", "realtime_trip_updates", "CT.json"},
}
}

// FetchRTJson fetches test protobuf in JSON format
// URL is relative to project root
func FetchRTJson(feed string, ftype string, url string, rtfinder model.RTFinder) error {
var msg pb.FeedMessage
jdata, err := ioutil.ReadFile(url)
if err != nil {
return err
}
if err := protojson.Unmarshal(jdata, &msg); err != nil {
return err
}
rtdata, err := proto.Marshal(&msg)
if err != nil {
return err
}
key := fmt.Sprintf("rtdata:%s:%s", feed, ftype)
return rtfinder.AddData(key, rtdata)
}
16 changes: 0 additions & 16 deletions server/gql/mutation_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/interline-io/transitland-lib/rt/pb"
"github.com/interline-io/transitland-server/auth/ancheck"
"github.com/interline-io/transitland-server/internal/testfinder"
"github.com/interline-io/transitland-server/internal/testutil"
"github.com/interline-io/transitland-server/model"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

func TestFeedVersionFetchResolver(t *testing.T) {
Expand Down Expand Up @@ -66,18 +62,6 @@ func TestValidateGtfsResolver(t *testing.T) {
http.Error(w, "not found", 404)
return
}
// If RT json, convert to RT PB
if strings.HasSuffix(p, ".json") {
var msg pb.FeedMessage
if err := protojson.Unmarshal(buf, &msg); err != nil {
panic(err)
}
rtdata, err := proto.Marshal(&msg)
if err != nil {
panic(err)
}
buf = rtdata
}
w.Write(buf)
}))
vars := hw{
Expand Down

0 comments on commit 4a9ad00

Please sign in to comment.