From 41cb9ebe6c1700b3d310def692eae4356244d03b Mon Sep 17 00:00:00 2001 From: Paul Balogh Date: Sat, 20 Jan 2024 09:06:17 -0600 Subject: [PATCH] Cleaning up configuration and tests Signed-off-by: Paul Balogh --- config.yaml | 2 +- db/place_suite_test.go | 142 ----------------------------------------- db/place_test.go | 6 +- 3 files changed, 2 insertions(+), 148 deletions(-) delete mode 100644 db/place_suite_test.go diff --git a/config.yaml b/config.yaml index 01d3a33..5e1a47d 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,4 @@ -DatabaseURI: "/Users/paul/Develop/weesvc-gorilla/gorm.db" +DatabaseURI: "./gorm.db" # Specify dialect as "postgres" or "sqlite3". "sqlite3" is default. #Dialect: sqlite3 #Verbose: true diff --git a/db/place_suite_test.go b/db/place_suite_test.go deleted file mode 100644 index bd08d79..0000000 --- a/db/place_suite_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package db - -import ( - "context" - "log" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - - "github.com/weesvc/weesvc-gorilla/model" - "github.com/weesvc/weesvc-gorilla/testhelpers" -) - -// Here we're testing each CRUD method independently, but we're using a single container -// for the entire test suite. This _may_ be ok, but you need to be wary of what is changing -// in the database as suite tests may collide. -// -// With this method, we've refactored some container work into the `testhelpers` package. - -// PlaceTestSuite contains shared state amongst all test(s) within the suite. -type PlaceTestSuite struct { - suite.Suite - pgContainer *testhelpers.PostgresContainer - ctx context.Context - placeDb *Database // Reference to our test fixture -} - -// SetupSuite executes prior to any test(s) in order to prepare the shared state. -func (suite *PlaceTestSuite) SetupSuite() { - suite.ctx = context.Background() - pgContainer, err := testhelpers.CreatePostgresContainer(suite.ctx) - if err != nil { - log.Fatal(err) - } - suite.pgContainer = pgContainer - placeDb, err := New(&Config{ - DatabaseURI: pgContainer.ConnectionString, - Dialect: "postgres", - Verbose: true, - }) - if err != nil { - log.Fatal(err) - } - suite.placeDb = placeDb -} - -// TearDownSuite executes cleanup after all test(s) have run. -func (suite *PlaceTestSuite) TearDownSuite() { - if err := suite.pgContainer.Terminate(suite.ctx); err != nil { - log.Fatalf("error terminating postgres container: %s", err) - } -} - -func (suite *PlaceTestSuite) Test_GetPlaces() { - places, err := suite.placeDb.GetPlaces() - if assert.NoError(suite.T(), err) { - assert.Greater(suite.T(), len(places), 0) - } -} - -func (suite *PlaceTestSuite) Test_GetPlaceByID() { - fetchId := uint(6) - place, err := suite.placeDb.GetPlaceByID(fetchId) - if assert.NoError(suite.T(), err) { - assert.Equal(suite.T(), fetchId, place.ID) - assert.Equal(suite.T(), "MIA", place.Name) - assert.Equal(suite.T(), "Miami International Airport, FL, USA", place.Description) - assert.Equal(suite.T(), 25.79516, place.Latitude) - assert.Equal(suite.T(), -80.27959, place.Longitude) - assert.NotNil(suite.T(), place.CreatedAt) - assert.NotNil(suite.T(), place.UpdatedAt) - } -} - -func (suite *PlaceTestSuite) Test_CreatePlace() { - newPlace := &model.Place{ - ID: 20, - Name: "Kerid Crater", - Description: "Kerid Crater, Iceland", - Latitude: 64.04126, - Longitude: -20.88530, - } - err := suite.placeDb.CreatePlace(newPlace) - if assert.NoError(suite.T(), err) { - // Verify our inserted newPlace - created, err := suite.placeDb.GetPlaceByID(newPlace.ID) - if assert.NoError(suite.T(), err) { - assert.Equal(suite.T(), newPlace.ID, created.ID) - assert.Equal(suite.T(), newPlace.Name, created.Name) - assert.Equal(suite.T(), newPlace.Description, created.Description) - assert.Equal(suite.T(), newPlace.Latitude, created.Latitude) - assert.Equal(suite.T(), newPlace.Longitude, created.Longitude) - assert.NotNil(suite.T(), created.CreatedAt) - assert.NotNil(suite.T(), created.UpdatedAt) - } - } -} - -func (suite *PlaceTestSuite) Test_UpdatePlace() { - original, err := suite.placeDb.GetPlaceByID(7) - if assert.NoError(suite.T(), err) { - changes := &model.Place{ - ID: original.ID, - Name: "The Alamo", - Description: "The Alamo, San Antonio, TX, USA", - Latitude: 29.42590, - Longitude: -98.48625, - } - if assert.NoError(suite.T(), suite.placeDb.UpdatePlace(changes)) { - // Verify the updated place - updated, err := suite.placeDb.GetPlaceByID(original.ID) - if assert.NoError(suite.T(), err) { - assert.Equal(suite.T(), original.ID, updated.ID) - assert.Equal(suite.T(), changes.Name, updated.Name) - assert.Equal(suite.T(), changes.Description, updated.Description) - assert.Equal(suite.T(), changes.Latitude, updated.Latitude) - assert.Equal(suite.T(), changes.Longitude, updated.Longitude) - assert.Equal(suite.T(), original.CreatedAt, updated.CreatedAt) - assert.NotEqual(suite.T(), original.UpdatedAt, updated.UpdatedAt) - } - } - } -} - -func (suite *PlaceTestSuite) Test_DeletePlaceByID() { - deleteID := uint(1) - _, err := suite.placeDb.GetPlaceByID(deleteID) - if assert.NoError(suite.T(), err) { - if assert.NoError(suite.T(), suite.placeDb.DeletePlaceByID(deleteID)) { - // Verify no longer retrievable - _, err = suite.placeDb.GetPlaceByID(deleteID) - assert.EqualError(suite.T(), err, "unable to get place: record not found") - } - } -} - -// TestDatabase_TestSuite executes the suite of tests. -func TestDatabase_TestSuite(t *testing.T) { - t.Parallel() - suite.Run(t, new(PlaceTestSuite)) -} diff --git a/db/place_test.go b/db/place_test.go index ae5fbab..d9533ef 100644 --- a/db/place_test.go +++ b/db/place_test.go @@ -11,11 +11,6 @@ import ( "github.com/stretchr/testify/assert" ) -// Here we're testing each CRUD method independently, but we're using a fresh container -// for each test. This _may_ be ok, but could be a bit of overhead for CICD pipelines. -// -// With this method, we've refactored some container work into the `testhelpers` package. - func TestDatabase_GetPlaces(t *testing.T) { t.Parallel() placeDb := setupDatabase(t) @@ -113,6 +108,7 @@ func TestDatabase_DeletePlaceByID(t *testing.T) { } } +// setupDatabase creates an isolated `Database` instance backed by a Postgres Testcontainer. func setupDatabase(t *testing.T) *Database { ctx := context.Background()