Browse Source

Infrastructure tests now use config correctly to set up test DB.

Frederic G. MARAND 5 years ago
parent
commit
2495579d44

+ 1 - 1
.gitignore

@@ -8,7 +8,7 @@
 cmd/kurz/kurz
 cmd/kurzd/kurzd
 doc/*/*.svg
-cover.out
+/cover*
 
 # Folders
 _obj

+ 8 - 1
cmd/kurzd/export_content.go

@@ -4,11 +4,18 @@ import (
 	"code.osinet.fr/fgm/kurz/infrastructure"
 	"database/sql"
 	"fmt"
-
 	"github.com/spf13/cobra"
 	"gopkg.in/yaml.v2"
+	"time"
 )
 
+type MapEntry struct {
+	Hash                uint64
+	Url                 string
+	Date1, Date2, Date3 time.Time
+	RefCount            uint32
+}
+
 var cmdExportContent = &cobra.Command{
 	Use:   "content",
 	Short: "Export the database contents",

+ 5 - 12
cmd/kurzd/kurzd.go

@@ -2,10 +2,8 @@
 package main
 
 import (
+	"fmt"
 	"os"
-	"time"
-
-	_ "github.com/go-sql-driver/mysql"
 )
 
 /**
@@ -30,13 +28,8 @@ kurzd
 	stop                            Stop all instances of kurzd on the current server (restricted)
 */
 func main() {
-	Execute()
-	os.Exit(0)
-}
-
-type MapEntry struct {
-	Hash                uint64
-	Url                 string
-	Date1, Date2, Date3 time.Time
-	RefCount            uint32
+	if err := cmd.Execute(); err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
 }

+ 0 - 7
cmd/kurzd/rootCmd.go

@@ -31,10 +31,3 @@ func init() {
 	cobra.OnInitialize(initConfig)
 	verbose = *cmd.PersistentFlags().BoolP("verbose", "v", false, "Add -v for verbose operation")
 }
-
-func Execute() {
-	if err := cmd.Execute(); err != nil {
-		fmt.Println(err)
-		os.Exit(1)
-	}
-}

+ 1 - 1
domain/doc.go

@@ -12,5 +12,5 @@ Its SPI is made of:
 
   - interfaces ShortURLRepository and TargetURLRepository
   - configuration function RegisterRepositories()
- */
+*/
 package domain

+ 8 - 4
domain/domain_spi.go

@@ -7,12 +7,16 @@ type ShortURLRepository interface {
 	GetTarget(su ShortURL) (TargetURL, error)
 }
 
+/*
+TargetURLRepository is the interface infrastructure needs to implement to
+provide the domain with access to Target URLs.
+*/
 type TargetURLRepository interface {
-	/*
-	GetShort returns the ShortURL for a given TargetURL.
 
-	In the results, isNew will be true if the ShortURL was created for this occasion.
-	 */
+	/*
+		GetShort returns the ShortURL for a given TargetURL.
+		In the results, isNew will be true if the ShortURL was created for this occasion.
+	*/
 	GetShort(tu TargetURL) (su ShortURL, isNew bool, err error)
 }
 

+ 4 - 4
domain/future/domain.go

