Skip to content

Commit

Permalink
Modify test codes for counter
Browse files Browse the repository at this point in the history
As the panic methods in counter were replaced,
the related test codes also were modified.
  • Loading branch information
USER authored and USER committed Aug 4, 2023
1 parent 83c565a commit 2a6c54c
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions pkg/document/crdt/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,55 @@ import (

func TestCounter(t *testing.T) {
t.Run("new counter test", func(t *testing.T) {
intCntWithInt32Value := crdt.NewCounter(crdt.IntegerCnt, int32(math.MaxInt32), time.InitialTicket)
intCntWithInt32Value, err := crdt.NewCounter(crdt.IntegerCnt, int32(math.MaxInt32), time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.IntegerCnt, intCntWithInt32Value.ValueType())

intCntWithInt64Value := crdt.NewCounter(crdt.IntegerCnt, int64(math.MaxInt32+1), time.InitialTicket)
intCntWithInt64Value, err := crdt.NewCounter(crdt.IntegerCnt, int64(math.MaxInt32+1), time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.IntegerCnt, intCntWithInt64Value.ValueType())

intCntWithIntValue := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket)
intCntWithIntValue, err := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.IntegerCnt, intCntWithIntValue.ValueType())

intCntWithDoubleValue := crdt.NewCounter(crdt.IntegerCnt, 0.5, time.InitialTicket)
intCntWithDoubleValue, err := crdt.NewCounter(crdt.IntegerCnt, 0.5, time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.IntegerCnt, intCntWithDoubleValue.ValueType())

intCntWithUnsupportedValue := func() { crdt.NewCounter(crdt.IntegerCnt, "", time.InitialTicket) }
assert.Panics(t, intCntWithUnsupportedValue)
_, err = crdt.NewCounter(crdt.IntegerCnt, "", time.InitialTicket)
assert.EqualError(t, err, "unsupported type")

longCntWithInt32Value := crdt.NewCounter(crdt.LongCnt, int32(math.MaxInt32), time.InitialTicket)
longCntWithInt32Value, err := crdt.NewCounter(crdt.LongCnt, int32(math.MaxInt32), time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.LongCnt, longCntWithInt32Value.ValueType())

longCntWithInt64Value := crdt.NewCounter(crdt.LongCnt, int64(math.MaxInt32+1), time.InitialTicket)
longCntWithInt64Value, err := crdt.NewCounter(crdt.LongCnt, int64(math.MaxInt32+1), time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.LongCnt, longCntWithInt64Value.ValueType())

longCntWithIntValue := crdt.NewCounter(crdt.LongCnt, math.MaxInt32+1, time.InitialTicket)
longCntWithIntValue, err := crdt.NewCounter(crdt.LongCnt, math.MaxInt32+1, time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.LongCnt, longCntWithIntValue.ValueType())

longCntWithDoubleValue := crdt.NewCounter(crdt.LongCnt, 0.5, time.InitialTicket)
longCntWithDoubleValue, err := crdt.NewCounter(crdt.LongCnt, 0.5, time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, crdt.LongCnt, longCntWithDoubleValue.ValueType())

longCntWithUnsupportedValue := func() { crdt.NewCounter(crdt.LongCnt, "", time.InitialTicket) }
assert.Panics(t, longCntWithUnsupportedValue)
_, err = crdt.NewCounter(crdt.LongCnt, "", time.InitialTicket)
assert.EqualError(t, err, "unsupported type")
})

t.Run("increase test", func(t *testing.T) {
var x = 5
var y int64 = 10
var z = 3.14
integer := crdt.NewCounter(crdt.IntegerCnt, x, time.InitialTicket)
long := crdt.NewCounter(crdt.LongCnt, y, time.InitialTicket)
double := crdt.NewCounter(crdt.IntegerCnt, z, time.InitialTicket)
integer, err := crdt.NewCounter(crdt.IntegerCnt, x, time.InitialTicket)
assert.NoError(t, err)
long, err := crdt.NewCounter(crdt.LongCnt, y, time.InitialTicket)
assert.NoError(t, err)
double, err := crdt.NewCounter(crdt.IntegerCnt, z, time.InitialTicket)
assert.NoError(t, err)

integerOperand := crdt.NewPrimitive(x, time.InitialTicket)
longOperand := crdt.NewPrimitive(y, time.InitialTicket)
Expand All @@ -90,29 +101,23 @@ func TestCounter(t *testing.T) {
assert.Equal(t, double.Marshal(), "21")

// error process test
// TODO: it should be modified to error check
// when 'Remove panic from server code (#50)' is completed.
unsupportedTypePanicTest := func() {
r := recover()
assert.NotNil(t, r)
assert.Equal(t, r, "unsupported type")
unsupportedTypeErrorTest := func(v interface{}) {
_, err = crdt.NewCounter(crdt.IntegerCnt, v, time.InitialTicket)
assert.EqualError(t, err, "unsupported type")
}
unsupportedTest := func(v interface{}) {
defer unsupportedTypePanicTest()
crdt.NewCounter(crdt.IntegerCnt, v, time.InitialTicket)
}
unsupportedTest("str")
unsupportedTest(true)
unsupportedTest([]byte{2})
unsupportedTest(gotime.Now())
unsupportedTypeErrorTest("str")
unsupportedTypeErrorTest(true)
unsupportedTypeErrorTest([]byte{2})
unsupportedTypeErrorTest(gotime.Now())

assert.Equal(t, integer.Marshal(), "23")
assert.Equal(t, long.Marshal(), "28")
assert.Equal(t, double.Marshal(), "21")
})

t.Run("Counter value overflow test", func(t *testing.T) {
integer := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket)
integer, err := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket)
assert.NoError(t, err)
assert.Equal(t, integer.ValueType(), crdt.IntegerCnt)

operand := crdt.NewPrimitive(1, time.InitialTicket)
Expand Down

0 comments on commit 2a6c54c

Please sign in to comment.