Skip to content

Commit

Permalink
Merge pull request #7 from lefinal/feat-add-uuid
Browse files Browse the repository at this point in the history
feat: add uuid
  • Loading branch information
lefinal authored Jul 3, 2022
2 parents b2caabd + 6ddfa5b commit 3cbcc1d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require github.com/stretchr/testify v1.7.1

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
Expand Down
11 changes: 11 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nulls

import "github.com/gofrs/uuid"

// NewUUID creates a new valid uuid.NullUUID.
func NewUUID(id uuid.UUID) uuid.NullUUID {
return uuid.NullUUID{
UUID: id,
Valid: true,
}
}
123 changes: 123 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package nulls

import (
"encoding/json"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)

func newUUIDV4() uuid.UUID {
id, err := uuid.NewV4()
if err != nil {
panic(err)
}
return id
}

// TestNewUUID tests NewUUID.
func TestNewUUID(t *testing.T) {
id := newUUIDV4()
s := NewUUID(id)
assert.True(t, s.Valid, "should be valid")
assert.Equal(t, id, s.UUID, "should contain correct value")
}

// UUIDMarshalJSONSuite tests UUID.MarshalJSON.
type UUIDMarshalJSONSuite struct {
suite.Suite
}

func (suite *UUIDMarshalJSONSuite) TestNotValid() {
s := uuid.NullUUID{UUID: newUUIDV4()}
raw, err := json.Marshal(s)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}

func (suite *UUIDMarshalJSONSuite) TestOK() {
id := newUUIDV4()
s := NewUUID(id)
raw, err := json.Marshal(s)
suite.Require().NoError(err, "should not fail")
suite.Equal(marshalMust(id), raw, "should return correct value")
}

func TestUUID_MarshalJSON(t *testing.T) {
suite.Run(t, new(UUIDMarshalJSONSuite))
}

// UUIDUnmarshalJSONSuite tests UUID.UnmarshalJSON.
type UUIDUnmarshalJSONSuite struct {
suite.Suite
}

func (suite *UUIDUnmarshalJSONSuite) TestNull() {
var s uuid.NullUUID
err := json.Unmarshal(jsonNull, &s)
suite.Require().NoError(err, "should not fail")
suite.False(s.Valid, "should not be valid")
}

func (suite *UUIDUnmarshalJSONSuite) TestOK() {
id := newUUIDV4()
var s uuid.NullUUID
err := json.Unmarshal(marshalMust(id), &s)
suite.Require().NoError(err, "should not fail")
suite.True(s.Valid, "should be valid")
suite.Equal(id, s.UUID, "should unmarshal correct value")
}

func TestUUID_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(UUIDUnmarshalJSONSuite))
}

// UUIDScanSuite tests UUID.Scan.
type UUIDScanSuite struct {
suite.Suite
}

func (suite *UUIDScanSuite) TestNull() {
var s uuid.NullUUID
err := s.Scan(nil)
suite.Require().NoError(err, "should not fail")
suite.False(s.Valid, "should not be valid")
}

func (suite *UUIDScanSuite) TestOK() {
id := newUUIDV4()
var s uuid.NullUUID
err := s.Scan(id)
suite.Require().NoError(err, "should not fail")
suite.True(s.Valid, "should be valid")
suite.Equal(id, s.UUID, "should scan correct value")
}

func TestUUID_Scan(t *testing.T) {
suite.Run(t, new(UUIDScanSuite))
}

// UUIDValueSuite tests UUID.Value.
type UUIDValueSuite struct {
suite.Suite
}

func (suite *UUIDValueSuite) TestNull() {
s := uuid.NullUUID{UUID: newUUIDV4()}
raw, err := s.Value()
suite.Require().NoError(err, "should not fail")
suite.Nil(raw, "should return correct value")
}

func (suite *UUIDValueSuite) TestOK() {
id := newUUIDV4()
s := NewUUID(id)
raw, err := s.Value()
suite.Require().NoError(err, "should not fail")
suite.EqualValues(id.String(), raw, "should return correct value")
}

func TestUUID_Value(t *testing.T) {
suite.Run(t, new(UUIDValueSuite))
}

0 comments on commit 3cbcc1d

Please sign in to comment.