diff --git a/go.mod b/go.mod index eb0b063..153f206 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5786f2b..889f590 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/uuid.go b/uuid.go new file mode 100644 index 0000000..427b0ea --- /dev/null +++ b/uuid.go @@ -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, + } +} diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..6a61448 --- /dev/null +++ b/uuid_test.go @@ -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)) +}