@@ -7,22 +7,22 @@ import (
 
 /*
 Domain represents a DNS domain.
- */
+*/
 type Domain interface {
 	String() string
 }
 
-/**
+/*
 HTTPStatus is a subset of int limited to the values enumerated in net/http,
 like StatusOK (200) or StatusNotFound (404).
- */
+*/
 type HTTPStatus interface {
 	Int() int
 }
 
 /**
 HTTPRedirectStatus is a subset of HTTPStatus limited to the values from 300 to 399.
- */
+*/
 type HTTPRedirectStatus HTTPStatus
 
 type ShortURLAssigner interface {

+ 1 - 2
domain/short_url_test.go

@@ -13,7 +13,7 @@ func TestNewShortURLHappy(t *testing.T) {
 		t.FailNow()
 	}
 
-	if su.URL.IsEmpty() || su.target.IsEmpty() || !su.target.MayRedirect(){
+	if su.URL.IsEmpty() || su.target.IsEmpty() || !su.target.MayRedirect() {
 		t.FailNow()
 	}
 }
@@ -26,4 +26,3 @@ func TestNewShortURLSadFromEmpty(t *testing.T) {
 		t.FailNow()
 	}
 }
-

+ 5 - 10
infrastructure/infratructure.go

@@ -3,13 +3,13 @@ package infrastructure
 import (
 	"database/sql"
 	"github.com/spf13/viper"
+
+	_ "github.com/go-sql-driver/mysql"
 )
 
 const exampleValidHTTPURL = "https://example.com"
 
-/**
-DbDial is almost a proxy to sql.Open but guarantees that db will be nil if err is not nil.
- */
+// DbDial is almost a proxy to sql.Open but guarantees that db will be nil if err is not nil.
 func DbDial(dbDriver, dbDsn string) (*sql.DB, error) {
 	db, err := sql.Open(dbDriver, dbDsn)
 	if err != nil {
@@ -18,9 +18,7 @@ func DbDial(dbDriver, dbDsn string) (*sql.DB, error) {
 	return db, nil
 }
 
-/**
-ParseDbCred returns DB information from the supported configuration sources.
- */
+// ParseDbCred returns DB information from the supported configuration sources.
 func ParseDbCred() (driver, dsn string) {
 	viper.SetDefault("database.driver", "mysql")
 	viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
@@ -31,12 +29,9 @@ func ParseDbCred() (driver, dsn string) {
 	return
 }
 
-/**
-ParseDbCred returns Test DB information from the supported configuration sources.
- */
+// ParseDbCred returns Test DB information from the supported configuration sources.
 func ParseTestDbCred() (driver, dsn string) {
 	viper.SetDefault("database.driver", "mysql")
-	viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
 	viper.SetDefault("database.test_dsn", "root:root@tcp(localhost:3306)/kurz_test")
 
 	driver = viper.Get("database.driver").(string)

+ 2 - 2
infrastructure/memory_test.go

@@ -3,7 +3,7 @@ package infrastructure
 import (
 	"code.osinet.fr/fgm/kurz/domain"
 	"testing"
-	)
+)
 
 func TestMemoryEmptyRepo(test *testing.T) {
 
@@ -55,4 +55,4 @@ func TestMemorySad(test *testing.T) {
 	if err == nil {
 		test.Error("Empty target URL has no valid short URL")
 	}
-}
+}

+ 32 - 4
infrastructure/mysql_test.go

@@ -1,12 +1,15 @@
 package infrastructure
 
 import (
-	"code.osinet.fr/fgm/kurz/domain"
 	"database/sql"
-	"github.com/pressly/goose"
+	"fmt"
 	"os"
 	"testing"
 
+	"code.osinet.fr/fgm/kurz/domain"
+	"github.com/pressly/goose"
+	"github.com/spf13/viper"
+
 	_ "github.com/go-sql-driver/mysql"
 )
 
@@ -65,7 +68,7 @@ func TestMySQLSad(test *testing.T) {
 
 /**
 mySQLTestSetup opens the database and initialized it from the production database schema.
- */
+*/
 func mySQLTestSetup(t *testing.T) *sql.DB {
 	db, err := DbDial(ParseTestDbCred())
 	if err != nil {
@@ -74,7 +77,23 @@ func mySQLTestSetup(t *testing.T) *sql.DB {
 
 	// Ensure current schema for test DB.
 	cwd, _ := os.Getwd()
-	goose.Up(db, cwd)
+	current, err := goose.GetDBVersion(db)
+	if err != nil {
+		t.Errorf("setup failed to obtain the test DB version: %s", err)
+		t.FailNow()
+	}
+	versions, err := goose.CollectMigrations(cwd, current, int64((1<<63)-1))
+	if err != nil {
+		t.Errorf("setup failed to obtain the test DB migrations: %s", err)
+		t.FailNow()
+	}
+	if versions != nil {
+		err = goose.Up(db, cwd)
+		if err != nil {
+			t.Errorf("setup failed to upgrade the test DB schema: %s", err)
+			t.FailNow()
+		}
+	}
 
 	// Ensure empty test DB.
 	_, err = db.Exec("DELETE FROM map")
@@ -96,6 +115,15 @@ func mySQLTestTeardown(t *testing.T, db *sql.DB) {
 }
 
 func TestMain(m *testing.M) {
+	viper.SetConfigName("config")
+	viper.AddConfigPath(".")
+	viper.AddConfigPath("$HOME/.kurz")
+	err := viper.ReadInConfig()
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err)
+		os.Exit(2)
+	}
+
 	exitCode := m.Run()
 	os.Exit(exitCode)
 }

+ 1 - 1
migrations/migrations.go

@@ -16,4 +16,4 @@ func simpleRun(tx *sql.Tx, instructions strings) error {
 	}
 
 	return nil
-}
+}