Skip to content

Commit

Permalink
Merge pull request #85 from Scalingo/fix/84/is_deleted_is_zero
Browse files Browse the repository at this point in the history
IsDeleted returns true if DeletedAt is not zero
  • Loading branch information
Soulou authored Apr 16, 2019
2 parents 3ecab11 + 9bc6b13 commit d9563d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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())
})
}
}

0 comments on commit d9563d8

Please sign in to comment.