mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 15:07:32 +00:00
fixup! fixup! finished events tests
This commit is contained in:
@@ -2,7 +2,6 @@ package domain
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/cache"
|
"github.com/zitadel/zitadel/backend/v3/storage/cache"
|
||||||
@@ -10,11 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
CreatedAt sql.Null[time.Time] `json:"-"`
|
DefaultOrgID string `json:"default_org_id"`
|
||||||
UpdatedAt sql.Null[time.Time] `json:"-"`
|
IAMProjectID string `json:"iam_project_id"`
|
||||||
DeletedAt sql.Null[time.Time] `json:"-"`
|
ConsoleClientId string `json:"console_client_id"`
|
||||||
|
ConsoleAppID string `json:"console_app_id"`
|
||||||
|
DefaultLanguage string `json:"default_language"`
|
||||||
|
CreatedAt time.Time `json:"-"`
|
||||||
|
UpdatedAt time.Time `json:"-"`
|
||||||
|
DeletedAt *time.Time `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type instanceCacheIndex uint8
|
type instanceCacheIndex uint8
|
||||||
@@ -40,6 +44,16 @@ type instanceColumns interface {
|
|||||||
IDColumn() database.Column
|
IDColumn() database.Column
|
||||||
// NameColumn returns the column for the name field.
|
// NameColumn returns the column for the name field.
|
||||||
NameColumn() database.Column
|
NameColumn() database.Column
|
||||||
|
// DefaultOrgIdColumn returns the column for the default org id field
|
||||||
|
DefaultOrgIdColumn() database.Column
|
||||||
|
// IAMProjectIDColumn returns the column for the default IAM org id field
|
||||||
|
IAMProjectIDColumn() database.Column
|
||||||
|
// ConsoleClientIDColumn returns the column for the default IAM org id field
|
||||||
|
ConsoleClientIDColumn() database.Column
|
||||||
|
// ConsoleAppIDColumn returns the column for the console client id field
|
||||||
|
ConsoleAppIDColumn() database.Column
|
||||||
|
// DefaultLanguageColumn returns the column for the default language field
|
||||||
|
DefaultLanguageColumn() database.Column
|
||||||
// CreatedAtColumn returns the column for the created at field.
|
// CreatedAtColumn returns the column for the created at field.
|
||||||
CreatedAtColumn() database.Column
|
CreatedAtColumn() database.Column
|
||||||
// UpdatedAtColumn returns the column for the updated at field.
|
// UpdatedAtColumn returns the column for the updated at field.
|
||||||
@@ -72,7 +86,7 @@ type InstanceRepository interface {
|
|||||||
// Member returns the member repository which is a sub repository of the instance repository.
|
// Member returns the member repository which is a sub repository of the instance repository.
|
||||||
// Member() MemberRepository
|
// Member() MemberRepository
|
||||||
|
|
||||||
Get(ctx context.Context, opts ...database.QueryOption) (*Instance, error)
|
Get(ctx context.Context, opts ...database.Condition) (*Instance, error)
|
||||||
|
|
||||||
Create(ctx context.Context, instance *Instance) error
|
Create(ctx context.Context, instance *Instance) error
|
||||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
|
Update(ctx context.Context, condition database.Condition, changes ...database.Change) error
|
||||||
|
@@ -12,6 +12,7 @@ type and struct {
|
|||||||
|
|
||||||
// Write implements [Condition].
|
// Write implements [Condition].
|
||||||
func (a *and) Write(builder *StatementBuilder) {
|
func (a *and) Write(builder *StatementBuilder) {
|
||||||
|
builder.WriteString(" WHERE ")
|
||||||
if len(a.conditions) > 1 {
|
if len(a.conditions) > 1 {
|
||||||
builder.WriteString("(")
|
builder.WriteString("(")
|
||||||
defer builder.WriteString(")")
|
defer builder.WriteString(")")
|
||||||
|
@@ -10,4 +10,3 @@ CREATE TABLE IF NOT EXISTS zitadel.instances(
|
|||||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
deleted_at TIMESTAMPTZ DEFAULT NULL
|
deleted_at TIMESTAMPTZ DEFAULT NULL
|
||||||
);
|
);
|
||||||
-- CREATE UNIQUE INDEX instance_name_index ON zitadel.instances (name);
|
|
||||||
|
@@ -54,7 +54,7 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
func TestServer_TestInstanceAddReduces(t *testing.T) {
|
func TestServer_TestInstanceAddReduces(t *testing.T) {
|
||||||
instanceName := gofakeit.Name()
|
instanceName := gofakeit.Name()
|
||||||
beforeAdd := time.Now()
|
beforeCreate := time.Now()
|
||||||
_, err := SystemClient.CreateInstance(CTX, &system.CreateInstanceRequest{
|
_, err := SystemClient.CreateInstance(CTX, &system.CreateInstanceRequest{
|
||||||
InstanceName: instanceName,
|
InstanceName: instanceName,
|
||||||
Owner: &system.CreateInstanceRequest_Machine_{
|
Owner: &system.CreateInstanceRequest_Machine_{
|
||||||
@@ -65,7 +65,7 @@ func TestServer_TestInstanceAddReduces(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
afterAdd := time.Now()
|
afterCreate := time.Now()
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -73,9 +73,7 @@ func TestServer_TestInstanceAddReduces(t *testing.T) {
|
|||||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
instance, err := instanceRepo.Get(CTX,
|
instance, err := instanceRepo.Get(CTX,
|
||||||
database.WithCondition(
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
require.NoError(ttt, err)
|
require.NoError(ttt, err)
|
||||||
// event instance.added
|
// event instance.added
|
||||||
@@ -89,9 +87,9 @@ func TestServer_TestInstanceAddReduces(t *testing.T) {
|
|||||||
// event instance.default.language.set
|
// event instance.default.language.set
|
||||||
require.NotNil(t, instance.DefaultLanguage)
|
require.NotNil(t, instance.DefaultLanguage)
|
||||||
// event instance.added
|
// event instance.added
|
||||||
assert.WithinRange(t, instance.CreatedAt, beforeAdd, afterAdd)
|
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
|
||||||
// event instance.added
|
// event instance.added
|
||||||
assert.WithinRange(t, instance.UpdatedAt, beforeAdd, afterAdd)
|
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
|
||||||
require.Nil(t, instance.DeletedAt)
|
require.Nil(t, instance.DeletedAt)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
}
|
}
|
||||||
@@ -121,9 +119,7 @@ func TestServer_TestInstanceUpdateNameReduces(t *testing.T) {
|
|||||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
instance, err := instanceRepo.Get(CTX,
|
instance, err := instanceRepo.Get(CTX,
|
||||||
database.WithCondition(
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
require.NoError(ttt, err)
|
require.NoError(ttt, err)
|
||||||
// event instance.changed
|
// event instance.changed
|
||||||
@@ -145,23 +141,19 @@ func TestServer_TestInstanceDeleteReduces(t *testing.T) {
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
beforeDelete := time.Now()
|
|
||||||
_, err = SystemClient.RemoveInstance(CTX, &system.RemoveInstanceRequest{
|
_, err = SystemClient.RemoveInstance(CTX, &system.RemoveInstanceRequest{
|
||||||
InstanceId: res.InstanceId,
|
InstanceId: res.InstanceId,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
afterDelete := time.Now()
|
|
||||||
|
|
||||||
instanceRepo := repository.InstanceRepository(pool)
|
instanceRepo := repository.InstanceRepository(pool)
|
||||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
instance, err := instanceRepo.Get(CTX,
|
instance, err := instanceRepo.Get(CTX,
|
||||||
database.WithCondition(
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
// event instance.removed
|
// event instance.removed
|
||||||
assert.WithinRange(t, *instance.DeletedAt, beforeDelete, afterDelete)
|
require.Nil(t, instance)
|
||||||
require.NoError(ttt, err)
|
require.NoError(ttt, err)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||||
@@ -26,34 +27,32 @@ func InstanceRepository(client database.QueryExecutor) domain.InstanceRepository
|
|||||||
// repository
|
// repository
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
|
|
||||||
const queryInstanceStmt = `SELECT id, name, created_at, updated_at, deleted_at` +
|
const queryInstanceStmt = `SELECT id, name, default_org_id, iam_project_id, console_client_id, console_app_id, default_language, created_at, updated_at, deleted_at` +
|
||||||
` FROM zitadel.instances`
|
` FROM zitadel.instances`
|
||||||
|
|
||||||
// Get implements [domain.InstanceRepository].
|
// Get implements [domain.InstanceRepository].
|
||||||
func (i *instance) Get(ctx context.Context, opts ...database.QueryOption) (*domain.Instance, error) {
|
// func (i *instance) Get(ctx context.Context, opts ...database.QueryOption) (*domain.Instance, error) {
|
||||||
|
func (i *instance) Get(ctx context.Context, opts ...database.Condition) (*domain.Instance, error) {
|
||||||
i.builder = database.StatementBuilder{}
|
i.builder = database.StatementBuilder{}
|
||||||
options := new(database.QueryOpts)
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
i.builder.WriteString(queryInstanceStmt)
|
i.builder.WriteString(queryInstanceStmt)
|
||||||
options.WriteCondition(&i.builder)
|
|
||||||
options.WriteOrderBy(&i.builder)
|
isNotDeletedCondition := database.IsNull(i.DeletedAtColumn())
|
||||||
options.WriteLimit(&i.builder)
|
opts = append(opts, isNotDeletedCondition)
|
||||||
options.WriteOffset(&i.builder)
|
andCondition := database.And(opts...)
|
||||||
|
andCondition.Write(&i.builder)
|
||||||
|
|
||||||
return scanInstance(i.client.QueryRow(ctx, i.builder.String(), i.builder.Args()...))
|
return scanInstance(i.client.QueryRow(ctx, i.builder.String(), i.builder.Args()...))
|
||||||
}
|
}
|
||||||
|
|
||||||
const createInstanceStmt = `INSERT INTO zitadel.instances (id, name)` +
|
const createInstanceStmt = `INSERT INTO zitadel.instances (id, name, default_org_id, iam_project_id, console_client_id, console_app_id, default_language)` +
|
||||||
` VALUES ($1, $2)` +
|
` VALUES ($1, $2, $3, $4, $5, $6, $7)` +
|
||||||
` RETURNING created_at, updated_at`
|
` RETURNING created_at, updated_at`
|
||||||
|
|
||||||
// Create implements [domain.InstanceRepository].
|
// Create implements [domain.InstanceRepository].
|
||||||
func (i *instance) Create(ctx context.Context, instance *domain.Instance) error {
|
func (i *instance) Create(ctx context.Context, instance *domain.Instance) error {
|
||||||
i.builder = database.StatementBuilder{}
|
i.builder = database.StatementBuilder{}
|
||||||
i.builder.AppendArgs(instance.ID, instance.Name)
|
i.builder.AppendArgs(instance.ID, instance.Name, instance.DefaultOrgID, instance.IAMProjectID, instance.ConsoleClientId, instance.ConsoleAppID, instance.DefaultLanguage)
|
||||||
i.builder.WriteString(createInstanceStmt)
|
i.builder.WriteString(createInstanceStmt)
|
||||||
|
|
||||||
return i.client.QueryRow(ctx, i.builder.String(), i.builder.Args()...).Scan(&instance.CreatedAt, &instance.UpdatedAt)
|
return i.client.QueryRow(ctx, i.builder.String(), i.builder.Args()...).Scan(&instance.CreatedAt, &instance.UpdatedAt)
|
||||||
@@ -62,7 +61,7 @@ func (i *instance) Create(ctx context.Context, instance *domain.Instance) error
|
|||||||
// Update implements [domain.InstanceRepository].
|
// Update implements [domain.InstanceRepository].
|
||||||
func (i instance) Update(ctx context.Context, condition database.Condition, changes ...database.Change) error {
|
func (i instance) Update(ctx context.Context, condition database.Condition, changes ...database.Change) error {
|
||||||
i.builder = database.StatementBuilder{}
|
i.builder = database.StatementBuilder{}
|
||||||
i.builder.WriteString(`UPDATE human_users SET `)
|
i.builder.WriteString(`UPDATE zitadel.instances SET `)
|
||||||
database.Changes(changes).Write(&i.builder)
|
database.Changes(changes).Write(&i.builder)
|
||||||
i.writeCondition(condition)
|
i.writeCondition(condition)
|
||||||
|
|
||||||
@@ -73,11 +72,13 @@ func (i instance) Update(ctx context.Context, condition database.Condition, chan
|
|||||||
|
|
||||||
// Delete implements [domain.InstanceRepository].
|
// Delete implements [domain.InstanceRepository].
|
||||||
func (i instance) Delete(ctx context.Context, condition database.Condition) error {
|
func (i instance) Delete(ctx context.Context, condition database.Condition) error {
|
||||||
i.builder.WriteString("DELETE FROM instance")
|
|
||||||
|
|
||||||
if condition == nil {
|
if condition == nil {
|
||||||
return errors.New("Delete must contain a condition") // (otherwise ALL instances will be deleted)
|
return errors.New("Delete must contain a condition") // (otherwise ALL instances will be deleted)
|
||||||
}
|
}
|
||||||
|
i.builder = database.StatementBuilder{}
|
||||||
|
i.builder.WriteString(`UPDATE zitadel.instances SET deleted_at = $1`)
|
||||||
|
i.builder.AppendArgs(time.Now())
|
||||||
|
|
||||||
i.writeCondition(condition)
|
i.writeCondition(condition)
|
||||||
return i.client.Exec(ctx, i.builder.String(), i.builder.Args()...)
|
return i.client.Exec(ctx, i.builder.String(), i.builder.Args()...)
|
||||||
}
|
}
|
||||||
@@ -124,6 +125,31 @@ func (instance) CreatedAtColumn() database.Column {
|
|||||||
return database.NewColumn("created_at")
|
return database.NewColumn("created_at")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultOrgIdColumn implements [domain.instanceColumns].
|
||||||
|
func (instance) DefaultOrgIdColumn() database.Column {
|
||||||
|
return database.NewColumn("default_org_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// IAMProjectIDColumn implements [domain.instanceColumns].
|
||||||
|
func (instance) IAMProjectIDColumn() database.Column {
|
||||||
|
return database.NewColumn("iam_project_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleClientIDColumn implements [domain.instanceColumns].
|
||||||
|
func (instance) ConsoleClientIDColumn() database.Column {
|
||||||
|
return database.NewColumn("console_client_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConsoleAppIDColumn implements [domain.instanceColumns].
|
||||||
|
func (instance) ConsoleAppIDColumn() database.Column {
|
||||||
|
return database.NewColumn("console_app_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultLanguageColumn implements [domain.instanceColumns].
|
||||||
|
func (instance) DefaultLanguageColumn() database.Column {
|
||||||
|
return database.NewColumn("default_language")
|
||||||
|
}
|
||||||
|
|
||||||
// UpdatedAtColumn implements [domain.instanceColumns].
|
// UpdatedAtColumn implements [domain.instanceColumns].
|
||||||
func (instance) UpdatedAtColumn() database.Column {
|
func (instance) UpdatedAtColumn() database.Column {
|
||||||
return database.NewColumn("updated_at")
|
return database.NewColumn("updated_at")
|
||||||
@@ -147,11 +173,22 @@ func scanInstance(scanner database.Scanner) (*domain.Instance, error) {
|
|||||||
err := scanner.Scan(
|
err := scanner.Scan(
|
||||||
&instance.ID,
|
&instance.ID,
|
||||||
&instance.Name,
|
&instance.Name,
|
||||||
|
&instance.DefaultOrgID,
|
||||||
|
&instance.IAMProjectID,
|
||||||
|
&instance.ConsoleClientId,
|
||||||
|
&instance.ConsoleAppID,
|
||||||
|
&instance.DefaultLanguage,
|
||||||
&instance.CreatedAt,
|
&instance.CreatedAt,
|
||||||
&instance.UpdatedAt,
|
&instance.UpdatedAt,
|
||||||
&instance.DeletedAt,
|
&instance.DeletedAt,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// if no results returned, this is not a error
|
||||||
|
// it just means the instance was not found
|
||||||
|
// the caller should check if the returned instance is nil
|
||||||
|
if err.Error() == "no rows in result set" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,100 +2,130 @@ package repository_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database/dbmock"
|
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
||||||
|
|
||||||
"go.uber.org/mock/gomock"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateInstance(t *testing.T) {
|
func TestCreateInstance(t *testing.T) {
|
||||||
instanceRepo := repository.InstanceRepository(pool)
|
instanceRepo := repository.InstanceRepository(pool)
|
||||||
|
instanceId := gofakeit.Name()
|
||||||
|
instanceName := gofakeit.Name()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
inst := domain.Instance{
|
inst := domain.Instance{
|
||||||
ID: "id",
|
ID: instanceId,
|
||||||
Name: "name",
|
Name: instanceName,
|
||||||
|
DefaultOrgID: "defaultOrgId",
|
||||||
|
IAMProjectID: "iamProject",
|
||||||
|
ConsoleClientId: "consoleCLient",
|
||||||
|
ConsoleAppID: "consoleApp",
|
||||||
|
DefaultLanguage: "defaultLanguage",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
beforeCreate := time.Now()
|
||||||
err := instanceRepo.Create(ctx, &inst)
|
err := instanceRepo.Create(ctx, &inst)
|
||||||
fmt.Printf("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> err = %+v\n", err)
|
|
||||||
fmt.Printf("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> inst = %+v\n", inst)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
afterCreate := time.Now()
|
||||||
|
|
||||||
instt, err := instanceRepo.Get(ctx,
|
instance, err := instanceRepo.Get(ctx,
|
||||||
database.WithCondition(
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
instanceRepo.NameCondition(database.TextOperationEqual, "name"),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
// require.NoError(t, err)
|
require.Equal(t, inst.ID, instance.ID)
|
||||||
fmt.Printf("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> err = %+v\n", err)
|
require.Equal(t, inst.Name, instance.Name)
|
||||||
fmt.Printf("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> inst = %+v\n", instt)
|
require.Equal(t, inst.DefaultOrgID, instance.DefaultOrgID)
|
||||||
|
require.Equal(t, inst.IAMProjectID, instance.IAMProjectID)
|
||||||
t.Skip("tests are meant as examples and are not real tests")
|
require.Equal(t, inst.ConsoleClientId, instance.ConsoleClientId)
|
||||||
t.Run("User filters", func(t *testing.T) {
|
require.Equal(t, inst.ConsoleAppID, instance.ConsoleAppID)
|
||||||
client := dbmock.NewMockClient(gomock.NewController(t))
|
require.Equal(t, inst.DefaultLanguage, instance.DefaultLanguage)
|
||||||
|
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
|
||||||
user := repository.UserRepository(client)
|
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
|
||||||
u, err := user.Get(context.Background(),
|
require.Nil(t, instance.DeletedAt)
|
||||||
database.WithCondition(
|
require.NoError(t, err)
|
||||||
database.And(
|
|
||||||
database.Or(
|
|
||||||
user.IDCondition("test"),
|
|
||||||
user.IDCondition("2"),
|
|
||||||
),
|
|
||||||
user.UsernameCondition(database.TextOperationStartsWithIgnoreCase, "test"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
database.WithOrderBy(user.CreatedAtColumn()),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, u)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("machine and human filters", func(t *testing.T) {
|
|
||||||
client := dbmock.NewMockClient(gomock.NewController(t))
|
|
||||||
|
|
||||||
user := repository.UserRepository(client)
|
|
||||||
machine := user.Machine()
|
|
||||||
human := user.Human()
|
|
||||||
email, err := human.GetEmail(context.Background(), database.And(
|
|
||||||
user.UsernameCondition(database.TextOperationStartsWithIgnoreCase, "test"),
|
|
||||||
database.Or(
|
|
||||||
machine.DescriptionCondition(database.TextOperationStartsWithIgnoreCase, "test"),
|
|
||||||
human.EmailVerifiedCondition(true),
|
|
||||||
database.IsNotNull(machine.DescriptionColumn()),
|
|
||||||
),
|
|
||||||
))
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, email)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// type dbInstruction string
|
func TestUpdateNameInstance(t *testing.T) {
|
||||||
|
instanceRepo := repository.InstanceRepository(pool)
|
||||||
|
instanceId := gofakeit.Name()
|
||||||
|
instanceName := gofakeit.Name()
|
||||||
|
|
||||||
// func TestArg(t *testing.T) {
|
ctx := context.Background()
|
||||||
// var bla any = "asdf"
|
inst := domain.Instance{
|
||||||
// instr, ok := bla.(dbInstruction)
|
ID: instanceId,
|
||||||
// assert.False(t, ok)
|
Name: instanceName,
|
||||||
// assert.Empty(t, instr)
|
DefaultOrgID: "defaultOrgId",
|
||||||
// bla = dbInstruction("asdf")
|
IAMProjectID: "iamProject",
|
||||||
// instr, ok = bla.(dbInstruction)
|
ConsoleClientId: "consoleCLient",
|
||||||
// assert.True(t, ok)
|
ConsoleAppID: "consoleApp",
|
||||||
// assert.Equal(t, instr, dbInstruction("asdf"))
|
DefaultLanguage: "defaultLanguage",
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func TestWriteUser(t *testing.T) {
|
err := instanceRepo.Create(ctx, &inst)
|
||||||
// t.Skip("tests are meant as examples and are not real tests")
|
require.NoError(t, err)
|
||||||
// t.Run("update user", func(t *testing.T) {
|
|
||||||
// user := repository.UserRepository(nil)
|
_, err = instanceRepo.Get(ctx,
|
||||||
// user.Human().Update(context.Background(), user.IDCondition("test"), user.SetUsername("test"))
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
// })
|
)
|
||||||
// }
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// update name
|
||||||
|
err = instanceRepo.Update(ctx,
|
||||||
|
database.Condition(
|
||||||
|
instanceRepo.IDCondition(instanceId),
|
||||||
|
),
|
||||||
|
instanceRepo.SetName("new_name"),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
instance, err := instanceRepo.Get(ctx,
|
||||||
|
instanceRepo.IDCondition(instanceId),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "new_name", instance.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdeDeleteInstance(t *testing.T) {
|
||||||
|
instanceRepo := repository.InstanceRepository(pool)
|
||||||
|
instanceId := gofakeit.Name()
|
||||||
|
instanceName := gofakeit.Name()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
inst := domain.Instance{
|
||||||
|
ID: instanceId,
|
||||||
|
Name: instanceName,
|
||||||
|
DefaultOrgID: "defaultOrgId",
|
||||||
|
IAMProjectID: "iamProject",
|
||||||
|
ConsoleClientId: "consoleCLient",
|
||||||
|
ConsoleAppID: "consoleApp",
|
||||||
|
DefaultLanguage: "defaultLanguage",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := instanceRepo.Create(ctx, &inst)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
instance, err := instanceRepo.Get(ctx,
|
||||||
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
|
)
|
||||||
|
require.NotNil(t, instance)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// delete instance
|
||||||
|
err = instanceRepo.Delete(ctx,
|
||||||
|
database.Condition(
|
||||||
|
instanceRepo.IDCondition(instanceId),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
instance, err = instanceRepo.Get(ctx,
|
||||||
|
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Nil(t, instance)
|
||||||
|
}
|
||||||
|
@@ -27,14 +27,14 @@ func (*instanceRelationalProjection) Init() *old_handler.Check {
|
|||||||
handler.NewTable([]*handler.InitColumn{
|
handler.NewTable([]*handler.InitColumn{
|
||||||
handler.NewColumn(InstanceColumnID, handler.ColumnTypeText),
|
handler.NewColumn(InstanceColumnID, handler.ColumnTypeText),
|
||||||
handler.NewColumn(InstanceColumnName, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnName, handler.ColumnTypeText, handler.Default("")),
|
||||||
handler.NewColumn(InstanceColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
||||||
handler.NewColumn(InstanceColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
||||||
handler.NewColumn(InstanceColumnDefaultOrgID, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnDefaultOrgID, handler.ColumnTypeText, handler.Default("")),
|
||||||
handler.NewColumn(InstanceColumnProjectID, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnProjectID, handler.ColumnTypeText, handler.Default("")),
|
||||||
handler.NewColumn(InstanceColumnConsoleID, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnConsoleID, handler.ColumnTypeText, handler.Default("")),
|
||||||
handler.NewColumn(InstanceColumnConsoleAppID, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnConsoleAppID, handler.ColumnTypeText, handler.Default("")),
|
||||||
handler.NewColumn(InstanceColumnSequence, handler.ColumnTypeInt64),
|
|
||||||
handler.NewColumn(InstanceColumnDefaultLanguage, handler.ColumnTypeText, handler.Default("")),
|
handler.NewColumn(InstanceColumnDefaultLanguage, handler.ColumnTypeText, handler.Default("")),
|
||||||
|
handler.NewColumn(CreatedAt, handler.ColumnTypeTimestamp),
|
||||||
|
handler.NewColumn(UpdatedAt, handler.ColumnTypeTimestamp),
|
||||||
|
handler.NewColumn(DeletedAt, handler.ColumnTypeTimestamp),
|
||||||
},
|
},
|
||||||
handler.NewPrimaryKey(InstanceColumnID),
|
handler.NewPrimaryKey(InstanceColumnID),
|
||||||
),
|
),
|
||||||
@@ -113,7 +113,7 @@ func (p *instanceRelationalProjection) reduceInstanceChanged(event eventstore.Ev
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *instanceRelationalProjection) reduceInstanceDelete(event eventstore.Event) (*handler.Statement, error) {
|
func (p *instanceRelationalProjection) reduceInstanceDelete(event eventstore.Event) (*handler.Statement, error) {
|
||||||
e, ok := event.(*instance.InstanceChangedEvent)
|
e, ok := event.(*instance.InstanceRemovedEvent)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user