Created
March 17, 2024 20:22
-
-
Save adoublef/f4275c21f99b9b62e75fb6cacdee965e to your computer and use it in GitHub Desktop.
Testcontainers
This file contains hidden or 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
package ccdb | |
import ( | |
"context" | |
"fmt" | |
tc "github.com/testcontainers/testcontainers-go" | |
"github.com/testcontainers/testcontainers-go/wait" | |
) | |
type CockroachDBContainer struct { | |
tc.Container | |
URI string | |
} | |
func NewCockroachDBContainer(ctx context.Context) (*CockroachDBContainer, error) { | |
req := tc.ContainerRequest{ | |
Image: "cockroachdb/cockroach:latest-v21.1", | |
ExposedPorts: []string{"26257/tcp", "8080/tcp"}, | |
WaitingFor: wait.ForHTTP("/health").WithPort("8080"), | |
Cmd: []string{"start-single-node", "--insecure"}, | |
} | |
container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ | |
ContainerRequest: req, | |
Started: true, | |
}) | |
if err != nil { | |
return nil, err | |
} | |
mappedPort, err := container.MappedPort(ctx, "26257") | |
if err != nil { | |
return nil, err | |
} | |
hostIP, err := container.Host(ctx) | |
if err != nil { | |
return nil, err | |
} | |
uri := fmt.Sprintf("postgres://root@%s:%s", hostIP, mappedPort.Port()) | |
return &CockroachDBContainer{Container: container, URI: uri}, nil | |
} |
This file contains hidden or 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
package ccdb | |
import ( | |
"context" | |
_ "embed" | |
"testing" | |
"time" | |
"github.com/google/uuid" | |
"github.com/hyphengolang/prelude/testing/is" | |
"github.com/jackc/pgx/v5/pgxpool" | |
) | |
type Task struct { | |
ID string `json:"id"` | |
Description string `json:"description"` | |
DateDue *time.Time `json:"date_due"` | |
DateCreated time.Time `json:"date_created"` | |
DateUpdated time.Time `json:"date_updated"` | |
} | |
//go:embed tasks.sql | |
var migration string | |
func TestIntegrationDBInsertSelect(t *testing.T) { | |
is := is.New(t) | |
ctx := context.Background() | |
ccdb, err := NewCockroachDBContainer(ctx) | |
is.NoErr(err) // test-container should start | |
rwc, err := pgxpool.New(ctx, ccdb.URI+"/projectmanagement") | |
is.NoErr(err) // pgxpool should connect | |
defer rwc.Close() | |
{ | |
_, err := rwc.Exec(ctx, migration) | |
is.NoErr(err) // migration should run | |
defer ccdb.Terminate(ctx) | |
} | |
now := time.Now() | |
tsk := Task{ID: uuid.NewString(), Description: "test task", DateCreated: now, DateUpdated: now} | |
{ | |
const insertQuery = `insert into "task" (id, description, date_due, date_created, date_updated) | |
values ($1, $2, $3, $4, $5)` | |
_, err := rwc.Exec(ctx, insertQuery, tsk.ID, tsk.Description, tsk.DateDue, tsk.DateCreated, tsk.DateUpdated) | |
is.NoErr(err) // insert should run | |
} | |
saved := Task{ID: tsk.ID} | |
{ | |
const selectQuery = `select id, description, date_due, date_created, date_updated from "task" where id = $1` | |
err := rwc.QueryRow(ctx, selectQuery, tsk.ID).Scan(&saved.ID, &saved.Description, &saved.DateDue, &saved.DateCreated, &saved.DateUpdated) | |
is.NoErr(err) // select should run | |
} | |
is.Equal(tsk.ID, saved.ID) // saved task id should match inserted task id | |
is.Equal(tsk.Description, saved.Description) // saved task description should match inserted task description | |
is.Equal(tsk.DateDue, saved.DateDue) // saved task date_due should match inserted task date_due | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment