diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2050be..ee563735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v5.5.8 (Apr 16 2019) + +* [paranoid] IsDeleted returns true if DeletedAt is not zero + ## v5.5.7 (Apr 12 2019) * [errors] Better handling of ValidationErrors diff --git a/mongo/document/paranoid.go b/mongo/document/paranoid.go index 0f674b8a..9ceaca00 100644 --- a/mongo/document/paranoid.go +++ b/mongo/document/paranoid.go @@ -13,7 +13,7 @@ type Paranoid struct { } func (p Paranoid) IsDeleted() bool { - return p.DeletedAt != nil + return p.DeletedAt != nil && !p.DeletedAt.IsZero() } func (d *Paranoid) setDeletedAt(t time.Time) { diff --git a/mongo/document/paranoid_test.go b/mongo/document/paranoid_test.go index 7d6a03ca..01f84535 100644 --- a/mongo/document/paranoid_test.go +++ b/mongo/document/paranoid_test.go @@ -3,6 +3,7 @@ package document import ( "context" "testing" + "time" "gopkg.in/mgo.v2/bson" @@ -223,3 +224,48 @@ func TestParanoid_Restore(t *testing.T) { assert.NoError(t, err) assert.False(t, doc.IsDeleted()) } + +func TestParanoid_IsDeleted(t *testing.T) { + examples := []struct { + name string + paranoidDoc func(t *testing.T) (*ParanoidDoc, func()) + expectedRes bool + }{ + { + name: "It should return true if DeletedAt is set", + paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) { + d, clean := NewTestParanoidDoc(t) + now := time.Now() + d.DeletedAt = &now + return d, clean + }, + expectedRes: true, + }, { + name: "It should return false if DeletedAt is nil", + paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) { + d, clean := NewTestParanoidDoc(t) + d.DeletedAt = nil + return d, clean + }, + expectedRes: false, + }, { + name: "It should return false if DeletedAt is zero", + paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) { + d, clean := NewTestParanoidDoc(t) + zero := time.Time{} + d.DeletedAt = &zero + return d, clean + }, + expectedRes: false, + }, + } + + for _, example := range examples { + t.Run(example.name, func(t *testing.T) { + fixtureParanoidDoc, clean := example.paranoidDoc(t) + defer clean() + + require.Equal(t, example.expectedRes, fixtureParanoidDoc.IsDeleted()) + }) + } +}