Skip to content

Commit

Permalink
refactor: namespace data formats (#113)
Browse files Browse the repository at this point in the history
* refactor: nameespace data formats

* refactor: rename storage to store

* refactor: move formats package to root
  • Loading branch information
ravisuhag authored Feb 22, 2022
1 parent 0452c6a commit aebf14e
Show file tree
Hide file tree
Showing 38 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/odpf/stencil/config"
"github.com/odpf/stencil/server"
"github.com/odpf/stencil/storage/postgres"
"github.com/odpf/stencil/store/postgres"
"github.com/spf13/cobra"

// Importing postgres driver
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion server/avro/schema_test.go → formats/avro/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package avro_test
import (
"testing"

"github.com/odpf/stencil/server/avro"
"github.com/odpf/stencil/formats/avro"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package json_test
import (
"testing"

"github.com/odpf/stencil/server/json"
"github.com/odpf/stencil/formats/json"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

"github.com/odpf/stencil/formats/protobuf"
"github.com/odpf/stencil/mocks"
"github.com/odpf/stencil/server/protobuf"
"github.com/odpf/stencil/server/schema"
"github.com/stretchr/testify/assert"
)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
"testing"

"github.com/odpf/stencil/server/protobuf"
"github.com/odpf/stencil/formats/protobuf"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package protobuf_test
import (
"testing"

"github.com/odpf/stencil/server/protobuf"
"github.com/odpf/stencil/formats/protobuf"
"github.com/odpf/stencil/server/schema"
"github.com/stretchr/testify/assert"
)
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions server/schema/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package provider
import (
"errors"

"github.com/odpf/stencil/server/avro"
"github.com/odpf/stencil/server/json"
"github.com/odpf/stencil/server/protobuf"
"github.com/odpf/stencil/formats/avro"
"github.com/odpf/stencil/formats/json"
"github.com/odpf/stencil/formats/protobuf"
"github.com/odpf/stencil/server/schema"
)

Expand Down
4 changes: 2 additions & 2 deletions server/schema/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/google/uuid"
"github.com/odpf/stencil/server/domain"
"github.com/odpf/stencil/storage"
"github.com/odpf/stencil/store"
)

func getNonEmpty(args ...string) string {
Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Service) cachedGetSchema(ctx context.Context, nsName, schemaName string
func (s *Service) CheckCompatibility(ctx context.Context, nsName, schemaName, format, compatibility string, current ParsedSchema) error {
prevMeta, prevSchemaData, err := s.GetLatest(ctx, nsName, schemaName)
if err != nil {
if errors.Is(err, storage.NoRowsErr) {
if errors.Is(err, store.NoRowsErr) {
return nil
}
return err
Expand Down
8 changes: 4 additions & 4 deletions server/schema/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/odpf/stencil/mocks"
"github.com/odpf/stencil/server/domain"
"github.com/odpf/stencil/server/schema"
"github.com/odpf/stencil/storage"
"github.com/odpf/stencil/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand All @@ -30,10 +30,10 @@ func TestSchemaCreate(t *testing.T) {
t.Run("should return error if namespace not found", func(t *testing.T) {
svc, nsService, _, _ := getSvc()
nsName := "testNamespace"
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{}, storage.NoRowsErr)
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{}, store.NoRowsErr)
_, err := svc.Create(ctx, nsName, "a", &domain.Metadata{}, []byte(""))
assert.NotNil(t, err)
assert.ErrorIs(t, err, storage.NoRowsErr)
assert.ErrorIs(t, err, store.NoRowsErr)
nsService.AssertExpectations(t)
})

Expand Down Expand Up @@ -67,7 +67,7 @@ func TestSchemaCreate(t *testing.T) {
data := []byte("data")
nsService.On("Get", mock.Anything, nsName).Return(domain.Namespace{Format: "protobuf"}, nil)
schemaProvider.On("ParseSchema", "protobuf", data).Return(parsedSchema, nil)
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(2), storage.NoRowsErr)
schemaRepo.On("GetLatestVersion", mock.Anything, nsName, "a").Return(int32(2), store.NoRowsErr)
parsedSchema.On("GetCanonicalValue").Return(scFile)
schemaRepo.On("CreateSchema", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(int32(1), nil)
scInfo, err := svc.Create(ctx, nsName, "a", &domain.Metadata{}, data)
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/newrelic/go-agent/v3/integrations/nrgrpc"
"github.com/odpf/stencil/config"
"github.com/odpf/stencil/storage/postgres"
"github.com/odpf/stencil/store/postgres"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down
2 changes: 1 addition & 1 deletion storage/db_error.go → store/db_error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package storage
package store

import (
"fmt"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/odpf/stencil/server/domain"
"github.com/odpf/stencil/storage"
"github.com/odpf/stencil/store"
)

func wrapError(err error, format string, args ...interface{}) error {
Expand All @@ -18,14 +18,14 @@ func wrapError(err error, format string, args ...interface{}) error {
}
var pgErr *pgconn.PgError
if errors.Is(err, pgx.ErrNoRows) {
return storage.NoRowsErr.WithErr(err, fmt.Sprintf(format, args...))
return store.NoRowsErr.WithErr(err, fmt.Sprintf(format, args...))
}
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" {
return storage.ConflictErr.WithErr(err, fmt.Sprintf(format, args...))
return store.ConflictErr.WithErr(err, fmt.Sprintf(format, args...))
}
}
return storage.UnknownErr.WithErr(err, fmt.Sprintf(format, args...))
return store.UnknownErr.WithErr(err, fmt.Sprintf(format, args...))
}

type searchData struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/odpf/stencil/server/domain"
"github.com/odpf/stencil/storage"
"github.com/odpf/stencil/storage/postgres"
"github.com/odpf/stencil/store"
"github.com/odpf/stencil/store/postgres"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,116 +48,116 @@ func assertNamespace(t *testing.T, expected, actual domain.Namespace) {

func TestStorage(t *testing.T) {
tearDown(t)
store := getStore(t)
db := getStore(t)
ctx := context.Background()
n := &domain.Namespace{ID: "test", Format: "protobuf", Compatibility: "FULL", Description: "testDesc"}
t.Run("Namespace", func(t *testing.T) {
t.Run("create: should create namespace", func(t *testing.T) {
ns, err := store.CreateNamespace(ctx, *n)
ns, err := db.CreateNamespace(ctx, *n)
assert.Nil(t, err)
assertNamespace(t, *n, ns)
})
t.Run("create: should return error on duplicate namespace name", func(t *testing.T) {
_, err := store.CreateNamespace(ctx, *n)
assert.ErrorIs(t, err, storage.ConflictErr)
_, err := db.CreateNamespace(ctx, *n)
assert.ErrorIs(t, err, store.ConflictErr)
})
t.Run("update: should update the namespace", func(t *testing.T) {
n.Description = "newDescription"
n.Format = "avro"
ns, err := store.UpdateNamespace(ctx, *n)
ns, err := db.UpdateNamespace(ctx, *n)
assert.Nil(t, err)
assertNamespace(t, *n, ns)
})
t.Run("update: should return error if namespace not found", func(t *testing.T) {
n.ID = "test2"
_, err := store.UpdateNamespace(ctx, *n)
assert.ErrorIs(t, err, storage.NoRowsErr)
_, err := db.UpdateNamespace(ctx, *n)
assert.ErrorIs(t, err, store.NoRowsErr)
n.ID = "test"
})
t.Run("get: should get the namespace", func(t *testing.T) {
ns, err := store.GetNamespace(ctx, "test")
ns, err := db.GetNamespace(ctx, "test")
assert.Nil(t, err)
assertNamespace(t, *n, ns)
})
t.Run("get: should return the error if namespace not found", func(t *testing.T) {
_, err := store.GetNamespace(ctx, "test1")
assert.ErrorIs(t, err, storage.NoRowsErr)
_, err := db.GetNamespace(ctx, "test1")
assert.ErrorIs(t, err, store.NoRowsErr)
})
t.Run("list: should list created namespaces", func(t *testing.T) {
ls, err := store.ListNamespaces(ctx)
ls, err := db.ListNamespaces(ctx)
assert.Nil(t, err)
assert.Equal(t, []string{"test"}, ls)
})
t.Run("delete: should delete namespace", func(t *testing.T) {
err := store.DeleteNamespace(ctx, "test")
err := db.DeleteNamespace(ctx, "test")
assert.Nil(t, err)
})
})

t.Run("schema", func(t *testing.T) {
n := &domain.Namespace{ID: "testschema", Format: "protobuf", Compatibility: "FULL", Description: "testDesc"}
_, err := store.CreateNamespace(ctx, *n)
_, err := db.CreateNamespace(ctx, *n)
assert.Nil(t, err)
meta := &domain.Metadata{
Format: "avro",
}
t.Run("create: should create schema", func(t *testing.T) {
versionNumber, err := store.CreateSchema(ctx, n.ID, "sName", meta, "uuid-1", &domain.SchemaFile{ID: "t1", Data: []byte("testdata")})
versionNumber, err := db.CreateSchema(ctx, n.ID, "sName", meta, "uuid-1", &domain.SchemaFile{ID: "t1", Data: []byte("testdata")})
assert.Nil(t, err)
assert.Equal(t, int32(1), versionNumber)
})
t.Run("create: should increment version number on new schema", func(t *testing.T) {
versionNumber, err := store.CreateSchema(ctx, n.ID, "sName", meta, "uuid-2", &domain.SchemaFile{ID: "t2", Data: []byte("testdata-2")})
versionNumber, err := db.CreateSchema(ctx, n.ID, "sName", meta, "uuid-2", &domain.SchemaFile{ID: "t2", Data: []byte("testdata-2")})
assert.Nil(t, err)
assert.Equal(t, int32(2), versionNumber)
})
t.Run("create: should return same version number if schema is same", func(t *testing.T) {
versionNumber, err := store.CreateSchema(ctx, n.ID, "sName", meta, "uuid-1", &domain.SchemaFile{ID: "t1", Data: []byte("testdata")})
versionNumber, err := db.CreateSchema(ctx, n.ID, "sName", meta, "uuid-1", &domain.SchemaFile{ID: "t1", Data: []byte("testdata")})
assert.Nil(t, err)
assert.Equal(t, int32(1), versionNumber)
})
t.Run("list_schemas: should return schema", func(t *testing.T) {
schemaList, err := store.ListSchemas(ctx, "testschema")
schemaList, err := db.ListSchemas(ctx, "testschema")
assert.Nil(t, err)
assert.Equal(t, []string{"sName"}, schemaList)
})
t.Run("list_versions: should return versions for specified schema", func(t *testing.T) {
schemaList, err := store.ListVersions(ctx, "testschema", "sName")
schemaList, err := db.ListVersions(ctx, "testschema", "sName")
assert.Nil(t, err)
assert.Equal(t, []int32{1, 2}, schemaList)
})
t.Run("get: should return specified schema", func(t *testing.T) {
s, err := store.GetSchema(ctx, n.ID, "sName", 1)
s, err := db.GetSchema(ctx, n.ID, "sName", 1)
assert.Nil(t, err)
assert.Equal(t, []byte("testdata"), s)
})
t.Run("getMetadata: should return metadata", func(t *testing.T) {
actual, err := store.GetSchemaMetadata(ctx, n.ID, "sName")
actual, err := db.GetSchemaMetadata(ctx, n.ID, "sName")
assert.Nil(t, err)
assert.Equal(t, meta.Format, actual.Format)
})
t.Run("updateMetadata: should update metadata", func(t *testing.T) {
actual, err := store.UpdateSchemaMetadata(ctx, n.ID, "sName", &domain.Metadata{Compatibility: "FULL"})
actual, err := db.UpdateSchemaMetadata(ctx, n.ID, "sName", &domain.Metadata{Compatibility: "FULL"})
assert.Nil(t, err)
assert.Equal(t, "FULL", actual.Compatibility)
})
t.Run("getLatestVersion: should return latest schema version", func(t *testing.T) {
s, err := store.GetLatestVersion(ctx, n.ID, "sName")
s, err := db.GetLatestVersion(ctx, n.ID, "sName")
assert.Nil(t, err)
assert.Equal(t, int32(2), s)
})
t.Run("deleteVersion: should delete specified version schema", func(t *testing.T) {
err := store.DeleteVersion(ctx, n.ID, "sName", int32(2))
err := db.DeleteVersion(ctx, n.ID, "sName", int32(2))
assert.Nil(t, err)
schemaList, err := store.ListVersions(ctx, "testschema", "sName")
schemaList, err := db.ListVersions(ctx, "testschema", "sName")
assert.Nil(t, err)
assert.Equal(t, []int32{1}, schemaList)
})

t.Run("deleteSchema: should delete specified schema", func(t *testing.T) {
err := store.DeleteSchema(ctx, n.ID, "sName")
err := db.DeleteSchema(ctx, n.ID, "sName")
assert.Nil(t, err)
schemaList, err := store.ListSchemas(ctx, "testschema")
schemaList, err := db.ListSchemas(ctx, "testschema")
assert.Nil(t, err)
assert.Equal(t, 0, len(schemaList))
})
Expand Down
2 changes: 1 addition & 1 deletion storage/store.go → store/store.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package storage
package store

import (
"github.com/odpf/stencil/server/domain"
Expand Down

0 comments on commit aebf14e

Please sign in to comment.