Skip to content

Commit

Permalink
enable golangci-lint for examples (#2128)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Jan 18, 2024
1 parent 90a6ffe commit d8f0c46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:

- name: golangci-lint
# TODO: Remove each example/module once it passes the golangci-lint
if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["examples/cockroachdb", "examples/toxiproxy", "modules/compose"]'), inputs.project-directory) }}
if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["modules/compose"]'), inputs.project-directory) }}
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
Expand Down
36 changes: 13 additions & 23 deletions examples/cockroachdb/cockroachdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Task represents a unit of work to complete. We're going to be using this in
Expand All @@ -16,12 +17,12 @@ import (
type task struct {
ID string `json:"id"`
Description string `json:"description"`
DateDue *time.Time `json:"date_due,string"`
DateCreated time.Time `json:"date_created,string"`
DateDue *time.Time `json:"date_due"`
DateCreated time.Time `json:"date_created"`
DateUpdated time.Time `json:"date_updated"`
}

func initCockroachDB(ctx context.Context, db sql.DB) error {
func initCockroachDB(ctx context.Context, db *sql.DB) error {
// Actual SQL for initializing the database should probably live elsewhere
const query = `CREATE DATABASE projectmanagement;
CREATE TABLE projectmanagement.task(
Expand All @@ -31,11 +32,10 @@ func initCockroachDB(ctx context.Context, db sql.DB) error {
date_created timestamp with time zone not null,
date_updated timestamp with time zone not null);`
_, err := db.ExecContext(ctx, query)

return err
}

func truncateCockroachDB(ctx context.Context, db sql.DB) error {
func truncateCockroachDB(ctx context.Context, db *sql.DB) error {
const query = `TRUNCATE projectmanagement.task`
_, err := db.ExecContext(ctx, query)
return err
Expand All @@ -49,26 +49,21 @@ func TestIntegrationDBInsertSelect(t *testing.T) {
ctx := context.Background()

cdbContainer, err := startContainer(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
t.Cleanup(func() {
if err := cdbContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

db, err := sql.Open("pgx", cdbContainer.URI+"/projectmanagement")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer db.Close()

err = initCockroachDB(ctx, *db)
if err != nil {
t.Fatal(err)
}
defer truncateCockroachDB(ctx, *db)
require.NoError(t, initCockroachDB(ctx, db))
defer func(t *testing.T, ctx context.Context, db *sql.DB) {
require.NoError(t, truncateCockroachDB(ctx, db))
}(t, ctx, db)

now := time.Now()

Expand All @@ -84,9 +79,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) {
tsk.DateDue,
tsk.DateCreated,
tsk.DateUpdated)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// Select data
savedTsk := task{ID: tsk.ID}
Expand All @@ -95,10 +88,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) {
where id = $1`
row := db.QueryRowContext(ctx, findQuery, tsk.ID)
err = row.Scan(&savedTsk.Description, &savedTsk.DateDue, &savedTsk.DateCreated, &savedTsk.DateUpdated)
if err != nil {
t.Fatal(err)
}

require.NoError(t, err)
assert.Equal(t, tsk.ID, savedTsk.ID)
assert.Equal(t, tsk.Description, savedTsk.Description)
assert.Equal(t, tsk.DateDue, savedTsk.DateDue)
Expand Down

0 comments on commit d8f0c46

Please sign in to comment.