Skip to content

Commit

Permalink
Add publish option for datasets independent of versions
Browse files Browse the repository at this point in the history
  • Loading branch information
eldeal committed Feb 21, 2018
1 parent 23462dd commit 41bb3d7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
43 changes: 24 additions & 19 deletions api/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,18 @@ func (api *DatasetAPI) putDataset(w http.ResponseWriter, r *http.Request) {
return
}

if err := api.dataStore.Backend.UpdateDataset(datasetID, dataset, currentDataset.Next.State); err != nil {
log.ErrorC("failed to update dataset resource", err, log.Data{"dataset_id": datasetID})
handleErrorType(datasetDocType, err, w)
return
if dataset.State == models.PublishedState {
if err := api.publishDataset(currentDataset, nil); err != nil {
log.ErrorC("failed to update dataset document to published", err, log.Data{"dataset_id": datasetID})
handleErrorType(versionDocType, err, w)
return
}
} else {
if err := api.dataStore.Backend.UpdateDataset(datasetID, dataset, currentDataset.Next.State); err != nil {
log.ErrorC("failed to update dataset resource", err, log.Data{"dataset_id": datasetID})
handleErrorType(datasetDocType, err, w)
return
}
}

setJSONContentType(w)
Expand All @@ -399,7 +407,8 @@ func (api *DatasetAPI) putVersion(w http.ResponseWriter, r *http.Request) {
return
}

if err = api.dataStore.Backend.CheckDatasetExists(datasetID, ""); err != nil {
currentDataset, err := api.dataStore.Backend.GetDataset(datasetID)
if err != nil {
log.ErrorC("failed to find dataset", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
Expand Down Expand Up @@ -442,7 +451,7 @@ func (api *DatasetAPI) putVersion(w http.ResponseWriter, r *http.Request) {
}

// Pass in newVersion variable to include relevant data needed for update on dataset API (e.g. links)
if err := api.publishDataset(datasetID, newVersion); err != nil {
if err := api.publishDataset(currentDataset, newVersion); err != nil {
log.ErrorC("failed to update dataset document once version state changes to publish", err, log.Data{"dataset_id": datasetID, "edition": edition, "version": version})
handleErrorType(versionDocType, err, w)
return
Expand Down Expand Up @@ -586,18 +595,14 @@ func createNewVersionDoc(currentVersion *models.Version, version *models.Version
return version
}

func (api *DatasetAPI) publishDataset(id string, version *models.Version) error {
currentDataset, err := api.dataStore.Backend.GetDataset(id)
if err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": id})
return err
}

currentDataset.Next.CollectionID = version.CollectionID
func (api *DatasetAPI) publishDataset(currentDataset *models.DatasetUpdate, version *models.Version) error {
if version != nil {
currentDataset.Next.CollectionID = version.CollectionID

currentDataset.Next.Links.LatestVersion = &models.LinkObject{
ID: version.Links.Version.ID,
HRef: version.Links.Version.HRef,
currentDataset.Next.Links.LatestVersion = &models.LinkObject{
ID: version.Links.Version.ID,
HRef: version.Links.Version.HRef,
}
}

currentDataset.Next.State = models.PublishedState
Expand All @@ -613,8 +618,8 @@ func (api *DatasetAPI) publishDataset(id string, version *models.Version) error
Next: currentDataset.Next,
}

if err := api.dataStore.Backend.UpsertDataset(id, newDataset); err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": id})
if err := api.dataStore.Backend.UpsertDataset(currentDataset.ID, newDataset); err != nil {
log.ErrorC("unable to update dataset", err, log.Data{"dataset_id": currentDataset.ID})
return err
}

Expand Down
85 changes: 40 additions & 45 deletions api/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,8 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
w := httptest.NewRecorder()

mockedDataStore := &storetest.StorerMock{
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand Down Expand Up @@ -943,7 +943,7 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
api.router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusOK)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 3)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 2)
Expand All @@ -968,8 +968,8 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
w := httptest.NewRecorder()

mockedDataStore := &storetest.StorerMock{
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand All @@ -994,7 +994,7 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
api.router.ServeHTTP(w, r)

So(w.Code, ShouldEqual, http.StatusOK)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 3)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 2)
Expand Down Expand Up @@ -1022,8 +1022,8 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
w := httptest.NewRecorder()

mockedDataStore := &storetest.StorerMock{
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand Down Expand Up @@ -1054,7 +1054,7 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
}

So(w.Code, ShouldEqual, http.StatusOK)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 2)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 1)
Expand All @@ -1079,9 +1079,6 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
w := httptest.NewRecorder()

mockedDataStore := &storetest.StorerMock{
CheckDatasetExistsFunc: func(string, string) error {
return nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
},
Expand Down Expand Up @@ -1137,7 +1134,6 @@ func TestPutVersionReturnsSuccessfully(t *testing.T) {
api := GetAPIWithMockedDatastore(mockedDataStore, generatorMock)
api.router.ServeHTTP(w, r)
So(w.Code, ShouldEqual, http.StatusOK)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 3)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 2)
Expand All @@ -1161,8 +1157,8 @@ func TestPutVersionGenerateDownloadsError(t *testing.T) {
GetVersionFunc: func(datasetID string, editionID string, version string, state string) (*models.Version, error) {
return &v, nil
},
CheckDatasetExistsFunc: func(ID string, state string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(ID string, editionID string, state string) error {
return nil
Expand Down Expand Up @@ -1196,8 +1192,8 @@ func TestPutVersionGenerateDownloadsError(t *testing.T) {
Convey("and the expected store calls are made with the expected parameters", func() {
genCalls := mockDownloadGenerator.GenerateCalls()

So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(mockedDataStore.CheckDatasetExistsCalls()[0].ID, ShouldEqual, "123")
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(mockedDataStore.GetDatasetCalls()[0].ID, ShouldEqual, "123")

So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(mockedDataStore.CheckEditionExistsCalls()[0].ID, ShouldEqual, "123")
Expand Down Expand Up @@ -1230,8 +1226,8 @@ func TestPutEmptyVersion(t *testing.T) {
GetVersionFunc: func(datasetID string, editionID string, version string, state string) (*models.Version, error) {
return &v, nil
},
CheckDatasetExistsFunc: func(ID string, state string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(ID string, editionID string, state string) error {
return nil
Expand Down Expand Up @@ -1267,8 +1263,8 @@ func TestPutEmptyVersion(t *testing.T) {
v.Downloads = xlsDownload
return &v, nil
},
CheckDatasetExistsFunc: func(ID string, state string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(ID string, editionID string, state string) error {
return nil
Expand Down Expand Up @@ -1298,9 +1294,8 @@ func TestPutEmptyVersion(t *testing.T) {
})

Convey("and the expected external calls are made with the correct parameters", func() {
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(mockedDataStore.CheckDatasetExistsCalls()[0].ID, ShouldEqual, "123")
So(mockedDataStore.CheckDatasetExistsCalls()[0].State, ShouldEqual, "")
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(mockedDataStore.GetDatasetCalls()[0].ID, ShouldEqual, "123")

So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(mockedDataStore.CheckEditionExistsCalls()[0].ID, ShouldEqual, "123")
Expand Down Expand Up @@ -1340,8 +1335,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return &models.Version{State: models.AssociatedState}, nil
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
}

Expand All @@ -1350,7 +1345,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Body.String(), ShouldEqual, "Failed to parse json body\n")
So(w.Code, ShouldEqual, http.StatusBadRequest)
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 0)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 0)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
})

Expand All @@ -1371,8 +1366,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return nil, errInternal
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
}

Expand All @@ -1381,7 +1376,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusInternalServerError)
So(w.Body.String(), ShouldEqual, "internal error\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 0)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 0)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
})

Expand All @@ -1402,8 +1397,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return &models.Version{}, errs.ErrVersionNotFound
},
CheckDatasetExistsFunc: func(string, string) error {
return errs.ErrDatasetNotFound
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return nil, errs.ErrDatasetNotFound
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand All @@ -1415,7 +1410,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusBadRequest)
So(w.Body.String(), ShouldEqual, "Dataset not found\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 0)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
})
Expand All @@ -1437,8 +1432,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return &models.Version{}, errs.ErrVersionNotFound
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return errs.ErrEditionNotFound
Expand All @@ -1450,7 +1445,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusBadRequest)
So(w.Body.String(), ShouldEqual, "Edition not found\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
})
Expand All @@ -1472,8 +1467,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return &models.Version{}, errs.ErrVersionNotFound
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand All @@ -1488,7 +1483,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusNotFound)
So(w.Body.String(), ShouldEqual, "Version not found\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 2)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 0)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
Expand Down Expand Up @@ -1541,8 +1536,8 @@ func TestPutVersionReturnsError(t *testing.T) {
State: models.PublishedState,
}, nil
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
}

