Skip to content

Commit

Permalink
WIP: Implement database tests for subject table
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
rhmdnd committed Jul 12, 2022
1 parent 5fc8793 commit 050bdb4
Show file tree
Hide file tree
Showing 25 changed files with 1,226 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/aws/aws-sdk-go v1.44.22
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/lib/pq v1.10.6
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.4
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
2 changes: 1 addition & 1 deletion migrations/000001_create_subject_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS subjects (
id serial PRIMARY KEY,
id uuid PRIMARY KEY,
name VARCHAR(255),
type VARCHAR(50)
);
7 changes: 7 additions & 0 deletions tests/integration/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package integration_test

type Subject struct {
ID string
Name string
Type string
}
68 changes: 64 additions & 4 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"errors"
"fmt"
"net"
"strings"
"testing"
"time"

migrate "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/google/uuid"
_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
gorm_postgres "gorm.io/driver/postgres"
"gorm.io/gorm"
)

func getDatabaseConnection(t *testing.T) *sql.DB {
Expand Down Expand Up @@ -66,15 +70,71 @@ func getMigrationHelper(t *testing.T) *migrate.Migrate {
return m
}

func getGormHelper() *gorm.DB {
connStr := "host=localhost user=dbadmin dbname=compliance password=secret port=5432 sslmode=disable"
gormDB, _ := gorm.Open(gorm_postgres.Open(connStr), &gorm.Config{})
return gormDB
}

func getUUIDString() string {
value, _ := uuid.NewRandom()
return value.String()
}

// Each test assumes the database is unmanaged. The test is responsible for
// setting up the state it requires for its test logic. This keeps the
// getMigrationHelper() method clean of any assumptions about what the tests
// expect from it.
func TestSubject(t *testing.T) { // nolint:paralleltest // database tests should not run in parallel
getMigrationHelper(t)
// m.Up()
// Test logic
// m.Down()
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
name := "cluster.example.com"
subjectTypeStr := getUUIDString()

s := Subject{ID: id, Name: name, Type: subjectTypeStr}
gormDB := getGormHelper()
gormDB.Create(&s)

subject := Subject{}
gormDB.First(&subject, "id = ?", id)

expectedSubject := Subject{ID: id, Name: name, Type: subjectTypeStr}
assert.Equal(t, expectedSubject.ID, subject.ID, "expected %s got %s", expectedSubject.ID, subject.ID)
assert.Equal(t, expectedSubject.Name, subject.Name, "expected %s got %s", expectedSubject.Name, subject.Name)
assert.Equal(t, expectedSubject.Type, subject.Type, "expected %s got %s", expectedSubject.Type, subject.Type)

if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
if err := m.Down(); err != nil {
t.Fatalf("Unable to downgrade database: %s", err)
}
}

func TestSubjectFailsWithLongName(t *testing.T) { // nolint:paralleltest // database tests should not run in parallel
m := getMigrationHelper(t)
if err := m.Up(); err != nil {
t.Fatalf("Unable to upgrade database: %s", err)
}

id := getUUIDString()
name := strings.Repeat("a", 256)
subjectTypeStr := getUUIDString()

s := Subject{ID: id, Name: name, Type: subjectTypeStr}
gormDB := getGormHelper()
err := gormDB.Create(&s).Error
assert.NotEmpty(t, err, "Shouldn't be able to insert values longer than 255 characters")
if err := m.Drop(); err != nil {
t.Fatalf("Unable to drop database: %s", err)
}
if err := m.Down(); err != nil {
t.Fatalf("Unable to downgrade database: %s", err)
}
}

func TestMigration(t *testing.T) { // nolint:paralleltest // database tests should not run in parallel
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/google/uuid/marshal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 050bdb4

Please sign in to comment.