diff --git a/migrations/000006_create_results_table.down.sql b/migrations/000006_create_results_table.down.sql new file mode 100644 index 00000000..e2882ffc --- /dev/null +++ b/migrations/000006_create_results_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS results; diff --git a/migrations/000006_create_results_table.up.sql b/migrations/000006_create_results_table.up.sql new file mode 100644 index 00000000..aebea81d --- /dev/null +++ b/migrations/000006_create_results_table.up.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS results ( + id uuid PRIMARY KEY, + name VARCHAR(255), + control VARCHAR(255), + outcome VARCHAR(255), + instruction TEXT, + metadata_id UUID, + subject_id UUID, + assessment_id UUID, + CONSTRAINT fk_results_metadata_id FOREIGN KEY (metadata_id) REFERENCES metadata (id), + CONSTRAINT fk_results_subject_id FOREIGN KEY (subject_id) REFERENCES subjects (id), + CONSTRAINT fk_results_assessment_id FOREIGN KEY (assessment_id) REFERENCES assessments (id) +); diff --git a/tests/integration_test.go b/tests/integration_test.go index 1c692f60..419b536b 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -303,7 +303,7 @@ func TestMigration(t *testing.T) { // nolint:paralleltest // database tests shou // Upgrade the database and make sure all upgrades apply cleanly. err = m.Up() version, dirty, _ = m.Version() - expectedVersion = uint(5) + expectedVersion = uint(6) assert.Equal(t, expectedVersion, version, "Database version mismatch: want %d but got %d", expectedVersion, version) assert.Equal(t, false, dirty, "Database state mismatch: want %t but got %t", false, dirty) assert.Equal(t, err, nil, "Error upgrading the database: %s", err) @@ -456,6 +456,55 @@ func TestAssessmentMigration(t *testing.T) { // nolint:paralleltest // database assert.False(t, result, "Table exist: %s", tableName) } +func TestResultMigration(t *testing.T) { // nolint:paralleltest // database tests should run serially + m := getMigrationHelper(t) + gormDB := getGormHelper() + tableName := "results" + + if err := m.Migrate(5); err != nil { + t.Fatalf("Unable to upgrade database: %s", err) + } + + // Ensure the metadata table doesn't exist before the upgrade + result := gormDB.Migrator().HasTable(tableName) + assert.False(t, result, "Table exists prior to migration: %s", tableName) + + // Ensure the table exists after running the migration + if err := m.Migrate(6); err != nil { + t.Fatalf("Unable to upgrade database: %s", err) + } + + result = gormDB.Migrator().HasTable(tableName) + assert.True(t, result, "Table doesn't exist: %s", tableName) + + // Check to make sure it has the expected columns + type results struct{} + columns := []string{"id", "name", "control", "outcome", "instruction", "metadata_id", "subject_id", "assessment_id"} + for _, s := range columns { + result = gormDB.Migrator().HasColumn(&results{}, s) + assert.True(t, result, "Column doesn't exist: %s", s) + } + + constraintName := "fk_results_metadata_id" + result = gormDB.Migrator().HasConstraint(&results{}, constraintName) + assert.True(t, result, "Table doesn't have constraint: %s", constraintName) + + constraintName = "fk_results_subject_id" + result = gormDB.Migrator().HasConstraint(&results{}, constraintName) + assert.True(t, result, "Table doesn't have constraint: %s", constraintName) + + constraintName = "fk_results_assessment_id" + result = gormDB.Migrator().HasConstraint(&results{}, constraintName) + assert.True(t, result, "Table doesn't have constraint: %s", constraintName) + + // Ensure the table is removed on downgrade + if err := m.Migrate(5); err != nil { + t.Fatalf("Unable to upgrade database: %s", err) + } + result = gormDB.Migrator().HasTable(tableName) + assert.False(t, result, "Table exists after downgrade: %s", tableName) +} + func TestInsertAssessmentSucceeds(t *testing.T) { // nolint:paralleltest // database tests should run serially m := getMigrationHelper(t) gormDB := getGormHelper() @@ -463,7 +512,6 @@ func TestInsertAssessmentSucceeds(t *testing.T) { // nolint:paralleltest // data if err := m.Migrate(4); err != nil { t.Fatalf("Unable to upgrade database: %s", err) } - id := getUUIDString() metadataID, err := insertMetadata() if err != nil { @@ -483,3 +531,53 @@ func TestInsertAssessmentSucceeds(t *testing.T) { // nolint:paralleltest // data assert.Equal(t, e.Name, a.Name, "expected %s got %s", e.Name, a.Name) assert.Equal(t, e.MetadataID, a.MetadataID, "expected %s got %s", e.MetadataID, a.MetadataID) } + +func TestInsertResultSucceeds(t *testing.T) { // nolint:paralleltest // database tests should run serially + m := getMigrationHelper(t) + gormDB := getGormHelper() + + if err := m.Migrate(6); err != nil { + t.Fatalf("Unable to upgrade database: %s", err) + } + + metadataID, err := insertMetadata() + if err != nil { + t.Fatalf("Unable to create necessary metadata: %s", err) + } + subjectID, err := insertSubject() + if err != nil { + t.Fatalf("Unable to create necessary subject: %s", err) + } + + id := getUUIDString() + name := getUUIDString() + control := getUUIDString() + outcome := getUUIDString() + instruction := strings.Repeat(getUUIDString(), 100) + + r := Result{ + ID: id, Name: name, Control: control, + Outcome: outcome, Instruction: instruction, + MetadataID: metadataID, SubjectID: subjectID, + } + + err = gormDB.Create(&r).Error + assert.Nil(t, err) + + a := Result{} + gormDB.First(&a, "id = ?", id) + + e := Result{ + ID: id, Name: name, Control: control, + Outcome: outcome, Instruction: instruction, + MetadataID: metadataID, SubjectID: subjectID, + } + + assert.Equal(t, e.ID, a.ID, "expected %s got %s", e.ID, a.ID) + assert.Equal(t, e.Name, a.Name, "expected %s got %s", e.Name, a.Name) + assert.Equal(t, e.Control, a.Control, "expected %s got %s", e.Control, a.Control) + assert.Equal(t, e.Outcome, a.Outcome, "expected %s got %s", e.Outcome, a.Outcome) + assert.Equal(t, e.Instruction, a.Instruction, "expected %s got %s", e.Instruction, a.Instruction) + assert.Equal(t, e.MetadataID, a.MetadataID, "expected %s got %s", e.MetadataID, a.MetadataID) + assert.Equal(t, e.SubjectID, a.SubjectID, "expected %s got %s", e.SubjectID, a.SubjectID) +} diff --git a/tests/utils.go b/tests/utils.go index 2e1647b7..5ec4f175 100644 --- a/tests/utils.go +++ b/tests/utils.go @@ -37,6 +37,16 @@ type Subject struct { MetadataID sql.NullString } +type Result struct { + ID string + Name string + Control string + Outcome string + Instruction string + MetadataID string + SubjectID string +} + func getDatabaseConnection(t *testing.T) *sql.DB { t.Helper() // Generlize this so that it can be used to connect to any Postgres @@ -127,3 +137,18 @@ func insertMetadata() (string, error) { return id, nil } + +func insertSubject() (string, error) { + gormDB := getGormHelper() + + id := getUUIDString() + name := getUUIDString() + subjectTypeStr := getUUIDString() + + s := Subject{ID: id, Name: name, Type: subjectTypeStr} + if err := gormDB.Create(&s).Error; err != nil { + return "", err + } + + return id, nil +}