github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/config_test.go (about) 1 package drivers 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestConfigMustString(t *testing.T) { 9 t.Parallel() 10 11 key := "string" 12 tests := []struct { 13 Config map[string]interface{} 14 Value string 15 }{ 16 { 17 Config: map[string]interface{}{key: "str"}, 18 Value: "str", 19 }, 20 { 21 Config: map[string]interface{}{key: ""}, 22 Value: "", 23 }, 24 { 25 Config: map[string]interface{}{key: 5}, 26 Value: "", 27 }, 28 { 29 Config: map[string]interface{}{}, 30 Value: "", 31 }, 32 } 33 34 for i, test := range tests { 35 var value string 36 var paniced interface{} 37 38 func() { 39 defer func() { 40 if r := recover(); r != nil { 41 paniced = r 42 } 43 }() 44 value = Config(test.Config).MustString(key) 45 }() 46 47 if len(test.Value) != 0 { 48 if paniced != nil { 49 t.Error(i, "wanted a value, but panic'd:", paniced) 50 } else if value != test.Value { 51 t.Error(i, "want:", test.Value, "got:", value) 52 } 53 } else { 54 if paniced == nil { 55 t.Error(i, "expected it to panic") 56 } 57 } 58 } 59 } 60 61 func TestConfigMustInt(t *testing.T) { 62 t.Parallel() 63 64 key := "integer" 65 tests := []struct { 66 Config map[string]interface{} 67 Value int 68 }{ 69 { 70 Config: map[string]interface{}{key: 5}, 71 Value: 5, 72 }, 73 { 74 Config: map[string]interface{}{key: 5.0}, 75 Value: 5, 76 }, 77 { 78 Config: map[string]interface{}{key: 0}, 79 Value: 0, 80 }, 81 { 82 Config: map[string]interface{}{}, 83 Value: 0, 84 }, 85 } 86 87 for i, test := range tests { 88 var value int 89 var paniced interface{} 90 91 func() { 92 defer func() { 93 if r := recover(); r != nil { 94 paniced = r 95 } 96 }() 97 value = Config(test.Config).MustInt(key) 98 }() 99 100 if test.Value != 0 { 101 if paniced != nil { 102 t.Error(i, "wanted a value, but panic'd:", paniced) 103 } else if value != test.Value { 104 t.Error(i, "want:", test.Value, "got:", value) 105 } 106 } else { 107 if paniced == nil { 108 t.Error(i, "expected it to panic") 109 } 110 } 111 } 112 } 113 114 func TestConfigString(t *testing.T) { 115 t.Parallel() 116 117 key := "string" 118 tests := []struct { 119 Config map[string]interface{} 120 Value string 121 Ok bool 122 }{ 123 { 124 Config: map[string]interface{}{key: "str"}, 125 Value: "str", 126 Ok: true, 127 }, 128 { 129 Config: map[string]interface{}{key: ""}, 130 Value: "", 131 Ok: false, 132 }, 133 { 134 Config: map[string]interface{}{key: 5}, 135 Value: "", 136 Ok: false, 137 }, 138 { 139 Config: map[string]interface{}{}, 140 Value: "", 141 Ok: false, 142 }, 143 } 144 145 for i, test := range tests { 146 value, ok := Config(test.Config).String(key) 147 148 if ok != test.Ok { 149 t.Error(i, "ok =", ok) 150 } 151 if value != test.Value { 152 t.Error(i, "want:", test.Value, "got:", value) 153 } 154 } 155 } 156 157 func TestConfigInt(t *testing.T) { 158 t.Parallel() 159 160 key := "integer" 161 tests := []struct { 162 Config map[string]interface{} 163 Value int 164 Ok bool 165 }{ 166 { 167 Config: map[string]interface{}{key: 5}, 168 Value: 5, 169 Ok: true, 170 }, 171 { 172 Config: map[string]interface{}{key: 5.0}, 173 Value: 5, 174 Ok: true, 175 }, 176 { 177 Config: map[string]interface{}{key: 0}, 178 Value: 0, 179 Ok: false, 180 }, 181 { 182 Config: map[string]interface{}{}, 183 Value: 0, 184 Ok: false, 185 }, 186 } 187 188 for i, test := range tests { 189 value, ok := Config(test.Config).Int(key) 190 191 if ok != test.Ok { 192 t.Error(i, "ok =", ok) 193 } 194 if value != test.Value { 195 t.Error(i, "want:", test.Value, "got:", value) 196 } 197 } 198 } 199 200 func TestConfigStringSlice(t *testing.T) { 201 t.Parallel() 202 203 key := "slice" 204 tests := []struct { 205 Config map[string]interface{} 206 Value []string 207 Ok bool 208 }{ 209 { 210 Config: map[string]interface{}{key: []string{"str"}}, 211 Value: []string{"str"}, 212 Ok: true, 213 }, 214 { 215 Config: map[string]interface{}{key: []interface{}{"str"}}, 216 Value: []string{"str"}, 217 Ok: true, 218 }, 219 { 220 Config: map[string]interface{}{key: []string{}}, 221 Value: nil, 222 Ok: false, 223 }, 224 { 225 Config: map[string]interface{}{key: 5}, 226 Value: nil, 227 Ok: false, 228 }, 229 { 230 Config: map[string]interface{}{}, 231 Value: nil, 232 Ok: false, 233 }, 234 } 235 236 for i, test := range tests { 237 value, ok := Config(test.Config).StringSlice(key) 238 239 if ok != test.Ok { 240 t.Error(i, "ok =", ok) 241 } 242 if !reflect.DeepEqual(value, test.Value) { 243 t.Error(i, "want:", test.Value, "got:", value) 244 } 245 } 246 } 247 248 func TestTablesFromList(t *testing.T) { 249 t.Parallel() 250 251 if TablesFromList(nil) != nil { 252 t.Error("expected a shortcut to getting nil back") 253 } 254 255 if got := TablesFromList([]string{"a.b", "b", "c.d"}); !reflect.DeepEqual(got, []string{"b"}) { 256 t.Error("list was wrong:", got) 257 } 258 } 259 260 func TestColumnsFromList(t *testing.T) { 261 t.Parallel() 262 263 if ColumnsFromList(nil, "table") != nil { 264 t.Error("expected a shortcut to getting nil back") 265 } 266 267 if got := ColumnsFromList([]string{"a.b", "b", "c.d", "c.a"}, "c"); !reflect.DeepEqual(got, []string{"d", "a"}) { 268 t.Error("list was wrong:", got) 269 } 270 if got := ColumnsFromList([]string{"a.b", "b", "c.d", "c.a"}, "b"); len(got) != 0 { 271 t.Error("list was wrong:", got) 272 } 273 if got := ColumnsFromList([]string{"*.b", "b", "c.d"}, "c"); !reflect.DeepEqual(got, []string{"b", "d"}) { 274 t.Error("list was wrong:", got) 275 } 276 }