From d39387a1bc94ffafa0117ae4016feb880fc9275c Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Wed, 15 Aug 2018 20:03:35 +0300 Subject: [PATCH] issue #126: test InputWriter --- .../executor/internal/postgres/input_test.go | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/storage/executor/internal/postgres/input_test.go b/pkg/storage/executor/internal/postgres/input_test.go index 1bcb60b..61d52b4 100644 --- a/pkg/storage/executor/internal/postgres/input_test.go +++ b/pkg/storage/executor/internal/postgres/input_test.go @@ -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()) @@ -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) + }) + }) +}