From 4de7c8cfa7a6bb3bdc80f062bfd9b51663038d38 Mon Sep 17 00:00:00 2001 From: Tosone Date: Tue, 19 Mar 2024 09:16:19 +0800 Subject: [PATCH] :white_check_mark: Update dao test (#344) * :sparkles: Update blob cacher * :white_check_mark: Update dao test --- pkg/dal/dao/audit_test.go | 14 +++++--------- pkg/dal/dao/blob_test.go | 13 +++++-------- pkg/dal/dao/namespace.go | 10 ++-------- pkg/dal/dao/namespace_test.go | 3 --- pkg/dal/dao/repository_test.go | 6 ++---- pkg/types/enums/enums.go | 1 + pkg/types/enums/enums_enum.go | 9 ++++++--- 7 files changed, 21 insertions(+), 35 deletions(-) diff --git a/pkg/dal/dao/audit_test.go b/pkg/dal/dao/audit_test.go index 0490e759..80dc11a8 100644 --- a/pkg/dal/dao/audit_test.go +++ b/pkg/dal/dao/audit_test.go @@ -43,17 +43,13 @@ func TestAuditServiceFactory(t *testing.T) { func TestAuditService(t *testing.T) { viper.SetDefault("log.level", "debug") logger.SetLevel("debug") - err := tests.Initialize(t) - assert.NoError(t, err) - err = tests.DB.Init() - assert.NoError(t, err) + assert.NoError(t, tests.Initialize(t)) + assert.NoError(t, tests.DB.Init()) defer func() { conn, err := dal.DB.DB() assert.NoError(t, err) - err = conn.Close() - assert.NoError(t, err) - err = tests.DB.DeInit() - assert.NoError(t, err) + assert.NoError(t, conn.Close()) + assert.NoError(t, tests.DB.DeInit()) }() ctx := log.Logger.WithContext(context.Background()) @@ -67,7 +63,7 @@ func TestAuditService(t *testing.T) { assert.NotNil(t, namespaceService) namespaceObj1 := &models.Namespace{Name: "test"} - err = namespaceService.Create(ctx, namespaceObj1) + err := namespaceService.Create(ctx, namespaceObj1) assert.NoError(t, err) userServiceFactory := dao.NewUserServiceFactory() diff --git a/pkg/dal/dao/blob_test.go b/pkg/dal/dao/blob_test.go index c3f7d6b4..4c289869 100644 --- a/pkg/dal/dao/blob_test.go +++ b/pkg/dal/dao/blob_test.go @@ -43,24 +43,21 @@ func TestBlobServiceFactory(t *testing.T) { func TestBlobService(t *testing.T) { viper.SetDefault("log.level", "debug") logger.SetLevel("debug") - err := tests.Initialize(t) - assert.NoError(t, err) - err = tests.DB.Init() - assert.NoError(t, err) + assert.NoError(t, tests.Initialize(t)) + assert.NoError(t, tests.DB.Init()) defer func() { conn, err := dal.DB.DB() assert.NoError(t, err) - err = conn.Close() - assert.NoError(t, err) + assert.NoError(t, conn.Close()) assert.NoError(t, tests.DB.DeInit()) }() ctx := log.Logger.WithContext(context.Background()) f := dao.NewBlobServiceFactory() - err = query.Q.Transaction(func(tx *query.Query) error { + err := query.Q.Transaction(func(tx *query.Query) error { blobService := f.New(tx) - err = blobService.Create(ctx, &models.Blob{ + err := blobService.Create(ctx, &models.Blob{ Digest: "sha256:123", Size: 123, ContentType: "test", diff --git a/pkg/dal/dao/namespace.go b/pkg/dal/dao/namespace.go index 3fb5735c..4e802ba5 100644 --- a/pkg/dal/dao/namespace.go +++ b/pkg/dal/dao/namespace.go @@ -203,12 +203,6 @@ func (s *namespaceService) UpdateByID(ctx context.Context, id int64, updates map if len(updates) == 0 { return nil } - matched, err := s.tx.Namespace.WithContext(ctx).Where(s.tx.Namespace.ID.Eq(id)).Updates(updates) - if err != nil { - return err - } - if matched.RowsAffected == 0 { - return gorm.ErrRecordNotFound - } - return nil + _, err := s.tx.Namespace.WithContext(ctx).Where(s.tx.Namespace.ID.Eq(id)).Updates(updates) + return err } diff --git a/pkg/dal/dao/namespace_test.go b/pkg/dal/dao/namespace_test.go index 3beb224a..ccc1cc68 100644 --- a/pkg/dal/dao/namespace_test.go +++ b/pkg/dal/dao/namespace_test.go @@ -111,9 +111,6 @@ func TestNamespaceService(t *testing.T) { err = namespaceService.UpdateByID(ctx, namespaceObj.ID, map[string]interface{}{query.Namespace.Description.ColumnName().String(): "test"}) assert.NoError(t, err) - err = namespaceService.UpdateByID(ctx, 10, map[string]interface{}{query.Namespace.Description.ColumnName().String(): "test"}) - assert.ErrorIs(t, err, gorm.ErrRecordNotFound) - err = namespaceService.DeleteByID(ctx, namespaceObj.ID) assert.NoError(t, err) diff --git a/pkg/dal/dao/repository_test.go b/pkg/dal/dao/repository_test.go index fcb16282..e58aae8a 100644 --- a/pkg/dal/dao/repository_test.go +++ b/pkg/dal/dao/repository_test.go @@ -51,10 +51,8 @@ func TestRepositoryService(t *testing.T) { defer func() { conn, err := dal.DB.DB() assert.NoError(t, err) - err = conn.Close() - assert.NoError(t, err) - err = tests.DB.DeInit() - assert.NoError(t, err) + assert.NoError(t, conn.Close()) + assert.NoError(t, tests.DB.DeInit()) }() ctx := log.Logger.WithContext(context.Background()) diff --git a/pkg/types/enums/enums.go b/pkg/types/enums/enums.go index 84bdbc94..379bb5b9 100644 --- a/pkg/types/enums/enums.go +++ b/pkg/types/enums/enums.go @@ -82,6 +82,7 @@ type CacherType string // redis, // kafka, // database, +// inmemory, // ) type WorkQueueType string diff --git a/pkg/types/enums/enums_enum.go b/pkg/types/enums/enums_enum.go index e17f0c96..fa4ddf49 100644 --- a/pkg/types/enums/enums_enum.go +++ b/pkg/types/enums/enums_enum.go @@ -1,7 +1,7 @@ // Code generated by go-enum DO NOT EDIT. -// Version: 0.5.10 -// Revision: f2bc4fe0810071d365417dd2e3ce1b4d64b82c47 -// Build Date: 2023-11-13T16:50:24Z +// Version: 0.6.0 +// Revision: 919e61c0174b91303753ee3898569a01abb32c97 +// Build Date: 2023-12-18T15:54:43Z // Built By: goreleaser package enums @@ -3081,6 +3081,8 @@ const ( WorkQueueTypeKafka WorkQueueType = "kafka" // WorkQueueTypeDatabase is a WorkQueueType of type database. WorkQueueTypeDatabase WorkQueueType = "database" + // WorkQueueTypeInmemory is a WorkQueueType of type inmemory. + WorkQueueTypeInmemory WorkQueueType = "inmemory" ) var ErrInvalidWorkQueueType = errors.New("not a valid WorkQueueType") @@ -3101,6 +3103,7 @@ var _WorkQueueTypeValue = map[string]WorkQueueType{ "redis": WorkQueueTypeRedis, "kafka": WorkQueueTypeKafka, "database": WorkQueueTypeDatabase, + "inmemory": WorkQueueTypeInmemory, } // ParseWorkQueueType attempts to convert a string to a WorkQueueType.