|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | "github.com/google/go-cmp/cmp" |
| 7 | + "github.com/sqlc-dev/sqlc/internal/plugin" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func TestTypeOverrides(t *testing.T) { |
@@ -115,3 +116,88 @@ func FuzzOverride(f *testing.F) { |
115 | 116 | o.parse(nil) |
116 | 117 | }) |
117 | 118 | } |
| 119 | + |
| 120 | +func TestOverride_MatchesColumn(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + type testCase struct { |
| 123 | + specName string |
| 124 | + override Override |
| 125 | + Column *plugin.Column |
| 126 | + engine string |
| 127 | + expected bool |
| 128 | + } |
| 129 | + |
| 130 | + testCases := []*testCase{ |
| 131 | + { |
| 132 | + specName: "matches with pg_catalog in schema and name", |
| 133 | + override: Override{ |
| 134 | + DBType: "json", |
| 135 | + Nullable: false, |
| 136 | + }, |
| 137 | + Column: &plugin.Column{ |
| 138 | + Name: "data", |
| 139 | + Type: &plugin.Identifier{ |
| 140 | + Schema: "pg_catalog", |
| 141 | + Name: "json", |
| 142 | + }, |
| 143 | + NotNull: true, |
| 144 | + IsArray: false, |
| 145 | + }, |
| 146 | + engine: "postgresql", |
| 147 | + expected: true, |
| 148 | + }, |
| 149 | + { |
| 150 | + specName: "matches only with name", |
| 151 | + override: Override{ |
| 152 | + DBType: "json", |
| 153 | + Nullable: false, |
| 154 | + }, |
| 155 | + Column: &plugin.Column{ |
| 156 | + Name: "data", |
| 157 | + Type: &plugin.Identifier{ |
| 158 | + Name: "json", |
| 159 | + }, |
| 160 | + NotNull: true, |
| 161 | + IsArray: false, |
| 162 | + }, |
| 163 | + engine: "postgresql", |
| 164 | + expected: true, |
| 165 | + }, |
| 166 | + { |
| 167 | + specName: "matches with pg_catalog in name", |
| 168 | + override: Override{ |
| 169 | + DBType: "json", |
| 170 | + Nullable: false, |
| 171 | + }, |
| 172 | + Column: &plugin.Column{ |
| 173 | + Name: "data", |
| 174 | + Type: &plugin.Identifier{ |
| 175 | + Name: "pg_catalog.json", |
| 176 | + }, |
| 177 | + NotNull: true, |
| 178 | + IsArray: false, |
| 179 | + }, |
| 180 | + engine: "postgresql", |
| 181 | + expected: true, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, test := range testCases { |
| 186 | + tt := *test |
| 187 | + t.Run(tt.specName, func(t *testing.T) { |
| 188 | + result := tt.override.MatchesColumn(tt.Column, tt.engine) |
| 189 | + if result != tt.expected { |
| 190 | + t.Errorf("mismatch; got %v; want %v", result, tt.expected) |
| 191 | + } |
| 192 | + if tt.engine == "postgresql" && tt.expected == true { |
| 193 | + tt.override.DBType = "pg_catalog." + tt.override.DBType |
| 194 | + result = tt.override.MatchesColumn(test.Column, tt.engine) |
| 195 | + if !result { |
| 196 | + t.Errorf("mismatch; got %v; want %v", result, tt.expected) |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + }) |
| 201 | + |
| 202 | + } |
| 203 | +} |
0 commit comments