@@ -281,8 +281,8 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
281
281
query = models .QueryFilterByCohortPairsHelper (filterCohortPairs , resultsDataSource , population .Id , "unionAndIntersect" ).
282
282
Select ("subject_id" )
283
283
_ = query .Scan (& subjectIds )
284
- // in this case we expect overlap the size of the largestCohort-5 :
285
- if len (subjectIds ) != (largestCohort .CohortSize - 5 ) {
284
+ // in this case we expect overlap the size of the largestCohort-6 (where 6 is the size of the overlap between extendedCopyOfSecondLargestCohort and largestCohort) :
285
+ if len (subjectIds ) != (largestCohort .CohortSize - 6 ) {
286
286
t .Errorf ("Expected %d overlap, found %d" , largestCohort .CohortSize - 5 , len (subjectIds ))
287
287
}
288
288
@@ -304,7 +304,7 @@ func TestQueryFilterByCohortPairsHelper(t *testing.T) {
304
304
Select ("subject_id" )
305
305
_ = query .Scan (& subjectIds )
306
306
// in this case we expect same as previous test above:
307
- if len (subjectIds ) != (largestCohort .CohortSize - 5 ) {
307
+ if len (subjectIds ) != (largestCohort .CohortSize - 6 ) {
308
308
t .Errorf ("Expected %d overlap, found %d" , largestCohort .CohortSize - 5 , len (subjectIds ))
309
309
}
310
310
@@ -495,8 +495,10 @@ func TestRetrieveBreakdownStatsBySourceIdAndCohortIdAndConceptIdsAndCohortPairsW
495
495
if len (stats ) > len (stats2 ) {
496
496
t .Errorf ("First query is more restrictive, so its stats should not be larger than stats2 of second query. Got %d and %d" , len (stats ), len (stats2 ))
497
497
}
498
- // test filtering with smallest cohort, lenght should be 1, since that's the size of the smallest cohort:
499
- // setting the same cohort id here (artificial...normally it should be two different ids):
498
+
499
+ // test filtering with secondLargestCohort, smallest and largestCohort.
500
+ // Lenght of result set should be 2 persons (one HIS, one ASN), since there is a overlap of 1 between secondLargestCohort and smallest cohort,
501
+ // and overlap of 2 between secondLargestCohort and largestCohort, BUT only 1 has a HARE value:
500
502
filterCohortPairs = []utils.CustomDichotomousVariableDef {
501
503
{
502
504
CohortDefinitionId1 : smallestCohort .Id ,
@@ -506,7 +508,14 @@ func TestRetrieveBreakdownStatsBySourceIdAndCohortIdAndConceptIdsAndCohortPairsW
506
508
stats3 , _ := conceptModel .RetrieveBreakdownStatsBySourceIdAndCohortIdAndConceptIdsAndCohortPairs (testSourceId ,
507
509
secondLargestCohort .Id , filterIds , filterCohortPairs , breakdownConceptId )
508
510
if len (stats3 ) != 2 {
509
- t .Errorf ("Expected only two items in resultset, found %d" , len (stats ))
511
+ t .Errorf ("Expected only two items in resultset, found %d" , len (stats3 ))
512
+ }
513
+ countPersons := 0
514
+ for _ , stat := range stats3 {
515
+ countPersons = countPersons + stat .NpersonsInCohortWithValue
516
+ }
517
+ if countPersons != 2 {
518
+ t .Errorf ("Expected only two persons in resultset, found %d" , countPersons )
510
519
}
511
520
}
512
521
@@ -731,9 +740,9 @@ func TestRetrieveHistogramDataBySourceIdAndCohortIdAndConceptIdsAndCohortPairs(t
731
740
filterConceptIds := []int64 {}
732
741
filterCohortPairs := []utils.CustomDichotomousVariableDef {}
733
742
data , _ := cohortDataModel .RetrieveHistogramDataBySourceIdAndCohortIdAndConceptIdsAndCohortPairs (testSourceId , largestCohort .Id , histogramConceptId , filterConceptIds , filterCohortPairs )
734
- // everyone in the largestCohort has the histogramConceptId:
735
- if len (data ) != largestCohort .CohortSize {
736
- t .Errorf ("expected 10 or more histogram data but got %d" , len (data ))
743
+ // everyone in the largestCohort has the histogramConceptId, but one person has NULL in the value_as_number :
744
+ if len (data ) != largestCohort .CohortSize - 1 {
745
+ t .Errorf ("expected %d histogram data but got %d" , largestCohort . CohortSize , len (data ))
737
746
}
738
747
739
748
// now filter on the extendedCopyOfSecondLargestCohort
@@ -766,15 +775,15 @@ func TestQueryFilterByConceptIdsHelper(t *testing.T) {
766
775
// Subtest1: correct alias "observation":
767
776
query := omopDataSource .Db .Table (omopDataSource .Schema + ".observation_continuous as observation" + omopDataSource .GetViewDirective ()).
768
777
Select ("observation.person_id" )
769
- query = models .QueryFilterByConceptIdsHelper (query , testSourceId , filterConceptIds , omopDataSource , "" , "observation" )
778
+ query = models .QueryFilterByConceptIdsHelper (query , testSourceId , filterConceptIds , omopDataSource , "" , "observation.person_id " )
770
779
meta_result := query .Scan (& personIds )
771
780
if meta_result .Error != nil {
772
781
t .Errorf ("Did NOT expect an error" )
773
782
}
774
783
// Subtest2: incorrect alias "observation"...should fail:
775
784
query = omopDataSource .Db .Table (omopDataSource .Schema + ".observation_continuous as observationWRONG" ).
776
785
Select ("*" )
777
- query = models .QueryFilterByConceptIdsHelper (query , testSourceId , filterConceptIds , omopDataSource , "" , "observation" )
786
+ query = models .QueryFilterByConceptIdsHelper (query , testSourceId , filterConceptIds , omopDataSource , "" , "observation.person_id " )
778
787
meta_result = query .Scan (& personIds )
779
788
if meta_result .Error == nil {
780
789
t .Errorf ("Expected an error" )
@@ -850,14 +859,14 @@ func TestErrorForRetrieveDataBySourceIdAndCohortIdAndConceptIdsOrderedByPersonId
850
859
tests .FixSomething (models .Results , "cohort" , "cohort_definition_id" )
851
860
}
852
861
853
- func TestRetrieveCohortOverlapStatsWithoutFilteringOnConceptValue (t * testing.T ) {
862
+ func TestRetrieveCohortOverlapStats (t * testing.T ) {
854
863
// Tests if we get the expected overlap
855
864
setUp (t )
856
865
caseCohortId := secondLargestCohort .Id
857
866
controlCohortId := secondLargestCohort .Id // to ensure we get some overlap, just repeat the same here...
858
867
otherFilterConceptIds := []int64 {}
859
868
filterCohortPairs := []utils.CustomDichotomousVariableDef {}
860
- stats , _ := cohortDataModel .RetrieveCohortOverlapStatsWithoutFilteringOnConceptValue (testSourceId , caseCohortId , controlCohortId ,
869
+ stats , _ := cohortDataModel .RetrieveCohortOverlapStats (testSourceId , caseCohortId , controlCohortId ,
861
870
otherFilterConceptIds , filterCohortPairs )
862
871
// basic test:
863
872
if stats .CaseControlOverlap != int64 (secondLargestCohort .CohortSize ) {
@@ -873,11 +882,11 @@ func TestRetrieveCohortOverlapStatsWithoutFilteringOnConceptValue(t *testing.T)
873
882
CohortDefinitionId2 : extendedCopyOfSecondLargestCohort .Id ,
874
883
ProvidedName : "test" },
875
884
}
876
- // then we expect overlap of 5 for extendedCopyOfSecondLargestCohort and largestCohort:
877
- stats , _ = cohortDataModel .RetrieveCohortOverlapStatsWithoutFilteringOnConceptValue (testSourceId , caseCohortId , controlCohortId ,
885
+ // then we expect overlap of 6 for extendedCopyOfSecondLargestCohort and largestCohort:
886
+ stats , _ = cohortDataModel .RetrieveCohortOverlapStats (testSourceId , caseCohortId , controlCohortId ,
878
887
otherFilterConceptIds , filterCohortPairs )
879
- if stats .CaseControlOverlap != 5 {
880
- t .Errorf ("Expected nr persons to be %d, found %d" , 5 , stats .CaseControlOverlap )
888
+ if stats .CaseControlOverlap != 6 {
889
+ t .Errorf ("Expected nr persons to be %d, found %d" , 6 , stats .CaseControlOverlap )
881
890
}
882
891
883
892
// extra test: different parameters that should return the same as above ^:
@@ -886,10 +895,10 @@ func TestRetrieveCohortOverlapStatsWithoutFilteringOnConceptValue(t *testing.T)
886
895
filterCohortPairs = []utils.CustomDichotomousVariableDef {}
887
896
otherFilterConceptIds = []int64 {histogramConceptId } // extra filter, to cover this part of the code...
888
897
// then we expect overlap of 5 for extendedCopyOfSecondLargestCohort and largestCohort (the filter on histogramConceptId should not matter
889
- // since all in largestCohort have an observation for this concept id):
890
- stats2 , _ := cohortDataModel .RetrieveCohortOverlapStatsWithoutFilteringOnConceptValue (testSourceId , caseCohortId , controlCohortId ,
898
+ // since all in largestCohort have an observation for this concept id except one person who has it but has value_as_number as NULL ):
899
+ stats2 , _ := cohortDataModel .RetrieveCohortOverlapStats (testSourceId , caseCohortId , controlCohortId ,
891
900
otherFilterConceptIds , filterCohortPairs )
892
- if stats2 .CaseControlOverlap != stats .CaseControlOverlap {
901
+ if stats2 .CaseControlOverlap != stats .CaseControlOverlap - 1 {
893
902
t .Errorf ("Expected nr persons to be %d, found %d" , stats .CaseControlOverlap , stats2 .CaseControlOverlap )
894
903
}
895
904
@@ -898,7 +907,7 @@ func TestRetrieveCohortOverlapStatsWithoutFilteringOnConceptValue(t *testing.T)
898
907
otherFilterConceptIds = []int64 {histogramConceptId , dummyContinuousConceptId }
899
908
// all other arguments are the same as test above, and we expect overlap of 0, showing the otherFilterConceptIds
900
909
// had the expected effect:
901
- stats3 , _ := cohortDataModel .RetrieveCohortOverlapStatsWithoutFilteringOnConceptValue (testSourceId , caseCohortId , controlCohortId ,
910
+ stats3 , _ := cohortDataModel .RetrieveCohortOverlapStats (testSourceId , caseCohortId , controlCohortId ,
902
911
otherFilterConceptIds , filterCohortPairs )
903
912
if stats3 .CaseControlOverlap != 0 {
904
913
t .Errorf ("Expected nr persons to be 0, found %d" , stats3 .CaseControlOverlap )
@@ -1014,3 +1023,17 @@ func TestAddTimeoutToQuery(t *testing.T) {
1014
1023
t .Errorf ("Expected result and NO error" )
1015
1024
}
1016
1025
}
1026
+
1027
+ func TestPersonConceptAndCountString (t * testing.T ) {
1028
+ a := models.PersonConceptAndCount {
1029
+ PersonId : 1 ,
1030
+ ConceptId : 2 ,
1031
+ Count : 3 ,
1032
+ }
1033
+
1034
+ expected := "(person_id=1, concept_id=2, count=3)"
1035
+ if a .String () != expected {
1036
+ t .Errorf ("Expected %s, found %s" , expected , a .String ())
1037
+ }
1038
+
1039
+ }
0 commit comments