-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_query_test.go
63 lines (55 loc) · 2.11 KB
/
delete_query_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package easymongo_test
import (
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
)
func TestDelete(t *testing.T) {
var err error
setup(t)
coll := createBatmanArchive(t)
t.Cleanup(func() {
coll.Drop()
})
t.Run("Delete one", func(t *testing.T) {
is := assert.New(t)
err = coll.Delete(bson.M{"name": "The Joker"}).One()
is.NoError(err, "Could not delete the document")
var e enemy
err = coll.Find(bson.M{"name": "The Joker"}).One(&e)
is.Error(err, "Error should be set to NotFound")
is.Empty(e.Name, "No document should have been returned")
})
t.Run("Delete one by ID", func(t *testing.T) {
is := assert.New(t)
var e enemy
err = coll.Find(bson.M{}).One(&e)
is.NoError(err, "Can't find a document to delete")
is.NotEmpty(e.ID, "Couldn't find a document to delete")
is.NotEmpty(e.Name, "Couldn't find a document to delete")
err = coll.DeleteByID(e.ID)
is.NoError(err, "Could not delete the document")
e = enemy{}
err = coll.FindByID(e.ID, &e)
is.Error(err, "We should not be able to find this document post-deletion")
is.Empty(e.Name, "We should not be able to find this document post-deletion")
})
t.Run("Delete many", func(t *testing.T) {
is := assert.New(t)
filter := bson.M{}
docCount, err := coll.Find(filter).Count()
is.NoError(err, "Could not find any documents prior to testing deletion")
is.NotEqual(0, docCount, "Could not find any documents prior to testing deletion")
deletedCount, err := coll.Delete(filter).Many()
is.NoError(err, "Could not delete all documents")
is.Equal(docCount, deletedCount, "Could not delete all the documents in the collection")
createBatmanArchive(t)
filter = bson.M{"notes": bson.M{"$ne": nil}}
docCount, err = coll.Find(filter).Count()
is.NoError(err, "Could not find any documents prior to testing deletion")
is.NotEmpty(docCount, "Could not find any documents prior to testing deletion")
deletedCount, err = coll.Delete(filter).Many()
is.NoError(err, "Could not delete all documents with filter query")
is.Equal(docCount, deletedCount, "Could not delete all the documents in the collection")
})
}