Skip to content

IsDeleted returns true if DeletedAt is not zero #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion mongo/document/paranoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions mongo/document/paranoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package document
import (
"context"
"testing"
"time"

"gopkg.in/mgo.v2/bson"

Expand Down Expand Up @@ -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())
})
}
}