Open platform to collect and prioritize feedback
from gotest go ginkgo
goenningd02d57b83a20
diff --git a/.gitignore b/.gitignoreindex b845f29a1..110130235 100644--- a/.gitignore+++ b/.gitignore@@ -1,6 +1,7 @@vendor/*/data/tmp/+*.coverprofilewchygin-bindiff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 9e5d39d0c..e4ad69b68 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -11,6 +11,7 @@"gin-bin": true,"gin-bin.exe": true,"wchy": true,- "tmp": true+ "tmp": true,+ "**/*.coverprofile": true}}\ No newline at end of filediff --git a/Makefile b/Makefileindex 052978e46..cfb9c3ea6 100644--- a/Makefile+++ b/Makefile@@ -10,19 +10,22 @@ define tag_dockerendeftest:- GO_ENV=test go test $$(go list ./... | grep -v /vendor/) -cover+ GO_ENV=test ginkgo -r -cover+ goverlint:golint -set_exit_statussetup-ci:- go get -u github.com/kardianos/govendor- go get -u github.com/golang/lint/golint- go get -u github.com/mattn/goveralls+ go install github.com/onsi/ginkgo/ginkgo+ go install github.com/kardianos/govendor+ go install github.com/modocache/gover+ go install github.com/golang/lint/golint+ go install github.com/mattn/goverallsgovendor sync-run-ci: lint- goveralls -service=travis-ci+run-ci:+ goveralls -coverprofile=gover.coverprofile -service=travis-ciifeq ($(TRAVIS_PULL_REQUEST), false)CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make builddocker login -u $(DOCKER_USER) -p $(DOCKER_PASS)diff --git a/env/env_suite_test.go b/env/env_suite_test.gonew file mode 100644index 000000000..be709615f--- /dev/null+++ b/env/env_suite_test.go@@ -0,0 +1,13 @@+package env_test++import (+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"++ "testing"+)++func TestSuite(t *testing.T) {+ RegisterFailHandler(Fail)+ RunSpecs(t, "env Test Suite")+}diff --git a/env/env_test.go b/env/env_test.goindex 42361a48f..97a2b2d5e 100644--- a/env/env_test.go+++ b/env/env_test.go@@ -1,12 +1,11 @@package env_testimport (- "testing"-"os""github.com/WeCanHearYou/wchy/env"- "github.com/stretchr/testify/assert"+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega")var envs = []struct {@@ -21,28 +20,34 @@ var envs = []struct {{"anything", "canhearyou.com", "development"},}-func TestGetEnvOrDefault_ShouldReturnNullWhenKeyIsUnknown(t *testing.T) {- value := env.GetEnvOrDefault("UNKNOWN_KEY", "some value")- assert.Equal(t, "some value", value)-}+var _ = Describe("GetEnvOrDefault", func() {+ It("should return null when key is unknown", func() {+ value := env.GetEnvOrDefault("UNKNOWN_KEY", "some value")+ Expect(value).To(Equal("some value"))+ })-func TestGetEnvOrDefault_ShouldReturnEnvironmentValue(t *testing.T) {- value := env.GetEnvOrDefault("PATH", "default path")- assert.NotEqual(t, "default path", value)-}+ It("should return env variable when key is known", func() {+ value := env.GetEnvOrDefault("PATH", "default path")+ Expect(value).NotTo(Equal("default path"))+ })+})-func TestGetCurrentDomain_ShouldReturnCorrectDomain(t *testing.T) {+var _ = Describe("GetCurrentDomain", func() {for _, testCase := range envs {- os.Setenv("GO_ENV", testCase.go_env)- actual := env.GetCurrentDomain()- assert.Equal(t, testCase.domain, actual)+ It("should return correct domain for "+testCase.go_env, func() {+ os.Setenv("GO_ENV", testCase.go_env)+ actual := env.GetCurrentDomain()+ Expect(actual).To(Equal(testCase.domain))+ })}-}+})-func TestEnvironment_ShouldReturnCorrectEnvironment(t *testing.T) {+var _ = Describe("Current", func() {for _, testCase := range envs {- os.Setenv("GO_ENV", testCase.go_env)- actual := env.Current()- assert.Equal(t, testCase.env, actual)+ It("should return correct environment for "+testCase.go_env, func() {+ os.Setenv("GO_ENV", testCase.go_env)+ actual := env.Current()+ Expect(actual).To(Equal(testCase.env))+ })}-}+})diff --git a/handlers/helper_test.go b/handlers/handlers_helper_test.gosimilarity index 100%rename from handlers/helper_test.gorename to handlers/handlers_helper_test.godiff --git a/handlers/handlers_suite_test.go b/handlers/handlers_suite_test.gonew file mode 100644index 000000000..59bbb5727--- /dev/null+++ b/handlers/handlers_suite_test.go@@ -0,0 +1,13 @@+package handlers_test++import (+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"++ "testing"+)++func TestSuite(t *testing.T) {+ RegisterFailHandler(Fail)+ RunSpecs(t, "handlers Test Suite")+}diff --git a/handlers/status_test.go b/handlers/status_test.goindex 9e8ab21ef..33936aaa2 100644--- a/handlers/status_test.go+++ b/handlers/status_test.go@@ -1,17 +1,16 @@package handlers_testimport (- "testing"-- "github.com/stretchr/testify/assert"+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega")-func Test_StatusHandler_ShouldReturnStatusBasedOnContext(t *testing.T) {- status, query := makeRequest("GET", "/api/status")+var _ = Describe("StatusHandler", func() {+ It("should return status based on context", func() {+ status, query := makeRequest("GET", "/api/status")- build, _ := query.String("build")- isHealthy, _ := query.Bool("healthy", "database")- assert.Equal(t, "today", build)- assert.Equal(t, false, isHealthy)- assert.Equal(t, 200, status)-}+ Expect(query.String("build")).To(Equal("today"))+ Expect(query.Bool("healthy", "database")).To(Equal(false))+ Expect(status).To(Equal(200))+ })+})diff --git a/handlers/tenant_test.go b/handlers/tenant_test.goindex 00faa1e43..96f55f15f 100644--- a/handlers/tenant_test.go+++ b/handlers/tenant_test.go@@ -1,24 +1,22 @@package handlers_testimport (- "testing"-- "github.com/stretchr/testify/assert"+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega")-func Test_GivenUnknownDomain_TenantByDomain_ShouldReturn404(t *testing.T) {- status, _ := makeRequest("GET", "/api/tenants/unknown")- assert.Equal(t, 404, status)-}+var _ = Describe("TenantHandler", func() {+ It("should return 404 when domain is unknown", func() {+ status, _ := makeRequest("GET", "/api/tenants/unknown")-func Test_GivenKnownDomain_TenantByDomain_ShouldReturnTenantData(t *testing.T) {- status, query := makeRequest("GET", "/api/tenants/trishop")+ Expect(status).To(Equal(404))+ })+ It("should return tenant data when domain is known", func() {+ status, query := makeRequest("GET", "/api/tenants/trishop")- id, _ := query.Int("id")- name, _ := query.String("name")- domain, _ := query.String("domain")- assert.Equal(t, 2, id)- assert.Equal(t, "The Triathlon Shop", name)- assert.Equal(t, "trishop", domain)- assert.Equal(t, 200, status)-}+ Expect(query.Int("id")).To(Equal(2))+ Expect(query.String("name")).To(Equal("The Triathlon Shop"))+ Expect(query.String("domain")).To(Equal("trishop"))+ Expect(status).To(Equal(200))+ })+})diff --git a/main.go b/main.goindex 617650975..7ba81c644 100644--- a/main.go+++ b/main.go@@ -4,7 +4,6 @@ import ("database/sql""os"- "github.com/gin-gonic/gin"_ "github.com/lib/pq""github.com/WeCanHearYou/wchy/context"@@ -24,10 +23,6 @@ func init() {log.Info("Application is starting...")log.Infof("GO_ENV: %s", env.Current())- if env.IsProduction() || env.IsStaging() {- gin.SetMode(gin.ReleaseMode)- }-db, _ = sql.Open("postgres", os.Getenv("DATABASE_URL"))ctx = context.WchyContext{diff --git a/router/router.go b/router/router.goindex b3ac645d2..324e6c247 100644--- a/router/router.go+++ b/router/router.go@@ -40,7 +40,14 @@ func stripPort(hostport string) string {// GetMainEngine returns main HTTP enginefunc GetMainEngine(ctx context.WchyContext) *gin.Engine {router := gin.New()- router.Use(gin.Logger())++ if env.IsDevelopment() {+ router.Use(gin.Logger())+ gin.SetMode(gin.DebugMode)+ } else {+ gin.SetMode(gin.ReleaseMode)+ }+router.Use(multiTenant(ctx))if env.IsTest() {diff --git a/services/health_test.go b/services/health_test.goindex a88019f6e..2c49a1b95 100644--- a/services/health_test.go+++ b/services/health_test.go@@ -2,40 +2,42 @@ package services_testimport ("fmt"- "testing""github.com/WeCanHearYou/wchy/services"- "github.com/stretchr/testify/assert"sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1")-func TestIsDatabaseOnline_ShouldReturnFalseIfQueryFails(t *testing.T) {- db, mock, _ := sqlmock.New()- defer db.Close()- mock.ExpectQuery("SELECT now()").WillReturnError(fmt.Errorf("some error"))+import (+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"+)- svc := &services.PostgresHealthCheckService{DB: db}- isOnline := svc.IsDatabaseOnline()+var _ = Describe("HealthCheckService.IsDatabaseOnline", func() {+ It("should return false if query fails", func() {+ db, mock, _ := sqlmock.New()+ defer db.Close()+ mock.ExpectQuery("SELECT now()").WillReturnError(fmt.Errorf("some error"))- assert.Equal(t, false, isOnline)+ svc := &services.PostgresHealthCheckService{DB: db}- if err := mock.ExpectationsWereMet(); err != nil {- t.Errorf("there were unfulfilled expections: %s", err)- }-}+ Expect(svc.IsDatabaseOnline()).To(BeFalse())-func TestIsDatabaseOnline_ShouldReturnTrueIfQuerySucceed(t *testing.T) {- db, mock, _ := sqlmock.New()- defer db.Close()+ if err := mock.ExpectationsWereMet(); err != nil {+ Fail(err.Error())+ }+ })- mock.ExpectQuery("SELECT now()").WillReturnRows(sqlmock.NewRows([]string{}))+ It("should return true if query succeed", func() {+ db, mock, _ := sqlmock.New()+ defer db.Close()+ mock.ExpectQuery("SELECT now()").WillReturnRows(sqlmock.NewRows([]string{}))- svc := &services.PostgresHealthCheckService{DB: db}- isOnline := svc.IsDatabaseOnline()+ svc := &services.PostgresHealthCheckService{DB: db}- assert.Equal(t, true, isOnline)+ Expect(svc.IsDatabaseOnline()).To(BeTrue())- if err := mock.ExpectationsWereMet(); err != nil {- t.Errorf("there were unfulfilled expections: %s", err)- }-}+ if err := mock.ExpectationsWereMet(); err != nil {+ Fail(err.Error())+ }+ })+})diff --git a/services/services_suite_test.go b/services/services_suite_test.gonew file mode 100644index 000000000..f4071fa62--- /dev/null+++ b/services/services_suite_test.go@@ -0,0 +1,13 @@+package services_test++import (+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"++ "testing"+)++func TestSuite(t *testing.T) {+ RegisterFailHandler(Fail)+ RunSpecs(t, "services Test Suite")+}diff --git a/services/tenant_test.go b/services/tenant_test.goindex ffc1d3d52..be383dad7 100644--- a/services/tenant_test.go+++ b/services/tenant_test.go@@ -1,56 +1,59 @@package services_testimport (- "testing"-"database/sql""github.com/WeCanHearYou/wchy/services"- "github.com/stretchr/testify/assert"sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"++ . "github.com/onsi/ginkgo"++ . "github.com/onsi/gomega")-func TestTenantByDomain_WhenSubdomainIsUnknown_ShouldReturnError(t *testing.T) {- db, mock, _ := sqlmock.New()- defer db.Close()+var _ = Describe("TenantService.GetByDomain", func() {+ It("TestTenantByDomain_WhenSubdomainIsUnknown_ShouldReturnError", func() {+ db, mock, _ := sqlmock.New()+ defer db.Close()- mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnError(sql.ErrNoRows)+ mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnError(sql.ErrNoRows)- svc := &services.PostgresTenantService{DB: db}- tenant, err := svc.GetByDomain("mydomain")+ svc := &services.PostgresTenantService{DB: db}+ tenant, err := svc.GetByDomain("mydomain")- assert.Nil(t, tenant)- assert.Error(t, err)-}+ Expect(tenant).To(BeNil())+ Expect(err).NotTo(BeNil())+ })-func TestTenantByDomain_WhenSubdomainIsKnown_ShouldReturnTenantInformation(t *testing.T) {- db, mock, _ := sqlmock.New()- defer db.Close()+ It("TestTenantByDomain_WhenSubdomainIsKnown_ShouldReturnTenantInformation", func() {+ db, mock, _ := sqlmock.New()+ defer db.Close()- rows := sqlmock.NewRows([]string{"id", "name", "subdomain"}).AddRow(234, "My Domain Inc.", "mydomain")- mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnRows(rows)+ rows := sqlmock.NewRows([]string{"id", "name", "subdomain"}).AddRow(234, "My Domain Inc.", "mydomain")+ mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnRows(rows)- svc := &services.PostgresTenantService{DB: db}- tenant, err := svc.GetByDomain("mydomain")+ svc := &services.PostgresTenantService{DB: db}+ tenant, err := svc.GetByDomain("mydomain")- assert.Equal(t, 234, tenant.ID)- assert.Equal(t, "My Domain Inc.", tenant.Name)- assert.Equal(t, "mydomain.test.canhearyou.com", tenant.Domain)- assert.Nil(t, err)-}+ Expect(tenant.ID).To(Equal(234))+ Expect(tenant.Name).To(Equal("My Domain Inc."))+ Expect(tenant.Domain).To(Equal("mydomain.test.canhearyou.com"))+ Expect(err).To(BeNil())+ })-func TestTenantByDomain_WhenCompleteDomainIsUsed_ShouldReturnTenantInformation(t *testing.T) {- db, mock, _ := sqlmock.New()- defer db.Close()+ It("TestTenantByDomain_WhenCompleteDomainIsUsed_ShouldReturnTenantInformation", func() {+ db, mock, _ := sqlmock.New()+ defer db.Close()- rows := sqlmock.NewRows([]string{"id", "name", "subdomain"}).AddRow(234, "My Domain Inc.", "mydomain")- mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnRows(rows)+ rows := sqlmock.NewRows([]string{"id", "name", "subdomain"}).AddRow(234, "My Domain Inc.", "mydomain")+ mock.ExpectQuery("SELECT id, name, subdomain FROM tenants WHERE subdomain = \\$1").WithArgs("mydomain").WillReturnRows(rows)- svc := &services.PostgresTenantService{DB: db}- tenant, err := svc.GetByDomain("mydomain.anydomain.com")+ svc := &services.PostgresTenantService{DB: db}+ tenant, err := svc.GetByDomain("mydomain.anydomain.com")- assert.Equal(t, 234, tenant.ID)- assert.Equal(t, "My Domain Inc.", tenant.Name)- assert.Equal(t, "mydomain.test.canhearyou.com", tenant.Domain)- assert.Nil(t, err)-}+ Expect(tenant.ID).To(Equal(234))+ Expect(tenant.Name).To(Equal("My Domain Inc."))+ Expect(tenant.Domain).To(Equal("mydomain.test.canhearyou.com"))+ Expect(err).To(BeNil())+ })+})