feat: Add PUT and PATCH datasets endpoints
This commit is contained in:
@@ -56,6 +56,19 @@ func (r *stubDatasetRepo) GetByID(_ context.Context, id uuid.UUID) (domain.Datas
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (r *stubDatasetRepo) UpdateFields(_ context.Context, id, categoryID uuid.UUID, name string, description, unit *string) (domain.Dataset, error) {
|
||||
d, ok := r.store[id]
|
||||
if !ok {
|
||||
return domain.Dataset{}, domain.ErrNotFound
|
||||
}
|
||||
d.CategoryID = categoryID
|
||||
d.Name = name
|
||||
d.Description = description
|
||||
d.Unit = unit
|
||||
r.store[id] = d
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (r *stubDatasetRepo) ListSummaries(_ context.Context, filter domain.DatasetFilter, limit, offset int) ([]domain.DatasetSummary, error) {
|
||||
r.lastFilter = filter
|
||||
r.lastLimit = limit
|
||||
@@ -1298,3 +1311,121 @@ func TestDatasetService_Delete_RemovesObject(t *testing.T) {
|
||||
t.Fatalf("object not removed: %v", store.removed)
|
||||
}
|
||||
}
|
||||
|
||||
func strPtr(s string) *string { return &s }
|
||||
|
||||
func TestDatasetService_Update_Success(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
id := uuid.New()
|
||||
repo.store[id] = domain.Dataset{ID: id, Name: "Old", Description: strPtr("old desc"), Unit: strPtr("old unit")}
|
||||
svc := newDatasetService(repo, &stubStore{}, true)
|
||||
|
||||
newCategory := uuid.New()
|
||||
got, err := svc.Update(context.Background(), id, DatasetUpdateInput{
|
||||
CategoryID: newCategory,
|
||||
Name: "New",
|
||||
Description: strPtr("new desc"),
|
||||
Unit: nil,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got.CategoryID != newCategory || got.Name != "New" {
|
||||
t.Fatalf("update not applied: %+v", got)
|
||||
}
|
||||
if got.Description == nil || *got.Description != "new desc" {
|
||||
t.Fatalf("description not updated: %v", got.Description)
|
||||
}
|
||||
if got.Unit != nil {
|
||||
t.Fatalf("unit should be cleared, got %v", *got.Unit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Update_MissingCategory(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
id := uuid.New()
|
||||
repo.store[id] = domain.Dataset{ID: id, Name: "Old"}
|
||||
svc := newDatasetService(repo, &stubStore{}, false)
|
||||
|
||||
_, err := svc.Update(context.Background(), id, DatasetUpdateInput{CategoryID: uuid.New(), Name: "New"})
|
||||
if !errors.Is(err, domain.ErrValidation) {
|
||||
t.Fatalf("want ErrValidation, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Update_NotFound(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
svc := newDatasetService(repo, &stubStore{}, true)
|
||||
|
||||
_, err := svc.Update(context.Background(), uuid.New(), DatasetUpdateInput{CategoryID: uuid.New(), Name: "New"})
|
||||
if !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("want ErrNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Patch_PartialUpdate(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
id := uuid.New()
|
||||
origCategory := uuid.New()
|
||||
repo.store[id] = domain.Dataset{
|
||||
ID: id, CategoryID: origCategory, Name: "Old",
|
||||
Description: strPtr("old desc"), Unit: strPtr("old unit"),
|
||||
}
|
||||
svc := newDatasetService(repo, &stubStore{}, true)
|
||||
|
||||
// Only Name is set; everything else must be left unchanged.
|
||||
got, err := svc.Patch(context.Background(), id, DatasetPatchInput{Name: strPtr("New")})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got.Name != "New" {
|
||||
t.Fatalf("name not updated: %+v", got)
|
||||
}
|
||||
if got.CategoryID != origCategory {
|
||||
t.Fatalf("category should be unchanged, got %v", got.CategoryID)
|
||||
}
|
||||
if got.Description == nil || *got.Description != "old desc" {
|
||||
t.Fatalf("description should be unchanged, got %v", got.Description)
|
||||
}
|
||||
if got.Unit == nil || *got.Unit != "old unit" {
|
||||
t.Fatalf("unit should be unchanged, got %v", got.Unit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Patch_ClearsNullableField(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
id := uuid.New()
|
||||
repo.store[id] = domain.Dataset{ID: id, Name: "Old", Description: strPtr("old desc")}
|
||||
svc := newDatasetService(repo, &stubStore{}, true)
|
||||
|
||||
got, err := svc.Patch(context.Background(), id, DatasetPatchInput{DescriptionSet: true, Description: nil})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got.Description != nil {
|
||||
t.Fatalf("description should be cleared, got %v", *got.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Patch_MissingCategory(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
id := uuid.New()
|
||||
repo.store[id] = domain.Dataset{ID: id, Name: "Old"}
|
||||
svc := newDatasetService(repo, &stubStore{}, false)
|
||||
|
||||
newCategory := uuid.New()
|
||||
_, err := svc.Patch(context.Background(), id, DatasetPatchInput{CategoryID: &newCategory})
|
||||
if !errors.Is(err, domain.ErrValidation) {
|
||||
t.Fatalf("want ErrValidation, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatasetService_Patch_NotFound(t *testing.T) {
|
||||
repo := newStubDatasetRepo()
|
||||
svc := newDatasetService(repo, &stubStore{}, true)
|
||||
|
||||
_, err := svc.Patch(context.Background(), uuid.New(), DatasetPatchInput{Name: strPtr("New")})
|
||||
if !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("want ErrNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user