From 6d29163dfb95dbc020dcf48e85478ff28948b208 Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Fri, 13 Dec 2024 19:11:13 +0100 Subject: [PATCH] chore: enable goconst (#904) --- .golangci.yaml | 7 ++++++- libs/common/hwgrpc/panic_interceptor_test.go | 6 ++++-- .../patient/aggregate/aggregate_test.go | 12 ++++++------ .../internal/patient/api/grpc_test.go | 6 +++--- .../internal/task/aggregate/aggregate_test.go | 18 +++++++++--------- .../tasks-svc/internal/task/api/grpc_test.go | 2 +- services/tasks-svc/stories/PatientCRUD_test.go | 11 +++++++---- services/tasks-svc/stories/TaskCRUD_test.go | 12 ++++++++---- 8 files changed, 44 insertions(+), 30 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index c2a6a778e..a2c6dce63 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -32,7 +32,6 @@ linters: - exhaustruct # TODO - gci # TODO - gocognit # TODO - - goconst # TODO - gocyclo # TODO - godox # TODO - nestif # TODO @@ -43,3 +42,9 @@ linters: - stylecheck # TODO - tagliatelle # TODO - testpackage # TODO + +linters-settings: + goconst: + ignore-tests: true + min-occurrences: 2 + ignore-strings: '(true|false)' # regex diff --git a/libs/common/hwgrpc/panic_interceptor_test.go b/libs/common/hwgrpc/panic_interceptor_test.go index 7eaf8ce6b..bcfab10e9 100644 --- a/libs/common/hwgrpc/panic_interceptor_test.go +++ b/libs/common/hwgrpc/panic_interceptor_test.go @@ -16,15 +16,17 @@ type recoveryAssertService struct { testpb.TestServiceServer } +const PanicValue = "panic" + func (s *recoveryAssertService) Ping(ctx context.Context, ping *testpb.PingRequest) (*testpb.PingResponse, error) { - if ping.GetValue() == "panic" { + if ping.GetValue() == PanicValue { panic("very bad thing happened") } return s.TestServiceServer.Ping(ctx, ping) } func (s *recoveryAssertService) PingList(ping *testpb.PingListRequest, stream testpb.TestService_PingListServer) error { - if ping.Value == "panic" { + if ping.Value == PanicValue { panic("very bad thing happened") } return s.TestServiceServer.PingList(ping, stream) diff --git a/services/tasks-svc/internal/patient/aggregate/aggregate_test.go b/services/tasks-svc/internal/patient/aggregate/aggregate_test.go index 1a7067264..728eabe3e 100644 --- a/services/tasks-svc/internal/patient/aggregate/aggregate_test.go +++ b/services/tasks-svc/internal/patient/aggregate/aggregate_test.go @@ -32,7 +32,7 @@ func TestPatientAggregate_CreatePatient(t *testing.T) { patientID := uuid.New() - patientHumanReadableIdentifier := "tester" + patientHumanReadableIdentifier := t.Name() patientNotes := "test-notes" patientAggregate := aggregate.NewPatientAggregate(patientID) @@ -64,7 +64,7 @@ func TestPatientAggregate_UpdateNotes(t *testing.T) { patientID := uuid.New() - patientHumanReadableIdentifier := "tester" + patientHumanReadableIdentifier := t.Name() initialPatientNotes := "test notes" updatedPatientNotes := "test notes updated" @@ -100,8 +100,8 @@ func TestPatientAggregate_UpdateHumanReadableIdentifier(t *testing.T) { patientID := uuid.New() - initialPatientHumanReadableIdentifier := "tester" - updatedPatientHumanReadableIdentifier := "tester updated" + initialPatientHumanReadableIdentifier := t.Name() + updatedPatientHumanReadableIdentifier := t.Name() + " updated" patientAggregate := aggregate.NewPatientAggregate(patientID) @@ -140,7 +140,7 @@ func TestPatientAggregate_DischargeReadmitPatient(t *testing.T) { ctx = auth.ContextWithOrganizationID(ctx, uuid.New()) patientID := uuid.New() - patientHumanReadableIdentifier := "tester" + patientHumanReadableIdentifier := t.Name() patientAggregate := aggregate.NewPatientAggregate(patientID) MustApplyEvent(t, patientAggregate, func() (hwes.Event, error) { @@ -183,7 +183,7 @@ func TestPatientAggregate_AssignUnassignBed(t *testing.T) { patientID := uuid.New() newBedID := uuid.New() - patientHumanReadableIdentifier := "tester" + patientHumanReadableIdentifier := t.Name() patientAggregate := aggregate.NewPatientAggregate(patientID) MustApplyEvent(t, patientAggregate, func() (hwes.Event, error) { diff --git a/services/tasks-svc/internal/patient/api/grpc_test.go b/services/tasks-svc/internal/patient/api/grpc_test.go index d171e92c8..435a37ad4 100644 --- a/services/tasks-svc/internal/patient/api/grpc_test.go +++ b/services/tasks-svc/internal/patient/api/grpc_test.go @@ -91,8 +91,8 @@ func TestPatientGrpcService_CreatePatient(t *testing.T) { defer teardown() // First, create a new Patient - humanReadableIdentifier := "Test patient" - notes := "notes" + humanReadableIdentifier := t.Name() + " patient" + notes := t.Name() createPatientResponse, err := client.CreatePatient(ctx, &pb.CreatePatientRequest{ HumanReadableIdentifier: humanReadableIdentifier, @@ -117,7 +117,7 @@ func TestPatientGrpcService_UpdatePatient(t *testing.T) { // Initial values humanReadableIdentifier1 := "Test patient" - notes1 := "notes" + notes1 := t.Name() // First, create a new patient createPatientResponse, err := client.CreatePatient(ctx, diff --git a/services/tasks-svc/internal/task/aggregate/aggregate_test.go b/services/tasks-svc/internal/task/aggregate/aggregate_test.go index 9de6da0ab..f270e1fcd 100644 --- a/services/tasks-svc/internal/task/aggregate/aggregate_test.go +++ b/services/tasks-svc/internal/task/aggregate/aggregate_test.go @@ -34,8 +34,8 @@ func TestTaskAggregate_UpdateName(t *testing.T) { taskID := uuid.New() patientID := uuid.New() - initialTaskName := "Test Task" - updatedTaskName := "Test Task Updated" + initialTaskName := t.Name() + updatedTaskName := t.Name() + " Updated" taskAggregate := aggregate.NewTaskAggregate(taskID) @@ -72,7 +72,7 @@ func TestTaskAggregate_UpdateDescription(t *testing.T) { patientID := uuid.New() initialTaskDescription := "" - updatedTaskDescription := "Text text" + updatedTaskDescription := t.Name() taskAggregate := aggregate.NewTaskAggregate(taskID) @@ -81,7 +81,7 @@ func TestTaskAggregate_UpdateDescription(t *testing.T) { ctx, taskAggregate, taskID, - "Test task", + t.Name(), patientID, pb.TaskStatus_TASK_STATUS_TODO, ) @@ -121,7 +121,7 @@ func TestTaskAggregate_UpdateSubtaskName(t *testing.T) { ctx, taskAggregate, taskID, - "Test task", + t.Name(), patientID, pb.TaskStatus_TASK_STATUS_TODO, ) @@ -155,8 +155,8 @@ func TestTaskAggregate_CompleteSubtask(t *testing.T) { subtaskID := uuid.New() patientID := uuid.New() - taskName := "Test Task" - subtaskName := "Test Subtask" + taskName := t.Name() + subtaskName := t.Name() + " Subtask" taskAggregate := aggregate.NewTaskAggregate(taskID) @@ -206,7 +206,7 @@ func TestTaskAggregate_AssignTask(t *testing.T) { taskID := uuid.New() patientID := uuid.New() - taskName := "Test Task" + taskName := t.Name() taskAggregate := aggregate.NewTaskAggregate(taskID) @@ -239,7 +239,7 @@ func TestTaskAggregate_DeleteTask(t *testing.T) { taskID := uuid.New() patientID := uuid.New() - taskName := "Test Task" + taskName := t.Name() taskAggregate := aggregate.NewTaskAggregate(taskID) diff --git a/services/tasks-svc/internal/task/api/grpc_test.go b/services/tasks-svc/internal/task/api/grpc_test.go index 96b7ddd01..b078f8e22 100644 --- a/services/tasks-svc/internal/task/api/grpc_test.go +++ b/services/tasks-svc/internal/task/api/grpc_test.go @@ -46,7 +46,7 @@ func TestTaskGrpcService_CreateTask_Validation(t *testing.T) { ctx, client, teardown := setup() defer teardown() - taskName := "Test task" + taskName := t.Name() // patientID empty -> Error _, err := client.CreateTask(ctx, &pb.CreateTaskRequest{ diff --git a/services/tasks-svc/stories/PatientCRUD_test.go b/services/tasks-svc/stories/PatientCRUD_test.go index 88cba18c4..9bc3bff04 100644 --- a/services/tasks-svc/stories/PatientCRUD_test.go +++ b/services/tasks-svc/stories/PatientCRUD_test.go @@ -419,17 +419,20 @@ func TestGetPatientList(t *testing.T) { // Create tasks // + ST1 := "ST 1" + ST2 := "ST 2" + task1Res, err := taskClient.CreateTask(ctx, &pb.CreateTaskRequest{ Name: t.Name() + " task 1", PatientId: patient1Id, Public: hwutil.PtrTo(true), Subtasks: []*pb.CreateTaskRequest_SubTask{ { - Name: "ST 1", + Name: ST1, Done: hwutil.PtrTo(true), }, { - Name: "ST 2", + Name: ST2, }, }, }) @@ -482,11 +485,11 @@ func TestGetPatientList(t *testing.T) { assert.Len(t, patient.Tasks[0].Subtasks, 2) f := 0 for _, st := range patient.Tasks[0].Subtasks { - if st.Name == "ST 1" { + if st.Name == ST1 { f++ assert.True(t, st.Done) } - if st.Name == "ST 2" { + if st.Name == ST2 { f++ assert.False(t, st.Done) } diff --git a/services/tasks-svc/stories/TaskCRUD_test.go b/services/tasks-svc/stories/TaskCRUD_test.go index b9ffe6047..26698b2c2 100644 --- a/services/tasks-svc/stories/TaskCRUD_test.go +++ b/services/tasks-svc/stories/TaskCRUD_test.go @@ -30,6 +30,10 @@ func TestCreateUpdateGetTask(t *testing.T) { // // create new task // + + ST1 := "SubTask 1" + ST2 := "SubTask 2" + createReq := &pb.CreateTaskRequest{ Name: t.Name() + " task", Description: hwutil.PtrTo("Some Description"), @@ -39,8 +43,8 @@ func TestCreateUpdateGetTask(t *testing.T) { InitialStatus: nil, AssignedUserId: nil, Subtasks: []*pb.CreateTaskRequest_SubTask{ - {Name: "ST 1"}, - {Name: "ST 2", Done: hwutil.PtrTo(true)}, + {Name: ST1}, + {Name: ST2, Done: hwutil.PtrTo(true)}, }, } createRes, err := taskClient.CreateTask(ctx, createReq) @@ -70,12 +74,12 @@ func TestCreateUpdateGetTask(t *testing.T) { assert.Len(t, task.GetSubtasks(), 2) found := 0 for _, st := range task.GetSubtasks() { - if st.Name == "ST 1" { + if st.Name == ST1 { found++ assert.False(t, st.GetDone()) assert.Equal(t, hwtesting.FakeTokenUser, st.GetCreatedBy()) } - if st.Name == "ST 2" { + if st.Name == ST2 { found++ assert.True(t, st.GetDone()) assert.Equal(t, hwtesting.FakeTokenUser, st.GetCreatedBy())