package test import ( "encoding/json" "git.sxidc.com/service-supports/dapr_api/state" "git.sxidc.com/service-supports/dapr_api/utils" "github.com/stretchr/testify/assert" "testing" "time" ) func TestStateSaveAndGet(t *testing.T) { api := state.NewAPI(daprHttpPort, 10*time.Second) defer state.DestroyAPI(api) key := utils.SimpleUUID() value := utils.SimpleUUID() err := api.SaveState(stateStoreName, []state.SaveStateRequest{ { Key: key, Value: value, }, }) if err != nil { t.Fatal(err) } data, etag, err := api.GetState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } assert.NotEmpty(t, etag, "etag为空") assert.Equal(t, value, data, "value不一致") err = api.DeleteState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } data, etag, err = api.GetState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } assert.Empty(t, etag, "etag不为空") assert.Empty(t, data, "value不为空") } func TestStateSaveAndGetJson(t *testing.T) { api := state.NewAPI(daprHttpPort, 10*time.Second) defer state.DestroyAPI(api) key := utils.SimpleUUID() value, err := json.Marshal(map[string]interface{}{"tree": map[string]interface{}{"value": "aaa"}}) if err != nil { t.Fatal(err) } err = api.SaveState(stateStoreName, []state.SaveStateRequest{ { Key: key, Value: string(value), }, }) if err != nil { t.Fatal(err) } data, etag, err := api.GetState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } assert.NotEmpty(t, etag, "etag为空") assert.Equal(t, string(value), data, "value不一致") err = api.DeleteState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } data, etag, err = api.GetState(stateStoreName, key, nil) if err != nil { t.Fatal(err) } assert.Empty(t, etag, "etag不为空") assert.Empty(t, string(data), "value不为空") } func TestStateGetBulk(t *testing.T) { api := state.NewAPI(daprHttpPort, 10*time.Second) defer state.DestroyAPI(api) key1 := utils.SimpleUUID() value1 := utils.SimpleUUID() key2 := utils.SimpleUUID() value2 := utils.SimpleUUID() err := api.SaveState(stateStoreName, []state.SaveStateRequest{ { Key: key1, Value: value1, }, { Key: key2, Value: value2, }, }) if err != nil { t.Fatal(err) } getStateBulkItems, err := api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{ Keys: []string{key1, key2}, Parallelism: 1, }) if err != nil { t.Fatal(err) } assert.Equal(t, 2, len(getStateBulkItems), "获取到的state数量不正确") for _, getStateBulkItem := range getStateBulkItems { assert.NotEmpty(t, getStateBulkItem.Etag, "etag为空") if getStateBulkItem.Key != key1 && getStateBulkItem.Key != key2 { t.Fatal("批量获取到的key错误") } if getStateBulkItem.Key == key1 { assert.Equal(t, value1, getStateBulkItem.Data, "value不一致") } if getStateBulkItem.Key == key2 { assert.Equal(t, value2, getStateBulkItem.Data, "value不一致") } } err = api.DeleteState(stateStoreName, key2, nil) if err != nil { t.Fatal(err) } err = api.DeleteState(stateStoreName, key1, nil) if err != nil { t.Fatal(err) } getStateBulkItems, err = api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{ Keys: []string{key1, key2}, Parallelism: 1, }) if err != nil { t.Fatal(err) } for _, getStateBulkItem := range getStateBulkItems { assert.Empty(t, getStateBulkItem.Etag, "获取到的etag不为空") assert.Empty(t, getStateBulkItem.Data, "获取到的data不为空") } } func TestTransaction(t *testing.T) { api := state.NewAPI(daprHttpPort, 10*time.Second) defer state.DestroyAPI(api) key1 := utils.SimpleUUID() value1 := utils.SimpleUUID() key2 := utils.SimpleUUID() value2 := utils.SimpleUUID() err := api.Transaction(stateStoreName, state.TransactionRequest{ Operations: []state.TransactionOperation{ { Operation: state.TransactionUpsert, Request: state.TransactionOperationRequest{ Key: key1, Value: value1, }, }, { Operation: state.TransactionUpsert, Request: state.TransactionOperationRequest{ Key: key2, Value: value2, }, }, }, }) if err != nil { t.Fatal(err) } getStateBulkItems, err := api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{ Keys: []string{key1, key2}, Parallelism: 1, }) if err != nil { t.Fatal(err) } assert.Equal(t, 2, len(getStateBulkItems), "获取到的state数量不正确") for _, getStateBulkItem := range getStateBulkItems { assert.NotEmpty(t, getStateBulkItem.Etag, "etag为空") if getStateBulkItem.Key != key1 && getStateBulkItem.Key != key2 { t.Fatal("批量获取到的key错误") } if getStateBulkItem.Key == key1 { assert.Equal(t, value1, getStateBulkItem.Data, "value不一致") } if getStateBulkItem.Key == key2 { assert.Equal(t, value2, getStateBulkItem.Data, "value不一致") } } err = api.Transaction(stateStoreName, state.TransactionRequest{ Operations: []state.TransactionOperation{ { Operation: state.TransactionDelete, Request: state.TransactionOperationRequest{ Key: key1, }, }, { Operation: state.TransactionDelete, Request: state.TransactionOperationRequest{ Key: key2, }, }, }, }) if err != nil { t.Fatal(err) } getStateBulkItems, err = api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{ Keys: []string{key1, key2}, Parallelism: 1, }) if err != nil { t.Fatal(err) } for _, getStateBulkItem := range getStateBulkItems { assert.Empty(t, getStateBulkItem.Etag, "获取到的etag不为空") assert.Empty(t, getStateBulkItem.Data, "获取到的data不为空") } }