summaryrefslogtreecommitdiffstats
path: root/snowflake_test.go
blob: e69644c39c941eef2d8f433b99a2f037fb9e91bd (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package snowflake

import (
	"encoding/json"
	"testing"
)

func TestNew(t *testing.T) {
	s := New()
	if s.IsZero() {
		t.Error("New() returned zero Snowflake")
	}
}

func TestNewUnique(t *testing.T) {
	seen := make(map[string]bool, 1000)

	for i := 0; i < 1000; i++ {
		s := New()

		str := s.String()
		if seen[str] {
			t.Fatalf("duplicate ID generated: %s", str)
		}
		seen[str] = true
	}
}

func TestIsZero(t *testing.T) {
	zero := Zero()
	if !zero.IsZero() {
		t.Error("Zero Snowflake should be zero")
	}

	nonZero := New()
	if nonZero.IsZero() {
		t.Error("New Snowflake should not be zero")
	}
}

func TestStringRoundTrip(t *testing.T) {
	original := New()
	str := original.String()
	parsed, err := FromString(str)
	if err != nil {
		t.Fatalf("FromString failed: %v", err)
	}

	if parsed != original {
		t.Errorf("round-trip mismatch: original=%+v, parsed=%+v", original, parsed)
	}
}

func TestStringRoundTripMany(t *testing.T) {
	for i := 0; i < 100; i++ {
		original := New()

		str := original.String()
		parsed, err := FromString(str)
		if err != nil {
			t.Fatalf("iteration %d: FromString failed: %v", i, err)
		}
		if parsed != original {
			t.Fatalf("iteration %d: round-trip mismatch", i)
		}
	}
}

func TestFromStringInvalidChars(t *testing.T) {
	_, err := FromString("!!invalid!!")
	if err != ErrInvalidEncoding {
		t.Errorf("expected ErrInvalidEncoding, got: %v", err)
	}
}

func TestFromStringEmpty(t *testing.T) {
	s, err := FromString("")
	if err != nil {
		t.Errorf("unexpected error for empty string: %v", err)
	}
	if !s.IsZero() {
		t.Error("empty string should produce zero Snowflake")
	}
}

func TestFromStringTooLong(t *testing.T) {
	_, err := FromString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
	if err != ErrInvalidEncoding {
		t.Errorf("expected ErrInvalidEncoding for overflow, got: %v", err)
	}
}

func TestHexString(t *testing.T) {
	s := New()
	hex := s.Hex()

	if len(hex) != IDByteSize*2 {
		t.Errorf("expected hex length %d, got %d", IDByteSize*2, len(hex))
	}

	for _, c := range hex {
		if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
			t.Errorf("invalid hex character: %c", c)
		}
	}
}

func TestZeroString(t *testing.T) {
	zero := Zero()
	str := zero.String()
	if str != "0" {
		t.Errorf("Zero Snowflake should stringify to %q, got: %q", "0", str)
	}

	parsed, err := FromString(str)
	if err != nil {
		t.Fatalf("FromString(%q) failed: %v", str, err)
	}
	if !parsed.IsZero() {
		t.Error("FromString(Zero().String()) should round-trip to zero")
	}
}

func TestHexRoundTrip(t *testing.T) {
	original := New()
	hex := original.Hex()
	parsed, err := FromHex(hex)
	if err != nil {
		t.Fatalf("FromHex failed: %v", err)
	}
	if parsed != original {
		t.Error("hex round-trip mismatch")
	}
}

func TestFromHexInvalid(t *testing.T) {
	_, err := FromHex("xyz")
	if err == nil {
		t.Error("expected error for invalid hex")
	}
}

func TestFromHexWrongLength(t *testing.T) {
	_, err := FromHex("01")
	if err != ErrInvalidIDLength {
		t.Errorf("expected ErrInvalidIDLength, got: %v", err)
	}
}

func TestMarshalText(t *testing.T) {
	s := New()
	text, err := s.MarshalText()
	if err != nil {
		t.Fatalf("MarshalText failed: %v", err)
	}

	var decoded Snowflake
	err = decoded.UnmarshalText(text)
	if err != nil {
		t.Fatalf("UnmarshalText failed: %v", err)
	}

	if decoded != s {
		t.Error("text marshal/unmarshal mismatch")
	}
}

func TestUnmarshalTextInvalid(t *testing.T) {
	var s Snowflake
	err := s.UnmarshalText([]byte("!!!"))
	if err == nil {
		t.Error("expected error for invalid text")
	}
}

