package bolt import ( "context" "github.com/gisle/stufflog/models" "github.com/stretchr/testify/assert" "testing" ) func TestUserRepository(t *testing.T) { if dbErr != nil { t.Fatal(dbErr) } ctx := context.Background() testUser1 := models.User{ ID: "U1234", Name: "Stuffer", } testUser2 := models.User{ ID: "U4321", Name: "Logger", } testUser3 := models.User{ ID: "U2396", Name: "Temper", } testUsers := []*models.User{ &testUser1, &testUser3, &testUser2, } t.Run("Save", func(t *testing.T) { assert.NoError(t, db.Users().Save(ctx, testUser1), "Save testUser1") assert.NoError(t, db.Users().Save(ctx, testUser2), "Save testUser2") assert.NoError(t, db.Users().Save(ctx, testUser3), "Save testUser3") }) t.Run("List", func(t *testing.T) { users, err := db.Users().List(ctx) assert.Equal(t, testUsers, users, "List 3 users") assert.NoError(t, err, "List 3 users") }) t.Run("Find", func(t *testing.T) { user, err := db.Users().FindID(ctx, testUser1.ID) assert.Equal(t, &testUser1, user, "Find testUser1") assert.NoError(t, err, "Find testUser1") user, err = db.Users().FindID(ctx, testUser2.ID) assert.Equal(t, &testUser2, user, "Find testUser2") assert.NoError(t, err, "Find testUser2") user, err = db.Users().FindID(ctx, testUser3.ID) assert.Equal(t, &testUser3, user, "Find testUser3") assert.NoError(t, err, "Find testUser3") user, err = db.Users().FindID(ctx, "UNonExistent") assert.Nil(t, user, "Find UNonExistent") assert.Error(t, err, "Find UNonExistent") }) t.Run("Remove", func(t *testing.T) { assert.NoError(t, db.Users().Remove(ctx, testUser3), "Remove testUser3") user, err := db.Users().FindID(ctx, testUser1.ID) assert.Equal(t, &testUser1, user, "Find non-deleted testUser1") assert.NoError(t, err, "Find non-deleted testUser1") user, err = db.Users().FindID(ctx, testUser2.ID) assert.Equal(t, &testUser2, user, "Find non-deleted testUser2") assert.NoError(t, err, "Find non-deleted testUser2") user, err = db.Users().FindID(ctx, testUser3.ID) assert.Nil(t, user, "Find deleted testUser3") assert.Error(t, err, "Find deleted testUser3") users, err := db.Users().List(ctx) assert.Equal(t, []*models.User{&testUser1, &testUser2}, users, "List 2 remaining users") assert.NoError(t, err, "List 2 remaining users") }) }