Skip to content

Commit

Permalink
完善单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
wanjiewu committed May 8, 2024
1 parent 241cd5a commit 0411781
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
53 changes: 47 additions & 6 deletions ci_dataindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cos

import (
"context"
"fmt"
"net/http"
"testing"
)
Expand All @@ -15,8 +16,11 @@ func TestCIService_CreateDataSet(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Dataset\":{\"BindCount\":0,\"CreateTime\":\"2024-05-07T18:36:24.838341549+08:00\",\"DatasetName\":\"dataset\",\"Description\":\"dataset test\",\"FileCount\":0,\"TemplateId\":\"Official:COSBasicMeta\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-05-07T18:36:24.838341633+08:00\"},\"RequestId\":\"NjYzYTA0MjhfM2FiNjI5MWVfNTQyMl8yZjM4ZTI=\"}")
})

client.CI.CreateDataSet(context.Background(), nil)

createJobOpt := &CreateDataSetOptions{
DatasetName: "adataset",
Description: "dataset test",
Expand All @@ -37,14 +41,17 @@ func TestCIService_UpdateDataset(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Dataset\":{\"BindCount\":0,\"CreateTime\":\"2024-05-07T18:36:24.838341549+08:00\",\"DatasetName\":\"dataset\",\"Description\":\"dataset test\",\"FileCount\":0,\"TemplateId\":\"Official:COSBasicMeta\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-05-07T18:36:24.838341633+08:00\"},\"RequestId\":\"NjYzYTA0MjhfM2FiNjI5MWVfNTQyMl8yZjM4ZTI=\"}")
})

createJobOpt := &UpdateDatasetOptions{
client.CI.UpdateDataset(context.Background(), nil)

opt := &UpdateDatasetOptions{
DatasetName: "adataset",
Description: "dataset test",
TemplateId: "Official:COSBasicMeta",
}
_, _, err := client.CI.UpdateDataset(context.Background(), createJobOpt)
_, _, err := client.CI.UpdateDataset(context.Background(), opt)
if err != nil {
t.Fatalf("CI.UpdateDataset returned error: %v", err)
}
Expand All @@ -57,8 +64,11 @@ func TestCIService_DescribeDatasets(t *testing.T) {
mux.HandleFunc("/datasets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testHeader(t, r, "Content-Type", "application/json")
fmt.Fprint(w, "{\"Datasets\":[{\"BindCount\":0,\"CreateTime\":\"2024-05-06T19:49:17.49197866+08:00\",\"DatasetName\":\"adataset\",\"Description\":\"dataset test\",\"FileCount\":0,\"TemplateId\":\"Official:COSBasicMeta\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-05-06T19:49:17.49197874+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-05-07T18:36:24.838341549+08:00\",\"DatasetName\":\"dataset\",\"Description\":\"dataset test\",\"FileCount\":0,\"TemplateId\":\"Official:COSBasicMeta\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-05-07T18:36:24.838341633+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-28T16:58:29.972112328+08:00\",\"DatasetName\":\"test111\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-28T16:58:29.972112399+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-26T19:58:28.71611987+08:00\",\"DatasetName\":\"test11111\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-26T19:58:28.716119968+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-30T19:01:00.603324265+08:00\",\"DatasetName\":\"test111111\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-30T19:01:00.603324346+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-30T19:01:28.324249664+08:00\",\"DatasetName\":\"test1111111\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-30T19:01:28.324249747+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-30T19:06:22.973049681+08:00\",\"DatasetName\":\"test11111111\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-30T19:06:22.973049766+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-28T16:41:59.766417255+08:00\",\"DatasetName\":\"test111112\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-28T16:41:59.766417337+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-28T21:34:37.469900633+08:00\",\"DatasetName\":\"test11111222\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-28T21:34:37.469900718+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-28T21:35:03.76822133+08:00\",\"DatasetName\":\"test111112222\",\"Description\":\"数据集描述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-28T21:35:03.768221411+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-28T14:11:26.88710993+08:00\",\"DatasetName\":\"test12\",\"Description\":\"\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-28T14:11:26.887110011+08:00\"},{\"BindCount\":0,\"CreateTime\":\"2024-04-30T19:06:34.117713669+08:00\",\"DatasetName\":\"test1asfdsasdfafsdfa1111111\",\"Description\":\"数asdfa据集asdf描asdfad述\",\"FileCount\":0,\"TemplateId\":\"Official:Empty\",\"TotalFileSize\":0,\"UpdateTime\":\"2024-04-30T19:06:34.117713747+08:00\"}],\"NextToken\":\"\",\"RequestId\":\"NjYzYTFlNTVfNTc2ODk0MGJfNjZkM18zNmUyZTA=\"}")
})

client.CI.DescribeDatasets(context.Background(), nil)

opt := &DescribeDatasetsOptions{
MaxResults: 100,
}
Expand All @@ -77,8 +87,11 @@ func TestCIService_DeleteDataset(t *testing.T) {
testMethod(t, r, http.MethodDelete)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprintf(w, "{\"Code\":\"InvalidArgument\",\"Message\":\"dataset not empty\",\"RequestId\":\"NjYzYjZiNmRfM2FiNjI5MWVfNTQyMl8zMzVkZTY=\",\"TraceId\":\"\"}")
})

client.CI.DeleteDataset(context.Background(), nil)

opt := &DeleteDatasetOptions{
DatasetName: "adataset",
}
Expand All @@ -95,8 +108,10 @@ func TestCIService_DescribeDataset(t *testing.T) {
mux.HandleFunc("/dataset", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testHeader(t, r, "Content-Type", "application/json")
fmt.Fprint(w, "{\"Dataset\":{\"BindCount\":0,\"CreateTime\":\"2024-05-06T19:49:17.49197866+08:00\",\"DatasetName\":\"adataset\",\"Description\":\"dataset test\",\"FileCount\":1,\"TemplateId\":\"Official:COSBasicMeta\",\"TotalFileSize\":495199,\"UpdateTime\":\"2024-05-06T19:49:17.49197874+08:00\"},\"RequestId\":\"NjYzYjZjYzFfNjg2ODk0MGJfNzI0M18zMmUzMzE=\"}")
})

client.CI.DescribeDataset(context.Background(), nil)
opt := &DescribeDatasetOptions{
DatasetName: "adataset",
Statistics: true,
Expand All @@ -116,8 +131,11 @@ func TestCIService_CreateFileMetaIndex(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"EventId\":\"wi78e458510d3511ef95635254008dc19b\",\"RequestId\":\"NjYzYjZlM2VfNjg2ODk0MGJfNzIyMV8zMzA4MWE=\"}")
})

client.CI.CreateFileMetaIndex(context.Background(), nil)

opt := &CreateFileMetaIndexOptions{
DatasetName: "adataset",
File: &File{
Expand Down Expand Up @@ -146,8 +164,11 @@ func TestCIService_UpdateFileMetaIndex(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"EventId\":\"wi78e458510d3511ef95635254008dc19b\",\"RequestId\":\"NjYzYjZlM2VfNjg2ODk0MGJfNzIyMV8zMzA4MWE=\"}")
})

client.CI.UpdateFileMetaIndex(context.Background(), nil)

opt := &UpdateFileMetaIndexOptions{
DatasetName: "adataset",
File: &File{
Expand All @@ -174,8 +195,11 @@ func TestCIService_DescribeFileMetaIndex(t *testing.T) {
mux.HandleFunc("/filemeta", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testHeader(t, r, "Content-Type", "application/json")
fmt.Fprint(w, "{\"Files\":[{\"COSCRC64\":\"447296710575197191\",\"COSStorageClass\":\"STANDARD\",\"ContentType\":\"image/gif\",\"CreateTime\":\"2024-05-08T20:21:18.766475412+08:00\",\"CustomId\":\"123\",\"CustomLabels\":{\"age\":\"18\",\"level\":\"18\"},\"DatasetName\":\"adataset\",\"ETag\":\"\\\"c3ad99087956ff0c3d8293ab35747030\\\"\",\"FileModifiedTime\":\"2024-05-06T20:54:07+08:00\",\"Filename\":\"1.gif\",\"MediaType\":\"video\",\"ObjectACL\":\"default\",\"ObjectId\":\"64992b92f79f8ffad132586c4ca26cd4d5dd19783b746e5f6b14dc773f1c0f20\",\"OwnerID\":\"2832742109\",\"Size\":495199,\"URI\":\"cos://test1-1253960454/1.gif\",\"UpdateTime\":\"2024-05-08T20:28:14.884074916+08:00\"}],\"RequestId\":\"NjYzYjcwOWVfMzliNjI5MWVfNmFiZV8zNGM4NDE=\"}")
})

client.CI.DescribeFileMetaIndex(context.Background(), nil)

opt := &DescribeFileMetaIndexOptions{
DatasetName: "adataset",
Uri: "cos://test-1250000000/1.gif",
Expand All @@ -193,8 +217,11 @@ func TestCIService_DeleteFileMetaIndex(t *testing.T) {
mux.HandleFunc("/filemeta", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
testHeader(t, r, "Content-Type", "application/json")
fmt.Fprint(w, "{\"RequestId\":\"NjYzYjcxMTZfMmRiNjI5MWVfYWU1XzMxMjk3NQ==\"}")
})

client.CI.DeleteFileMetaIndex(context.Background(), nil)

opt := &DeleteFileMetaIndexOptions{
DatasetName: "adataset",
Uri: "cos://test1-1250000000/1.gif",
Expand All @@ -214,8 +241,11 @@ func TestCIService_DatasetSimpleQuery(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"NextToken\":\"\",\"RequestId\":\"NjYzYjczNjdfNzQ2ODk0MGJfM2NlN18zMWY3YWU=\"}")
})

client.CI.DatasetSimpleQuery(context.Background(), nil)

opt := &DatasetSimpleQueryOptions{
DatasetName: "adataset",
Query: &Query{
Expand All @@ -239,8 +269,11 @@ func TestCIService_DatasetSimpleQueryAggregations(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Aggregations\":[{\"Field\":\"ContentType\",\"Operation\":\"group\"}],\"NextToken\":\"\",\"RequestId\":\"NjYzYjc0MDFfNTc2ODk0MGJfNjZkNl8zZDA2NjU=\"}")
})

client.CI.DatasetSimpleQuery(context.Background(), nil)

opt := &DatasetSimpleQueryOptions{
DatasetName: "adataset",
Aggregations: []*Aggregation{},
Expand All @@ -264,8 +297,11 @@ func TestCIService_CreateDatasetBinding(t *testing.T) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Binding\":{\"CreateTime\":\"2024-05-08T20:47:20.632182296+08:00\",\"DatasetName\":\"adataset\",\"Detail\":\"\",\"State\":\"Running\",\"URI\":\"cos://test1-1253960454\",\"UpdateTime\":\"2024-05-08T20:47:20.632182375+08:00\"},\"RequestId\":\"NjYzYjc0NThfNmQ2ODk0MGJfYmUyXzMyZWE3ZA==\"}")
})

