Skip to content

Commit

Permalink
Make store testable
Browse files Browse the repository at this point in the history
Store driver name is now a parameter on Store class. It make possible
to test some aspects of store class. Also, unit tests included.
  • Loading branch information
ecanuto committed Aug 14, 2020
1 parent 6a7db96 commit 4549bc2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ func init() {
}

func main() {
store := store.New()
defer store.Close() // Without gin graceful stop it is not really working
driver := os.Getenv("STORAGE_DRIVER")
if driver == "" {
driver = "memory"
}

store, err := store.New(driver)
if err != nil {
log.Fatal(err)
}
defer store.Close()

r := gin.Default()
r.GET("/:hash", func(c *gin.Context) {
Expand Down
42 changes: 28 additions & 14 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"time"

"github.com/teris-io/shortid"
)
Expand All @@ -20,10 +21,16 @@ type storeDriver interface {

var drivers = make(map[string]storeDriver)

type storeData struct {
URL string `json:"url" binding:"required"`
CreatedAt time.Time `json:"createdAt" binding:"required"`
}

// Store is the database storage driver.
type Store struct {
driver storeDriver
AuthToken string
driver storeDriver
driverName string
AuthToken string
}

// Register makes a store driver available by the provided name.
Expand All @@ -32,22 +39,29 @@ func Register(name string, driver storeDriver) {
}

// New creates a new instance of Store object
func New() *Store {
s := new(Store)
s.AuthToken = os.Getenv("AUTHORIZATION_TOKEN")

// Get storage driver or use firestore as default.
name := os.Getenv("STORAGE_DRIVER")
if name == "" {
name = "memory"
func New(driverName string) (*Store, error) {
if driverName == "" {
return nil, errors.New("Driver name cannot be empty. Use .Default method instead")
}

driver, exists := drivers[driverName]
if !exists {
return nil, errors.New("Driver does not exists")
}
log.Printf("Storage driver: %s", name)

// Initialize storage driver.
s.driver = drivers[name]
s := &Store{
driver: driver,
driverName: driverName,
AuthToken: os.Getenv("AUTHORIZATION_TOKEN"),
}
s.driver.init()

return s
return s, nil
}

// Default ...
func Default() (*Store, error) {
return New("memory")
}

// Close cleanup driver connection
Expand Down
33 changes: 33 additions & 0 deletions pkg/store/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package store

import "testing"

func TestStoreEmptyDriver(t *testing.T) {
exp := "Driver name cannot be empty. Use .Default method instead"
_, err := New("")

if err.Error() != exp {
t.Errorf("Expected error \"%v\" and got \"%v\".", exp, err)
}
}

func TestStoreInvalidDriver(t *testing.T) {
exp := "Driver does not exists"
_, err := New("nonono")

if err.Error() != exp {
t.Errorf("Expected error \"%v\" and got \"%v\".", exp, err)
}
}

func TestStoreDefaultDriver(t *testing.T) {
exp := "memory"
store, err := Default()
if err != nil {
t.Errorf("Error \"%v\".", err)
}
if store.driverName != exp {
t.Errorf("Expected driver \"%v\" and got \"%v\".", exp, store.driverName)
}
defer store.Close()
}

0 comments on commit 4549bc2

Please sign in to comment.