Expand All @@ -1551,7 +1546,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusForbidden)
So(w.Body.String(), ShouldEqual, "unable to update version as it has been published\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 0)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 0)
})

Convey("When the request body is invalid return status bad request", t, func() {
Expand All @@ -1571,8 +1566,8 @@ func TestPutVersionReturnsError(t *testing.T) {
GetVersionFunc: func(string, string, string, string) (*models.Version, error) {
return &models.Version{State: "associated"}, nil
},
CheckDatasetExistsFunc: func(string, string) error {
return nil
GetDatasetFunc: func(datasetID string) (*models.DatasetUpdate, error) {
return &models.DatasetUpdate{}, nil
},
CheckEditionExistsFunc: func(string, string, string) error {
return nil
Expand All @@ -1587,7 +1582,7 @@ func TestPutVersionReturnsError(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusBadRequest)
So(w.Body.String(), ShouldEqual, "Missing collection_id for association between version and a collection\n")
So(len(mockedDataStore.GetVersionCalls()), ShouldEqual, 2)
So(len(mockedDataStore.CheckDatasetExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.GetDatasetCalls()), ShouldEqual, 1)
So(len(mockedDataStore.CheckEditionExistsCalls()), ShouldEqual, 1)
So(len(mockedDataStore.UpdateVersionCalls()), ShouldEqual, 0)
So(len(generatorMock.GenerateCalls()), ShouldEqual, 0)
Expand Down

0 comments on commit 41bb3d7

Please sign in to comment.