Skip to content

Commit

Permalink
refactor(star): rename starring to star
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed Jan 26, 2022
1 parent ad65a65 commit e036ca8
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 468 deletions.
32 changes: 16 additions & 16 deletions lib/mock/starring_repository.go → lib/mock/star_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions starring/errors.go → star/errors.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package starring
package star

import (
"errors"
"fmt"
)

var (
ErrNoStarringInformation = errors.New("no starring information")
ErrNoStarInformation = errors.New("no star information")
)

type NotFoundError struct {
Expand All @@ -16,7 +16,7 @@ type NotFoundError struct {
}

func (e NotFoundError) Error() string {
return fmt.Sprintf("could not find starring asset urn \"%s\" with type \"%s\"", e.AssetURN, e.AssetType)
return fmt.Sprintf("could not find starred asset urn \"%s\" with type \"%s\"", e.AssetURN, e.AssetType)
}

type DuplicateRecordError struct {
Expand All @@ -26,7 +26,7 @@ type DuplicateRecordError struct {
}

func (e DuplicateRecordError) Error() string {
return fmt.Sprintf("duplicate starring asset urn \"%s\" and type \"%s\" with user id \"%s\"", e.AssetURN, e.AssetType, e.UserID)
return fmt.Sprintf("duplicate starred asset urn \"%s\" and type \"%s\" with user id \"%s\"", e.AssetURN, e.AssetType, e.UserID)
}

type InvalidError struct {
Expand Down
34 changes: 34 additions & 0 deletions star/star.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package star

//go:generate mockery --name Repository --outpkg mock --output ../lib/mock/ --structname StarRepository --filename star_repository.go

import (
"context"
"time"
)

type Star struct {
ID string
AssetURN string
AssetType string
CreatedAt time.Time
UpdatedAt time.Time
}

func (s *Star) Validate() error {
if s == nil {
return ErrNoStarInformation
}

if s.AssetURN == "" || s.AssetType == "" {
return InvalidError{AssetURN: s.AssetURN, AssetType: s.AssetType}
}

return nil
}

type Repository interface {
Create(ctx context.Context, star *Star) (string, error)
GetUserIDs(ctx context.Context, assetURN, assetType string) ([]string, error)
GetByUserID(ctx context.Context, userID string) ([]Star, error)
}
45 changes: 45 additions & 0 deletions star/star_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package star

import (
"testing"

"gotest.tools/assert"
)

func TestValidate(t *testing.T) {
type testCase struct {
Title string
Star *Star
ExpectError error
}

var testCases = []testCase{
{
Title: "should return error no star information if user is nil",
Star: nil,
ExpectError: ErrNoStarInformation,
},
{
Title: "should return error invalid if assert urn is empty",
Star: &Star{AssetType: "asset-type"},
ExpectError: InvalidError{AssetType: "asset-type"},
},
{
Title: "should return error invalid if assert type is empty",
Star: &Star{AssetURN: "asset-urn"},
ExpectError: InvalidError{AssetURN: "asset-urn"},
},
{
Title: "should return nil if star is valid",
Star: &Star{AssetURN: "asset-urn", AssetType: "asset-type"},
ExpectError: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.Title, func(t *testing.T) {

err := testCase.Star.Validate()
assert.Equal(t, testCase.ExpectError, err)
})
}
}
35 changes: 0 additions & 35 deletions starring/starring.go

This file was deleted.

50 changes: 0 additions & 50 deletions starring/starring_test.go

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS stars;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CREATE TABLE starrings (
CREATE TABLE stars (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
asset_urn text NOT NULL,
asset_type text NOT NULL,
asset_urn text NOT NULL,
created_at timestamp DEFAULT NOW(),
updated_at timestamp DEFAULT NOW()
);

CREATE UNIQUE INDEX starrings_idx_user_id_asset_type_asset_urn ON starrings(user_id,asset_type,asset_urn);
CREATE UNIQUE INDEX stars_idx_user_id_asset_type_asset_urn ON stars(user_id,asset_type,asset_urn);
23 changes: 11 additions & 12 deletions store/postgres/starring_model.go → store/postgres/star_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@ package postgres
import (
"time"

"github.com/odpf/columbus/starring"
"github.com/odpf/columbus/star"
)

type StarringModel struct {
type StarModel struct {
ID string `db:"id"`
UserID string `db:"user_id"`
AssetURN string `db:"asset_urn"`
AssetType string `db:"asset_type"`
AssetURN string `db:"asset_urn"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}

func (s *StarringModel) toStarring() *starring.Starring {
return &starring.Starring{
func (s *StarModel) toStar() *star.Star {
return &star.Star{
ID: s.ID,
UserID: s.UserID,
AssetURN: s.AssetURN,
AssetType: s.AssetType,
AssetURN: s.AssetURN,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
}
}

type StarringModels []StarringModel
type StarModels []StarModel

func (sms StarringModels) toStarrings() []starring.Starring {
starrings := []starring.Starring{}
func (sms StarModels) toStars() []star.Star {
stars := []star.Star{}

for _, sm := range sms {
starrings = append(starrings, *sm.toStarring())
stars = append(stars, *sm.toStar())
}

return starrings
return stars
}
28 changes: 28 additions & 0 deletions store/postgres/star_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package postgres

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestStarModel(t *testing.T) {
t.Run("successfully build build star from star model", func(t *testing.T) {
sm := &StarModel{
ID: "id",
AssetURN: "asseturn",
AssetType: "asserttype",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

s := sm.toStar()

assert.Equal(t, s.ID, sm.ID)
assert.Equal(t, s.AssetType, sm.AssetType)
assert.Equal(t, s.AssetURN, sm.AssetURN)
assert.True(t, s.CreatedAt.Equal(sm.CreatedAt))
assert.True(t, s.UpdatedAt.Equal(sm.UpdatedAt))
})
}
Loading

0 comments on commit e036ca8

Please sign in to comment.