Skip to content

Commit a4dd9b9

Browse files
authored
Add cohort details expression to endpoint (#113)
* add cohort details expression to endpoint
1 parent 4f2ab47 commit a4dd9b9

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

models/cohortdefinition.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type CohortDefinition struct {
2626
ExpressionType string `json:",omitempty"`
2727
CreatedById int `json:",omitempty"`
2828
ModifiedById int `json:",omitempty"`
29+
Expression string `json:",omitempty"`
2930
}
3031

3132
type CohortDefinitionStats struct {
@@ -35,11 +36,14 @@ type CohortDefinitionStats struct {
3536
}
3637

3738
func (h CohortDefinition) GetCohortDefinitionById(id int) (*CohortDefinition, error) {
39+
atlasDb := db.GetAtlasDB()
3840
db2 := db.GetAtlasDB().Db
3941
var cohortDefinition *CohortDefinition
4042
query := db2.Model(&CohortDefinition{}).
41-
Select("id, name, description").
42-
Where("id = ?", id)
43+
Select("cohort_definition.id, cohort_definition.name, cohort_definition.description, cohort_definition_details.expression").
44+
Where("cohort_definition.id = ?", id).
45+
Joins("INNER JOIN " + atlasDb.Schema + ".cohort_definition_details ON cohort_definition.id = cohort_definition_details.id")
46+
4347
query, cancel := utils.AddTimeoutToQuery(query)
4448
defer cancel()
4549
meta_result := query.Scan(&cohortDefinition)

tests/models_tests/models_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,13 @@ func TestGetAllCohortDefinitionsAndStatsOrderBySizeDescWhenCohortDefinitionIsMis
722722
firstCohort.Id,
723723
firstCohort.Name,
724724
firstCohort.Name))
725+
726+
tests.ExecAtlasSQLString(fmt.Sprintf("insert into %s.cohort_definition_details (id,expression,hash_code) "+
727+
"values (%d, '{\"expression\" : \"%d\" }', %d)",
728+
db.GetAtlasDB().Schema,
729+
firstCohort.Id,
730+
firstCohort.Id,
731+
555444))
725732
}
726733

727734
func TestGetCohortName(t *testing.T) {
@@ -1022,6 +1029,9 @@ func TestGetCohortDefinitionById(t *testing.T) {
10221029
if allCohortDefinitions[0].Id != foundCohortDefinition.Id {
10231030
t.Errorf("Expected data not found")
10241031
}
1032+
if foundCohortDefinition.Expression != "{\"expression\" : \"1\" }" {
1033+
t.Errorf("Expected expression data not found")
1034+
}
10251035
}
10261036

10271037
func TestRetrieveDataByOriginalCohortAndNewCohort(t *testing.T) {

tests/setup_local_db/ddl_atlas.sql

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ CREATE TABLE atlas.cohort_definition
3838
CONSTRAINT PK_cohort_definition PRIMARY KEY (id)
3939
);
4040

41+
CREATE TABLE atlas.cohort_definition_details
42+
(
43+
id integer NOT NULL ,
44+
expression text NOT NULL,
45+
hash_code integer,
46+
CONSTRAINT PK_cohort_definition_details PRIMARY KEY (id),
47+
CONSTRAINT fk_cohort_definition_details_to_cohort_definition FOREIGN KEY (id)
48+
REFERENCES atlas.cohort_definition (id) MATCH SIMPLE
49+
ON UPDATE NO ACTION
50+
ON DELETE CASCADE
51+
);
52+
4153
CREATE TABLE atlas.sec_role
4254
(
4355
id integer NOT NULL,

tests/setup_local_db/test_data_atlas.sql

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ values
2727
(4,'Test cohort4','Extra Larger cohort')
2828
;
2929

30+
insert into atlas.cohort_definition_details
31+
(id,expression,hash_code)
32+
values
33+
(1,'{"expression" : "1" }',987654321),
34+
(2,'{"expression" : "2" }',1234567890),
35+
(3,'{"expression" : "3" }', 33333445),
36+
(4,'{"expression" : "4" }', 555444),
37+
(32,'{"expression" : "32" }', 323232)
38+
;
39+
3040
insert into atlas.sec_role
3141
(id, name, system_role)
3242
values

0 commit comments

Comments
 (0)