Skip to content

Commit

Permalink
moved all code needed for testing the custom gorm type ConceptType in…
Browse files Browse the repository at this point in the history
…to the test functions

previous test code was forcing the RetriveAllBySourceId to query both concept_class_id next to the mapping for modified ConceptType
  • Loading branch information
pieterlukasse committed May 11, 2022
1 parent 9092888 commit 4001cc3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
41 changes: 20 additions & 21 deletions models/concept.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ type ConceptI interface {
RetrieveBreakdownStatsBySourceIdAndCohortIdAndConceptIds(sourceId int, cohortDefinitionId int, filterConceptIds []int, breakdownConceptId int) ([]*ConceptBreakdown, error)
}
type Concept struct {
ConceptId int `json:"concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptClassId string `json:"concept_class_id"`
ConceptType ConceptType `json:"concept_type"`
ConceptId int `json:"concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptType ConceptType `json:"concept_type"`
}

type ConceptAndPersonsWithDataStats struct {
Expand All @@ -27,23 +26,23 @@ type ConceptAndPersonsWithDataStats struct {
}

type ConceptStats struct {
ConceptId int `json:"concept_id"`
PrefixedConceptId string `json:"prefixed_concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptType string `json:"concept_type"`
CohortSize int `json:"cohort_size"`
NmissingRatio float32 `json:"n_missing_ratio"`
ConceptId int `json:"concept_id"`
PrefixedConceptId string `json:"prefixed_concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptType ConceptType `json:"concept_type"`
CohortSize int `json:"cohort_size"`
NmissingRatio float32 `json:"n_missing_ratio"`
}

type ConceptSimple struct {
ConceptId int `json:"concept_id"`
PrefixedConceptId string `json:"prefixed_concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptType string `json:"concept_type"`
ConceptId int `json:"concept_id"`
PrefixedConceptId string `json:"prefixed_concept_id"`
ConceptName string `json:"concept_name"`
DomainId string `json:"domain_id"`
DomainName string `json:"domain_name"`
ConceptType ConceptType `json:"concept_type"`
}

type ConceptBreakdown struct {
Expand All @@ -60,7 +59,7 @@ func (h Concept) RetriveAllBySourceId(sourceId int) ([]*Concept, error) {

var concepts []*Concept
result := omopDataSource.Db.Model(&Concept{}).
Select("concept_id, concept_name, domain.domain_id, domain.domain_name, concept_class_id, concept_class_id as concept_type").
Select("concept_id, concept_name, domain.domain_id, domain.domain_name, concept_class_id as concept_type").
Joins("INNER JOIN " + omopDataSource.Schema + ".domain as domain ON concept.domain_id = domain.domain_id").
Order("concept_name").
Scan(&concepts)
Expand Down
49 changes: 47 additions & 2 deletions tests/models_tests/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,34 @@ func TestRetriveAllBySourceId(t *testing.T) {
if len(concepts) != 4 {
t.Errorf("Found %d", len(concepts))
}
if concepts[0].ConceptType == "?" {
t.Errorf("Expected ConceptType value, Found '?' as value instead'")
if strings.Contains(string(concepts[0].ConceptType), "unexpected missing value") {
t.Errorf("Expected ConceptType value, Found 'missing value' instead'")
}
if strings.Contains(string(concepts[0].ConceptType), "_") || len(concepts[0].ConceptType) == 0 {
t.Errorf("Expected ConceptType value to be length > 0 and not contain _, but found %s", concepts[0].ConceptType)
}
}

type TestConceptFields struct {
ConceptClassId string
ConceptType models.ConceptType
}

func TestCustomGormDataTypeConceptType(t *testing.T) {
setUp(t)
var concepts []*TestConceptFields
omopDataSource := tests.GetOmopDataSource()
// in this test we want to check if the custom ConceptType field is indeed a subset of concept_class_id,
// so we query both concept_class_id and "concept_class_id as concept_type" (this last one should result in transformed
// values after Scan() is done):
omopDataSource.Db.Model(&models.Concept{}).
Select("concept_class_id, concept_class_id as concept_type").
Scan(&concepts)

if len(concepts) != 4 {
t.Errorf("Found %d", len(concepts))
}

// in the current version we extract the concept type from concept class id (see models/customgormtypes.go). This test
// ensures this is the case:
for _, concept := range concepts {
Expand All @@ -118,6 +140,29 @@ func TestRetriveAllBySourceId(t *testing.T) {
}
}

func TestCustomGormDataTypeConceptTypeWrongMapping(t *testing.T) {
setUp(t)
var concepts []*TestConceptFields
omopDataSource := tests.GetOmopDataSource()
// in this test we want to check if the custom ConceptType field contains an error if the mapping
// is made from another field that does not contain the expected format.
omopDataSource.Db.Model(&models.Concept{}).
Select("concept_class_id, concept_name as concept_type").
Scan(&concepts)

if len(concepts) != 4 {
t.Errorf("Found %d", len(concepts))
}

// in the current version we extract the concept type from concept class id (see models/customgormtypes.go). This test
// ensures this is the case:
for _, concept := range concepts {
if !strings.Contains(string(concept.ConceptType), "unexpected missing value") {
t.Errorf("The ConceptType should contain 'missing value' error in this case")
}
}
}

func TestRetrieveStatsBySourceIdAndCohortIdAndConceptIds(t *testing.T) {
setUp(t)
conceptsStats, _ := conceptModel.RetrieveStatsBySourceIdAndCohortIdAndConceptIds(testSourceId,
Expand Down
8 changes: 8 additions & 0 deletions tests/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/uc-cdis/cohort-middleware/db"
"github.com/uc-cdis/cohort-middleware/models"
"github.com/uc-cdis/cohort-middleware/utils"
)

func GetTestSourceId() int {
Expand Down Expand Up @@ -58,6 +59,12 @@ func ExecSQLString(sqlString string, sourceId int) {
}
}

func GetOmopDataSource() *utils.DbAndSchema {
var dataSourceModel = new(models.Source)
omopDataSource := dataSourceModel.GetDataSource(GetTestSourceId(), models.Omop)
return omopDataSource
}

func GetSchemaNameForType(sourceType models.SourceType) string {
sourceModel := new(models.Source)
dbSchema, _ := sourceModel.GetSourceSchemaNameBySourceIdAndSourceType(GetTestSourceId(), sourceType)
Expand All @@ -84,6 +91,7 @@ func GetIntAttributeValue[T any](item T, attributeName string) int {

// returns an int array with the attribute values of the given attribute
// for each item in "items" array.
// TODO - can also simply be done wit something like: db.Model(&users).Pluck("age", &ages), where var ages []int64
func MapIntAttr[T any](items []T, attributeName string) []int {
result := make([]int, len(items))
for i := range items {
Expand Down

0 comments on commit 4001cc3

Please sign in to comment.