Skip to content
Snippets Groups Projects
Commit 507b35aafde6 authored by Axel Prel's avatar Axel Prel
Browse files

remove context in NewAPITester

parent 9b011e15618d
No related branches found
No related tags found
No related merge requests found
Showing
with 52 additions and 86 deletions
Release history Release history
=============== ===============
- apitester: remove context in NewAPITester
- go 1.24.1 - go 1.24.1
- template-varlist: fix error handling - template-varlist: fix error handling
......
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
"orus.io/orus-io/go-orusapi/database" "orus.io/orus-io/go-orusapi/database"
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
"orus.io/orus-io/rednerd/testutils"
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestAccountTemplateUpdate(t *testing.T) { func TestAccountTemplateUpdate(t *testing.T) {
...@@ -14,12 +13,8 @@ ...@@ -14,12 +13,8 @@
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestAccountTemplateUpdate(t *testing.T) { func TestAccountTemplateUpdate(t *testing.T) {
log := testutils.GetLogger(t) tester := apitester.NewAPITester(t, nil)
ctx := t.Context()
ctx = log.WithContext(ctx)
tester := apitester.NewAPITester(ctx, t, nil)
tmpl := models.Template{ tmpl := models.Template{
Account: "janedoe", Account: "janedoe",
Name: "hello", Name: "hello",
...@@ -52,7 +47,7 @@ ...@@ -52,7 +47,7 @@
t.Run("Update body", func(t *testing.T) { t.Run("Update body", func(t *testing.T) {
defer func() { defer func() {
_, _ = database.Exec(tester.DB, squirrel.Delete(models.TemplateDBTable), &log) _, _ = database.Exec(tester.DB, squirrel.Delete(models.TemplateDBTable), &tester.Logger)
}() }()
var responseTemplate models.Template var responseTemplate models.Template
...@@ -77,7 +72,7 @@ ...@@ -77,7 +72,7 @@
t.Run("Rename", func(t *testing.T) { t.Run("Rename", func(t *testing.T) {
defer func() { defer func() {
_, _ = database.Exec(tester.DB, squirrel.Delete(models.TemplateDBTable), &log) _, _ = database.Exec(tester.DB, squirrel.Delete(models.TemplateDBTable), &tester.Logger)
}() }()
var responseTemplate models.Template var responseTemplate models.Template
......
...@@ -14,9 +14,7 @@ ...@@ -14,9 +14,7 @@
) )
func TestRenderLogs(t *testing.T) { func TestRenderLogs(t *testing.T) {
ctx := t.Context() tester := apitester.NewAPITester(t, nil)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
now := time.Now() now := time.Now()
......
...@@ -3,5 +3,4 @@ ...@@ -3,5 +3,4 @@
import ( import (
"net/http" "net/http"
"testing" "testing"
"time"
...@@ -7,3 +6,2 @@ ...@@ -7,3 +6,2 @@
"github.com/rs/zerolog"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -9,4 +7,3 @@ ...@@ -9,4 +7,3 @@
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"orus.io/orus-io/go-orusapi/database"
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
...@@ -11,6 +8,5 @@ ...@@ -11,6 +8,5 @@
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
"orus.io/orus-io/rednerd/testutils"
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
...@@ -14,33 +10,4 @@ ...@@ -14,33 +10,4 @@
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func AddUserActivity(
t *testing.T,
log zerolog.Logger,
db *database.TestDB,
username string,
qtyCurrentMonth, qtyLastMonth int,
) {
t.Helper()
renderList := make([]*models.RenderLog, 0, qtyCurrentMonth+qtyLastMonth)
for range qtyCurrentMonth {
rd := models.NewRenderLog(time.Now(), "text", "mjml", username)
rd.SetDuration(time.Second)
renderList = append(renderList, rd)
}
for range qtyLastMonth {
rd := models.NewRenderLog(time.Now().AddDate(0, -1, 0), "text", "mjml", username)
rd.SetDuration(time.Second)
renderList = append(renderList, rd)
}
for _, rlog := range renderList {
_, err := database.Exec(db, database.SQLInsert(rlog), &log)
require.NoError(t, err)
}
}
func TestUserGet(t *testing.T) { func TestUserGet(t *testing.T) {
...@@ -46,9 +13,5 @@ ...@@ -46,9 +13,5 @@
func TestUserGet(t *testing.T) { func TestUserGet(t *testing.T) {
log := testutils.GetLogger(t) tester := apitester.NewAPITester(t, nil)
ctx := t.Context()
ctx = log.WithContext(ctx)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
// login as admin and create a user // login as admin and create a user
...@@ -58,7 +21,7 @@ ...@@ -58,7 +21,7 @@
// adding some dummy activity for the user JohnDoe // adding some dummy activity for the user JohnDoe
qtyCurrentMonth := 4 qtyCurrentMonth := 4
qtyLastMonth := 2 qtyLastMonth := 2
AddUserActivity(t, log, tester.DB, "johndoe", tester.AddUserActivity("johndoe",
qtyCurrentMonth, qtyLastMonth) qtyCurrentMonth, qtyLastMonth)
tester.Logout() tester.Logout()
......
...@@ -8,8 +8,7 @@ ...@@ -8,8 +8,7 @@
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
"orus.io/orus-io/rednerd/testutils"
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestUserList(t *testing.T) { func TestUserList(t *testing.T) {
...@@ -12,12 +11,8 @@ ...@@ -12,12 +11,8 @@
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestUserList(t *testing.T) { func TestUserList(t *testing.T) {
log := testutils.GetLogger(t) tester := apitester.NewAPITester(t, nil)
ctx := t.Context()
ctx = log.WithContext(ctx)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
// login as admin and create 10 users // login as admin and create 10 users
...@@ -27,7 +22,7 @@ ...@@ -27,7 +22,7 @@
john := "johndoe_" + strconv.Itoa(k) john := "johndoe_" + strconv.Itoa(k)
tester.CreateUser(john, "password"+strconv.Itoa(k)) tester.CreateUser(john, "password"+strconv.Itoa(k))
// adding some dummy activity for the user JohnDoe // adding some dummy activity for the user JohnDoe
AddUserActivity(t, log, tester.DB, john, 4, 2) tester.AddUserActivity(john, 4, 2)
} }
tester.Logout() tester.Logout()
......
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
"orus.io/orus-io/rednerd/testutils"
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestUserUpdate(t *testing.T) { func TestUserUpdate(t *testing.T) {
...@@ -11,12 +10,8 @@ ...@@ -11,12 +10,8 @@
"orus.io/orus-io/rednerd/testutils/apitester" "orus.io/orus-io/rednerd/testutils/apitester"
) )
func TestUserUpdate(t *testing.T) { func TestUserUpdate(t *testing.T) {
log := testutils.GetLogger(t) tester := apitester.NewAPITester(t, nil)
ctx := t.Context()
ctx = log.WithContext(ctx)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
// login as admin and create a user // login as admin and create a user
...@@ -27,7 +22,7 @@ ...@@ -27,7 +22,7 @@
// adding some dummy activity for the user JohnDoe // adding some dummy activity for the user JohnDoe
qtyCurrentMonth := 4 qtyCurrentMonth := 4
qtyLastMonth := 2 qtyLastMonth := 2
AddUserActivity(t, log, tester.DB, "johndoe", tester.AddUserActivity("johndoe",
qtyCurrentMonth, qtyLastMonth) qtyCurrentMonth, qtyLastMonth)
r := tester.APITest("user_get"). r := tester.APITest("user_get").
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
func TestAuthentication(t *testing.T) { func TestAuthentication(t *testing.T) {
ctx := t.Context() ctx := t.Context()
tester := apitester.NewAPITester(ctx, t, nil) tester := apitester.NewAPITester(t, nil)
defer tester.Close() defer tester.Close()
t.Run("WebToken", func(t *testing.T) { t.Run("WebToken", func(t *testing.T) {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
func TestForgotPassword(t *testing.T) { func TestForgotPassword(t *testing.T) {
ctx := t.Context() ctx := t.Context()
tester := apitester.NewAPITester(ctx, t, nil) tester := apitester.NewAPITester(t, nil)
defer tester.Close() defer tester.Close()
tester.Register(&models.Registration{ tester.Register(&models.Registration{
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {
ctx := t.Context() ctx := t.Context()
tester := apitester.NewAPITester(ctx, t, nil) tester := apitester.NewAPITester(t, nil)
defer tester.Close() defer tester.Close()
t.Run("register", func(t *testing.T) { t.Run("register", func(t *testing.T) {
......
...@@ -13,9 +13,7 @@ ...@@ -13,9 +13,7 @@
) )
func TestRender(t *testing.T) { func TestRender(t *testing.T) {
ctx := t.Context() tester := apitester.NewAPITester(t, nil)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
defer tester.Logout() defer tester.Logout()
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
func TestResetPassword(t *testing.T) { func TestResetPassword(t *testing.T) {
ctx := t.Context() ctx := t.Context()
tester := apitester.NewAPITester(ctx, t, nil) tester := apitester.NewAPITester(t, nil)
defer tester.Close() defer tester.Close()
tester.Register(&models.Registration{ tester.Register(&models.Registration{
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
ctx := t.Context() ctx := t.Context()
ctx = log.WithContext(ctx) ctx = log.WithContext(ctx)
tester := apitester.NewAPITester(ctx, t, nil) tester := apitester.NewAPITester(t, nil)
defer tester.Close() defer tester.Close()
defer tester.Logout() defer tester.Logout()
......
...@@ -11,9 +11,7 @@ ...@@ -11,9 +11,7 @@
) )
func TestUserManagement(t *testing.T) { func TestUserManagement(t *testing.T) {
ctx := t.Context() tester := apitester.NewAPITester(t, nil)
tester := apitester.NewAPITester(ctx, t, nil)
tester.Debug() tester.Debug()
defer tester.Close() defer tester.Close()
......
...@@ -10,9 +10,7 @@ ...@@ -10,9 +10,7 @@
) )
func TestVarlist(t *testing.T) { func TestVarlist(t *testing.T) {
ctx := t.Context() tester := apitester.NewAPITester(t, nil)
tester := apitester.NewAPITester(ctx, t, nil)
defer tester.Close() defer tester.Close()
defer tester.Logout() defer tester.Logout()
......
...@@ -23,11 +23,9 @@ ...@@ -23,11 +23,9 @@
func TestWPDHeavyLoad(t *testing.T) { func TestWPDHeavyLoad(t *testing.T) {
engineOptions := cmd.NewEngineOptions() engineOptions := cmd.NewEngineOptions()
ctx := t.Context()
require.NoError(t, engineOptions.SetupEngineOptions(nil)) require.NoError(t, engineOptions.SetupEngineOptions(nil))
engineOptions.SetOptions( engineOptions.SetOptions(
"WPD", &wpd.EngineOptions{ "WPD", &wpd.EngineOptions{
WorkerPoolSize: 10, WorkerPoolSize: 10,
}) })
...@@ -28,12 +26,13 @@ ...@@ -28,12 +26,13 @@
require.NoError(t, engineOptions.SetupEngineOptions(nil)) require.NoError(t, engineOptions.SetupEngineOptions(nil))
engineOptions.SetOptions( engineOptions.SetOptions(
"WPD", &wpd.EngineOptions{ "WPD", &wpd.EngineOptions{
WorkerPoolSize: 10, WorkerPoolSize: 10,
}) })
tester := apitester.NewAPITester(ctx, t, &engineOptions, func(s *restapi.Server) { tester := apitester.NewAPITester(
s.WriteTimeout = time.Minute * 5 t, &engineOptions, func(s *restapi.Server) {
}) s.WriteTimeout = time.Minute * 5
})
defer tester.Close() defer tester.Close()
defer tester.Logout() defer tester.Logout()
......
...@@ -55,11 +55,10 @@ ...@@ -55,11 +55,10 @@
// NewAPITester setup a APITester. // NewAPITester setup a APITester.
// false positive on thelper that I can't figure out. // false positive on thelper that I can't figure out.
// it should allow t *testing.T param to be second here but weirdly does not. // it should allow t *testing.T param to be second here but weirdly does not.
func NewAPITester( //nolint:thelper func NewAPITester(
ctx context.Context,
t *testing.T, t *testing.T,
engineOptions *cmd.EngineOptions, engineOptions *cmd.EngineOptions,
serverOptions ...ServerOption, serverOptions ...ServerOption,
) *APITester { ) *APITester {
t.Helper() t.Helper()
...@@ -60,10 +59,10 @@ ...@@ -60,10 +59,10 @@
t *testing.T, t *testing.T,
engineOptions *cmd.EngineOptions, engineOptions *cmd.EngineOptions,
serverOptions ...ServerOption, serverOptions ...ServerOption,
) *APITester { ) *APITester {
t.Helper() t.Helper()
testerCtx, cancel := context.WithCancel(ctx) testerCtx, cancel := context.WithCancel(t.Context())
log := testutils.GetLogger(t) log := testutils.GetLogger(t)
testLog := testutils.NewTestLogger(t) testLog := testutils.NewTestLogger(t)
......
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
import ( import (
"net/http" "net/http"
"time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -5,5 +6,6 @@ ...@@ -5,5 +6,6 @@
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"orus.io/orus-io/go-orusapi/database"
"orus.io/orus-io/rednerd/models" "orus.io/orus-io/rednerd/models"
) )
...@@ -66,3 +68,27 @@ ...@@ -66,3 +68,27 @@
End() End()
r.JSON(users) r.JSON(users)
} }
func (tester *APITester) AddUserActivity(
username string,
qtyCurrentMonth, qtyLastMonth int,
) {
renderList := make([]*models.RenderLog, 0, qtyCurrentMonth+qtyLastMonth)
for range qtyCurrentMonth {
rd := models.NewRenderLog(time.Now(), "text", "mjml", username)
rd.SetDuration(time.Second)
renderList = append(renderList, rd)
}
for range qtyLastMonth {
rd := models.NewRenderLog(time.Now().AddDate(0, -1, 0), "text", "mjml", username)
rd.SetDuration(time.Second)
renderList = append(renderList, rd)
}
for _, rlog := range renderList {
_, err := database.Exec(tester.DB, database.SQLInsert(rlog), &tester.Logger)
require.NoError(tester.t, err)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment