|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package bsonutil |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/types" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIDToBsonBinary_ValidUUID(t *testing.T) { |
| 12 | + id := "550e8400-e29b-41d4-a716-446655440000" |
| 13 | + bin := IDToBsonBinary(id) |
| 14 | + |
| 15 | + if bin.Subtype != 0x00 { |
| 16 | + t.Errorf("expected subtype 0x00, got 0x%02x", bin.Subtype) |
| 17 | + } |
| 18 | + if len(bin.Data) != 16 { |
| 19 | + t.Errorf("expected 16 bytes, got %d", len(bin.Data)) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestIDToBsonBinary_PanicsOnInvalidUUID(t *testing.T) { |
| 24 | + defer func() { |
| 25 | + if r := recover(); r == nil { |
| 26 | + t.Error("expected panic on invalid UUID, got none") |
| 27 | + } |
| 28 | + }() |
| 29 | + IDToBsonBinary("not-a-uuid") |
| 30 | +} |
| 31 | + |
| 32 | +func TestIDToBsonBinary_PanicsOnEmptyString(t *testing.T) { |
| 33 | + defer func() { |
| 34 | + if r := recover(); r == nil { |
| 35 | + t.Error("expected panic on empty string, got none") |
| 36 | + } |
| 37 | + }() |
| 38 | + IDToBsonBinary("") |
| 39 | +} |
| 40 | + |
| 41 | +func TestBsonBinaryToID_Roundtrip(t *testing.T) { |
| 42 | + id := "550e8400-e29b-41d4-a716-446655440000" |
| 43 | + bin := IDToBsonBinary(id) |
| 44 | + got := BsonBinaryToID(bin) |
| 45 | + if got != id { |
| 46 | + t.Errorf("roundtrip failed: got %q, want %q", got, id) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestNewIDBsonBinary_ProducesValidUUID(t *testing.T) { |
| 51 | + bin := NewIDBsonBinary() |
| 52 | + if bin.Subtype != 0x00 { |
| 53 | + t.Errorf("expected subtype 0x00, got 0x%02x", bin.Subtype) |
| 54 | + } |
| 55 | + if len(bin.Data) != 16 { |
| 56 | + t.Errorf("expected 16 bytes, got %d", len(bin.Data)) |
| 57 | + } |
| 58 | + |
| 59 | + // Convert back and validate UUID format |
| 60 | + id := BsonBinaryToID(bin) |
| 61 | + if !types.ValidateID(id) { |
| 62 | + t.Errorf("generated ID is not valid UUID format: %q", id) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestNewIDBsonBinary_Uniqueness(t *testing.T) { |
| 67 | + seen := make(map[string]bool) |
| 68 | + for i := 0; i < 100; i++ { |
| 69 | + id := BsonBinaryToID(NewIDBsonBinary()) |
| 70 | + if seen[id] { |
| 71 | + t.Fatalf("duplicate ID generated: %q", id) |
| 72 | + } |
| 73 | + seen[id] = true |
| 74 | + } |
| 75 | +} |
0 commit comments