From 4efc23eda843f84eb6beeae5dc07980dab285ac6 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sun, 24 Dec 2017 17:41:22 +0100 Subject: [PATCH] added boltdb storage unit testing --- handlers/public.go | 4 +- stores/boltdb/{bolt.go => boltdb.go} | 0 stores/boltdb/boltdb_test.go | 67 ++++++++++++++++++++++++++++ util/config.go | 26 ++++++----- 4 files changed, 83 insertions(+), 14 deletions(-) rename stores/boltdb/{bolt.go => boltdb.go} (100%) create mode 100644 stores/boltdb/boltdb_test.go diff --git a/handlers/public.go b/handlers/public.go index 71b6d9b..593d663 100644 --- a/handlers/public.go +++ b/handlers/public.go @@ -58,9 +58,9 @@ func (h *Handler) handleAccess(c *gin.Context) { if err != nil { if strings.Contains(err.Error(), shared.ErrNoEntryFound.Error()) { return - http.Error(c.Writer, fmt.Sprintf("could not get and crease visitor counter: %v, ", err), http.StatusInternalServerError) - return } + http.Error(c.Writer, fmt.Sprintf("could not get and crease visitor counter: %v, ", err), http.StatusInternalServerError) + return } // No password set if len(entry.Password) == 0 { diff --git a/stores/boltdb/bolt.go b/stores/boltdb/boltdb.go similarity index 100% rename from stores/boltdb/bolt.go rename to stores/boltdb/boltdb.go diff --git a/stores/boltdb/boltdb_test.go b/stores/boltdb/boltdb_test.go new file mode 100644 index 0000000..370e2a6 --- /dev/null +++ b/stores/boltdb/boltdb_test.go @@ -0,0 +1,67 @@ +package boltdb + +import ( + "os" + "testing" + "time" + + "github.com/maxibanki/golang-url-shortener/stores/shared" +) + +func getStore(t *testing.T) (*BoltStore, func()) { + store, err := New("test.db") + if err != nil { + t.Errorf("could not get store: %v", err) + } + return store, func() { + store.Close() + os.Remove("test.db") + } +} + +func TestBoltDB(t *testing.T) { + store, cleanup := getStore(t) + givenEntryID := "x1df" + givenEntry := shared.Entry{ + DeletionURL: "foo", + RemoteAddr: "127.0.0.1", + Public: shared.EntryPublicData{ + CreatedOn: time.Now(), + URL: "google.com", + }, + } + if err := store.CreateEntry(givenEntry, givenEntryID, "google01234"); err != nil { + t.Errorf("could not create entry: %v", err) + } + entryBeforeIncreasement, err := store.GetEntryByID(givenEntryID) + if err != nil { + t.Errorf("could not get entry: %v", err) + } + if err := store.IncreaseVisitCounter(givenEntryID); err != nil { + t.Errorf("could not increase visit counter: %v", err) + } + entryAfterIncreasement, err := store.GetEntryByID(givenEntryID) + if err != nil { + t.Errorf("could not get entry: %v", err) + } + if entryBeforeIncreasement.Public.VisitCount+1 != entryAfterIncreasement.Public.VisitCount { + t.Errorf("Visit counter hasn't increased; before: %d, after: %d", entryBeforeIncreasement.Public.VisitCount, entryAfterIncreasement.Public.VisitCount) + } + if err := store.RegisterVisitor(givenEntryID, "whooop", shared.Visitor{ + IP: "foo", + Referer: "foo", + }); err != nil { + t.Errorf("Failed to register visitor: %v", err) + } + visitors, err := store.GetVisitors(givenEntryID) + if err != nil { + t.Errorf("could not get visitors: %v", err) + } + if len(visitors) != 1 { + t.Errorf("Expected visitor length: %d; got: %d", err) + } + if err := store.DeleteEntry(givenEntryID); err != nil { + t.Errorf("could not delte entry: %v", err) + } + cleanup() +} diff --git a/util/config.go b/util/config.go index 4fddeb6..8ee5b66 100644 --- a/util/config.go +++ b/util/config.go @@ -13,6 +13,7 @@ import ( "gopkg.in/yaml.v2" ) +// Configuration are the available config values type Configuration struct { ListenAddr string `yaml:"ListenAddr" env:"LISTEN_ADDR"` BaseURL string `yaml:"BaseURL" env:"BASE_URL"` @@ -30,16 +31,15 @@ type oAuthConf struct { ClientSecret string `yaml:"ClientSecret" env:"CLIENT_SECRET"` } -var ( - config = Configuration{ - ListenAddr: ":8080", - BaseURL: "http://localhost:3000", - DataDir: "data", - EnableDebugMode: false, - UseSSL: false, - ShortedIDLength: 4, - } -) +// config contains the default values +var config = Configuration{ + ListenAddr: ":8080", + BaseURL: "http://localhost:3000", + DataDir: "data", + EnableDebugMode: false, + UseSSL: false, + ShortedIDLength: 4, +} // ReadInConfig loads the Configuration and other needed folders for further usage func ReadInConfig() error { @@ -53,7 +53,7 @@ func ReadInConfig() error { } else { logrus.Info("No configuration file found, using defaults with environment variable overrides.") } - if err := config.ApplyEnvironmentConfig(); err != nil { + if err := config.applyEnvironmentConfig(); err != nil { return errors.Wrap(err, "could not apply environment configuration") } config.DataDir, err = filepath.Abs(config.DataDir) @@ -68,7 +68,7 @@ func ReadInConfig() error { return nil } -func (c *Configuration) ApplyEnvironmentConfig() error { +func (c *Configuration) applyEnvironmentConfig() error { return c.setDefaultValue(reflect.ValueOf(c), reflect.TypeOf(*c), -1, "GUS") } @@ -117,10 +117,12 @@ func (o oAuthConf) Enabled() bool { return o.ClientSecret != "" } +// GetConfig returns the configuration from the memory func GetConfig() Configuration { return config } +// SetConfig sets the configuration into the memory func SetConfig(c Configuration) { config = c }