|
1 | 1 | package models_tests
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "log"
|
5 | 6 | "os"
|
6 | 7 | "strings"
|
@@ -609,6 +610,34 @@ func TestGetAllCohortDefinitionsAndStatsOrderBySizeDesc(t *testing.T) {
|
609 | 610 | }
|
610 | 611 | }
|
611 | 612 |
|
| 613 | +// Tests whether the code deals correctly with the (error) situation where |
| 614 | +// the `cohort_definition` and `cohort` tables are not in sync (more specifically |
| 615 | +// the situation where a cohort still exists in `cohort` table but not in `cohort_definition`). |
| 616 | +func TestGetAllCohortDefinitionsAndStatsOrderBySizeDescWhenCohortDefinitionIsMissing(t *testing.T) { |
| 617 | + setUp(t) |
| 618 | + cohortDefinitions, _ := cohortDefinitionModel.GetAllCohortDefinitionsAndStatsOrderBySizeDesc(testSourceId) |
| 619 | + if len(cohortDefinitions) != len(allCohortDefinitions) { |
| 620 | + t.Errorf("Found %d", len(cohortDefinitions)) |
| 621 | + } |
| 622 | + |
| 623 | + // remove one cohort_definition record and verify that the list is now indeed smaller: |
| 624 | + firstCohort := cohortDefinitions[0] |
| 625 | + tests.ExecAtlasSQLString(fmt.Sprintf("delete from %s.cohort_definition where id = %d", |
| 626 | + db.GetAtlasDB().Schema, firstCohort.Id)) |
| 627 | + cohortDefinitions, _ = cohortDefinitionModel.GetAllCohortDefinitionsAndStatsOrderBySizeDesc(testSourceId) |
| 628 | + if len(cohortDefinitions) != len(allCohortDefinitions)-1 { |
| 629 | + t.Errorf("Number of cohor_definition records expected to be %d, found %d", |
| 630 | + len(allCohortDefinitions)-1, len(cohortDefinitions)) |
| 631 | + } |
| 632 | + // restore: |
| 633 | + tests.ExecAtlasSQLString(fmt.Sprintf("insert into %s.cohort_definition (id,name,description) "+ |
| 634 | + "values (%d, '%s', '%s')", |
| 635 | + db.GetAtlasDB().Schema, |
| 636 | + firstCohort.Id, |
| 637 | + firstCohort.Name, |
| 638 | + firstCohort.Name)) |
| 639 | +} |
| 640 | + |
612 | 641 | func TestGetCohortName(t *testing.T) {
|
613 | 642 | setUp(t)
|
614 | 643 | allCohortDefinitions, _ := cohortDefinitionModel.GetAllCohortDefinitions()
|
|
0 commit comments