llzai/axonhub
0
1package objects
2
3import (
4 "bytes"
5 "testing"
6)
7
8func TestGUID_MarshalGQL(t *testing.T) {
9 type fields struct {
10 Type string
11 UUID int
12 }
13
14 tests := []struct {
15 name string
16 fields fields
17 wantW string
18 }{
19 {
20 name: "gid",
21 fields: fields{
22 Type: "type",
23 UUID: 1,
24 },
25 wantW: `"gid://axonhub/type/1"`,
26 },
27 }
28 for _, tt := range tests {
29 t.Run(tt.name, func(t *testing.T) {
30 guid := GUID{
31 Type: tt.fields.Type,
32 ID: tt.fields.UUID,
33 }
34 w := &bytes.Buffer{}
35 guid.MarshalGQL(w)
36
37 if gotW := w.String(); gotW != tt.wantW {
38 t.Errorf("GUID.MarshalGQL() = %v, want %v", gotW, tt.wantW)
39 }
40 })
41 }
42}
43
44func TestGUID_UnmarshalGQL(t *testing.T) {
45 type fields struct {
46 Type string
47 ID int
48 }
49
50 type args struct {
51 v any
52 }
53
54 tests := []struct {
55 name string
56 fields fields
57 args args
58 wantErr bool
59 }{
60 {
61 name: "gid",
62 fields: fields{
63 Type: "type",
64 ID: 1,
65 },
66 args: args{
67 v: "gid://axonhub/type/1",
68 },
69 },
70 {
71 name: "empty",
72 fields: fields{
73 Type: "",
74 ID: 0,
75 },
76 args: args{
77 v: "",
78 },
79 wantErr: true,
80 },
81 {
82 name: "invalid",
83 fields: fields{
84 Type: "type",
85 ID: 0,
86 },
87 args: args{
88 v: "gid://axonhub/type/invalid",
89 },
90 wantErr: true,
91 },
92 {
93 name: "invalid prefix",
94 fields: fields{
95 Type: "type",
96 ID: 0,
97 },
98 args: args{
99 v: "guid://invalid/1",
100 },
101 wantErr: true,
102 },
103 {
104 name: "old format should fail",
105 fields: fields{
106 Type: "type",
107 ID: 0,
108 },
109 args: args{
110 v: "gid://type/1",
111 },
112 wantErr: true,
113 },
114 {
115 name: "missing axonhub namespace",
116 fields: fields{
117 Type: "type",
118 ID: 0,
119 },
120 args: args{
121 v: "gid://other/type/1",
122 },
123 wantErr: true,
124 },
125 }
126 for _, tt := range tests {
127 t.Run(tt.name, func(t *testing.T) {
128 guid := &GUID{
129 Type: tt.fields.Type,
130 ID: tt.fields.ID,
131 }
132
133 err := guid.UnmarshalGQL(tt.args.v)
134 if (err != nil) != tt.wantErr {
135 t.Errorf("GUID.UnmarshalGQL() error = %v, wantErr %v", err, tt.wantErr)
136 }
137 })
138 }
139}
140 