From b5e3b2cf85aa6d600cc92364921266cb2b61ddad Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Fri, 1 Dec 2023 16:47:30 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20Delete()?= =?UTF-8?q?=20=D0=B2=D0=B5=D0=B7=D0=B4=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/grpc/server_grpc/server_grpc_test.go_ | 3 +- bin/templates/pkg/db/crud/crud.go_ | 52 ++----------------- bin/templates/pkg/db/crud/crud_test.go_ | 28 ++-------- .../grpc_client/grpc_client_table_test.go_ | 42 ++------------- .../nrpc_client/nrpc_client_table_test.go_ | 41 ++------------- 5 files changed, 19 insertions(+), 147 deletions(-) diff --git a/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ index b44b962..a22cb90 100644 --- a/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ +++ b/bin/templates/internal/app/grpc/server_grpc/server_grpc_test.go_ @@ -7,6 +7,7 @@ import ( "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" + "math" ) // LawsuitStatusType_ID_Test - ID таблицы для тестирования @@ -37,7 +38,7 @@ func Test_server_LawsuitStatusType_Delete(t *testing.T) { ctx := context.Background() Request := grpc_proto.RequestId{} - Request.Id = LawsuitStatusType_ID_Test + Request.Id = math.MinInt64 Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion() server1 := &ServerGRPC{} diff --git a/bin/templates/pkg/db/crud/crud.go_ b/bin/templates/pkg/db/crud/crud.go_ index e6e3ada..f936783 100644 --- a/bin/templates/pkg/db/crud/crud.go_ +++ b/bin/templates/pkg/db/crud/crud.go_ @@ -209,7 +209,7 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { return err } -// Delete_ctx - записывает is_deleted = true +// Delete_ctx - удаляет запись в БД func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error @@ -218,53 +218,11 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws return err } - m2 := lawsuit_status_types.LawsuitStatusType{} - m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) - if err != nil { - return err - } + db := postgres_gorm.GetConnection() + db.WithContext(ctx) - m2.IsDeleted = true - m.IsDeleted = true - - err = crud.Save_ctx(ctx, &m2) - - return err -} - -// Restore - записывает is_deleted = true -func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { - var err error - - ctxMain := contextmain.GetContext() - ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) - defer ctxCancelFunc() - - err = crud.Restore_ctx(ctx, m) - return err -} - -// Restore_ctx - записывает is_deleted = true -func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { - var err error - - if micro.ContextDone(ctx) == true { - err = context.Canceled - return err - } - - m2 := lawsuit_status_types.LawsuitStatusType{} - m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) - if err != nil { - return err - } - - m2.IsDeleted = false - m.IsDeleted = false - - err = crud.Save_ctx(ctx, &m2) + tx := db.Delete(m) + err = tx.Error return err } diff --git a/bin/templates/pkg/db/crud/crud_test.go_ b/bin/templates/pkg/db/crud/crud_test.go_ index 875143c..eb32103 100644 --- a/bin/templates/pkg/db/crud/crud_test.go_ +++ b/bin/templates/pkg/db/crud/crud_test.go_ @@ -5,6 +5,7 @@ import ( "github.com/ManyakRus/starter/postgres_gorm" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" + "math" ) const Postgres_ID_Test = 1 @@ -64,35 +65,12 @@ func TestDelete(t *testing.T) { crud := Crud_DB{} Otvet := lawsuit_status_types.LawsuitStatusType{} - Otvet.ID = Postgres_ID_Test - err := crud.Read(&Otvet) + Otvet.ID = math.MinInt64 + err := crud.Delete(&Otvet) if err != nil { t.Error("TestDelete() error: ", err) } - if Otvet.IsDeleted == false { - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - } else { - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - - } - } diff --git a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ index 2bd2c6d..2aa11ff 100644 --- a/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ +++ b/bin/templates/pkg/network/grpc/grpc_client/grpc_client_table_test.go_ @@ -4,6 +4,7 @@ import ( config "github.com/ManyakRus/starter/config" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_client" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "math" "testing" ) @@ -110,44 +111,11 @@ func TestDelete(t *testing.T) { crud := Crud_GRPC{} Otvet := lawsuit_status_types.LawsuitStatusType{} - Otvet.ID = Postgres_ID_Test - err := crud.Read(&Otvet) + Otvet.ID = math.MinInt64 + + err := crud.Delete(&Otvet) if err != nil { - t.Error("TestRead() error: ", err) - } - - if Otvet.IsDeleted == false { - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - } else { - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } + t.Error("TestDelete() error: ", err) } } diff --git a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ index 8e58f6a..8905600 100644 --- a/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ +++ b/bin/templates/pkg/network/nrpc/nrpc_client/nrpc_client_table_test.go_ @@ -5,6 +5,7 @@ import ( "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/nrpc/nrpc_client" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "testing" + "math" ) // Postgres_ID_Test - ID таблицы для тестирования @@ -110,44 +111,10 @@ func TestDelete(t *testing.T) { crud := Crud_NRPC{} Otvet := lawsuit_status_types.LawsuitStatusType{} - Otvet.ID = Postgres_ID_Test - err := crud.Read(&Otvet) + Otvet.ID = math.MinInt64 + err := crud.Delete(&Otvet) if err != nil { - t.Error("TestRead() error: ", err) - } - - if Otvet.IsDeleted == false { - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - } else { - err = crud.Restore(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } - - err = crud.Delete(&Otvet) - if err != nil { - t.Error("TestDelete() error: ", err) - } - if Otvet.ID == 0 { - t.Error("TestDelete() error: ID =0") - } + t.Error("TestDelete() error: ", err) } }