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 issue1 = models.Issue{ ProjectID: "STUFF", OwnerID: "Admin", AssigneeID: "", StatusStage: models.IssueStageActive, StatusName: "IN PROGRESS", DueTime: time.Now().UTC().Add(time.Hour * 72).Truncate(time.Hour * 24).Add(time.Hour * 16), Name: "Do Stuff", Title: "Do some important stuff.", Description: "Stuff and things, items and artifacts, objects and creations.", } var issue2 = models.Issue{ ProjectID: "MODELING", OwnerID: "Test", AssigneeID: "Test", StatusStage: models.IssueStagePostponed, StatusName: "TOO HARD", Name: "Hard Surface Course", Title: "Run through the Hard Surface modeling course.", Description: "maek robit", } var issue3 = models.Issue{ ProjectID: "MODELING", OwnerID: "Test", AssigneeID: "", StatusStage: models.IssueStagePending, StatusName: "TO DO", Name: "Isometric Room Scene", Title: "Create an isometric room-box scene.", Description: "furniture and stuff.", } var issue3Updated = models.Issue{ ProjectID: "MODELING", OwnerID: "Test", AssigneeID: "Admin", StatusStage: models.IssueStageActive, StatusName: "WORK IN PROGRESS", Name: "Room Scene", Title: "Create a room-box scene.", Description: "THREE DIMENSIONAL DESK CLUTTER", } var issue4 = models.Issue{ ProjectID: "DINNER", OwnerID: "Test", AssigneeID: "Test", StatusStage: models.IssueStagePending, StatusName: "SHOPPING LIST", DueTime: mustParseTime("2020-04-22T17:30:00.000+02:00"), Name: "Spaghetti Carbonara", Title: "WEDNESDAY 2020-04-22: Spaghetti Carbonara", Description: "See shopping list", } func TestIssueRepository(t *testing.T) { issues := testDB.issues ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() assert.NoError(t, clearTable("issue")) // INSERT result, err := issues.Insert(ctx, issue1) assert.NoError(t, err) assert.Equal(t, "STUFF-1", result.ID) if result != nil { issue1.ID = result.ID issue1.CreatedTime = result.CreatedTime.UTC() issue1.UpdatedTime = result.UpdatedTime.UTC() } result, err = issues.Insert(ctx, issue2) assert.NoError(t, err) assert.Equal(t, "MODELING-1", result.ID) if result != nil { issue2.ID = result.ID issue2.CreatedTime = result.CreatedTime.UTC() issue2.UpdatedTime = result.UpdatedTime.UTC() } result, err = issues.Insert(ctx, issue3) assert.NoError(t, err) assert.Equal(t, "MODELING-2", result.ID) if result != nil { issue3.ID = result.ID issue3.CreatedTime = result.CreatedTime.UTC() issue3.UpdatedTime = result.UpdatedTime.UTC() issue3Updated.ID = result.ID issue3Updated.CreatedTime = result.CreatedTime.UTC() issue3Updated.UpdatedTime = result.UpdatedTime.UTC() } result, err = issues.Insert(ctx, issue4) assert.NoError(t, err) assert.Equal(t, "DINNER-1", result.ID) if result != nil { issue4.ID = result.ID issue4.CreatedTime = result.CreatedTime.UTC() issue4.UpdatedTime = result.UpdatedTime.UTC() } // FIND result, err = issues.Find(ctx, issue1.ID) assert.NoError(t, err) assert.Equal(t, &issue1, result) result, err = issues.Find(ctx, issue3.ID) assert.NoError(t, err) assert.Equal(t, &issue3, result) // FIND't result, err = issues.Find(ctx, "NONEXISTENT-666") assert.Error(t, err) assert.Nil(t, result) assert.True(t, slerrors.IsNotFound(err)) // LIST results, err := issues.List(ctx, models.IssueFilter{}) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3, &issue1}, results) results, err = issues.List(ctx, models.IssueFilter{ ProjectIDs: []string{"DINNER", "MODELING"}, }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3}, results) results, err = issues.List(ctx, models.IssueFilter{ IssueIDs: []string{"MODELING-2", "DINNER-1"}, }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue4, &issue3}, results) results, err = issues.List(ctx, models.IssueFilter{ OwnerIDs: []string{"Test"}, }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3}, results) results, err = issues.List(ctx, models.IssueFilter{ AssigneeIDs: []string{"", "Admin"}, }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue3, &issue1}, results) results, err = issues.List(ctx, models.IssueFilter{ MinStage: ptrInt(models.IssueStageActive), MaxStage: ptrInt(models.IssueStageReview), }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue1}, results) results, err = issues.List(ctx, models.IssueFilter{ Limit: ptrInt(2), }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue4, &issue2}, results) results, err = issues.List(ctx, models.IssueFilter{ OwnerIDs: []string{"Admin"}, AssigneeIDs: []string{""}, Search: ptrString("stuff"), }) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue1}, results) // SAVE issue3Updated.UpdatedTime = time.Now().UTC().Truncate(time.Second) err = issues.Save(ctx, issue3Updated) assert.NoError(t, err) // FIND after SAVE result, err = issues.Find(ctx, issue3.ID) assert.NoError(t, err) assert.Equal(t, &issue3Updated, result) // DELETE err = issues.Delete(ctx, issue4) assert.NoError(t, err) // LIST after DELETE and SAVE results, err = issues.List(ctx, models.IssueFilter{}) assert.NoError(t, err) assert.Equal(t, []*models.Issue{&issue2, &issue3Updated, &issue1}, results) }