Open platform to collect and prioritize feedback
real unit testing
Guilherme Oenning2c98384559f3
diff --git a/.gitignore b/.gitignoreindex 110130235..93e37d86d 100644--- a/.gitignore+++ b/.gitignore@@ -4,5 +4,6 @@ tmp/*.coverprofilewchy+wchy.exegin-bingin-bin.exe\ No newline at end of filediff --git a/.vscode/settings.json b/.vscode/settings.jsonindex b68c124cf..26c003e5f 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -9,6 +9,7 @@"gin-bin": true,"gin-bin.exe": true,"wchy": true,+ "wchy.exe": true,"tmp": true},"vsicons.presets.angular": falsediff --git a/env/env_test.go b/env/env_test.goindex d928852f0..9533aa691 100644--- a/env/env_test.go+++ b/env/env_test.go@@ -12,12 +12,13 @@ var envs = []struct {go_env stringdomain stringenv string+ isEnv func() bool}{- {"test", "test.canhearyou.com", "test"},- {"staging", "staging.canhearyou.com", "staging"},- {"development", "dev.canhearyou.com", "development"},- {"production", "canhearyou.com", "production"},- {"anything", "canhearyou.com", "development"},+ {"test", "test.canhearyou.com", "test", env.IsTest},+ {"staging", "staging.canhearyou.com", "staging", env.IsStaging},+ {"development", "dev.canhearyou.com", "development", env.IsDevelopment},+ {"production", "canhearyou.com", "production", env.IsProduction},+ {"anything", "canhearyou.com", "development", env.IsDevelopment},}func TestGetEnvOrDefault(t *testing.T) {@@ -49,3 +50,13 @@ func TestCurrent(t *testing.T) {Expect(actual).To(Equal(testCase.env))}}++func TestIsEnvironment(t *testing.T) {+ RegisterTestingT(t)++ for _, testCase := range envs {+ os.Setenv("GO_ENV", testCase.go_env)+ actual := testCase.isEnv()+ Expect(actual).To(BeTrue())+ }+}diff --git a/handlers/handlers_helper_test.go b/handlers/handlers_helper_test.goindex d377f5de8..0c2a29e88 100644--- a/handlers/handlers_helper_test.go+++ b/handlers/handlers_helper_test.go@@ -5,29 +5,39 @@ import ("net/http""net/http/httptest"- "github.com/WeCanHearYou/wchy/context"- "github.com/WeCanHearYou/wchy/models"- "github.com/WeCanHearYou/wchy/router"- "github.com/WeCanHearYou/wchy/services"+ "github.com/gin-gonic/gin""github.com/jmoiron/jsonq")-func makeRequest(method, url string) (int, *jsonq.JsonQuery) {- ctx := context.WchyContext{- Health: &services.InMemoryHealthCheckService{Status: false},- Tenant: &services.InMemoryTenantService{Tenants: []*models.Tenant{- &models.Tenant{ID: 1, Name: "Orange Inc.", Domain: "orange"},- &models.Tenant{ID: 2, Name: "The Triathlon Shop", Domain: "trishop"},- }},- Settings: context.WchySettings{- BuildTime: "today",- },+type testServer struct {+ engine *gin.Engine+}++func NewTestServer() *testServer {+ engine := gin.New()+ return &testServer{+ engine: engine,}- router := router.GetMainEngine(ctx)+}++func (s *testServer) do(f gin.HandlerFunc) {+ s.engine.Use(f)+}++func (s *testServer) param(key, value string) {+ s.do(func(c *gin.Context) {+ c.Params = append(c.Params, gin.Param{Key: key, Value: value})+ })+}++func (s *testServer) register(handler gin.HandlerFunc) {+ s.engine.Handle("GET", "/", handler)+}- request, _ := http.NewRequest(method, url, nil)+func (s *testServer) request() (int, *jsonq.JsonQuery) {+ request, _ := http.NewRequest("GET", "/", nil)response := httptest.NewRecorder()- router.ServeHTTP(response, request)+ s.engine.ServeHTTP(response, request)status := response.Result().StatusCodediff --git a/handlers/status_test.go b/handlers/status_test.goindex eb70dee46..73bf49b8c 100644--- a/handlers/status_test.go+++ b/handlers/status_test.go@@ -3,16 +3,33 @@ package handlers_testimport ("testing"+ "github.com/WeCanHearYou/wchy/context"+ "github.com/WeCanHearYou/wchy/handlers". "github.com/onsi/gomega")+type inMemoryHealthCheckService struct {+ status bool+}++func (svc inMemoryHealthCheckService) IsDatabaseOnline() bool {+ return svc.status+}+func TestStatusHandler(t *testing.T) {RegisterTestingT(t)+ ctx := context.WchyContext{+ Health: &inMemoryHealthCheckService{status: false},+ Settings: context.WchySettings{+ BuildTime: "today",+ },+ }- status, query := makeRequest("GET", "/api/status")+ server := NewTestServer()+ server.register(handlers.Status(ctx))+ status, query := server.request()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 168d98ce0..a1a4aedcd 100644--- a/handlers/tenant_test.go+++ b/handlers/tenant_test.go@@ -3,13 +3,40 @@ package handlers_testimport ("testing"+ "github.com/WeCanHearYou/wchy/context"+ "github.com/WeCanHearYou/wchy/handlers"+ "github.com/WeCanHearYou/wchy/models"+ "github.com/WeCanHearYou/wchy/services". "github.com/onsi/gomega")+type inMemoryTenantService struct {+ tenants []*models.Tenant+}++func (svc inMemoryTenantService) GetByDomain(domain string) (*models.Tenant, error) {+ for _, tenant := range svc.tenants {+ if tenant.Domain == domain {+ return tenant, nil+ }+ }+ return nil, services.ErrNotFound+}++var ctx context.WchyContext = context.WchyContext{+ Tenant: &inMemoryTenantService{tenants: []*models.Tenant{+ &models.Tenant{ID: 1, Name: "Orange Inc.", Domain: "orange"},+ &models.Tenant{ID: 2, Name: "The Triathlon Shop", Domain: "trishop"},+ }},+}+func TestTenantHandler_404(t *testing.T) {RegisterTestingT(t)- status, _ := makeRequest("GET", "/api/tenants/unknown")+ server := NewTestServer()+ server.param("domain", "unknown")+ server.register(handlers.TenantByDomain(ctx))+ status, _ := server.request()Expect(status).To(Equal(404))}@@ -17,7 +44,10 @@ func TestTenantHandler_404(t *testing.T) {func TestTenantHandler_200(t *testing.T) {RegisterTestingT(t)- status, query := makeRequest("GET", "/api/tenants/trishop")+ server := NewTestServer()+ server.param("domain", "trishop")+ server.register(handlers.TenantByDomain(ctx))+ status, query := server.request()Expect(query.Int("id")).To(Equal(2))Expect(query.String("name")).To(Equal("The Triathlon Shop"))diff --git a/services/health.go b/services/health.goindex b160223da..9335db64b 100644--- a/services/health.go+++ b/services/health.go@@ -21,13 +21,3 @@ func (svc PostgresHealthCheckService) IsDatabaseOnline() bool {return true}--// InMemoryHealthCheckService is a simple general purpose health check service-type InMemoryHealthCheckService struct {- Status bool-}--// IsDatabaseOnline checks if database is online-func (svc InMemoryHealthCheckService) IsDatabaseOnline() bool {- return svc.Status-}diff --git a/services/tenant.go b/services/tenant.goindex 2f87d7df2..6fd5645f8 100644--- a/services/tenant.go+++ b/services/tenant.go@@ -13,21 +13,6 @@ type TenantService interface {GetByDomain(domain string) (*models.Tenant, error)}-// InMemoryTenantService contains read and write operations for tenants-type InMemoryTenantService struct {- Tenants []*models.Tenant-}--// GetByDomain returns a tenant based on its domain-func (svc InMemoryTenantService) GetByDomain(domain string) (*models.Tenant, error) {- for _, tenant := range svc.Tenants {- if tenant.Domain == domain {- return tenant, nil- }- }- return nil, ErrNotFound-}-// PostgresTenantService contains read and write operations for tenantstype PostgresTenantService struct {DB *sql.DB