-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[common] Add unit test for convertions
- Loading branch information
1 parent
2d4612d
commit 246ded3
Showing
4 changed files
with
160 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package common | ||
|
||
import ( | ||
s "go.uber.org/cadence/.gen/go/shared" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPtrOf(t *testing.T) { | ||
assert.Equal(t, "a", *PtrOf("a")) | ||
assert.Equal(t, 1, *PtrOf(1)) | ||
assert.Equal(t, int32(1), *PtrOf(int32(1))) | ||
assert.Equal(t, int64(1), *PtrOf(int64(1))) | ||
assert.Equal(t, float64(1.1), *PtrOf(float64(1.1))) | ||
assert.Equal(t, true, *PtrOf(true)) | ||
} | ||
|
||
func TestPtrHelpers(t *testing.T) { | ||
assert.Equal(t, int32(1), *Int32Ptr(1)) | ||
assert.Equal(t, int64(1), *Int64Ptr(1)) | ||
assert.Equal(t, float64(1.1), *Float64Ptr(1.1)) | ||
assert.Equal(t, true, *BoolPtr(true)) | ||
assert.Equal(t, "a", *StringPtr("a")) | ||
assert.Equal(t, s.TaskList{Name: PtrOf("a")}, *TaskListPtr(s.TaskList{Name: PtrOf("a")})) | ||
assert.Equal(t, s.DecisionTypeScheduleActivityTask, *DecisionTypePtr(s.DecisionTypeScheduleActivityTask)) | ||
assert.Equal(t, s.EventTypeWorkflowExecutionStarted, *EventTypePtr(s.EventTypeWorkflowExecutionStarted)) | ||
assert.Equal(t, s.QueryTaskCompletedTypeCompleted, *QueryTaskCompletedTypePtr(s.QueryTaskCompletedTypeCompleted)) | ||
assert.Equal(t, s.TaskListKindNormal, *TaskListKindPtr(s.TaskListKindNormal)) | ||
assert.Equal(t, s.QueryResultTypeFailed, *QueryResultTypePtr(s.QueryResultTypeFailed)) | ||
} | ||
|
||
func TestCeilHelpers(t *testing.T) { | ||
assert.Equal(t, int32(2), Int32Ceil(1.1)) | ||
assert.Equal(t, int64(2), Int64Ceil(1.1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/apache/thrift/lib/go/thrift" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTListSerialize(t *testing.T) { | ||
t.Run("nil", func(t *testing.T) { | ||
data, err := TListSerialize(nil) | ||
assert.NoError(t, err) | ||
assert.Nil(t, data) | ||
}) | ||
t.Run("normal", func(t *testing.T) { | ||
ts := []thrift.TStruct{ | ||
&mockThriftStruct{Field1: "value1", Field2: 1}, | ||
&mockThriftStruct{Field1: "value2", Field2: 2}, | ||
} | ||
|
||
_, err := TListSerialize(ts) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestTListDeserialize(t *testing.T) { | ||
ts := []thrift.TStruct{ | ||
&mockThriftStruct{}, | ||
&mockThriftStruct{}, | ||
} | ||
|
||
data, err := TListSerialize(ts) | ||
assert.NoError(t, err) | ||
|
||
err = TListDeserialize(ts, data) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestIsUseThriftEncoding(t *testing.T) { | ||
ts := []interface{}{ | ||
&mockThriftStruct{}, | ||
&mockThriftStruct{}, | ||
} | ||
|
||
result := IsUseThriftEncoding(ts) | ||
assert.True(t, result) | ||
|
||
ts = []interface{}{ | ||
&mockThriftStruct{}, | ||
"string", | ||
} | ||
|
||
result = IsUseThriftEncoding(ts) | ||
assert.False(t, result) | ||
} | ||
|
||
func TestIsUseThriftDecoding(t *testing.T) { | ||
ts := []interface{}{ | ||
&mockThriftStruct{}, | ||
&mockThriftStruct{}, | ||
} | ||
|
||
assert.True(t, IsUseThriftDecoding(ts)) | ||
|
||
ts = []interface{}{ | ||
&mockThriftStruct{}, | ||
"string", | ||
} | ||
|
||
assert.False(t, IsUseThriftDecoding(ts)) | ||
} | ||
|
||
func TestIsThriftType(t *testing.T) { | ||
assert.True(t, IsThriftType(&mockThriftStruct{})) | ||
|
||
assert.False(t, IsThriftType(mockThriftStruct{})) | ||
} | ||
|
||
type mockThriftStruct struct { | ||
Field1 string | ||
Field2 int | ||
} | ||
|
||
func (m *mockThriftStruct) Read(ctx context.Context, iprot thrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
func (m *mockThriftStruct) Write(ctx context.Context, oprot thrift.TProtocol) error { | ||
return nil | ||
} | ||
|
||
func (m *mockThriftStruct) String() string { | ||
return "" | ||
} |