Browse Source

added boltdb storage unit testing

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
4efc23eda8
  1. 4
      handlers/public.go
  2. 0
      stores/boltdb/boltdb.go
  3. 67
      stores/boltdb/boltdb_test.go
  4. 26
      util/config.go

4
handlers/public.go

@ -58,9 +58,9 @@ func (h *Handler) handleAccess(c *gin.Context) {
if err != nil { if err != nil {
if strings.Contains(err.Error(), shared.ErrNoEntryFound.Error()) { if strings.Contains(err.Error(), shared.ErrNoEntryFound.Error()) {
return 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 // No password set
if len(entry.Password) == 0 { if len(entry.Password) == 0 {

0
stores/boltdb/bolt.go → stores/boltdb/boltdb.go

67
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()
}

26
util/config.go

@ -13,6 +13,7 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// Configuration are the available config values
type Configuration struct { type Configuration struct {
ListenAddr string `yaml:"ListenAddr" env:"LISTEN_ADDR"` ListenAddr string `yaml:"ListenAddr" env:"LISTEN_ADDR"`
BaseURL string `yaml:"BaseURL" env:"BASE_URL"` BaseURL string `yaml:"BaseURL" env:"BASE_URL"`
@ -30,16 +31,15 @@ type oAuthConf struct {
ClientSecret string `yaml:"ClientSecret" env:"CLIENT_SECRET"` ClientSecret string `yaml:"ClientSecret" env:"CLIENT_SECRET"`
} }
var ( // config contains the default values
config = Configuration{ var config = Configuration{
ListenAddr: ":8080", ListenAddr: ":8080",
BaseURL: "http://localhost:3000", BaseURL: "http://localhost:3000",
DataDir: "data", DataDir: "data",
EnableDebugMode: false, EnableDebugMode: false,
UseSSL: false, UseSSL: false,
ShortedIDLength: 4, ShortedIDLength: 4,
} }
)
// ReadInConfig loads the Configuration and other needed folders for further usage // ReadInConfig loads the Configuration and other needed folders for further usage
func ReadInConfig() error { func ReadInConfig() error {
@ -53,7 +53,7 @@ func ReadInConfig() error {
} else { } else {
logrus.Info("No configuration file found, using defaults with environment variable overrides.") 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") return errors.Wrap(err, "could not apply environment configuration")
} }
config.DataDir, err = filepath.Abs(config.DataDir) config.DataDir, err = filepath.Abs(config.DataDir)
@ -68,7 +68,7 @@ func ReadInConfig() error {
return nil return nil
} }
func (c *Configuration) ApplyEnvironmentConfig() error { func (c *Configuration) applyEnvironmentConfig() error {
return c.setDefaultValue(reflect.ValueOf(c), reflect.TypeOf(*c), -1, "GUS") return c.setDefaultValue(reflect.ValueOf(c), reflect.TypeOf(*c), -1, "GUS")
} }
@ -117,10 +117,12 @@ func (o oAuthConf) Enabled() bool {
return o.ClientSecret != "" return o.ClientSecret != ""
} }
// GetConfig returns the configuration from the memory
func GetConfig() Configuration { func GetConfig() Configuration {
return config return config
} }
// SetConfig sets the configuration into the memory
func SetConfig(c Configuration) { func SetConfig(c Configuration) {
config = c config = c
} }

Loading…
Cancel
Save