aboutsummaryrefslogtreecommitdiff
path: root/tests/bulk_delete_test.go
blob: dfe846892730d8958e834fbd870f1d48b868d1bf (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/******************************************************************************
*
*  Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
******************************************************************************/

package tests

import (
	"fmt"
	"strings"
	"testing"

	"github.com/majewsky/schwift"
)

func TestBulkDeleteSuccess(t *testing.T) {
	testWithAccount(t, func(a *schwift.Account) {
		c, err := a.Container("schwift-test-bulkdelete").EnsureExists()
		expectSuccess(t, err)
		objs, err := createTestObjects(c)
		expectSuccess(t, err)

		numDeleted, numNotFound, err := c.Account().BulkDelete(objs, nil, nil)
		expectSuccess(t, err)
		expectInt(t, numDeleted, len(objs))
		expectInt(t, numNotFound, 0)
		expectContainerExistence(t, c, true)

		numDeleted, numNotFound, err = c.Account().BulkDelete(objs, nil, nil)
		expectSuccess(t, err)
		expectInt(t, numDeleted, 0)
		expectInt(t, numNotFound, len(objs))
		expectContainerExistence(t, c, true)

		objs, err = createTestObjects(c)
		expectSuccess(t, err)
		cs := []*schwift.Container{c}

		numDeleted, numNotFound, err = c.Account().BulkDelete(objs, cs, nil)
		expectSuccess(t, err)
		expectInt(t, numDeleted, len(objs)+1)
		expectInt(t, numNotFound, 0)
		expectContainerExistence(t, c, false)
	})
}

func TestBulkDeleteError(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		objs, err := createTestObjects(c)
		expectSuccess(t, err)
		objs = objs[1:]
		cs := []*schwift.Container{c}

		//not deleting all objects should lead to 409 Conflict when deleting the Container
		numDeleted, numNotFound, err := c.Account().BulkDelete(objs, cs, nil)
		expectInt(t, numDeleted, len(objs))
		expectInt(t, numNotFound, 0)
		expectError(t, err, "400 Bad Request (+1 object errors)")
		expectContainerExistence(t, c, true)
	})
}

func createTestObjects(c *schwift.Container) ([]*schwift.Object, error) {
	var objs []*schwift.Object
	for idx := 1; idx <= 5; idx++ {
		obj := c.Object(fmt.Sprintf("object%d", idx))
		err := obj.Upload(strings.NewReader("example"), nil, nil)
		if err != nil {
			return nil, err
		}
		objs = append(objs, obj)
	}
	return objs, nil
}