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 InputReader
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Aug 15, 2018
1 parent d39387a commit 3eb0570
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions pkg/storage/executor/internal/postgres/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,62 @@ import (
)

func TestInputReader(t *testing.T) {
token := Token()
token, id := Token(), "10000000-2000-4000-8000-160000000000"
t.Run("read by ID", 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()
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(`SELECT "(?:.+)" FROM "input"`).
WithArgs(sqlmock.AnyArg()).
WillReturnRows(
sqlmock.
NewRows([]string{"schema_id", "data", "created_at"}).
AddRow(id, `{"input":["test"]}`, time.Now()),
)

var exec executor.InputReader = postgres.NewInputContext(ctx, conn)
input, err := exec.ReadByID(token, id)
assert.NoError(t, err)
assert.Equal(t, id, input.SchemaID)
assert.Equal(t, []byte(`{"input":["test"]}`), input.Data)
assert.NotEmpty(t, input.CreatedAt)
})
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(`SELECT "(?:.+)" FROM "input"`).
WithArgs(sqlmock.AnyArg()).
WillReturnRows(
sqlmock.
NewRows([]string{"schema_id", "data", "created_at"}).
AddRow("10000000-2000-4000-8000-160000000004", "{}", time.Now()),
)
mock.
ExpectQuery(`SELECT "(?:.+)" FROM "input"`).
WithArgs(sqlmock.AnyArg()).
WillReturnError(errors.Simple("test"))

var exec executor.InputReader = postgres.NewInputContext(ctx, conn)
input, err := exec.ReadByID(token, "10000000-2000-4000-8000-160000000000")
assert.NoError(t, err)
assert.Equal(t, "10000000-2000-4000-8000-160000000004", input.SchemaID)
var exec executor.InputReader = postgres.NewInputContext(ctx, conn)
input, err := exec.ReadByID(token, id)
assert.Error(t, err)
assert.Empty(t, input.SchemaID)
assert.Empty(t, input.Data)
assert.Empty(t, input.CreatedAt)
})
})
t.Run("read by filter", func(t *testing.T) {
// TODO
})
}

func TestInputWriter(t *testing.T) {
var id = "10000000-2000-4000-8000-160000000000"
t.Run("write", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -56,20 +82,20 @@ func TestInputWriter(t *testing.T) {

mock.
ExpectQuery(`INSERT INTO "input"`).
WithArgs("10000000-2000-4000-8000-160000000000", []byte(`{"input":["test"]}`)).
WithArgs(id, []byte(`{"input":["test"]}`)).
WillReturnRows(
sqlmock.
NewRows([]string{"id", "created_at"}).
AddRow("10000000-2000-4000-8000-160000000000", time.Now()),
AddRow(id, 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"}},
SchemaID: id,
VerifiedData: map[string][]string{"input": {"test"}},
})
assert.NoError(t, err)
assert.Equal(t, "10000000-2000-4000-8000-160000000000", input.ID)
assert.Equal(t, id, input.ID)
assert.NotEmpty(t, input.CreatedAt)
})
t.Run("serialization error", func(t *testing.T) {
Expand All @@ -86,13 +112,13 @@ func TestInputWriter(t *testing.T) {

mock.
ExpectQuery(`INSERT INTO "input"`).
WithArgs("10000000-2000-4000-8000-160000000000", []byte(`{"input":["test"]}`)).
WithArgs(id, []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"}},
SchemaID: id,
VerifiedData: map[string][]string{"input": {"test"}},
})
assert.Error(t, err)
assert.Empty(t, input.ID)
Expand Down

0 comments on commit 3eb0570

Please sign in to comment.