-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql2json_test.go
More file actions
96 lines (92 loc) · 2.38 KB
/
sql2json_test.go
File metadata and controls
96 lines (92 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dbf
import (
"testing"
)
// Testing AnyToJson function using table driven tests
func TestAnyToJson(t *testing.T) {
cases := []struct {
name string
data interface{}
expected string
expectedError error
}{
{
name: "Valid Struct",
data: struct{ Name string }{"John"},
expected: `{"Name":"John"}`,
expectedError: nil,
},
{
name: "Valid Map",
data: map[string]string{"name": "John", "age": "30"},
expected: `{"age":"30","name":"John"}`,
expectedError: nil,
},
{
name: "Valid String",
data: "Hello World",
expected: `"Hello World"`,
expectedError: nil,
},
{
name: "Valid Int",
data: 123,
expected: `123`,
expectedError: nil,
},
{
name: "Valid ArraySlice",
data: []string{"John", "Doe"},
expected: `["John","Doe"]`,
expectedError: nil,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
jsonBytes, err := AnyToJson(tc.data)
if err != nil && tc.expectedError == nil {
t.Errorf("Test %s failed: expected no error, but got: %v", tc.name, err)
return
}
if err == nil && tc.expectedError != nil {
t.Errorf("Test %s failed: expected error %v, but got nil", tc.name, tc.expectedError)
return
}
if err != nil && tc.expectedError != nil {
if err.Error() != tc.expectedError.Error() {
t.Errorf("Test %s failed: expected error %v, but got %v", tc.name, tc.expectedError, err)
}
return
}
if string(jsonBytes) != tc.expected {
t.Errorf("Test %s failed:\nexpected:\n%s\n\nbut got:\n%s\n", tc.name, tc.expected, string(jsonBytes))
}
})
}
}
func BenchmarkAnyToJson(b *testing.B) {
data := struct {
ID int64 `json:"id"`
Product string `json:"product"`
Description interface{} `json:"description"`
Price float64 `json:"price"`
Qty int64 `json:"qty"`
Date string `json:"date"`
}{
ID: 123,
Product: "Product test",
Description: "This is a product",
Price: 1500.0,
Qty: 100,
Date: "2024-12-25",
}
b.Run("Benchmarking AnyToJson", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := AnyToJson(data)
if err != nil {
b.Fatalf("Error during benchmarking: %v", err)
}
}
})
}