package bolt import ( "context" "github.com/gisle/stufflog/models" "github.com/stretchr/testify/assert" "testing" ) func TestActivityRepository(t *testing.T) { if dbErr != nil { t.Fatal(dbErr) } strPtr := func(s string) *string { return &s } ctx := context.Background() testUser1 := models.User{ ID: "U1234", Name: "Stuffer", } testUser2 := models.User{ ID: "U4321", Name: "Logger", } testActivity1 := models.Activity{ ID: "A01", Name: "3D Modelling", UserID: testUser1.ID, Icon: "cube", SubActivities: []models.SubActivity{ {ID: "S1001", Name: "Practice", UnitName: "minute", Value: 15}, {ID: "S1002", Name: "Tutorial", UnitName: "minute", Value: 20}, {ID: "S1003", Name: "Scene", UnitName: "minute", Value: 30}, }, } testActivity2 := models.Activity{ ID: "A02", Name: "Roleplay", UserID: testUser2.ID, Icon: "dice-d20", SubActivities: []models.SubActivity{ {ID: "S2001", Name: "Writing", UnitName: "word", Value: 1}, {ID: "S2002", Name: "Planning", UnitName: "word", Value: 2}, {ID: "S2003", Name: "Publish", UnitName: "word", Value: 1}, {ID: "S2003", Name: "Write & Publish", UnitName: "word", Value: 3}, {ID: "S2004", Name: "IRC RP", UnitName: "minute", Value: 6.6}, }, } testActivity3 := models.Activity{ ID: "A03", Name: "Coding", UserID: testUser1.ID, Icon: "code", SubActivities: []models.SubActivity{ {ID: "S3001", Name: "Rust NameGen", UnitName: "minute", Value: 20}, {ID: "S3002", Name: "Learn GoLang", UnitName: "minute", Value: 25}, }, } testActivities := []*models.Activity{&testActivity1, &testActivity2, &testActivity3} testActivity3Update := models.ActivityUpdate{ SetName: strPtr("Programming"), AddSub: &models.SubActivity{Name: "Website Rewrite", UnitName: "minute", Value: 15}, EditSub: &models.SubActivity{ID: "S3001", Name: "Rust SetName Generator", UnitName: "minute", Value: 22}, RemoveSub: strPtr("S3002"), } testActivity3Updated := models.Activity{ ID: "A03", Name: "Programming", UserID: testUser1.ID, Icon: "code", SubActivities: []models.SubActivity{ {ID: "S3001", Name: "Rust SetName Generator", UnitName: "minute", Value: 22}, {Name: "Website Rewrite", UnitName: "minute", Value: 15}, }, } t.Run("Insert", func(t *testing.T) { assert.NoError(t, db.Activities().Insert(ctx, testActivity1), "Save testActivity1") assert.NoError(t, db.Activities().Insert(ctx, testActivity2), "Save testActivity2") assert.NoError(t, db.Activities().Insert(ctx, testActivity3), "Save testActivity3") }) t.Run("Find", func(t *testing.T) { activity, err := db.Activities().FindID(ctx, testActivity1.ID) assert.NoError(t, err, "Find testActivity1") assert.Equal(t, &testActivity1, activity, "Find testActivity1") activity, err = db.Activities().FindID(ctx, testActivity2.ID) assert.NoError(t, err, "Find testActivity2") assert.Equal(t, &testActivity2, activity, "Find testActivity2") activity, err = db.Activities().FindID(ctx, testActivity3.ID) assert.NoError(t, err, "Find testActivity3") assert.Equal(t, &testActivity3, activity, "Find testActivity3") }) t.Run("List", func(t *testing.T) { activities, err := db.Activities().List(ctx) assert.Equal(t, testActivities, activities, "List 3 activities") assert.NoError(t, err, "List 3 activities") activities, err = db.Activities().ListUser(ctx, testUser1) assert.Equal(t, []*models.Activity{&testActivity1, &testActivity3}, activities, "List 2 activities for testUser1") assert.NoError(t, err, "List 2 activities for testUser1") }) t.Run("Update", func(t *testing.T) { activity, err := db.Activities().Update(ctx, testActivity3, []*models.ActivityUpdate{&testActivity3Update}) if activity != nil && len(activity.SubActivities) > 1 { testActivity3Updated.SubActivities[1].ID = activity.SubActivities[1].ID } assert.Equal(t, &testActivity3Updated, activity, "Update activity") assert.NoError(t, err, "Update activity") }) t.Run("Remove", func(t *testing.T) { assert.NoError(t, db.Activities().Remove(ctx, testActivity2), "Delete testActivity2") activities, err := db.Activities().List(ctx) assert.Equal(t, []*models.Activity{&testActivity1, &testActivity3Updated}, activities, "List 2 remaining activities") assert.NoError(t, err, "List 2 remaining activities") }) }