-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoncrunch_test.go
More file actions
63 lines (56 loc) · 1.28 KB
/
jsoncrunch_test.go
File metadata and controls
63 lines (56 loc) · 1.28 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
package tok_test
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/GrayCodeAI/tok"
)
func TestCompressJSON_LargeArray(t *testing.T) {
var items []string
for i := 0; i < 50; i++ {
items = append(items, fmt.Sprintf(`{"id":%d,"status":"ok"}`, i))
}
// inject an error item
items[25] = `{"id":25,"status":"error"}`
input := "[" + join(items, ",") + "]"
out := tok.CompressJSON(input, 20)
var result []json.RawMessage
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("invalid JSON output: %v", err)
}
if len(result) > 20 {
t.Errorf("expected <= 20 items, got %d", len(result))
}
if len(result) >= 50 {
t.Error("expected array to be reduced")
}
// error item must be kept
if !strings.Contains(out, `"error"`) {
t.Error("expected error item preserved")
}
}
func TestCompressJSON_SmallArray(t *testing.T) {
input := `[1,2,3]`
out := tok.CompressJSON(input, 20)
if out != input {
t.Errorf("expected unchanged, got %s", out)
}
}
func TestCompressJSON_NotArray(t *testing.T) {
input := `{"key":"value"}`
out := tok.CompressJSON(input, 20)
if out != input {
t.Errorf("expected unchanged, got %s", out)
}
}
func join(s []string, sep string) string {
r := ""
for i, v := range s {
if i > 0 {
r += sep
}
r += v
}
return r
}