|
| 1 | +package generator |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestScalarMapping(t *testing.T) { |
| 8 | + config := &Config{ |
| 9 | + Scalars: map[string]ScalarMapping{ |
| 10 | + "ID": { |
| 11 | + Model: []string{ |
| 12 | + "github.com/google/uuid.UUID", |
| 13 | + "github.com/gofrs/uuid.UUID", |
| 14 | + }, |
| 15 | + }, |
| 16 | + "DateTime": { |
| 17 | + Model: []string{ |
| 18 | + "time.Time", |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + goTypePath string |
| 27 | + expectedName string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "Google UUID maps to ID", |
| 31 | + goTypePath: "github.com/google/uuid.UUID", |
| 32 | + expectedName: "ID", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Gofrs UUID maps to ID", |
| 36 | + goTypePath: "github.com/gofrs/uuid.UUID", |
| 37 | + expectedName: "ID", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "time.Time maps to DateTime", |
| 41 | + goTypePath: "time.Time", |
| 42 | + expectedName: "DateTime", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Unmapped type returns empty", |
| 46 | + goTypePath: "github.com/example/CustomType", |
| 47 | + expectedName: "", |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + result := config.GetScalarForGoType(tt.goTypePath) |
| 54 | + if result != tt.expectedName { |
| 55 | + t.Errorf("GetScalarForGoType(%q) = %q, want %q", tt.goTypePath, result, tt.expectedName) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestIsBuiltInScalar(t *testing.T) { |
| 62 | + tests := []struct { |
| 63 | + name string |
| 64 | + scalarName string |
| 65 | + expected bool |
| 66 | + }{ |
| 67 | + {"Int is built-in", "Int", true}, |
| 68 | + {"Float is built-in", "Float", true}, |
| 69 | + {"String is built-in", "String", true}, |
| 70 | + {"Boolean is built-in", "Boolean", true}, |
| 71 | + {"ID is built-in", "ID", true}, |
| 72 | + {"DateTime is custom", "DateTime", false}, |
| 73 | + {"UUID is custom", "UUID", false}, |
| 74 | + {"JSON is custom", "JSON", false}, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + result := IsBuiltInScalar(tt.scalarName) |
| 80 | + if result != tt.expected { |
| 81 | + t.Errorf("IsBuiltInScalar(%q) = %v, want %v", tt.scalarName, result, tt.expected) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestGetUsedCustomScalars(t *testing.T) { |
| 88 | + config := &Config{ |
| 89 | + KnownScalars: []string{"DateTime", "Upload"}, |
| 90 | + Scalars: map[string]ScalarMapping{ |
| 91 | + "ID": { |
| 92 | + Model: []string{"github.com/google/uuid.UUID"}, |
| 93 | + }, |
| 94 | + "DateTime": { |
| 95 | + Model: []string{"time.Time"}, |
| 96 | + }, |
| 97 | + "Upload": { |
| 98 | + Model: []string{"github.com/99designs/gqlgen/graphql.Upload"}, |
| 99 | + }, |
| 100 | + "CustomScalar": { |
| 101 | + Model: []string{"github.com/example/pkg.CustomType"}, |
| 102 | + }, |
| 103 | + "Int": { |
| 104 | + Model: []string{"int64"}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + customScalars := config.GetUsedCustomScalars() |
| 110 | + |
| 111 | + // Should only return CustomScalar |
| 112 | + // - ID and Int are built-in GraphQL scalars |
| 113 | + // - DateTime and Upload are in known_scalars |
| 114 | + expectedCount := 1 // Only CustomScalar |
| 115 | + if len(customScalars) != expectedCount { |
| 116 | + t.Errorf("GetUsedCustomScalars() returned %d scalars, want %d. Got: %v", len(customScalars), expectedCount, customScalars) |
| 117 | + } |
| 118 | + |
| 119 | + // Check that CustomScalar is in the list |
| 120 | + found := false |
| 121 | + for _, scalar := range customScalars { |
| 122 | + if scalar == "CustomScalar" { |
| 123 | + found = true |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | + if !found { |
| 128 | + t.Errorf("GetUsedCustomScalars() did not include 'CustomScalar'") |
| 129 | + } |
| 130 | + |
| 131 | + // Check that built-in scalars and known_scalars are not included |
| 132 | + for _, scalar := range customScalars { |
| 133 | + if scalar == "ID" || scalar == "Int" { |
| 134 | + t.Errorf("GetUsedCustomScalars() should not include built-in scalar %q", scalar) |
| 135 | + } |
| 136 | + if scalar == "DateTime" || scalar == "Upload" { |
| 137 | + t.Errorf("GetUsedCustomScalars() should not include known_scalar %q", scalar) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments