diff --git a/pkg/storage/executor/internal/postgres/input_test.go b/pkg/storage/executor/internal/postgres/input_test.go index 61d52b4..f9ce985 100644 --- a/pkg/storage/executor/internal/postgres/input_test.go +++ b/pkg/storage/executor/internal/postgres/input_test.go @@ -14,29 +14,54 @@ 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 @@ -44,6 +69,7 @@ func TestInputReader(t *testing.T) { } 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()) @@ -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) { @@ -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)