-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add datastore integration tests
- Loading branch information
1 parent
1cb5c2d
commit 3211eb6
Showing
5 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package datastore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
"github.com/rafael-piovesan/go-rocket-ride/entity/audit" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/migrate" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testcontainer" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testfixtures" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tabbed/pqtype" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
"github.com/uptrace/bun/driver/pgdriver" | ||
) | ||
|
||
func TestAuditRecord(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// database up | ||
dsn, terminate, err := testcontainer.NewPostgresContainer() | ||
require.NoError(t, err) | ||
defer terminate(ctx) | ||
|
||
// migrations up | ||
err = migrate.Up(dsn, "db/migrations") | ||
require.NoError(t, err) | ||
|
||
// test fixtures up | ||
userID := int64(gofakeit.Number(0, 1000)) | ||
err = testfixtures.Load(dsn, []string{"db/fixtures/users"}, map[string]interface{}{ | ||
"UserId": userID, | ||
"UserEmail": gofakeit.Email(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// conntect to database | ||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) | ||
db := bun.NewDB(sqldb, pgdialect.New()) | ||
|
||
store := NewStore(db) | ||
|
||
t.Run("Create Audit Record", func(t *testing.T) { | ||
ip := pqtype.CIDR{} | ||
err := ip.Scan(gofakeit.IPv4Address()) | ||
require.NoError(t, err) | ||
|
||
ar := &entity.AuditRecord{ | ||
Action: audit.ActionCreateRide, | ||
Data: []byte("{\"data\": \"foo\"}"), | ||
OriginIP: ip.IPNet.String(), | ||
ResourceID: int64(gofakeit.Number(0, 1000)), | ||
ResourceType: audit.ResourceTypeRide, | ||
UserID: userID, | ||
} | ||
|
||
res, err := store.CreateAuditRecord(ctx, ar) | ||
|
||
if assert.NoError(t, err) { | ||
ar.ID = res.ID | ||
assert.Equal(t, ar, res) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package datastore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
"github.com/rafael-piovesan/go-rocket-ride/entity/idempotency" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/migrate" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testcontainer" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testfixtures" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
"github.com/uptrace/bun/driver/pgdriver" | ||
) | ||
|
||
func TestIdempotencyKey(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// database up | ||
dsn, terminate, err := testcontainer.NewPostgresContainer() | ||
require.NoError(t, err) | ||
defer terminate(ctx) | ||
|
||
// migrations up | ||
err = migrate.Up(dsn, "db/migrations") | ||
require.NoError(t, err) | ||
|
||
// test fixtures up | ||
userID := int64(gofakeit.Number(0, 1000)) | ||
idemKey := gofakeit.UUID() | ||
err = testfixtures.Load(dsn, []string{"db/fixtures/users"}, map[string]interface{}{ | ||
"UserId": userID, | ||
"UserEmail": gofakeit.Email(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// conntect to database | ||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) | ||
db := bun.NewDB(sqldb, pgdialect.New()) | ||
|
||
store := NewStore(db) | ||
|
||
// test entity | ||
now := time.Now() | ||
ttime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC) | ||
ik := &entity.IdempotencyKey{ | ||
IdempotencyKey: idemKey, | ||
LastRunAt: ttime, | ||
LockedAt: &ttime, | ||
RequestMethod: gofakeit.HTTPMethod(), | ||
RequestParams: []byte("{\"data\": \"foo\"}"), | ||
RequestPath: fmt.Sprintf("/%s/%s", gofakeit.AnimalType(), gofakeit.Animal()), | ||
RecoveryPoint: idempotency.RecoveryPointStarted, | ||
UserID: userID, | ||
} | ||
|
||
t.Run("Idempotency Key not found", func(t *testing.T) { | ||
_, err := store.GetIdempotencyKey(ctx, idemKey, userID) | ||
assert.ErrorIs(t, err, sql.ErrNoRows) | ||
}) | ||
|
||
t.Run("Create Idempotency Key", func(t *testing.T) { | ||
res, err := store.CreateIdempotencyKey(ctx, ik) | ||
if assert.NoError(t, err) { | ||
ik.ID = res.ID | ||
assert.Equal(t, ik, res) | ||
} | ||
}) | ||
|
||
t.Run("Update Idempotency Key", func(t *testing.T) { | ||
now = time.Now() | ||
|
||
rps := []idempotency.RecoveryPoint{ | ||
idempotency.RecoveryPointCreated, | ||
idempotency.RecoveryPointCharged, | ||
idempotency.RecoveryPointFinished, | ||
} | ||
|
||
idx := gofakeit.Number(0, len(rps)-1) | ||
|
||
resCode := idempotency.ResponseCodeOK | ||
resBody := idempotency.ResponseBody{Message: "OK"} | ||
|
||
ik.LastRunAt = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC) | ||
ik.LockedAt = nil | ||
ik.RecoveryPoint = rps[idx] | ||
ik.ResponseCode = &resCode | ||
ik.ResponseBody = &resBody | ||
|
||
res, err := store.UpdateIdempotencyKey(ctx, ik) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, ik, res) | ||
} | ||
}) | ||
|
||
t.Run("Get Idempotency Key", func(t *testing.T) { | ||
res, err := store.GetIdempotencyKey(ctx, idemKey, userID) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, ik, res) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package datastore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/rafael-piovesan/go-rocket-ride/entity" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/migrate" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testcontainer" | ||
"github.com/rafael-piovesan/go-rocket-ride/pkg/testfixtures" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
"github.com/uptrace/bun/driver/pgdriver" | ||
) | ||
|
||
func TestRide(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// database up | ||
dsn, terminate, err := testcontainer.NewPostgresContainer() | ||
require.NoError(t, err) | ||
defer terminate(ctx) | ||
|
||
// migrations up | ||
err = migrate.Up(dsn, "db/migrations") | ||
require.NoError(t, err) | ||
|
||
// test fixtures up | ||
userID := int64(gofakeit.Number(0, 1000)) | ||
keyID := int64(gofakeit.Number(0, 1000)) | ||
|
||
err = testfixtures.Load( | ||
dsn, | ||
[]string{ | ||
"db/fixtures/users", | ||
"db/fixtures/idempotency_keys", | ||
}, | ||
map[string]interface{}{ | ||
"UserId": userID, | ||
"UserEmail": gofakeit.Email(), | ||
"KeyId": keyID, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
// conntect to database | ||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) | ||
db := bun.NewDB(sqldb, pgdialect.New()) | ||
|
||
store := NewStore(db) | ||
|
||
// test entity | ||
ride := &entity.Ride{ | ||
IdempotencyKeyID: &keyID, | ||
OriginLat: "0.0000000000", | ||
OriginLon: "0.0000000000", | ||
TargetLat: "0.0000000000", | ||
TargetLon: "0.0000000000", | ||
UserID: userID, | ||
} | ||
|
||
t.Run("Ride not found", func(t *testing.T) { | ||
_, err := store.GetRideByIdempotencyKeyID(ctx, keyID) | ||
assert.ErrorIs(t, err, sql.ErrNoRows) | ||
}) | ||
|
||
t.Run("Create Ride", func(t *testing.T) { | ||
res, err := store.CreateRide(ctx, ride) | ||
if assert.NoError(t, err) { | ||
ride.ID = res.ID | ||
assert.Equal(t, ride, res) | ||
} | ||
}) | ||
|
||
t.Run("Update Ride", func(t *testing.T) { | ||
stripeID := gofakeit.UUID() | ||
ride.StripeChargeID = &stripeID | ||
|
||
res, err := store.UpdateRide(ctx, ride) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, ride, res) | ||
} | ||
}) | ||
|
||
t.Run("Get Ride By Idempotency Key ID", func(t *testing.T) { | ||
res, err := store.GetRideByIdempotencyKeyID(ctx, keyID) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, ride, res) | ||
} | ||
}) | ||
} |
Oops, something went wrong.