func TestMarshalJSON(t *testing.T) {
	s := New()
	data, err := s.MarshalJSON()
	if err != nil {
		t.Fatalf("MarshalJSON failed: %v", err)
	}

	var decoded Snowflake
	err = json.Unmarshal(data, &decoded)
	if err != nil {
		t.Fatalf("json.Unmarshal failed: %v", err)
	}

	if decoded != s {
		t.Error("JSON marshal/unmarshal mismatch")
	}
}

func TestUnmarshalJSONMissingQuotes(t *testing.T) {
	var s Snowflake
	err := s.UnmarshalJSON([]byte(`invalid`))
	if err != ErrInvalidEncoding {
		t.Errorf("expected ErrInvalidEncoding, got: %v", err)
	}
}

func TestUnmarshalJSONEmpty(t *testing.T) {
	var s Snowflake
	err := s.UnmarshalJSON([]byte(`""`))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if !s.IsZero() {
		t.Error("empty JSON string should produce zero Snowflake")
	}
}

func TestUnmarshalJSONInvalidInner(t *testing.T) {
	var s Snowflake
	err := s.UnmarshalJSON([]byte(`"!!!"`))
	if err != ErrInvalidEncoding {
		t.Errorf("expected ErrInvalidEncoding for invalid inner content, got: %v", err)
	}
}

func TestDriverValue(t *testing.T) {
	s := New()
	val, err := s.Value()
	if err != nil {
		t.Fatalf("Value failed: %v", err)
	}

	bytes, ok := val.([]byte)
	if !ok {
		t.Fatal("Value should return []byte")
	}

	if len(bytes) != IDByteSize {
		t.Errorf("expected byte length %d, got %d", IDByteSize, len(bytes))
	}
}

func TestDriverScan(t *testing.T) {
	original := New()
	bytes := make([]byte, IDByteSize)
	copy(bytes, original.bytes[:])

	var s Snowflake
	if err := s.Scan(bytes); err != nil {
		t.Fatalf("Scan failed: %v", err)
	}

	if s != original {
		t.Error("Scan produced mismatched value")
	}
}

func TestDriverScanString(t *testing.T) {
	original := New()
	bytes := make([]byte, IDByteSize)
	copy(bytes, original.bytes[:])

	var s Snowflake
	if err := s.Scan(string(bytes)); err != nil {
		t.Fatalf("Scan(string) failed: %v", err)
	}

	if s != original {
		t.Error("Scan(string) produced mismatched value")
	}
}

func TestDriverScanNil(t *testing.T) {
	var s Snowflake
	err := s.Scan(nil)
	if err != nil {
		t.Fatalf("Scan(nil) failed: %v", err)
	}

	if !s.IsZero() {
		t.Error("Scan(nil) should produce zero Snowflake")
	}
}

func TestDriverScanWrongLength(t *testing.T) {
	var s Snowflake
	err := s.Scan([]byte{1, 2, 3})
	if err != ErrInvalidIDLength {
		t.Errorf("expected ErrInvalidIDLength, got: %v", err)
	}
}

func TestDriverScanZeroBytes(t *testing.T) {
	var s Snowflake
	zeroBytes := make([]byte, IDByteSize)
	err := s.Scan(zeroBytes)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if !s.IsZero() {
		t.Error("Scan with all-zero bytes should produce zero Snowflake")
	}
}

func TestDriverScanUnsupportedType(t *testing.T) {
	var s Snowflake
	err := s.Scan(42)
	if err == nil {
		t.Error("expected error for unsupported type")
	}
}

func TestJSONEmbedding(t *testing.T) {
	type Wrapper struct {
		Name string     `json:"name"`
		ID   Snowflake  `json:"id"`
		Ref  *Snowflake `json:"ref,omitempty"`
	}

	s := New()
	w := Wrapper{
		Name: "test",
		ID:   s,
		Ref:  &s,
	}

	data, err := json.Marshal(&w)
	if err != nil {
		t.Fatalf("marshal failed: %v", err)
	}

	var decoded Wrapper
	err = json.Unmarshal(data, &decoded)
	if err != nil {
		t.Fatalf("unmarshal failed: %v", err)
	}

	if decoded.ID != s {
		t.Error("embedded ID mismatch")
	}

	if decoded.Ref == nil || *decoded.Ref != s {
		t.Error("embedded ref mismatch")
	}
}