client.CI.CreateDatasetBinding(context.Background(), nil)

opt := &CreateDatasetBindingOptions{
DatasetName: "adataset",
URI: "cos://test1-1250000000",
Expand All @@ -279,14 +315,15 @@ func TestCIService_CreateDatasetBinding(t *testing.T) {
func TestCIService_DescribeDatasetBinding(t *testing.T) {
setup()
defer teardown()
// wantBody := "{\"DatasetName\":\"adataset\",\"URI\":\"cos://test1-1250000000\"}"

mux.HandleFunc("/datasetbinding", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testHeader(t, r, "Content-Type", "application/json")
// testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Binding\":{\"CreateTime\":\"2024-05-08T20:47:20.632182296+08:00\",\"DatasetName\":\"adataset\",\"Detail\":\"\",\"State\":\"Running\",\"URI\":\"cos://test1-1253960454\",\"UpdateTime\":\"2024-05-08T20:47:20.632182375+08:00\"},\"RequestId\":\"NjYzYjc0YTRfNTc2ODk0MGJfNjZkN18zYzcyNjY=\"}")
})

client.CI.DescribeDatasetBinding(context.Background(), nil)

opt := &DescribeDatasetBindingOptions{
DatasetName: "adataset",
URI: "cos://test1-1250000000",
Expand All @@ -300,14 +337,15 @@ func TestCIService_DescribeDatasetBinding(t *testing.T) {
func TestCIService_DescribeDatasetBindings(t *testing.T) {
setup()
defer teardown()
// wantBody := "{\"DatasetName\":\"adataset\",\"URI\":\"cos://test1-1250000000\"}"

mux.HandleFunc("/datasetbindings", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testHeader(t, r, "Content-Type", "application/json")
// testBody(t, r, wantBody)
fmt.Fprint(w, "{\"Bindings\":[{\"CreateTime\":\"2024-05-08T20:47:20.632182296+08:00\",\"DatasetName\":\"adataset\",\"Detail\":\"\",\"State\":\"Running\",\"URI\":\"cos://test1-1253960454\",\"UpdateTime\":\"2024-05-08T20:47:20.632182375+08:00\"}],\"NextToken\":\"\",\"RequestId\":\"NjYzYjc1MDBfNmQ2ODk0MGJfYmUyXzMyZWRlNQ==\"}")
})

client.CI.DescribeDatasetBindings(context.Background(), nil)

opt := &DescribeDatasetBindingsOptions{
DatasetName: "adataset",
// MaxResults: 3,
Expand All @@ -327,8 +365,11 @@ func TestCIService_DeleteDatasetBinding(t *testing.T) {
testMethod(t, r, http.MethodDelete)
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, wantBody)
fmt.Fprint(w, "{\"RequestId\":\"NjYzYjc1NDZfNTc2ODk0MGJfNjZkNF8zYzA2MTI=\"}")
})

client.CI.DeleteDatasetBinding(context.Background(), nil)

opt := &DeleteDatasetBindingOptions{
DatasetName: "adataset",
URI: "cos://test1-1250000000",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/google/go-querystring v1.0.0
github.com/google/uuid v1.1.1
github.com/grafov/m3u8 v0.12.0 // indirect
github.com/mitchellh/mapstructure v1.4.3
github.com/mozillazg/go-httpheader v0.2.1
github.com/stretchr/testify v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grafov/m3u8 v0.12.0 h1:T6iTwTsSEtMcwkayef+FJO8kj+Sglr4Lh81Zj8Ked/4=
github.com/grafov/m3u8 v0.12.0/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSrwhe88TQQ=
Expand Down

0 comments on commit 0411781

Please sign in to comment.