|
1 | 1 | package sqlmock |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "database/sql/driver" |
| 6 | + "errors" |
5 | 7 | "fmt" |
| 8 | + "reflect" |
6 | 9 | "testing" |
7 | 10 | "time" |
8 | 11 | ) |
@@ -137,3 +140,92 @@ func TestBuildQuery(t *testing.T) { |
137 | 140 | t.Error(err) |
138 | 141 | } |
139 | 142 | } |
| 143 | + |
| 144 | +type CustomConverter struct{} |
| 145 | + |
| 146 | +func (s CustomConverter) ConvertValue(v interface{}) (driver.Value, error) { |
| 147 | + switch v.(type) { |
| 148 | + case string: |
| 149 | + return v.(string), nil |
| 150 | + case []string: |
| 151 | + return v.([]string), nil |
| 152 | + case int: |
| 153 | + return v.(int), nil |
| 154 | + default: |
| 155 | + return nil, errors.New(fmt.Sprintf("cannot convert %T with value %v", v, v)) |
| 156 | + } |
| 157 | +} |
| 158 | +func TestCustomValueConverterQueryScan(t *testing.T) { |
| 159 | + db, mock, _ := New(ValueConverterOption(CustomConverter{})) |
| 160 | + query := ` |
| 161 | + SELECT |
| 162 | + name, |
| 163 | + email, |
| 164 | + address, |
| 165 | + anotherfield |
| 166 | + FROM user |
| 167 | + where |
| 168 | + name = 'John' |
| 169 | + and |
| 170 | + address = 'Jakarta' |
| 171 | +
|
| 172 | + ` |
| 173 | + expectedStringValue := "ValueOne" |
| 174 | + expectedIntValue := 2 |
| 175 | + expectedArrayValue := []string{"Three", "Four"} |
| 176 | + mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"})) |
| 177 | + row := db.QueryRow(query) |
| 178 | + var stringValue string |
| 179 | + var intValue int |
| 180 | + var arrayValue []string |
| 181 | + if e := row.Scan(&stringValue, &intValue, &arrayValue); e != nil { |
| 182 | + t.Error(e) |
| 183 | + } |
| 184 | + if stringValue != expectedStringValue { |
| 185 | + t.Errorf("Expectation %s does not met: %s", expectedStringValue, stringValue) |
| 186 | + } |
| 187 | + if intValue != expectedIntValue { |
| 188 | + t.Errorf("Expectation %d does not met: %d", expectedIntValue, intValue) |
| 189 | + } |
| 190 | + if !reflect.DeepEqual(expectedArrayValue, arrayValue) { |
| 191 | + t.Errorf("Expectation %v does not met: %v", expectedArrayValue, arrayValue) |
| 192 | + } |
| 193 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 194 | + t.Error(err) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func TestCustomValueConverterExec(t *testing.T) { |
| 199 | + db, mock, _ := New(ValueConverterOption(CustomConverter{})) |
| 200 | + expectedQuery := "INSERT INTO tags \\(name,email,age,hobbies\\) VALUES \\(\\?,\\?,\\?,\\?\\)" |
| 201 | + query := "INSERT INTO tags (name,email,age,hobbies) VALUES (?,?,?,?)" |
| 202 | + name := "John" |
| 203 | + email := "j@jj.j" |
| 204 | + age := 12 |
| 205 | + hobbies := []string{"soccer", "netflix"} |
| 206 | + mock.ExpectBegin() |
| 207 | + mock.ExpectPrepare(expectedQuery) |
| 208 | + mock.ExpectExec(expectedQuery).WithArgs(name, email, age, hobbies).WillReturnResult(NewResult(1, 1)) |
| 209 | + mock.ExpectCommit() |
| 210 | + |
| 211 | + ctx := context.Background() |
| 212 | + tx, e := db.BeginTx(ctx, nil) |
| 213 | + if e != nil { |
| 214 | + t.Error(e) |
| 215 | + return |
| 216 | + } |
| 217 | + stmt, e := db.PrepareContext(ctx, query) |
| 218 | + if e != nil { |
| 219 | + t.Error(e) |
| 220 | + return |
| 221 | + } |
| 222 | + _, e = stmt.Exec(name, email, age, hobbies) |
| 223 | + if e != nil { |
| 224 | + t.Error(e) |
| 225 | + return |
| 226 | + } |
| 227 | + tx.Commit() |
| 228 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 229 | + t.Error(err) |
| 230 | + } |
| 231 | +} |
0 commit comments