123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- 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不为空")
- }
- }
|