diff --git a/docker-compose.yml b/docker-compose.yml index d71cd3d16..72912eb78 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,7 @@ services: db: container_name: postgres - #image: europe-west1-docker.pkg.dev/marble-infra/marble/postgresql-db:latest # custom image of postgres 15 with pg_cron extension added - image: postgres:15.2-alpine + image: europe-west1-docker.pkg.dev/marble-infra/marble/postgresql-db:latest # custom image of postgres 15 with pg_cron extension added shm_size: 1g restart: always environment: @@ -15,7 +14,7 @@ services: volumes: - postgres-db:/data/postgres healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] + test: [ "CMD-SHELL", "pg_isready -U postgres" ] interval: 2s timeout: 1s retries: 5 diff --git a/mocks/scenario_testrun_repository.go b/mocks/scenario_testrun_repository.go index a8401dfc8..86e431a2c 100644 --- a/mocks/scenario_testrun_repository.go +++ b/mocks/scenario_testrun_repository.go @@ -52,3 +52,13 @@ func (s *ScenarioTestrunRepository) ListTestRunsByScenarioID(ctx context.Context } return args.Get(0).([]models.ScenarioTestRun), args.Error(1) } + +func (s *ScenarioTestrunRepository) ListRunningTestRun(ctx context.Context, + exec repositories.Executor, +) ([]models.ScenarioTestRun, error) { + args := s.Called(ctx, exec) + if args.Get(0) == nil { + return nil, args.Error(1) + } + return args.Get(0).([]models.ScenarioTestRun), args.Error(1) +} diff --git a/repositories/scenario_testrun.go b/repositories/scenario_testrun.go index 13f73878d..bee90e09a 100644 --- a/repositories/scenario_testrun.go +++ b/repositories/scenario_testrun.go @@ -16,7 +16,7 @@ type ScenarioTestRunRepository interface { testrunId string, input models.ScenarioTestRunCreateDbInput, ) error - ListRunningTestRun(ctx context.Context, exec Executor) ([]models.ScenarioTestRun, error) + ListRunningTestRun(ctx context.Context, exec Executor, organizationId string) ([]models.ScenarioTestRun, error) ListTestRunsByScenarioID(ctx context.Context, exec Executor, scenarioID string) ([]models.ScenarioTestRun, error) GetTestRunByLiveVersionID( ctx context.Context, @@ -109,22 +109,32 @@ func (repo *MarbleDbRepository) GetTestRunByLiveVersionID( } func (repo *MarbleDbRepository) ListRunningTestRun( - ctx context.Context, exec Executor, + ctx context.Context, exec Executor, organizationId string, ) ([]models.ScenarioTestRun, error) { if err := validateMarbleDbExecutor(exec); err != nil { return nil, err } - query := selectTestruns(). - Where(squirrel.Eq{"status": models.Up}). + query := NewQueryBuilder(). + Select(` + tr.id, + tr.scenario_iteration_id, + tr.live_scenario_iteration_id, + tr.created_at, + tr.expires_at, + tr.status`). + From(dbmodels.TABLE_SCENARIO_TESTRUN + " AS tr"). + Join(dbmodels.TABLE_SCENARIO_ITERATIONS + " AS scit ON scit.id = tr.scenario_iteration_id"). + Where(squirrel.And{ + squirrel.Eq{"tr.status": models.Up}, + squirrel.Eq{"scit.org_id": organizationId}, + }). OrderBy("created_at DESC") - testruns, err := SqlToListOfModels(ctx, exec, query, dbmodels.AdaptScenarioTestrun) - if err != nil { - return nil, err - } - if len(testruns) == 0 { - return nil, nil - } - return testruns, nil + return SqlToListOfModels( + ctx, + exec, + query, + dbmodels.AdaptScenarioTestrun, + ) } func (repo *MarbleDbRepository) ListTestRunsByScenarioID(ctx context.Context, diff --git a/usecases/scenario_testrun_usecase.go b/usecases/scenario_testrun_usecase.go index cb18cc8ce..4d07f2673 100644 --- a/usecases/scenario_testrun_usecase.go +++ b/usecases/scenario_testrun_usecase.go @@ -55,7 +55,7 @@ func (usecase *ScenarioTestRunUsecase) CreateScenarioTestRun( } // we should not have any existing testrun for this scenario - testRuns, err := usecase.repository.ListRunningTestRun(ctx, exec) + testRuns, err := usecase.repository.ListRunningTestRun(ctx, exec, organizationId) if err != nil { return models.ScenarioTestRun{}, err } @@ -164,10 +164,7 @@ func (usecase *ScenarioTestRunUsecase) CancelTestRunById(ctx context.Context, if err := usecase.enforceSecurity.ReadTestRun(testRun.OrganizationId); err != nil { return models.ScenarioTestRun{}, err } - if testRun.Status == models.Down { - return testRun, nil - } - if testRun.Status == models.Up { + if testRun.Status != models.Down { if err := usecase.repository.UpdateTestRunStatus(ctx, exec, testRunId, models.Down); err != nil { return models.ScenarioTestRun{}, err @@ -181,5 +178,5 @@ func (usecase *ScenarioTestRunUsecase) CancelTestRunById(ctx context.Context, } return updatedTestRun, nil } - return models.ScenarioTestRun{}, models.BadParameterError + return testRun, nil }