Skip to content

Commit d9563d8

Browse files
authored
Merge pull request #85 from Scalingo/fix/84/is_deleted_is_zero
IsDeleted returns true if DeletedAt is not zero
2 parents 3ecab11 + 9bc6b13 commit d9563d8

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v5.5.8 (Apr 16 2019)
4+
5+
* [paranoid] IsDeleted returns true if DeletedAt is not zero
6+
37
## v5.5.7 (Apr 12 2019)
48

59
* [errors] Better handling of ValidationErrors

mongo/document/paranoid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Paranoid struct {
1313
}
1414

1515
func (p Paranoid) IsDeleted() bool {
16-
return p.DeletedAt != nil
16+
return p.DeletedAt != nil && !p.DeletedAt.IsZero()
1717
}
1818

1919
func (d *Paranoid) setDeletedAt(t time.Time) {

mongo/document/paranoid_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package document
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"gopkg.in/mgo.v2/bson"
89

@@ -223,3 +224,48 @@ func TestParanoid_Restore(t *testing.T) {
223224
assert.NoError(t, err)
224225
assert.False(t, doc.IsDeleted())
225226
}
227+
228+
func TestParanoid_IsDeleted(t *testing.T) {
229+
examples := []struct {
230+
name string
231+
paranoidDoc func(t *testing.T) (*ParanoidDoc, func())
232+
expectedRes bool
233+
}{
234+
{
235+
name: "It should return true if DeletedAt is set",
236+
paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) {
237+
d, clean := NewTestParanoidDoc(t)
238+
now := time.Now()
239+
d.DeletedAt = &now
240+
return d, clean
241+
},
242+
expectedRes: true,
243+
}, {
244+
name: "It should return false if DeletedAt is nil",
245+
paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) {
246+
d, clean := NewTestParanoidDoc(t)
247+
d.DeletedAt = nil
248+
return d, clean
249+
},
250+
expectedRes: false,
251+
}, {
252+
name: "It should return false if DeletedAt is zero",
253+
paranoidDoc: func(t *testing.T) (*ParanoidDoc, func()) {
254+
d, clean := NewTestParanoidDoc(t)
255+
zero := time.Time{}
256+
d.DeletedAt = &zero
257+
return d, clean
258+
},
259+
expectedRes: false,
260+
},
261+
}
262+
263+
for _, example := range examples {
264+
t.Run(example.name, func(t *testing.T) {
265+
fixtureParanoidDoc, clean := example.paranoidDoc(t)
266+
defer clean()
267+
268+
require.Equal(t, example.expectedRes, fixtureParanoidDoc.IsDeleted())
269+
})
270+
}
271+
}

0 commit comments

Comments
 (0)