3
0

Fix tests

This commit is contained in:
Tomaž Jerman
2023-10-27 16:33:37 +02:00
parent b7c2ea8f25
commit 5e3810302f
8 changed files with 51 additions and 29 deletions

View File

@@ -8,16 +8,6 @@ import (
)
type (
dalModeler interface {
SearchModels(ctx context.Context) (out dal.ModelSet, err error)
ReplaceModel(ctx context.Context, model *dal.Model) (err error)
RemoveModel(ctx context.Context, connectionID, ID uint64) (err error)
GetConnectionByID(uint64) *dal.ConnectionWrap
SearchModelIssues(resourceID uint64) (out []error)
}
dalDater interface {
Create(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, vv ...dal.ValueGetter) error
Update(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, rr ...dal.ValueGetter) (err error)
@@ -27,9 +17,4 @@ type (
Delete(ctx context.Context, m dal.ModelRef, operations dal.OperationSet, pkv ...dal.ValueGetter) (err error)
Truncate(ctx context.Context, m dal.ModelRef, operations dal.OperationSet) (err error)
}
dalService interface {
dalModeler
dalDater
}
)

View File

@@ -11,6 +11,7 @@ import (
"github.com/cortezaproject/corteza/server/compose/dalutils"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/modern-go/reflect2"
"go.uber.org/zap"
"github.com/cortezaproject/corteza/server/pkg/revisions"
@@ -40,7 +41,7 @@ type (
store store.Storer
locale ResourceTranslationsManagerService
dal dalModelManager
dal dal.FullService
schemaAltManager schemaAltManager
}
@@ -1150,9 +1151,12 @@ func DalModelReplace(ctx context.Context, s store.Storer, am schemaAltManager, d
}
for _, m := range models {
currentAlts, err = am.ModelAlterations(ctx, m)
if err != nil {
return
if !reflect2.IsNil(am) {
// @todo this would need to use s from here, not service
currentAlts, err = am.ModelAlterations(ctx, m)
if err != nil {
return
}
}
newAlts, err = dmm.ReplaceModel(ctx, currentAlts, m)
@@ -1160,9 +1164,11 @@ func DalModelReplace(ctx context.Context, s store.Storer, am schemaAltManager, d
return
}
err = am.SetAlterations(ctx, s, m, currentAlts, newAlts...)
if err != nil {
return
if !reflect2.IsNil(am) {
err = am.SetAlterations(ctx, s, m, currentAlts, newAlts...)
if err != nil {
return
}
}
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/sqlite"
sysService "github.com/cortezaproject/corteza/server/system/service"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
@@ -50,7 +51,7 @@ func makeTestModuleService(t *testing.T, mods ...any) *module {
case store.Storer:
t.Log("using custom Store to initialize Module service")
svc.store = c
case dalService:
case dal.FullService:
t.Log("using custom DAL to initialize Module service")
t.Log("make sure you manually reload models!")
svc.dal = c
@@ -104,7 +105,7 @@ func makeTestModuleService(t *testing.T, mods ...any) *module {
svc.dal = dalAux
t.Log("reloading DAL models")
req.NoError(DalModelReload(ctx, svc.store, dalAux))
req.NoError(DalModelReload(ctx, svc.store, sysService.DefaultDalSchemaAlteration, dalAux))
}
return svc

View File

@@ -18,6 +18,7 @@ import (
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/store/adapters/rdbms/drivers/sqlite"
sysService "github.com/cortezaproject/corteza/server/system/service"
sysTypes "github.com/cortezaproject/corteza/server/system/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -60,7 +61,7 @@ func makeTestRecordService(t *testing.T, mods ...any) *record {
case store.Storer:
t.Log("using custom Store to initialize Record service")
svc.store = c
case dalService:
case dal.FullService:
t.Log("using custom DAL to initialize Record service")
t.Log("make sure you manually reload models!")
svc.dal = c
@@ -117,7 +118,7 @@ func makeTestRecordService(t *testing.T, mods ...any) *record {
req.NoError(err)
t.Log("reloading DAL models")
req.NoError(DalModelReload(ctx, svc.store, dalAux))
req.NoError(DalModelReload(ctx, svc.store, sysService.DefaultDalSchemaAlteration, dalAux))
}
return svc

View File

@@ -59,6 +59,9 @@ type (
Run(ctx context.Context, pp Pipeline) (iter Iterator, err error)
Dryrun(ctx context.Context, pp Pipeline) (err error)
ApplyAlteration(ctx context.Context, alts ...*Alteration) (errs []error, err error)
ReloadModel(ctx context.Context, currentAlts []*Alteration, model *Model) (newAlts []*Alteration, err error)
SearchConnectionIssues(connectionID uint64) (out []Issue)
SearchModelIssues(resourceID uint64) (out []Issue)
}

View File

@@ -1,6 +1,7 @@
package ddl
import (
"fmt"
"strings"
"testing"
@@ -39,6 +40,28 @@ func (d mockDriver) IndexFieldModifiers(attr *dal.Attribute, mm ...dal.IndexFiel
return IndexFieldModifiers(attr, d.QuoteIdent, mm...)
}
// Copied over to avoid import cycle
// @todo improve
func IndexFieldModifiers(attr *dal.Attribute, quoteIdent func(i string) string, mm ...dal.IndexFieldModifier) (string, error) {
var (
modifier string
out = quoteIdent(attr.StoreIdent())
)
for _, m := range mm {
switch m {
case dal.IndexFieldModifierLower:
modifier = "LOWER"
default:
return "", fmt.Errorf("unknown index field modifier: %s", m)
}
out = fmt.Sprintf("%s(%s)", modifier, out)
}
return out, nil
}
func TestModelToTable(t *testing.T) {
tests := []struct {

View File

@@ -93,6 +93,7 @@ func init() {
goqu.SetDefaultPrepared(true)
}
// @note copied to data_definer_test to avoid import cycle; if modified, fixup both parts
func IndexFieldModifiers(attr *dal.Attribute, quoteIdent func(i string) string, mm ...dal.IndexFieldModifier) (string, error) {
var (
modifier string

View File

@@ -2,10 +2,11 @@ package tests
import (
"context"
"testing"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/stretchr/testify/require"
"testing"
)
func TestModelManagement(t *testing.T) {
@@ -41,7 +42,7 @@ func TestModelManagement(t *testing.T) {
req.NoError(err)
req.NoError(svc.ReplaceConnection(ctx, cw, true))
req.NoError(svc.ReplaceModel(ctx, &dal.Model{
_, err = svc.ReplaceModel(ctx, nil, &dal.Model{
Ident: dalTableName,
ConnectionID: dalConnID,
Attributes: dal.AttributeSet{
@@ -59,7 +60,8 @@ func TestModelManagement(t *testing.T) {
Store: &dal.CodecAlias{Ident: "rel_owner"},
},
},
}))
})
req.NoError(err)
_, err = conn.db.Exec("DROP TABLE " + dalTableName)
req.NoError(err)