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_json_test.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestJsonGetName(t *testing.T) {
  24. assert.Equal(t, "json", (&JsonParser{}).GetName())
  25. }
  26. func TestJsonGetDefaultContext(t *testing.T) {
  27. assert.Equal(t, []byte("{}"), (&JsonParser{}).GetDefaultContent())
  28. }
  29. func TestJsonEncode(t *testing.T) {
  30. TestCases := []struct {
  31. CaseName string
  32. UndoLog *undo.BranchUndoLog
  33. ExpectErr bool
  34. ExpectBytes string
  35. }{
  36. {
  37. CaseName: "pass",
  38. UndoLog: &undo.BranchUndoLog{
  39. Xid: "123456",
  40. BranchID: 123456,
  41. Logs: []undo.SQLUndoLog{},
  42. },
  43. ExpectErr: false,
  44. ExpectBytes: `{"xid":"123456","branchId":123456,"sqlUndoLogs":[]}`,
  45. },
  46. }
  47. for _, Case := range TestCases {
  48. t.Run(Case.CaseName, func(t *testing.T) {
  49. logParser := &JsonParser{}
  50. bytes, err := logParser.Encode(Case.UndoLog)
  51. if Case.ExpectErr {
  52. assert.NotNil(t, err)
  53. } else {
  54. assert.Nil(t, err)
  55. }
  56. assert.Equal(t, Case.ExpectBytes, string(bytes))
  57. })
  58. }
  59. }
  60. func TestJsonDecode(t *testing.T) {
  61. TestCases := []struct {
  62. CaseName string
  63. ExpectUndoLog undo.BranchUndoLog
  64. ExpectErr bool
  65. InputBytes string
  66. }{
  67. {
  68. CaseName: "pass",
  69. ExpectUndoLog: undo.BranchUndoLog{
  70. Xid: "123456",
  71. BranchID: 123456,
  72. Logs: []undo.SQLUndoLog{},
  73. },
  74. ExpectErr: false,
  75. InputBytes: `{"xid":"123456","branchId":123456,"sqlUndoLogs":[]}`,
  76. },
  77. }
  78. for _, Case := range TestCases {
  79. t.Run(Case.CaseName, func(t *testing.T) {
  80. logParser := &JsonParser{}
  81. undoLog, err := logParser.Decode([]byte(Case.InputBytes))
  82. if Case.ExpectErr {
  83. assert.NotNil(t, err)
  84. } else {
  85. assert.Nil(t, err)
  86. }
  87. assert.Equal(t, undoLog.Xid, Case.ExpectUndoLog.Xid)
  88. assert.Equal(t, undoLog.BranchID, Case.ExpectUndoLog.BranchID)
  89. assert.Equal(t, len(undoLog.Logs), len(Case.ExpectUndoLog.Logs))
  90. })
  91. }
  92. }