Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved config passing #196

Merged
merged 17 commits into from
Dec 19, 2023
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
PGPASSWORD: for_testing
PGHOST: localhost
PGPORT: 5432
TL_TEST_STORAGE: /tmp/tlserver
TL_TEST_SERVER_DATABASE_URL: postgres://root:for_testing@localhost:5432/tlv2_test_server?sslmode=disable
TL_DATABASE_URL: postgres://root:for_testing@localhost:5432/tlv2_test_server?sslmode=disable
services:
Expand Down
27 changes: 17 additions & 10 deletions actions/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ import (
"github.com/interline-io/transitland-lib/tldb"
"github.com/interline-io/transitland-mw/auth/authn"
"github.com/interline-io/transitland-mw/auth/authz"
"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/internal/dbutil"
"github.com/interline-io/transitland-server/model"
"github.com/jmoiron/sqlx"
"google.golang.org/protobuf/proto"
)

func StaticFetch(ctx context.Context, cfg config.Config, dbf model.Finder, feedId string, feedSrc io.Reader, feedUrl string, checker model.Checker) (*model.FeedVersionFetchResult, error) {
func StaticFetch(ctx context.Context, feedId string, feedSrc io.Reader, feedUrl string) (*model.FeedVersionFetchResult, error) {
cfg := model.ForContext(ctx)
dbf := cfg.Finder

urlType := "static_current"
feed, err := fetchCheckFeed(ctx, dbf, checker, feedId, urlType, feedUrl)
feed, err := fetchCheckFeed(ctx, feedId, urlType, feedUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,8 +88,10 @@ func StaticFetch(ctx context.Context, cfg config.Config, dbf model.Finder, feedI
return &mr, nil
}

func RTFetch(ctx context.Context, cfg config.Config, dbf model.Finder, rtf model.RTFinder, target string, feedId string, feedUrl string, urlType string, checker model.Checker) error {
feed, err := fetchCheckFeed(ctx, dbf, checker, feedId, urlType, feedUrl)
func RTFetch(ctx context.Context, target string, feedId string, feedUrl string, urlType string) error {
cfg := model.ForContext(ctx)

feed, err := fetchCheckFeed(ctx, feedId, urlType, feedUrl)
if err != nil {
return err
}
Expand All @@ -108,7 +112,7 @@ func RTFetch(ctx context.Context, cfg config.Config, dbf model.Finder, rtf model
// Make request
var rtMsg *pb.FeedMessage
var fetchErr error
if err := tldb.NewPostgresAdapterFromDBX(dbf.DBX()).Tx(func(atx tldb.Adapter) error {
if err := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()).Tx(func(atx tldb.Adapter) error {
m, fr, err := fetch.RTFetch(atx, fetchOpts)
if err != nil {
return err
Expand All @@ -129,7 +133,7 @@ func RTFetch(ctx context.Context, cfg config.Config, dbf model.Finder, rtf model
return errors.New("invalid rt data")
}
key := fmt.Sprintf("rtdata:%s:%s", target, urlType)
return rtf.AddData(key, rtdata)
return cfg.RTFinder.AddData(key, rtdata)
}

type CheckFetchWaitResult struct {
Expand Down Expand Up @@ -216,7 +220,7 @@ func CheckFetchWaitBatch(ctx context.Context, db sqlx.Ext, feedIds []int, urlTyp
return nil, err
}
for _, fetch := range lastFetches {
a, _ := checks[fetch.ID]
a := checks[fetch.ID]
a.CheckedAt = now
a.ID = fetch.ID
a.OnestopID = fetch.OnestopID
Expand Down Expand Up @@ -250,9 +254,12 @@ func chunkBy[T any](items []T, chunkSize int) (chunks [][]T) {
return append(chunks, items)
}

func fetchCheckFeed(ctx context.Context, dbf model.Finder, checker model.Checker, feedId string, urlType string, url string) (*model.Feed, error) {
func fetchCheckFeed(ctx context.Context, feedId string, urlType string, url string) (*model.Feed, error) {
cfg := model.ForContext(ctx)
checker := cfg.Checker

// Check feed exists
feeds, err := dbf.FindFeeds(ctx, nil, nil, nil, &model.FeedFilter{OnestopID: &feedId})
feeds, err := cfg.Finder.FindFeeds(ctx, nil, nil, nil, &model.FeedFilter{OnestopID: &feedId})
if err != nil {
return nil, err
}
Expand Down
16 changes: 9 additions & 7 deletions actions/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package actions

import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

sq "github.com/Masterminds/squirrel"

"github.com/interline-io/transitland-lib/dmfr"
"github.com/interline-io/transitland-server/internal/dbutil"
"github.com/interline-io/transitland-server/internal/testfinder"
"github.com/interline-io/transitland-server/internal/testconfig"
"github.com/interline-io/transitland-server/internal/testutil"
"github.com/interline-io/transitland-server/model"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestStaticFetchWorker(t *testing.T) {
return

}
buf, err := ioutil.ReadFile(testutil.RelPath(tc.serveFile))
buf, err := os.ReadFile(testutil.RelPath(tc.serveFile))
if err != nil {
http.Error(w, "404", 404)
return
Expand All @@ -110,9 +110,11 @@ func TestStaticFetchWorker(t *testing.T) {

// Setup job
feedUrl := ts.URL + "/" + tc.serveFile
testfinder.FindersTxRollback(t, nil, nil, func(te model.Finders) {
testconfig.ConfigTxRollback(t, testconfig.Options{}, func(cfg model.Config) {
cfg.Checker = nil // disable checker for this test
ctx := model.WithConfig(context.Background(), cfg)
// Run job
if result, err := StaticFetch(context.Background(), te.Config, te.Finder, tc.feedId, nil, feedUrl, nil); err != nil && !tc.expectError {
if result, err := StaticFetch(ctx, tc.feedId, nil, feedUrl); err != nil && !tc.expectError {
_ = result
t.Fatal("unexpected error", err)
} else if err == nil && tc.expectError {
Expand All @@ -123,8 +125,8 @@ func TestStaticFetchWorker(t *testing.T) {
// Check output
ff := dmfr.FeedFetch{}
if err := dbutil.Get(
context.Background(),
te.Finder.DBX(),
ctx,
cfg.Finder.DBX(),
sq.StatementBuilder.
Select("ff.*").
From("feed_fetches ff").
Expand Down
20 changes: 15 additions & 5 deletions actions/fv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/interline-io/transitland-lib/tl/tt"
"github.com/interline-io/transitland-lib/tldb"
"github.com/interline-io/transitland-mw/auth/authz"
"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/model"
)

func FeedVersionImport(ctx context.Context, cfg config.Config, dbf model.Finder, checker model.Checker, fvid int) (*model.FeedVersionImportResult, error) {
func FeedVersionImport(ctx context.Context, fvid int) (*model.FeedVersionImportResult, error) {
cfg := model.ForContext(ctx)
checker := cfg.Checker
dbf := cfg.Finder
if checker == nil {
return nil, authz.ErrUnauthorized
}
Expand All @@ -38,7 +40,10 @@ func FeedVersionImport(ctx context.Context, cfg config.Config, dbf model.Finder,
return &mr, nil
}

func FeedVersionUnimport(ctx context.Context, cfg config.Config, dbf model.Finder, checker model.Checker, fvid int) (*model.FeedVersionUnimportResult, error) {
func FeedVersionUnimport(ctx context.Context, fvid int) (*model.FeedVersionUnimportResult, error) {
cfg := model.ForContext(ctx)
checker := cfg.Checker
dbf := cfg.Finder
if checker == nil {
return nil, authz.ErrUnauthorized
}
Expand All @@ -59,7 +64,10 @@ func FeedVersionUnimport(ctx context.Context, cfg config.Config, dbf model.Finde
return &mr, nil
}

func FeedVersionUpdate(ctx context.Context, cfg config.Config, dbf model.Finder, checker model.Checker, fvid int, values model.FeedVersionSetInput) error {
func FeedVersionUpdate(ctx context.Context, fvid int, values model.FeedVersionSetInput) error {
cfg := model.ForContext(ctx)
checker := cfg.Checker
dbf := cfg.Finder
if checker == nil {
return authz.ErrUnauthorized
}
Expand Down Expand Up @@ -93,7 +101,9 @@ func FeedVersionUpdate(ctx context.Context, cfg config.Config, dbf model.Finder,
return nil
}

func FeedVersionDelete(ctx context.Context, cfg config.Config, dbf model.Finder, checker model.Checker, fvid int) (*model.FeedVersionDeleteResult, error) {
func FeedVersionDelete(ctx context.Context, fvid int) (*model.FeedVersionDeleteResult, error) {
cfg := model.ForContext(ctx)
checker := cfg.Checker
if checker == nil {
return nil, authz.ErrUnauthorized
}
Expand Down
5 changes: 3 additions & 2 deletions actions/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/interline-io/transitland-lib/tl/tt"
"github.com/interline-io/transitland-lib/tlcsv"
"github.com/interline-io/transitland-lib/validator"
"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/model"
)

Expand All @@ -21,7 +20,9 @@ type hasGeometries interface {
}

// ValidateUpload takes a file Reader and produces a validation package containing errors, warnings, file infos, service levels, etc.
func ValidateUpload(ctx context.Context, cfg config.Config, src io.Reader, feedURL *string, rturls []string) (*model.ValidationResult, error) {
func ValidateUpload(ctx context.Context, src io.Reader, feedURL *string, rturls []string) (*model.ValidationResult, error) {
cfg := model.ForContext(ctx)

// Check inputs
rturlsok := []string{}
for _, rturl := range rturls {
Expand Down
20 changes: 0 additions & 20 deletions config/config.go

This file was deleted.

20 changes: 4 additions & 16 deletions finders/dbfinder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package dbfinder

import (
"context"
"log"
"os"
"testing"

"github.com/interline-io/transitland-server/internal/testutil"
"github.com/interline-io/transitland-server/model"
"github.com/stretchr/testify/assert"
)

var TestFinder model.Finder

func TestMain(m *testing.M) {
if a, ok := testutil.CheckTestDB(); !ok {
log.Print(a)
return
}
func TestFinder_FindFeedVersionServiceWindow(t *testing.T) {
db := testutil.MustOpenTestDB()
dbf := NewFinder(db, nil)
TestFinder = dbf
os.Exit(m.Run())
}
testFinder := dbf

func TestFinder_FindFeedVersionServiceWindow(t *testing.T) {
fvm := map[string]int{}
fvs, err := TestFinder.FindFeedVersions(context.TODO(), nil, nil, nil, nil)
fvs, err := testFinder.FindFeedVersions(context.TODO(), nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -64,7 +52,7 @@ func TestFinder_FindFeedVersionServiceWindow(t *testing.T) {
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
start, end, best, err := TestFinder.FindFeedVersionServiceWindow(context.TODO(), tc.fvid)
start, end, best, err := testFinder.FindFeedVersionServiceWindow(context.TODO(), tc.fvid)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions finders/gbfsfinder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGbfsFinder(t *testing.T) {
}
client := testutil.MustOpenTestRedisClient()
gbf := NewFinder(client)
setupGbfs(nil, gbf)
testSetupGbfs(gbf)

tcs := []struct {
p xy.Point
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestGbfsFinder(t *testing.T) {

}

func setupGbfs(dbf model.Finder, gbf model.GbfsFinder) error {
func testSetupGbfs(gbf model.GbfsFinder) error {
// Setup
sourceFeedId := "gbfs-test"
ts := httptest.NewServer(&gbfs.TestGbfsServer{Language: "en", Path: testutil.RelPath("test/data/gbfs")})
Expand Down
Loading
Loading