-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
56 lines (47 loc) · 1.35 KB
/
main_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
package main
import (
"context"
"fmt"
"os"
"testing"
"github.com/Houndie/square-go"
"github.com/Houndie/square-go/catalog"
"github.com/Houndie/square-go/objects"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func TestMain(m *testing.M) {
resource.TestMain(m)
}
func init() {
resource.AddTestSweepers("sweep_catalog_objects", &resource.Sweeper{
Name: "sweep_catalog_objects",
F: func(r string) error {
token := os.Getenv("TEST_TOKEN")
if token == "" {
return fmt.Errorf("Cannot sweep, test token not set")
}
client, err := square.NewClient(token, objects.Sandbox)
if err != nil {
return fmt.Errorf("error creating square client in sweeper: %w", err)
}
listRes, err := client.Catalog.List(context.Background(), &catalog.ListRequest{})
if err != nil {
return fmt.Errorf("error listing catalog items: %w", err)
}
deleteIDs := []string{}
for listRes.Objects.Next() {
deleteIDs = append(deleteIDs, listRes.Objects.Value().Object.ID)
}
if err := listRes.Objects.Error(); err != nil {
return fmt.Errorf("error iterating through list objects: %w", err)
}
_, err = client.Catalog.BatchDelete(context.Background(), &catalog.BatchDeleteRequest{
ObjectIDs: deleteIDs,
})
if err != nil {
return fmt.Errorf("error batch deleting objects: %w", err)
}
return nil
},
})
}