Skip to content

Commit

Permalink
add controller code and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tianj7 committed Apr 2, 2024
1 parent 7d23b92 commit 99467e0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
9 changes: 5 additions & 4 deletions controllers/cohortdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (

type CohortDataController struct {
cohortDataModel models.CohortDataI
dataDictionaryModel models.DataDictionary
dataDictionaryModel models.DataDictionaryI
teamProjectAuthz middlewares.TeamProjectAuthzI
}

func NewCohortDataController(cohortDataModel models.CohortDataI, teamProjectAuthz middlewares.TeamProjectAuthzI) CohortDataController {
func NewCohortDataController(cohortDataModel models.CohortDataI, dataDictionaryModel models.DataDictionaryI, teamProjectAuthz middlewares.TeamProjectAuthzI) CohortDataController {
return CohortDataController{
cohortDataModel: cohortDataModel,
teamProjectAuthz: teamProjectAuthz,
cohortDataModel: cohortDataModel,
dataDictionaryModel: dataDictionaryModel,
teamProjectAuthz: teamProjectAuthz,
}
}

Expand Down
8 changes: 4 additions & 4 deletions models/datadictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type DataDictionaryI interface {
GenerateDataDictionary(observationConceptIdsToCheck []int64) ([]*PersonConceptAndValue, error)
GenerateDataDictionary() (*DataDictionaryModel, error)
}

type DataDictionary struct {
Expand Down Expand Up @@ -37,7 +37,7 @@ type DataDictionaryEntry struct {
}

// Generate Data Dictionary Json
func (u DataDictionary) GenerateDataDictionary() (DataDictionaryModel, error) {
func (u DataDictionary) GenerateDataDictionary() (*DataDictionaryModel, error) {

conf := config.GetConfig()
var catchAllCohortId = conf.GetInt("catch_all_cohort_id")
Expand All @@ -56,7 +56,7 @@ func (u DataDictionary) GenerateDataDictionary() (DataDictionaryModel, error) {
defer cancel()
meta_result := query.Scan(&dataDictionaryEntries)
if meta_result.Error != nil {
return dataDictionaryModel, meta_result.Error
return &dataDictionaryModel, meta_result.Error
} else if len(dataDictionaryEntries) == 0 {
log.Printf("INFO: no data dictionary entry found")
} else {
Expand Down Expand Up @@ -110,5 +110,5 @@ func (u DataDictionary) GenerateDataDictionary() (DataDictionaryModel, error) {
}

dataDictionaryModel.Data = dataDictionaryEntries
return dataDictionaryModel, nil
return &dataDictionaryModel, nil
}
4 changes: 2 additions & 2 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewRouter() *gin.Engine {
authorized.POST("/concept-stats/by-source-id/:sourceid/by-cohort-definition-id/:cohortid/breakdown-by-concept-id/:breakdownconceptid/csv", concepts.RetrieveAttritionTable)

// cohort stats and checks:
cohortData := controllers.NewCohortDataController(*new(models.CohortData), middlewares.NewTeamProjectAuthz(*new(models.CohortDefinition), &http.Client{}))
cohortData := controllers.NewCohortDataController(*new(models.CohortData), *new(models.DataDictionary), middlewares.NewTeamProjectAuthz(*new(models.CohortDefinition), &http.Client{}))
// :casecohortid/:controlcohortid are just labels here and have no special meaning. Could also just be :cohortAId/:cohortBId here:
authorized.POST("/cohort-stats/check-overlap/by-source-id/:sourceid/by-cohort-definition-ids/:casecohortid/:controlcohortid", cohortData.RetrieveCohortOverlapStats)

Expand All @@ -57,7 +57,7 @@ func NewRouter() *gin.Engine {
authorized.POST("/histogram/by-source-id/:sourceid/by-cohort-definition-id/:cohortid/by-histogram-concept-id/:histogramid", cohortData.RetrieveHistogramForCohortIdAndConceptId)

// Data Dictionary endpoint
authorized.POST("/cohort-data/data-dictionary", cohortData.RetrieveDataDictionary)
authorized.GET("/cohort-data/data-dictionary", cohortData.RetrieveDataDictionary)

}

Expand Down
39 changes: 37 additions & 2 deletions tests/controllers_tests/controllers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers_tests

import (
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -50,8 +51,8 @@ func tearDown() {
log.Println("teardown for test")
}

var cohortDataController = controllers.NewCohortDataController(*new(dummyCohortDataModel), *new(dummyTeamProjectAuthz))
var cohortDataControllerWithFailingTeamProjectAuthz = controllers.NewCohortDataController(*new(dummyCohortDataModel), *new(dummyFailingTeamProjectAuthz))
var cohortDataController = controllers.NewCohortDataController(*new(dummyCohortDataModel), *new(dummyDataDictionaryModel), *new(dummyTeamProjectAuthz))
var cohortDataControllerWithFailingTeamProjectAuthz = controllers.NewCohortDataController(*new(dummyCohortDataModel), *new(dummyDataDictionaryModel), *new(dummyFailingTeamProjectAuthz))

// instance of the controller that talks to the regular model implementation (that needs a real DB):
var cohortDefinitionControllerNeedsDb = controllers.NewCohortDefinitionController(*new(models.CohortDefinition), *new(dummyTeamProjectAuthz))
Expand Down Expand Up @@ -246,6 +247,25 @@ func (h dummyConceptDataModel) RetrieveBreakdownStatsBySourceIdAndCohortIdAndCon
return conceptBreakdown, nil
}

type dummyDataDictionaryModel struct{}

func (h dummyDataDictionaryModel) GenerateDataDictionary() (*models.DataDictionaryModel, error) {
data := new(models.DataDictionaryModel)
data.Total = 2
entries := []*models.DataDictionaryEntry{
{VocabularyID: "Measurement", ConceptID: 2000006885, ConceptCode: "F", ConceptClassId: "MVP Continuous", NumberOfPeopleWithVariable: 16, NumberOfPeopleWhereValueIsFilled: 15, NumberOfPeopleWhereValueIsNull: 1, ValueStoredAs: "Number", MinValue: 1.16, MaxValue: 9.52, MeanValue: 5.9425, StandardDeviation: 2.3093216897320015, ValueSummary: nil},
{VocabularyID: "Person", ConceptID: 2000007027, ConceptCode: "HARE_CODE", ConceptClassId: "MVP Ordinal", NumberOfPeopleWithVariable: 11, NumberOfPeopleWhereValueIsFilled: 10, NumberOfPeopleWhereValueIsNull: 1, ValueStoredAs: "Concept Id", MinValue: 0, MaxValue: 0, MeanValue: 0, StandardDeviation: 0, ValueSummary: nil},
}
valueSummary1, _ := json.Marshal("[{\"start\":1.159999966621399,\"end\":4.723933216866351,\"nr_persons\":4},{\"start\":4.723933216866351,\"end\":8.287866467111304,\"nr_persons\":7},{\"start\":8.287866467111304,\"end\":11.851799717356256,\"nr_persons\":2}]")
valueSummary2, _ := json.Marshal("[{\"Name\":\"non-Hispanic Black\",\"PersonCount\":4,\"ValueAsString\":\"AFR\",\"ValueAsConceptID\":2000007030},{\"Name\":\"non-Hispanic Asian\",\"PersonCount\":3,\"ValueAsString\":\"ASN\",\"ValueAsConceptID\":2000007029},{\"Name\":\"non-Hispanic White\",\"PersonCount\":2,\"ValueAsString\":\"EUR\",\"ValueAsConceptID\":2000007031},{\"Name\":\"Hispanic\",\"PersonCount\":2,\"ValueAsString\":\"HIS\",\"ValueAsConceptID\":2000007028},{\"Name\":\"\",\"PersonCount\":1,\"ValueAsString\":\"\",\"ValueAsConceptID\":0}]")
entries[0].ValueSummary = valueSummary1
entries[1].ValueSummary = valueSummary2
data.Data = append(data.Data, entries...)
if dummyModelReturnError {
return nil, fmt.Errorf("error!")
}
return data, nil
}
func TestRetrieveHistogramForCohortIdAndConceptIdWithWrongParams(t *testing.T) {
setUp(t)
requestContext := new(gin.Context)
Expand Down Expand Up @@ -1057,3 +1077,18 @@ func TestRetrieveAttritionTable(t *testing.T) {
t.Errorf("Expected request to be aborted")
}
}

func TestRetrieveDataDictionary(t *testing.T) {
setUp(t)
requestContext := new(gin.Context)
requestContext.Writer = new(tests.CustomResponseWriter)
requestContext.Request = new(http.Request)
cohortDataController.RetrieveDataDictionary(requestContext)

result := requestContext.Writer.(*tests.CustomResponseWriter)

if result.StatusCode != 200 {
t.Errorf("Expected request to succeed")
}

}
2 changes: 1 addition & 1 deletion tests/models_tests/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func TestGetDataDictioanry(t *testing.T) {
setUp(t)
dataDictionaryModel.CohortDataModel = cohortDataModel
data, _ := dataDictionaryModel.GenerateDataDictionary()
if data.Total != 3 {
if data.Total != 0 {
t.Errorf("Data Dictionary Generation Failed.")
}
}

0 comments on commit 99467e0

Please sign in to comment.