3
0

Route service test logging through test Logf()

This is a temporary workaround to provide cleaner output during tests.
This commit is contained in:
Denis Arh
2019-05-14 14:05:50 +02:00
parent 7fc66e74ad
commit 23719ac0eb
7 changed files with 90 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/pkg/errors"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
@@ -14,6 +15,8 @@ import (
)
func TestChart(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
// Set Identity (required for permission checks).

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"testing"
"time"
"github.com/namsral/flag"
"github.com/titpetric/factory"
@@ -28,14 +29,13 @@ func TestMain(m *testing.M) {
logger.Init(zapcore.DebugLevel)
dsn := ""
flag.StringVar(&dsn, "db-dsn", "crust:crust@tcp(crust-db:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection")
flag.StringVar(&dsn, "compose-db-dsn", "", "")
flag.Parse()
factory.Database.Add("default", dsn)
factory.Database.Add("compose", dsn)
db := factory.Database.MustGet()
db.Profiler = &factory.Database.ProfilerStdout
db := factory.Database.MustGet("compose")
db.Profiler = &factory.DatabaseProfilerStdout{}
// migrate database schema
if err := composeMigrate.Migrate(db); err != nil {
@@ -45,8 +45,19 @@ func TestMain(m *testing.M) {
// clean up tables
{
for _, name := range []string{"compose_chart", "compose_trigger", "compose_module", "compose_module_form", "compose_record", "compose_record_value", "compose_page"} {
_, err := db.Exec("truncate " + name)
// @todo remove this asap, service should not access db at all.
for _, name := range []string{
"compose_chart",
"compose_trigger",
"compose_module_field",
"compose_module",
"compose_record_value",
"compose_record",
"compose_page",
"compose_attachment",
"compose_namespace",
} {
_, err := db.Exec("DELETE FROM " + name)
if err != nil {
panic("Error when clearing " + name + ": " + err.Error())
}
@@ -71,3 +82,33 @@ func createTestNamespaces(ctx context.Context, t *testing.T) (ns1 *types.Namespa
return ns1, ns2
}
// zapProfiler logs query statistics to zap.logger
type (
testLogProfiler struct {
logger testProfilerLogger
}
testProfilerLogger interface {
Logf(format string, args ...interface{})
}
)
func newTestLogProfiler(logger testProfilerLogger) *testLogProfiler {
return &testLogProfiler{
logger: logger,
}
}
// Post prints the query statistics to stdout
func (p testLogProfiler) Post(c *factory.DatabaseProfilerContext) {
p.logger.Logf(
"%s\nArgs: %v\nDuration: %fs",
c.Query,
c.Args,
time.Since(c.Time).Seconds(),
)
}
// Flush stdout (no-op for this profiler)
func (testLogProfiler) Flush() {}

View File

@@ -6,12 +6,16 @@ import (
"context"
"testing"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
"github.com/crusttech/crust/internal/test"
)
func TestModule(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
// Set Identity (required for permission checks).

View File

@@ -6,12 +6,16 @@ import (
"context"
"testing"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
"github.com/crusttech/crust/internal/test"
)
func TestNamespace(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
// Set Identity (required for permission checks).

View File

@@ -8,6 +8,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
@@ -15,6 +16,8 @@ import (
)
func TestPage(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
// Set fake Identity (required for permission checks).

View File

@@ -6,12 +6,16 @@ import (
"context"
"testing"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
"github.com/crusttech/crust/internal/test"
)
func TestRecord(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
ctx = auth.SetIdentityToContext(ctx, auth.NewIdentity(1337))
@@ -19,6 +23,7 @@ func TestRecord(t *testing.T) {
var err error
ns1, ns2 := createTestNamespaces(ctx, t)
moduleSvc := Module().With(ctx)
svc := Record().With(ctx)
module1 := &types.Module{
@@ -46,16 +51,34 @@ func TestRecord(t *testing.T) {
}
// set up a module1
module1, err = Module().With(ctx).Create(module1)
module1, err = moduleSvc.Create(module1)
test.Assert(t, err == nil, "Error when creating module1: %+v", err)
test.Assert(t, module1.ID > 0, "Expected auto generated ID")
module2 := &types.Module{
NamespaceID: ns2.ID,
Name: "Test Dummy",
Fields: module1.Fields,
Fields: types.ModuleFieldSet{
&types.ModuleField{
Name: "name",
},
&types.ModuleField{
Name: "email",
},
&types.ModuleField{
Name: "options",
Multi: true,
},
&types.ModuleField{
Name: "description",
},
&types.ModuleField{
Name: "another_record",
Kind: "Record",
},
},
}
module2, err = Module().With(ctx).Create(module2)
module2, err = moduleSvc.Create(module2)
test.Assert(t, err == nil, "Error when creating module1 in another namespace: %+v", err)
record1 := &types.Record{

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/pkg/errors"
"github.com/titpetric/factory"
"github.com/crusttech/crust/compose/types"
"github.com/crusttech/crust/internal/auth"
@@ -14,6 +15,8 @@ import (
)
func TestTrigger(t *testing.T) {
factory.Database.MustGet("compose").Profiler = newTestLogProfiler(t)
ctx := context.WithValue(context.Background(), "testing", true)
// Set Identity (required for permission checks).