Skip to content

Commit

Permalink
Add a table for compliance results
Browse files Browse the repository at this point in the history
This commit adds the basic schema we've outlined in
#84 (comment)
Future patches might include additional columns we need, depending on
how this is used.

Fixes #31
  • Loading branch information
rhmdnd committed Aug 3, 2022
1 parent 059c06d commit 4b75e3b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions migrations/000006_create_results_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS results;
13 changes: 13 additions & 0 deletions migrations/000006_create_results_table.up.sql
Original file line number Diff line number Diff line change
@@ -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)
);
102 changes: 100 additions & 2 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -456,14 +456,62 @@ 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()

if err := m.Migrate(4); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
metadataID, err := insertMetadata()
if err != nil {
Expand All @@ -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)
}
25 changes: 25 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 4b75e3b

Please sign in to comment.