Split up services test files.
This commit is contained in:
parent
d28f02f8d6
commit
5805afe947
2 changed files with 52 additions and 47 deletions
|
|
@ -1,58 +1,11 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"goweb/config"
|
|
||||||
"goweb/ent"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
c *Container
|
|
||||||
ctx echo.Context
|
|
||||||
usr *ent.User
|
|
||||||
rec *httptest.ResponseRecorder
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
// Set the environment to test
|
|
||||||
config.SwitchEnvironment(config.EnvTest)
|
|
||||||
|
|
||||||
// Create a new container
|
|
||||||
c = NewContainer()
|
|
||||||
|
|
||||||
// Create a web context
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
|
||||||
rec = httptest.NewRecorder()
|
|
||||||
ctx = c.Web.NewContext(req, rec)
|
|
||||||
|
|
||||||
// Create a test user
|
|
||||||
var err error
|
|
||||||
usr, err = c.ORM.User.
|
|
||||||
Create().
|
|
||||||
SetEmail("test@test.dev").
|
|
||||||
SetPassword("abc").
|
|
||||||
SetName("Test User").
|
|
||||||
Save(context.Background())
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run tests
|
|
||||||
exitVal := m.Run()
|
|
||||||
os.Exit(exitVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewContainer(t *testing.T) {
|
func TestNewContainer(t *testing.T) {
|
||||||
assert.NotNil(t, c.Web)
|
assert.NotNil(t, c.Web)
|
||||||
assert.NotNil(t, c.Config)
|
assert.NotNil(t, c.Config)
|
||||||
|
|
|
||||||
52
services/services_test.go
Normal file
52
services/services_test.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"goweb/config"
|
||||||
|
"goweb/ent"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
c *Container
|
||||||
|
ctx echo.Context
|
||||||
|
usr *ent.User
|
||||||
|
rec *httptest.ResponseRecorder
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
// Set the environment to test
|
||||||
|
config.SwitchEnvironment(config.EnvTest)
|
||||||
|
|
||||||
|
// Create a new container
|
||||||
|
c = NewContainer()
|
||||||
|
|
||||||
|
// Create a web context
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
ctx = c.Web.NewContext(req, rec)
|
||||||
|
|
||||||
|
// Create a test user
|
||||||
|
var err error
|
||||||
|
usr, err = c.ORM.User.
|
||||||
|
Create().
|
||||||
|
SetEmail("test@test.dev").
|
||||||
|
SetPassword("abc").
|
||||||
|
SetName("Test User").
|
||||||
|
Save(context.Background())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run tests
|
||||||
|
exitVal := m.Run()
|
||||||
|
os.Exit(exitVal)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue