-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawlabgo_test.go
40 lines (34 loc) · 956 Bytes
/
crawlabgo_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
package crawlabgo
import (
"os"
"testing"
)
func setCrawlabEnv() {
os.Setenv("CRAWLAB_TASK_ID", "377e3d13-d650-4098-be3e-37c852393b54")
os.Setenv("CRAWLAB_IS_DEDUP", "false")
os.Setenv("CRAWLAB_DEDUP_METHOD", "ignore")
os.Setenv("CRAWLAB_MONGO_DB", "crawlab_test")
os.Setenv("CRAWLAB_COLLECTION", "results_gotest")
os.Setenv("CRAWLAB_DEDUP_METHOD", "ignore")
}
type Item struct {
Name string `bson:"name"`
Age int `bson:"age"`
TaskID string `bson:"task_id"`
}
func TestSaveItem(t *testing.T) {
setCrawlabEnv()
t.Run("save item", func(t *testing.T) {
err := SaveItem(&Item{Name: "zhangweiii", Age: 1})
if err != nil {
t.Fatal(err)
}
})
t.Run("save item with overwrite", func(t *testing.T) {
SaveItem(&Item{Name: "overwrite", Age: 1})
os.Setenv("CRAWLAB_DEDUP_METHOD", "overwrite")
os.Setenv("CRAWLAB_DEDUP_FIELD", "Name")
os.Setenv("CRAWLAB_IS_DEDUP", "true")
SaveItem(&Item{Name: "overwrite", Age: 2})
})
}