You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser_protobuf_test.go 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package parser
  18. import (
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. "seata.apache.org/seata-go/pkg/datasource/sql/undo"
  22. )
  23. func TestProtobufGetName(t *testing.T) {
  24. assert.Equal(t, "protobuf", (&ProtobufParser{}).GetName())
  25. }
  26. func TestProtobufDefaultContext(t *testing.T) {
  27. assert.Equal(t, []byte{}, (&ProtobufParser{}).GetDefaultContent())
  28. }
  29. func TestProtobufEncodeDecode(t *testing.T) {
  30. TestCases := []struct {
  31. CaseName string
  32. UndoLog *undo.BranchUndoLog
  33. ExpectErr bool
  34. }{
  35. {
  36. CaseName: "pass",
  37. UndoLog: &undo.BranchUndoLog{
  38. Xid: "123456",
  39. BranchID: 123456,
  40. Logs: []undo.SQLUndoLog{},
  41. },
  42. ExpectErr: false,
  43. },
  44. }
  45. for _, Case := range TestCases {
  46. t.Run(Case.CaseName, func(t *testing.T) {
  47. parser := &ProtobufParser{}
  48. encodedBytes, err := parser.Encode(Case.UndoLog)
  49. if Case.ExpectErr {
  50. assert.NotNil(t, err)
  51. return
  52. }
  53. assert.Nil(t, err)
  54. decodedUndoLog, err := parser.Decode(encodedBytes)
  55. if Case.ExpectErr {
  56. assert.NotNil(t, err)
  57. return
  58. }
  59. assert.Nil(t, err)
  60. assert.Equal(t, Case.UndoLog.Xid, decodedUndoLog.Xid)
  61. assert.Equal(t, Case.UndoLog.BranchID, decodedUndoLog.BranchID)
  62. assert.Equal(t, len(Case.UndoLog.Logs), len(decodedUndoLog.Logs))
  63. })
  64. }
  65. }
  66. func TestConvertInterfaceToAnyAndBack(t *testing.T) {
  67. originalValue := map[string]interface{}{
  68. "key1": "value1",
  69. "key2": float64(123), // Use float64 to match JSON default behavior
  70. "key3": true,
  71. }
  72. anyValue, err := convertInterfaceToAny(originalValue)
  73. assert.NoError(t, err, "convertInterfaceToAny should not return an error")
  74. convertedValue, err := convertAnyToInterface(anyValue)
  75. assert.NoError(t, err, "convertAnyToInterface should not return an error")
  76. assert.Equal(t, originalValue, convertedValue, "The converted value should match the original")
  77. }