Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
issue #126: test InputWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Aug 15, 2018
1 parent c1db99c commit d39387a
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion pkg/storage/executor/internal/postgres/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"testing"
"time"

"github.com/kamilsk/form-api/pkg/errors"
"github.com/kamilsk/form-api/pkg/storage/executor"
"github.com/kamilsk/form-api/pkg/storage/executor/internal/postgres"
"github.com/kamilsk/form-api/pkg/storage/query"
"github.com/stretchr/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestNewInputContext(t *testing.T) {
func TestInputReader(t *testing.T) {
token := Token()
t.Run("read by ID", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -40,3 +42,61 @@ func TestNewInputContext(t *testing.T) {
// TODO
})
}

func TestInputWriter(t *testing.T) {
t.Run("write", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, mock, err := sqlmock.New()
assert.NoError(t, err)
conn, err := db.Conn(ctx)
assert.NoError(t, err)
defer conn.Close()

mock.
ExpectQuery(`INSERT INTO "input"`).
WithArgs("10000000-2000-4000-8000-160000000000", []byte(`{"input":["test"]}`)).
WillReturnRows(
sqlmock.
NewRows([]string{"id", "created_at"}).
AddRow("10000000-2000-4000-8000-160000000000", time.Now()),
)

var exec executor.InputWriter = postgres.NewInputContext(ctx, conn)
input, err := exec.Write(query.WriteInput{
SchemaID:"10000000-2000-4000-8000-160000000000",
VerifiedData: map[string][]string{"input":{"test"}},
})
assert.NoError(t, err)
assert.Equal(t, "10000000-2000-4000-8000-160000000000", input.ID)
assert.NotEmpty(t, input.CreatedAt)
})
t.Run("serialization error", func(t *testing.T) {
// TODO
})
t.Run("database error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, mock, err := sqlmock.New()
assert.NoError(t, err)
conn, err := db.Conn(ctx)
assert.NoError(t, err)
defer conn.Close()

mock.
ExpectQuery(`INSERT INTO "input"`).
WithArgs("10000000-2000-4000-8000-160000000000", []byte(`{"input":["test"]}`)).
WillReturnError(errors.Simple("test"))

var exec executor.InputWriter = postgres.NewInputContext(ctx, conn)
input, err := exec.Write(query.WriteInput{
SchemaID:"10000000-2000-4000-8000-160000000000",
VerifiedData: map[string][]string{"input":{"test"}},
})
assert.Error(t, err)
assert.Empty(t, input.ID)
assert.Empty(t, input.CreatedAt)
})
})
}

0 comments on commit d39387a

Please sign in to comment.