package mysqldriver import ( "context" "git.aiterp.net/stufflog/server/internal/slerrors" "git.aiterp.net/stufflog/server/models" "github.com/stretchr/testify/assert" "testing" "time" ) var projectStatus1 = models.ProjectStatus{ ProjectID: project1.ID, Stage: models.IssueStagePending, Name: "Ready", Description: "This task is ready to be done.", } var projectStatus2 = models.ProjectStatus{ ProjectID: project1.ID, Stage: models.IssueStageInactive, Name: "Idea", Description: "Maybe do this?", } var projectStatus3 = models.ProjectStatus{ ProjectID: project2.ID, Stage: models.IssueStageActive, Name: "In Progress", Description: "It is being done.", } var projectStatus4 = models.ProjectStatus{ ProjectID: project2.ID, Stage: models.IssueStageReview, Name: "Rendered", Description: "It is rendered and awaiting feedback.", } var projectStatus5 = models.ProjectStatus{ ProjectID: project3.ID, Stage: models.IssueStageCompleted, Name: "Om Nom Nom", Description: "Sustenance intake complete.", } var projectStatus6 = models.ProjectStatus{ ProjectID: project1.ID, Stage: models.IssueStageFailed, Name: "Bad Stuff", Description: "Stuff could not be done.", } var projectStatus7 = models.ProjectStatus{ ProjectID: project2.ID, Stage: models.IssueStagePostponed, Name: "Too Hard", Description: "Do it later.", } var projectStatus7Updated = models.ProjectStatus{ ProjectID: project2.ID, Stage: models.IssueStageInactive, Name: "Too Hard", Description: "Do it later, when you're less of newbie.", } func TestProjectStatusRepository(t *testing.T) { statuses := testDB.projectStatuses ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() assert.NoError(t, clearTable("project_status")) // INSERT assert.NoError(t, statuses.Save(ctx, projectStatus1)) assert.NoError(t, statuses.Save(ctx, projectStatus2)) assert.NoError(t, statuses.Save(ctx, projectStatus3)) assert.NoError(t, statuses.Save(ctx, projectStatus4)) assert.NoError(t, statuses.Save(ctx, projectStatus5)) assert.NoError(t, statuses.Save(ctx, projectStatus6)) assert.NoError(t, statuses.Save(ctx, projectStatus7)) // FIND result, err := statuses.Find(ctx, project2.ID, projectStatus3.Name) assert.NoError(t, err) assert.Equal(t, &projectStatus3, result) result, err = statuses.Find(ctx, project1.ID, projectStatus6.Name) assert.NoError(t, err) assert.Equal(t, &projectStatus6, result) // FINDn't result, err = statuses.Find(ctx, project3.ID, projectStatus6.Name) assert.Error(t, err) assert.True(t, slerrors.IsNotFound(err)) assert.Nil(t, result) result, err = statuses.Find(ctx, project2.ID, "Non-existent Name") assert.Error(t, err) assert.True(t, slerrors.IsNotFound(err)) assert.Nil(t, result) // LIST results, err := statuses.List(ctx, models.ProjectStatusFilter{}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus5, &projectStatus3, &projectStatus4, &projectStatus7, &projectStatus2, &projectStatus1, &projectStatus6, }, results) results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project1.ID}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus2, &projectStatus1, &projectStatus6, }, results) results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MinStage: ptrInt(models.IssueStageReview)}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus4, &projectStatus7, }, results) results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MaxStage: ptrInt(models.IssueStageReview)}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus3, &projectStatus4, }, results) results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project3.ID, MaxStage: ptrInt(models.IssueStagePending)}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{}, results) // UPDATE assert.NoError(t, statuses.Save(ctx, projectStatus7Updated)) // FIND after UPDATE result, err = statuses.Find(ctx, project2.ID, projectStatus7.Name) assert.NoError(t, err) assert.Equal(t, &projectStatus7Updated, result) // LIST after UPDATE results, err = statuses.List(ctx, models.ProjectStatusFilter{}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus5, &projectStatus7Updated, &projectStatus3, &projectStatus4, &projectStatus2, &projectStatus1, &projectStatus6, }, results) // DELETE assert.NoError(t, statuses.Delete(ctx, projectStatus7Updated)) // LIST after DELETE results, err = statuses.List(ctx, models.ProjectStatusFilter{}) assert.NoError(t, err) assert.Equal(t, []*models.ProjectStatus{ &projectStatus5, &projectStatus3, &projectStatus4, &projectStatus2, &projectStatus1, &projectStatus6, }, results) }