Skip to content

Commit

Permalink
added more tests and fixed linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Oct 18, 2023
1 parent 1b0272b commit 49b7c75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
14 changes: 10 additions & 4 deletions internal/enricher/geolocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type GeoDBFetcher interface {
GetDB(ctx context.Context, key string, downloadPath string) error
GetDB(ctx context.Context, key, downloadPath string) error
}

type Geolocation struct {
Expand All @@ -37,12 +37,12 @@ type geoEnricher struct {

func NewGeoEnricher(
dbProvider GeoDBFetcher,
config *config.Config,
conf *config.Config,
log logger.Logger,
statClient stats.Stats,
) (PipelineEnricher, error) {
upstreamDBKey := config.GetString("Geolocation.db.key", "geolite2City.mmdb")
downloadPath := config.GetString("Geolocation.db.downloadPath", "geolite2City.mmdb")
upstreamDBKey := conf.GetString("Geolocation.db.key", "geolite2City.mmdb")
downloadPath := conf.GetString("Geolocation.db.downloadPath", "geolite2City.mmdb")

err := dbProvider.GetDB(context.Background(), upstreamDBKey, downloadPath)
if err != nil {
Expand Down Expand Up @@ -119,6 +119,12 @@ type geoDB struct {
// GetDB simply fetches the database from the upstream located at the key defined
// in the argument and stores it in the downloadPath provided.
func (db *geoDB) GetDB(ctx context.Context, key, downloadPath string) error {
// If the file already exists, do not go into the loop of downloading
// the file again from s3.
if _, err := os.Stat(downloadPath); err == nil {
return nil
}

f, err := os.Create(downloadPath)
if err != nil {
return fmt.Errorf("creating a file to store db contents: %w", err)
Expand Down
35 changes: 23 additions & 12 deletions internal/enricher/geolocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package enricher

import (
"context"
"errors"
"fmt"
"testing"

Expand All @@ -23,9 +22,8 @@ func (p *GeoTestDBProvider) GetDB(ctx context.Context, key, downloadPath string)

func TestGeolocationEnrichment_Setup(t *testing.T) {
var (
defaultConf = config.New()
logger = logger.NewLogger()
stats = stats.Default
defaultLog = logger.NewLogger()
defaultStats = stats.Default
)

t.Run("inexistent db file causes enricher to fail setup", func(t *testing.T) {
Expand All @@ -34,30 +32,43 @@ func TestGeolocationEnrichment_Setup(t *testing.T) {
c := config.New()
c.Set("Geolocation.db.downloadPath", "./testdata/invalid-db-path")

_, err := NewGeoEnricher(&GeoTestDBProvider{}, defaultConf, logger, stats)
_, err := NewGeoEnricher(&GeoTestDBProvider{}, c, defaultLog, defaultStats)
require.NotNil(t, err)
t.Log(err)
require.True(t, errors.Is(err, geolocation.ErrInvalidDatabase))
require.ErrorIs(t, err, geolocation.ErrInvalidDatabase)
})

t.Run("corrupted db file causes enricher to fail", func(t *testing.T) {
t.Run("corrupted db file causes enricher to fail setup", func(t *testing.T) {
t.Parallel()

c := config.New()
c.Set("Geolocation.db.downloadPath", "./testdata/corrupted_city_test.mmdb")

_, err := NewGeoEnricher(&GeoTestDBProvider{}, defaultConf, logger, stats)
_, err := NewGeoEnricher(&GeoTestDBProvider{}, c, defaultLog, defaultStats)
require.NotNil(t, err)
t.Log(err)
require.True(t, errors.Is(err, geolocation.ErrInvalidDatabase))
require.ErrorIs(t, err, geolocation.ErrInvalidDatabase)
})

t.Run("correctly downloaded file causes enricher to setup correctly", func(t *testing.T) {
t.Parallel()

c := config.New()
c.Set("Geolocation.db.downloadPath", "./testdata/city_test.mmdb")

_, err := NewGeoEnricher(&GeoTestDBProvider{}, c, defaultLog, defaultStats)
require.Nil(t, err)
})
}

func TestGeolocationEnrichment_Success(t *testing.T) {
c := config.New()
c.Set("Geolocation.db.downloadPath", "./testdata/city_test.mmdb")
enricher, err := NewGeoEnricher(&GeoTestDBProvider{}, c, logger.NewLogger(), stats.Default)

// The original fetcher works here because the file exists at the
// path and it assumes it is correct.
fetcher, err := NewGeoDBFetcher(c, logger.NewLogger())
require.Nil(t, err)

enricher, err := NewGeoEnricher(fetcher, c, logger.NewLogger(), stats.Default)
require.Nil(t, err)

t.Run("it silently returns without enrichment if ip is empty", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions services/geolocation/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type maxmindDBReader struct {
}

func NewMaxmindDBReader(dbLoc string) (*maxmindDBReader, error) {

reader, err := maxminddb.Open(dbLoc)
if err != nil {

Expand Down

0 comments on commit 49b7c75

Please sign in to comment.